JSP & Binary Data

R

Randhir Singh

Hello All,

I'm working on a web application that enables users to download
certificates via a link on the website. As we all know, its
impossible to write binary data in JSP, besides using
ServletOutputStream. However, using ServletOutputStream is not
working for me. The binary data (PKI certificate)is held in a LDAP
directory. The following is a snippet of the code that im using to
pull the certificate.

.....
.....
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=" + cn
+ extension);

ServletOutputStream outputStream = response.getOutputStream();
String value = sr.getAttributes().get(attribute).get(0).toString();
byte [] binValue = value.getBytes();
outputStream.write(binValue);
outputStream.flush();
.....
.....

This code prompts me with a save dialogue and allows me to save the
certificate in a p7b file... and ofcourse it is invalid. When I open
the certificate in a text editor, I see many "?" and other charactors
(some of which are correct, ie CRL distribution point, etc), but the
"?" makes me believe that this is not the binary data. If anyone out
there can point me in the right direction or tell me what im doing
wrong it would be GREATLY appreciated!

Thanks a lot.
 
A

Anton Spaans

Get a Writer from the response, not an outputstream.
A Writer writes character-strings, not byte-arrays. Then you don't need to
call 'getBytes()' on the String value.

If you want to send binary data, do not translate the original certificate
data into a string. This translation into a string (and back to a
byte-array) may mess up your certificate data. The ..get(0) method, what
does it return?

If you can get a binary array *directly* from the 'get(0)' method, do this.

If you really need to use the intermediate String value, be sure what the
'get(0).toString()' returns: What kind of encoding is the resulting
String..? Then you may need to call 'getBytes(String pEncoding)' instead....

-- Anton.
 
J

Jeff

Here's how I've done dynamic PDF's. The 'buffer' variable
is a ByteArrayOutputStream to which the dynamic PDF is
written.

response.reset();
response.addHeader("Content-disposition", "inline; filename=Invoice.pdf");
response.setContentType("application/pdf");
response.setContentLength(buffer.size());
buffer.writeTo(response.getOutputStream());
response.flushBuffer();

However, I believe that your real problem is String-byte conversion.
I think you need to work on how you are getting the raw certificate
data. Forget about downloading for the moment and have the servlet write
the certificate to a local file on the server. Then verify that file.
Until you can do that successfully, downloading is pointless.

HTH,

Jeff
 
C

Chris Smith

Randhir said:
Hello All,

I'm working on a web application that enables users to download
certificates via a link on the website. As we all know, its
impossible to write binary data in JSP, besides using
ServletOutputStream. However, using ServletOutputStream is not
working for me. The binary data (PKI certificate)is held in a LDAP
directory. The following is a snippet of the code that im using to
pull the certificate.

Are you doing this inside of a JSP page? If so, why are you so
reluctant to do the right thing and use a servlet?
String value = sr.getAttributes().get(attribute).get(0).toString();
byte [] binValue = value.getBytes();
outputStream.write(binValue);
[...]

When I open the certificate in a text editor, I see many "?" and
other charactors (some of which are correct, ie CRL distribution
point, etc), but the "?" makes me believe that this is not the
binary data.

Binary data? It looks to me like you're trying to write Strings. Even
more oddly, you are converting the Strings to binary form according to
the default locale of the web server, which is almost certainly not what
you want, since the clients probably don't care what nationality of
people happen to have set up the web server. And furthermore, some
characters in the Strings are not supported in the character encoding
specified by the default locale of the web server, which is why you're
getting '?' characters.

The questions you need to ask yourself are: is there a standard
character encoding for the certificate data I'm providing, and if so
what is it? If not, why don't I want to use getWriter instead of
getOutputStream? Or, is the data supposed to be text at all (and if
not, why the heck is it in a String, and do I have any control over
that?)

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,165
Latest member
JavierBrak
Top