Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

Broken links in mail

Avatar

Level 3

HI All,

I am constructing page url from the below lines of code and displaying in mail template but it has broken displaying in the mail template.

Construction of page in service:

         final String pagepath = externalizer.authorLink(resourceResolver, payload);


Mail template(output):

Hi ContentReviewer,
 
This  page/document http://100.66.0.19:4502/content/dam/briggs/documents/Copy of AEM defects - Priority.xlsx has been required approval to publish.
 
 
Please check the inbox to complete the task.
http://100.66.0.19:4502/inbox
 
 
Regards,
Your local friendly CQ5 Admin

Construction of page in service:

         final String pagepath = externalizer.authorLink(resourceResolver, payload);

Can please some one help me to how to fix this issue.

Thanks in advance.

1 Accepted Solution

Avatar

Correct answer by
Administrator

Hi

As mentioned by Kunal, you need to encode names before using it.

Example :-  

Option 1:-

URL :-http://example.com/query?q= random word £500 bank $

It should be encoded to :- http://example.com/query?q=random%20word%20%A3500%20bank%20%24


URLEncoder should be the way to go. You only need to keep in mind to encode only the individual query string parameter name and/or value, not the entire URL, for sure not the query string parameter separator character & nor the parameter name-value separator character =.

String q = "random word £500 bank $";
String url = "http://example.com/query?q=" + URLEncoder.encode(q, "UTF-8");

 

Option 2:-

Use of Java.net.URI Class

The java.net.URI class can help; in the documentation of URL you find

Note, the URI class does perform escaping of its component fields in certain circumstances. The recommended way to manage the encoding and decoding of URLs is to use an URI
Use one of the constructors with more than one argument, like:

URI uri = new URI(
    "http", 
    "search.barnesandnoble.com", 
    "/booksearch/first book.pdf",
    null);
URL url = uri.toURL();
//or String request = uri.toString();

 

Some Reference Links:-

1. http://stackoverflow.com/questions/10786042/java-url-encoding-of-query-string-parameters

2. http://stackoverflow.com/questions/724043/http-url-address-encoding-in-java

3. http://www.mkyong.com/java/how-to-encode-a-url-string-or-form-parameter-in-java/

4. http://www.java2novice.com/java_networking/url_encode/

I hope this would help you.

Thanks and Regards

Kautuk Sahni

View solution in original post

0 Replies

Avatar

Level 9
Hey venketesh, If you are talking about the url which does not appear as link (xlsx file),  actually link formating is incorrect. There are spaces in file name. Try with an asset which has no space in the name. To fix this,  try encoding before using externalizer service. Ignore typo error Jitendra

Avatar

Correct answer by
Administrator

Hi

As mentioned by Kunal, you need to encode names before using it.

Example :-  

Option 1:-

URL :-http://example.com/query?q= random word £500 bank $

It should be encoded to :- http://example.com/query?q=random%20word%20%A3500%20bank%20%24


URLEncoder should be the way to go. You only need to keep in mind to encode only the individual query string parameter name and/or value, not the entire URL, for sure not the query string parameter separator character & nor the parameter name-value separator character =.

String q = "random word £500 bank $";
String url = "http://example.com/query?q=" + URLEncoder.encode(q, "UTF-8");

 

Option 2:-

Use of Java.net.URI Class

The java.net.URI class can help; in the documentation of URL you find

Note, the URI class does perform escaping of its component fields in certain circumstances. The recommended way to manage the encoding and decoding of URLs is to use an URI
Use one of the constructors with more than one argument, like:

URI uri = new URI(
    "http", 
    "search.barnesandnoble.com", 
    "/booksearch/first book.pdf",
    null);
URL url = uri.toURL();
//or String request = uri.toString();

 

Some Reference Links:-

1. http://stackoverflow.com/questions/10786042/java-url-encoding-of-query-string-parameters

2. http://stackoverflow.com/questions/724043/http-url-address-encoding-in-java

3. http://www.mkyong.com/java/how-to-encode-a-url-string-or-form-parameter-in-java/

4. http://www.java2novice.com/java_networking/url_encode/

I hope this would help you.

Thanks and Regards

Kautuk Sahni

Avatar

Level 9

what are you getting when you directly hitting the URL i.e.

http://100.66.0.19:4502/content/dam/briggs/documents/Copy of AEM defects - Priority.xlsx 

is it resolving or throwing any error.

Thanks,

Kishore

Avatar

Level 9

@Kishor,

It should work because browser encode the URL and matches the resource. It won't throw any exception if such file exists. Link is not formed correctly because of the name of the file.

Kishore@CQ wrote...

what are you getting when you directly hitting the URL i.e.

http://100.66.0.19:4502/content/dam/briggs/documents/Copy of AEM defects - Priority.xlsx 

is it resolving or throwing any error.

Thanks,

Kishore

 

Avatar

Level 9

@Tomar

Might work but want to know before suggesting further on this issue.

Avatar

Level 3

Hi All,

Thanks for help and option is working fine but take the url using URIobj.toASCIIString().

 

                    URL url= new URL("url");
                    URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
                    String finalpage=uri.toASCIIString();

 

Thanks,

Venkatesham