Getting binary file and text information - java

Currently, I have a servlet act as web service.
When I pass in parameters using POST, it will return me an executable binary file (application/octet-stream). However, beside binary file, I would also like to get additional information (in text format) about this binary file.
Is it possible to achieve this by using only single POST request? But, how is it possible, to switch from application/octet-stream to text/plain within single POST response?

It is not possible to change the MIME type within a single response.
However, i think t is possible to put your additional information into the response header using the HttpServletResponse.addHeader method.

You could return a multipart MIME response (multipart/mixed; boundary=XXX instead of application/octet-stream) with the binary part encoded in Base64.
I'm not sure if the JavaMail API can be used to construct the content, but it's worth a look.

Within a single POST it isn't possible. But two ideas:
Show your text file as another web page that starts the download of your executable
Bundle both files into a zip archive

Related

SDMX-ML: SAS libname XML

Eurostat data can be downloaded via a REST API. The response format of the API is a XML file formatted according to the SDMX-ML standard. With SAS, very conveniently, one can access XML files with the libname statement and the XML or XMLv2 engine.
Currently, I am using the xmlv2 engine together with the automap= option to generate an xmlmap to access the data. It works. But the resulting SAS data sets are very unstructured, and for another data set to be downloaded the data structure might change. Also the request might depend on the DSD-file that Eurostat provides for each database item within a different XML file.
Here comes the code:
%let path = /your/working/directory/;
filename map "&path.map.txt";
filename resp "&path.resp.txt";
proc http
URL="http://ec.europa.eu/eurostat/SDMX/diss-web/rest/data/cdh_e_fos/..PC.FOS1.BE/?startperiod=2005&endPeriod=2011"
METHOD="GET"
OUT=resp;
run;quit;
libname resp XMLv2 automap=REPLACE xmlmap=map;
proc datasets;
copy out=WORK in=resp;
run;quit;
With the code above, you can view all downloaded data in your WORK library. Its a mess.
To download another time series change parameters of the URL according to Eurostat's description.
So here is my question
Is there a way to easily generate a xmlmap from a call to the DSD file so that the data are stored in a well structured way?
As the SDMX-ML standard is widely used in public institutions such as the ECB, Eurostat, OECD... I am wondering if somebody has implemented requests to the databases, already. I know about the tool from Banca Italia which uses a javaObject. However, I was wondering if there might be a solution without the javaObject.

Servlets upload file from form without enctype="multipart/form-data"

is there a possiblity to upload a file from html forms without using multipart/form-data?
I'm using Servlets 2.5 so I can't use getPart method and I still want to use getParameter method to get values from fields.
Use apache commons fileupload to handle mpe requests, like everyone else.
The multipart/form-data encoding type is a fundamental part of the HTML file upload mechanism - see for instance the RFC where it was first standardised, particularly this section headed "File transfer with ENCTYPE=x-www-form-urlencoded".
While there are other methods of uploading files now available (e.g. backed by Flash widgets) none of these can allow you to submit part of the form in normal x-www-form-encoded format but also attach uploaded files.
Perhaps you could write a wrapper object which wraps an API which supports multipart/form-data and implements the getParameter method you want on top of it for non-file fields.
You have to use the enctype form attribute to upload files.
And since you're not using Servlet 3.0, you have to create your own filter if you want to keep using the request.getParameter() calls to get data from the input fields. You can find full examples here and here.

How to embed images to the html body of an email using the java library Jodd Email?

At the main page of the Jodd email library http://jodd.org/doc/email.html
there is a very specific example on how to use the library to embed an image (and not just simply attach it as a file) to an email you are about to send.
Unfortunately the resulting Content-Type of the part of the email that contains the image is:
Content-Type: application/octet-stream
But in order to display it correctly we need this Content-Type:
Content-Type: image/png
if you have a png image for instance.
But I cannot seem to find how to configure this inside the Jodd email library..
This is what I am seeking for. Thank you :)
If you followed the example from Jodd site then you embedded your files using method embedFile(). This method is a 'shortcut' method for:
attach(new FileAttachment(file));
where attach() is the central, generic method for attaching content. FileAttachment rely on javax.mail for setting the content type, probably based on extension.
Therefore, to set content type manually, use generic attach() method. For example, embedding file like this:
.embedFile("d:\\c.xxx")
would set content type to "application/octet-stream" as it is not recognized for xxx extension. Instead, you can use the following:
.attach(new ByteArrayAttachment(
FileUtil.readBytes("d:\\c.xxx"), "image/png", "c.png", "c.png"))
where you can manually set the content type regardless the file name. If you don't want to load file bytes, you can pass InputStream instead etc.
Another solution (if you want to keep using embedFile) is to check your mime type settings.
Note: since there are many combinations how to attach content (bytes, input stream, file, inline...), attach methods will be refactored in Jodd 3.4.1. in order to provide more developer friendly api. Stay tuned ;)

Setting a string in a body of httpResponse

I need help. In my current development one of the requirements says:
The server will return 200-OK as a response(httpresponse).
If the panelist is verified then as a result, the server must also
return the panelist id of this panelist.
The server will place the panelist id inside the body of the 200-OK
response in the following way:
<tdcp>
<cmd>
<ack cmd=”Init”>
<panelistid>3849303</panelistid>
</ack>
</cmd>
Now I am able to put the httpresponse as
httpServletResponse.setStatus(HttpServletResponse.SC_OK);
And I can put
String responseToClient= "<tdcp><cmd><ack cmd=”Init”><panelistid>3849303</panelistid></ack></cmd></tdcp>";
Now what does putting the above xml inside the body of 200-OK response mean and how can it be achieved?
You can write the XML directly to the response as follows:
This example uses a ServletResponse.getWriter(), which is a PrintWriter to write a String to the response.
String responseToClient= "<tdcp><cmd><ack cmd=”Init”><panelistid>3849303</panelistid></ack></cmd></tdcp>";
httpServletResponse.setStatus(HttpServletResponse.SC_OK);
httpServletResponse.getWriter().write(responseToClient);
httpServletResponse.getWriter().flush();
You simply need to get the output stream (or output writer) of the servlet response, and write to that. See ServletResponse.getOutputStream() and ServletResponse.getWriter() for more details.
(Or simply read any servlet tutorial - without the ability to include data in response bodies, servlets would be pretty useless :)
If that's meant to be XML, Word has already spoiled things for you by changing the attribute quote symbol to ” instead of ".
It is worth having a look at JAXP if you want to generate XML using Java. Writing strings with < etc. in them won't scale and you'll run into problems with encodings of non-ASCII characters.

SOAP: Reading SOAP response's embedded file

In SOAP Client application. I am using javax.xml.soap api. I am getting the soap response. A part of it, shown below.
<ns5:XXX type="Full" format="HTML">
<ns5:EmbeddedFile MIMEType="text/html"
fileExtension="html"
fileName="ZZZ.html">
<ns5:Document>...</ns5:Document>
</ns5:EmbeddedFile>
</ns5:XXX>
The value between the Document tag is in the Base64 format.
I need to know two things, as in the above code you will see that, the fileName is zzz.html.
where this zzz.html file will stored or exits. I search for in my local machine i do not find.
Another thing i would like to know that the between the Document tags it show long text messages in the Base64 format. Is this is the document that exists in the zzz.html. If it is so how to read that document.
Thanks
This appears to be a custom way of embedding file content to a SOAP message being used by the service you are calling - a standard way of doing this would have been using Soap Attachments.
In this specific case, it does look like the file content is being embedded as Base64 data between the Document tags, and the meta information of the file is the attributes of EmbeddedFile tag. You will basically have to decode the Base64 encoded content - see here
and here on how to, move the contents to a file with the name in the meta information tag.

Categories