Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

Component that create a file, how to?

Avatar

Level 3

Hey guys, how are you doing?

 

I'm trying to create a .txt file on my content repository everytime a specific template is used on AEM 6.0. So, I tought about put this code in a component .jsp file and then including this component on this specific template.

My two questions is: can someone give me a sample code of simply creating a .txt file somewhere in the content repository (/content/files, for example) to be used on a component?

And, is this the correct/best way to achieve what I want to achieve?

 

I have some idea on how to do this using a .java class, creating a new resource and all, and then calling this as a service on a component, but I believe that there has to be an easier way, like directly in the component.

 

Thanks very much.

1 Accepted Solution

Avatar

Correct answer by
Level 8

For something like that where it's associated to a site I'd make it a selector based script of the template in question rather than generating an actual text file. So you'd end up with a solution that looks something like this:

  1. Create a script in your Home with Robot template named robot.txt.jsp or something similar. If the contents of the robots.txt file are static you just put the text in that file, or if you want authors to have control you add appropriate fields you your template page properties and write JSP code to pull the right content and display it. 
  2. Create a rewrite rule in Apache that rewrites the address you want for the robots.txt file from say www.mysite.com/robots.txt to /content/mysite/home.robots.txt. Make sure you set .txt files to be cachable in dispatcher. The paths for the rewrite rule are obviously dependent on your specific solution. 

View solution in original post

8 Replies

Avatar

Level 10

Hi Mat.Barreto,

Better to upload such files in dam and refer in component.

Thanks,
Sham
Tweet @adobe_sham

Avatar

Level 8

Could you provide a some details on what the purpose of the text file is? Generally putting code like that in a component would typically not be best practice, but where to the put the logic would depend a little on what the purpose text file is. One option is to leverage a either a JCR Observer or Sling Event to handle the file creation. You would listen for new node creations and then filter based on the node type and template value. Check out these blog posts for some of these options:

One of the complexities to consider is whether or not you need this text file to be present in the publish server, and whether or not you want to create the the text file in publish when new pages are first published, or if you want activate the text files from the author server to the publish server. 

Avatar

Level 3

The text file will be a robots.txt file, for SEO purposes. So, it need to be on publish server.

However, this file need to be automaticaly created everytime the user creates a new site using the, for example, "Home with robot" template.

Is there a better way to achieve this?

Avatar

Level 3

Hi Sham,

 

Thanks for the replying, but in my case the files need to be created dynamicaly, everytime a new page is created using one specific template, in a specific and diferent path everytime.

So, it can't be a existing file.

Avatar

Level 10

Ok. What is the plan with that text file? When will you render it on that page.   If i understand usecase correctly can we achieved using oob text or image component along with cq:template.  Image component can render image from both dam and locally. With this you have file stored at page level only.   High level idea is [1]. 

[1]  http://blogs.adobe.com/experiencedelivers/experience-management/defaults-in-your-component/

Avatar

Correct answer by
Level 8

For something like that where it's associated to a site I'd make it a selector based script of the template in question rather than generating an actual text file. So you'd end up with a solution that looks something like this:

  1. Create a script in your Home with Robot template named robot.txt.jsp or something similar. If the contents of the robots.txt file are static you just put the text in that file, or if you want authors to have control you add appropriate fields you your template page properties and write JSP code to pull the right content and display it. 
  2. Create a rewrite rule in Apache that rewrites the address you want for the robots.txt file from say www.mysite.com/robots.txt to /content/mysite/home.robots.txt. Make sure you set .txt files to be cachable in dispatcher. The paths for the rewrite rule are obviously dependent on your specific solution. 

Avatar

Level 10

Mat.Barreto wrote...

The text file will be a robots.txt file, for SEO purposes. So, it need to be on publish server.

However, this file need to be automaticaly created everytime the user creates a new site using the, for example, "Home with robot" template.

Is there a better way to achieve this?

 

http://adobe-consulting-services.github.io/acs-aem-commons/features/simple-sitemap.html

Avatar

Level 3

Orotas, thanks for the response.

This probably works, I've already used a similar approach to generate json content, however, as an selector based script, someone someday has to access an url with the selector to trigger the creation of the file, right?

There is no way to get one template to automatically create a file in the JCR when someone assign a page to it?