Link to home
Create AccountLog in
Avatar of C7Swill
C7Swill

asked on

Remote URL with encoded variables with FOP Servlet whitespace problem.

Hello there,

I have modified the FOP Servlet to allow it to read in an url as input instead of a local file. The urls are php files which take variables passed in a get line to generate the correct XML and XSL-FO files. Everything works perfectly if there is no whitespace or %20 in the url. I.E.

http://127.0.0.1:8080/fop/fop?xsl=http://domain.loc/xsldoc.php%3Ftemplate_id=60&xml=http://fireantxtt.loc/xmldoc.php%3Ftemplate_id=60%26GRP0=accessuser%7Csean%26GRP1=short_name%7CABBTurbidityMeter&remot

It pulls in the correct info and generates the pdf. As soon as one of the variables contains a %20, the servlet returns this error:

http://127.0.0.1:8080/fop/fop?xsl=http://domain.loc/xsldoc.php%3Ftemplate_id=60&xml=http://fireantxtt.loc/xmldoc.php%3Ftemplate_id=60%26GRP0=accessuser%7Csean%26GRP1=short_name%7CABB%20Turbidity%20Meter&remot

javax.servlet.ServletException: Server returned HTTP response code: 400 for URL: http://fireantxtt.loc/xmldoc.php?template_id=60&GRP0=accessuser|sean&GRP1=short_name|ABB Turbidity Meter
FopServlet.doGet(Unknown Source)
javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

I understand that it is most likely java automatically converting %20 back to whitespace before retreiving the url. Is there a way to keep urlencoding throughout. Below is my modified code which is called if &remot is appended to the url. I realize it's ugly bulky code but this is my first time using java.

                URL url = new URL(xmlParam);
                URLConnection urlconn = url.openConnection();

                  FileOutputStream foxml = new FileOutputStream ("temp.xml");
                  ByteArrayOutputStream tempxml = new ByteArrayOutputStream () ;
                  int lengthxml = urlconn.getContentLength();
                  InputStream isxml = urlconn.getInputStream();
                  int chxml ;
                  while ( (chxml = isxml.read())>0)
                  {
                      tempxml.write (chxml);
                  }
                  tempxml.writeTo(foxml);
                  isxml.close();

                URL url2 = new URL(xslParam);
                URLConnection urlconn2 = url2.openConnection();


                  FileOutputStream foxsl = new FileOutputStream ("temp.xsl");
                  ByteArrayOutputStream tempxsl = new ByteArrayOutputStream () ;
                  int lengthxsl = urlconn2.getContentLength();
                  InputStream isxsl = urlconn2.getInputStream();
                  int chxsl ;
                  while ( (chxsl = isxsl.read())>0)
                  {
                        tempxsl.write (chxsl);
                  }
                  tempxsl.writeTo(foxsl);
                  isxsl.close();

                XSLTInputHandler input =
                  new XSLTInputHandler(new File("temp.xml"),
                                       new File("temp.xsl"));
                renderXML(input, response);

Cheers
Avatar of Mick Barry
Mick Barry
Flag of Australia image

you can't keep the encoding throughout because the entire url is encoded. What you need to do is encode the parameter values in the url. Not sure if its possible but you may find this easier if you send each parameter (used in the embedded url) as a seperate paremeter in the original url.
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of C7Swill
C7Swill

ASKER

I've encoded the urls as well as the parameters to no avail. I think it may have something to do with an urlencoding function in java to pass the correctly urlencoded url to the fop servlet in this line:
URL url = new URL(xmlParam);
have you corrected the encoding of the url?
or are you trying to use it as it currently is?
Avatar of C7Swill

ASKER

I have encoded the parameters, then the urls and all other variations possible including raw encode which results in the servlet failing to even find the remote files. I am fairly certain that this problem needs to be solved inside the java servlet itself with an urlEncode before it makes the connection to the url as the url goes into java and presents an error where it represents the url as decoded:

http://fireantxtt.loc/xmldoc.php?template_id=60&GRP0=accessuser|sean&GRP1=short_name|ABB Turbidity Meter

It works however if I change the ABB%20Turbidity%20Meter to ABBTurbidityMeter in the url.

I am looking at URLEncoder.encode() for java.net but cannot seem to successfully implement it in my servlet. As I said, I only have a cursory knowledge of java.
I'm not sure the details of what you tried but the URL you posted above is incorrectly encoded.
Avatar of C7Swill

ASKER

The first url is the one java shows me in the error message.

The second should actually be ABB+Turbidity+Meter but changes when I copy and paste into this field on Experts Exchange to ABB%20Turbidity%20Meter

Do you have any suggestions as to what a the proper encoding would look like?
The URL parameter can only contain a-z, A-Z, 0-9, ".", "-", "*", and "_".
All other characters need to be encoded. You URL parameters contains things like &, % etc.

If you do the encoding as I described above it should get encoded correctly.
Avatar of C7Swill

ASKER

Works just fine, it is only when I add spaces or + or %20 to the url in my parameters that the error occurs. If I urlencode the parameters and then urlencode the location string http://fireantxtt.loc...etc..  the fop servlet cannot understand the location at all.

The correct solution is to double urlencode i.e. urlencode(urlencode($variable)) the single parameters so that whitespaces are converted to + and then to %2B, the hex of +. I see what you were trying to get at, but your solution didn't really work for my needs as as soon as I urlencode the complete url that is a parameter it fails and it does work with % signs in the url.
where was it failing?
how were you decoding it?
Avatar of C7Swill

ASKER

if I urlencoded the xml= url the fop servlet could not find the location to pull the xml file in through as it was converting the http://etc.. into an urlencoded string which java could not understand in its url.OpenConnection. The only thing I was looking for was how to properly encode whitespace or + or %20.
Wnenever you encode something, you need to make sure it is decoded at the other end correctly before using it.

> The only thing I was looking for was how to properly encode whitespace or + or %20.

the same way you encode any string. Java provides a method for it, you do not need to worry about individual characters if done correctly.