0

I have content of a file (x509crl) in base64 encoded value. I fired GET request to a servlet. By pasting following URL in a browser file download dialog pops up.

"http://host:port/myServlet?content=base64_encoded_value"

I was able to download a file, but I could not open a file. According to a pki expert in my workshop, if content of crl file is correct then OS should open it up just fine.

Here is the doGet method from my servlet class. I wonder what mistakes I made.

protected void doGet(HttpServletRequest request, HttpServletResponse response) {
    String content = request.getParameter("content");
    response.setContentType("application/pkix-crl");
    response.setHeader("Content-Disposition","attachment;filename=test.crl");
    byte[] byteArray = Base64.decodeBase64(content.getBytes());
    ServletOutputStream sos = response.getOutputStream();
    sos.write(byteArray);
    sos.close();
}

I am using commons-codec-1.3 and I didn't include any exception handling in the example.

DaeYoung
  • 1,161
  • 6
  • 27
  • 59
  • open it with what? How do you try to open the file? You have downloaded a certificate verification list. The OS needs an application to understand what to do with it. What do you expect to be done with it? – CharlieS Nov 17 '14 at 23:17
  • i do not know. i was told that OS should know how to open it if the crl file is correct. [update] i was referring to windows platform. i just saw that my co-worker just opened up sample crl file just fine. But not the file i was trying to download. i think something is not right. – DaeYoung Nov 17 '14 at 23:19
  • What OS are you using? – CharlieS Nov 17 '14 at 23:23
  • which application opened the file on your co-workers system? show a screenshot if you don't know. – CharlieS Nov 17 '14 at 23:35
  • when you double click it, do you get a message saying it is not valid, do you get some other error, or what happens? Please give as much information as you can. If you open it with an editor, what are the contents? – CharlieS Nov 17 '14 at 23:41
  • @CharlieS: I get a message saying it is not valid. Thank you for taking your time to look into this. – DaeYoung Nov 18 '14 at 15:10

2 Answers2

0

So you send the request:

http://host:port/myServlet?content=base64_encoded_value

which is consumed by the method in your servlet:

protected void doGet(HttpServletRequest request, HttpServletResponse response) {

Lets have a look at what you are doing:

    String content = request.getParameter("content");

Seems the String content will now contain "base64_encoded_value" Next, tell the client what we are sending, seems reasonable.

    response.setContentType("application/pkix-crl");
    response.setHeader("Content-Disposition","attachment;filename=test.crl");

Then we do this:

    byte[] byteArray = Base64.decodeBase64(content.getBytes());

This seems to be trying to convert the string "base64_encoded_value" to bytes and then decode them. They are not encoded base64, so byteArray probably contains nothing. The rest of it writes that nothing to an output stream in the response.

    ServletOutputStream sos = response.getOutputStream();
    sos.write(byteArray);
    sos.close();
}

I am guessing that you actually want to read a file or create a valid crl on the fly, then encode it and send it on, rather than trying to decode the string parameter.

CharlieS
  • 1,432
  • 1
  • 9
  • 10
  • @CharliesS: yes, i want to create a file/valid crl on the fly. i will encode it and test the code. Thank you again! – DaeYoung Nov 19 '14 at 15:11
0

After reading more threads, I realized my problem. The simple logic I posted was working right however when I passed raw base64 string in URL as part of query string, I should have url-encoded the value otherwise data is altered when the servlet receives it. After I url-encoded base64 string, I was able to download a valid crl.

The following thread gave me an answer.

Passing base64 encoded strings in URL

Community
  • 1
  • 1
DaeYoung
  • 1,161
  • 6
  • 27
  • 59