0

I have this code below that encodes a URL before it is send over the wire (email):

private static String urlFor(HttpServletRequest request, String code, String email, boolean forgot) {
    try {
        URI url = forgot
            ? new URI(request.getScheme(), null, request.getServerName(), request.getServerPort(), createHtmlLink(),
                    "code="+code+"&email="+email+"&forgot=true", null)
            : new URI(request.getScheme(), null, request.getServerName(), request.getServerPort(), createHtmlLink(),
                    "code="+code+"&email="+email, null);
        String s = url.toString();
        return s;
    } catch (URISyntaxException e) {
        throw new RuntimeException(e);
    }
}  

/**
 * Create the part of the URL taking into consideration if 
 * its running on dev mode or production
 * 
 * @return
 */
public static String createHtmlLink(){
    if (GAEUtils.isGaeProd()){
        return "/index.html#ConfirmRegisterPage;";
    } else {
        return "/index.html?gwt.codesvr=127.0.0.1:9997#ConfirmRegisterPage;";
    }
}

The problem with this is that the generated email looks like this:

http://127.0.0.1:8888/index.html%3Fgwt.codesvr=127.0.0.1:9997%23ConfirmRegisterPage;?code=fdc12e195d&[email protected]

The ? mark and # symbol is replaced with %3F and %23 where when the link is opened from the browser it will not open as it is incorrect.

What is the correct way to do this?

1
  • Encode only the query parameters, not the path segemts. Commented Jul 29, 2013 at 12:57

2 Answers 2

1

You need to combine the query-parts of the url and add the fragment as the correct parameter.

Something like this should work:

private static String urlFor(HttpServletRequest request, String code, String email, boolean forgot) {
    try {
        URI htmlLink = new URI(createHtmlLink());
        String query = htmlLink.getQuery();
        String fragment = htmlLink.getFragment();
        fragment += "code="+code+"&email="+email;
        if(forgot){
            fragment += "&forgot=true";
        }
        URI url = new URI(request.getScheme(), null, request.getServerName(), request.getServerPort(), htmlLink.getPath(),
                    query, fragment);
        String s = url.toString();
        return s;
    } catch (URISyntaxException e) {
        throw new RuntimeException(e);
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, I just tried your code but the output looks like this: http://127.0.0.1:8888/index.html?gwt.codesvr=127.0.0.1:9997&code=4522ebee9f&[email protected]#ConfirmRegisterPage; it should be http://127.0.0.1:8888/index.html?gwt.codesvr=127.0.0.1:9997#ConfirmRegisterPage;&code=4522ebee9f&[email protected]
@xybrek: Are you 100% sure about that? I am 90% sure that the "&code=...&email=..." should go in the query part and not in the fragment part, but if you are sure, just append the "&code..." to htmlLink.getFragment() instead of htmlLink.getQuery().
Very well then. I have updated the code to fit that output (I assume you mean "#ConfirmRegisterPage;code=45..." and not "#ConfirmRegisterPage;&code=45...").
1

You can use the Java API method URLEncoder#encode(). Encode the query parameters using the method.

A better API for doing this is the UriBuilder.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.