Broken links in mail | Community
Skip to main content
Level 3
January 5, 2016
Solved

Broken links in mail

  • January 5, 2016
  • 7 replies
  • 1720 views

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.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by kautuk_sahni

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

7 replies

Jitendra_S_Toma
Level 10
January 5, 2016
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
Kunal_Gaba_
January 5, 2016

You should encode the page name before using it in the URL. Check the following link - http://stackoverflow.com/questions/10786042/java-url-encoding-of-query-string-parameters

kautuk_sahni
Community Manager
kautuk_sahniCommunity ManagerAccepted solution
Community Manager
January 6, 2016

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

Kautuk Sahni
GK-007
Level 9
January 6, 2016

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

Jitendra_S_Toma
Level 10
January 6, 2016

@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

 

GK-007
Level 9
January 6, 2016

@Tomar

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

ApmagAuthor
Level 3
January 6, 2016

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