0

I am trying to use MessageFormat as follows,

String downloadsUrl = "http://host/downloads?tags={0}";
Object[] formatArgs = {"sequence%20diagram"};
String url = new MessageFormat(downloadsUrl).format(formatArgs);

However, when I look at the final url string, it is, http://host/downloads?tags=sequence diagram

Is there someway to retain the %20 and not have MessageFormat replace it with a space?

N.N.
  • 8,336
  • 12
  • 54
  • 94
  • 1
    But I run your code , the url is `http://host/downloads?tags=sequence%20diagram` – Ken Chan Dec 01 '11 at 17:44
  • How do you "look at the final url string"? In a debugger? Or in a browser? FireFox decodes the space when you mouseover the link. – laz Dec 01 '11 at 17:52
  • I pass the URL to Abdera to get an ATOM feed and it bombs with the message, Exception: java.lang.IllegalArgumentException: Invalid uri 'http:///libraryview.jsp?type_by=Downloads&search_by=class diagram': Invalid query. If I replace any space in the url string BEFORE passing it to Abdera, it works, so I do url.replaceAll(" ", "%20"); and then pass it to Abdera, it works. – user1075979 Dec 01 '11 at 17:53

2 Answers2

0

The code you provided does not add the space the code above returns "http://host/downloads?tags=sequence%20diagram"

Your target servlet is doing the substitution. Whatever "/downloads" is mapped to is parsing the tags parameter and performing url decoding. You can reconstruct possible encodings as follows. You will need to handle the UnsupportedEncodingException in the following.

String encoded = URLEncoder.encode( request.getParameter( name ), "UTF8" );

Unfortunately this is only a possible encoding and by default will convert spaces to "+". To get the "%20" back you will need to resort to

encoding = encoding.replaceAll( "+", "%20" );

This may work for you or not. In general it is more advisable to normalize on the decoded value instead of the encoded value as there are many possible encodings per decoded value.

Neil Essy
  • 3,607
  • 1
  • 19
  • 23
0

Based on this I'm going to guess putting single quotes around the value would work...

Community
  • 1
  • 1
MattJenko
  • 1,208
  • 8
  • 11