How to retrieve inputStream from the server using fileUploader and GWT 2.4? - gwt

I have a fileUploader widget that I'm using to select an xml file. I then have a button that calls my handler in the viewImpl class when the user submits the selected file. If I understand things correctly, from there I do a submit from the formPanel and the file is on the server.
#UiHandler("calculateComplexityButton")
void onClickCalculateComplexity(ClickEvent e){
formPanel.submit();
//How do I get the inputStream back to here????
presenter.getTask(inputStream);
}
My problem is how do I get the inputStream off the server? I tried using an RPC call for all this, but when I try to get the inputStream I'm not pulling anything off the server. I tried:
inputStream = request.getInputStream();
but it appears to be empty. Any ideas on this?
I dropped the RPC code and used a simple HTTPRequest I found here. That gets me to the servlet, but the request doesn't have the file stream. When I reach this line in the code:
FileItemIterator iter = upload.getItemIterator(request); //Nothing is here in iter.

You can not make an upload via RPC, thats why you have to submit your form to a servlet.
final FormPanel form = new FormPanel();
form.setEncoding(FormPanel.ENCODING_MULTIPART);
form.setMethod(FormPanel.METHOD_POST);
form.setAction("/upload");
So, when you do form.submit() it will send your file to the Action(Servlet). In the server side you can use the lib form apache (commons-fileupload). You have many different way to get your file, you can save on disk, read on memory....

Related

How to close InputStream which fed into Response(jax.rs)

#GET
#Path("/{id}/content")
#Produces({ "application/octet-stream" })
public Response getDocumentContentById(#PathParam("id") String docId) {
InputStream is = getDocumentStream(); // some method which gives stream
ResponseBuilder responseBuilder = Response.ok(is);
responseBuilder.header("Content-Disposition", "attachment; filename=" + fileName);
return responseBuilder.build();
}
Here how can I close the InputStream is ? If something(jax.rs) closes automatically. Please give me some information. Thank you.
When you're wanting to stream a custom response, the most reliable way I've found is to return an object that contains the InputStream (or which can obtain the stream in some other way at some point), and to define a MessageBodyWriter provider that will do the actual streaming at the right time.
For example, this code is part of Apache Taverna, and it streams back the zipped contents of a directory. All that the main code needs to do to use it is to return a ZipStream as the response (which can be packaged in a Response or not) and to ensure that it is dealing with returning the application/zip content type. The final point to note is that since this is dealing with CXF, you need to manually register the provider; unlike with Glassfish, they are not automatically picked up. This is a good thing in sophisticated scenarios, but it does mean that you need to do the registration.

Wicket fileUploadField.getFileUpload returning null on AjaxBehaviour

I am trying to get FileUpload from fileupload field on 'onChange' event in wicket.
But it is always giving null after selecting a file. My code looks like this,
fileUploadField.add(new AjaxEventBehavior("onChange") {
#Override
protected void onEvent(AjaxRequestTarget target) {
FileUpload fileUpload = fileUploadField.getFileUpload();
if(fileUpload!=null) {
//my code
}
}
}
I also tried with AjaxFormComponentUpdatingBehavior("onChange").
Please help me.
The FileUpload is really null using AJAX call, because file upload is processing in following steps.
Choose file in a form field
Post the form using HTTP POST request
Receive the HTTP POST request on the server side
Parse the request to a the FileUpload
If you just add an onChange ajax event on your file input field it causes just a GET request on an URL, but it doesn't send the form data.
You can try OnChangeAjaxBehavior or AjaxButton as shown in examples on http://www.wicket-library.com/wicket-examples/ajax/upload

AXIS 2 : java.lang.ClassCastException: com.sun.org.apache.xerces.internal.dom.DeferredElementNSImpl cannot be cast to org.apache.axiom.om.OMElement

I am using AXIS2 as client for handling SOAP response. The client stubs are generated using WSDL2JAVA command. To solve an issue, I am trying to read an xml response stored in .xml file in the generated stub, and assign to SOAPEnvelope. Below is the code written to load .xml content :
InputStream is = new ByteArrayInputStream((sb.toString()).getBytes());
javax.xml.parsers.DocumentBuilderFactory factory = avax.xml.parsers.DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
javax.xml.parsers.DocumentBuilder builder = null;
builder = factory.newDocumentBuilder();
org.w3c.dom.Document doc = builder.parse(is);
System.out.println("Got Document ..............");
is.close();
org.apache.axis2.saaj.util.SAAJUtil su = new org.apache.axis2.saaj.util.SAAJUtil();
org.apache.axiom.soap.SOAPEnvelope _returnEnv1 = su.getSOAPEnvelopeFromDOOMDocument(doc);
Am getting ClassCastException at the last line in the code (assigning to SOAPEnvelope).
Can someone please help me with this.
Axis2clients are used to send requests and receive response. Why are you trying to load response from a file? You should receive the response from the backend service. Check this guide to get to know about the clientapi. You can find detail guide in axis2 documentation also.

Playframework, binary pdf file attachment issue

I´ve got a pdf file as ByteArray and I want to know if there´s a way to attach it without creating the main file on the server.
The code provided by the Play documentation only allows real files to be attached.
EmailAttachment attachment = new EmailAttachment();
attachment.setDescription("A pdf document");
attachment.setPath(Play.getFile("rules.pdf").getPath());
I´m using the Playframework Mail module.
Thanks!
Since Play 1.x uses the Apache Commons Email library under the hood, you could use the MultiPartEmail#attach(DataSource ds, String name, String description) method:
import org.apache.commons.mail.*;
// create the mail
MultiPartEmail email = new MultiPartEmail();
email.setHostName("mail.myserver.com");
email.addTo("jdoe#somewhere.org", "John Doe");
email.setFrom("me#apache.org", "Me");
email.setSubject("The picture");
email.setMsg("Here is the picture you wanted");
// get your inputstream from your db
InputStream is = new BufferedInputStream(MyUtils.getBlob());
DataSource source = new ByteArrayDataSource(is, "application/pdf");
// add the attachment
email.attach(source, "somefile.pdf", "Description of some file");
// send the email
email.send();
Upcoming Play Version 1.3 will introduce a method attachDataSource(), which can be called from within a Mailer class. This will allow you to attach a ByteArray as an attachment to emails easily without the need to save them to the disk first or without the need to use the Apache Commons Emails. You can then use the "standard" Play way.
Here is the corresponding feature request in the play bugtracker:
http://play.lighthouseapp.com/projects/57987/tickets/1500-adding-maillerattachdatasource-functionality

GWT request to download file

I know it's not possible to send an ajax request or use GWT's RequestBuidler to send a request for a file download; needing a form to do it but how do I get a reference to the response when it returns with lets say an error.
The request i send is for a file download but if the file download is too big the Java servlet responds with an error, how do i get a reference to this error to handle it appropriately on the GWT side.
You can add a FormPanel.SubmitCompleteHandler to the form and parse the results in onSubmitComplete().
With extGWT you can parse the HTML response to know the HTML error code
For instance
com.extjs.gxt.ui.client.widget.Component.addListener(Events.Submit, new Listener<FormEvent>()
{
public void handleEvent(final FormEvent event)
{
String htmlResponse = event.getResultHtml();
(...)
}
});