You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Andre Juffer <aj...@cc.oulu.fi> on 2011/11/02 10:28:28 UTC
[cocoon3] @Context Request request
I was able to resolve one or two problems. The commons fileupload FAQ
gave a hint [1]. Thus, I looked for a solution that relies on a Filter
and came across [2]. This Filter creates a HttpServletRequestWrapper
(implements HttpServletRequest) that parses the request whenever dealing
with a file upload using commons fileupload. This works very nice. I was
supposed to see two FileItems [3] (which I never got up to this point)
and this is now exactly what I get. Thus, the request is parsed properly
and everything (including the image file) is available.
However, the filter continues with
aChain.doFilter(wrapper, aResponse);
and this eventually results in an exception (listing below just the
cocoon part):
com.sun.jersey.api.container.ContainerException:
javax.mail.MessagingException: Missing start boundary
at
com.sun.jersey.server.impl.model.method.dispatch.MultipartFormDispatchProvider.processForm(MultipartFormDispatchProvider.java:91)
....
at
org.apache.cocoon.rest.jaxrs.container.CocoonJAXRSServlet.service(CocoonJAXRSServlet.java:60)
....
at
org.apache.cocoon.servletservice.ServletServiceContext$PathDispatcher.forward(ServletServiceContext.java:481)
at
org.apache.cocoon.servletservice.ServletServiceContext$PathDispatcher.forward(ServletServiceContext.java:455)
at
org.apache.cocoon.servletservice.spring.ServletFactoryBean$ServiceInterceptor.invoke(ServletFactoryBean.java:245)
...
com.tribc.servlet.FileUploadFilter.doFilter(FileUploadFilter.java:70)
(This is the aChain.doFilter(wrapper, aResponse);
...
Therefore, the request now never reaches the ImageResource.
I cannot really understand the exception. The
javax.mail.MessagingException is puzzling. I have to assume that in [2]
something is not entirely correct, as the exception only occurs whenever
the filter is employed. I do not see it. As said, the parsing appears to
work just fine. Hopefully one of you has a clue.
Thanks,
André
[1] http://commons.apache.org/fileupload/faq.html#empty-parse
[2] http://www.javapractices.com/topic/TopicAction.do?Id=221
[3]
http://commons.apache.org/fileupload/apidocs/org/apache/commons/fileupload/FileItem.html
--
Andre H. Juffer | Phone: +358-8-553 1161
Biocenter Oulu and | Fax: +358-8-553-1141
Department of Biochemistry | Email: andre.juffer@oulu.fi
University of Oulu, Finland | WWW: www.biochem.oulu.fi/Biocomputing/
StruBioCat | WWW: www.strubiocat.oulu.fi
Triacle Biocomputing | WWW: www.triacle-bc.com
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org
Re: [cocoon3] @Context Request request
Posted by Andre Juffer <aj...@cc.oulu.fi>.
Hi All,
I have followed another track to be able to an upload image file that
are processed by a REST resource. It does not rely upon the @Context
Request request. The Filter I have tried before (while actually working
correctly) did cause particular issues that I was not able to correct.
Below is a copy of a simple REST ImageResource that is capable of
extracting a single image and storing it in a given location. There is
no need to modify anything in web.xml (as with the Filer).
Here is the code:
<code>
import org.apache.cocoon.configuration.Settings;
import org.apache.cocoon.rest.jaxrs.response.URLResponseBuilder;
import javax.ws.rs.*;
import javax.ws.rs.core.*;
import com.sun.jersey.multipart.*;
import java.util.*;
import java.io.*;
/**
*
* @author André Juffer, Triacle Biocomputing
*/
@Path("/image")
public class ImageResource {
private String tempFolder;
public ImageResource()
{
this.tempFolder = null;
}
public void setTempFolder(String tempFolder)
{
this.tempFolder = tempFolder;
}
/**
* Uploads a single image.
* @param parts Requests parts.
* @return
*/
@POST
@Path("/creator/{id}")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces({"application/xml", "application/json"})
public Response newImage(@PathParam("id") String creatorId,
final FormDataMultiPart parts)
{
// Find part with image.
InputStream inputStream = null;
String filename = null;
Iterator<BodyPart> iterator = parts.getBodyParts().iterator();
while ( iterator.hasNext() && inputStream == null ) {
BodyPart bodyPart = iterator.next();
if ( this.isImage(bodyPart) ) {
inputStream = this.getInputStream(bodyPart);
filename = this.getFilename(bodyPart);
}
}
// NOTE: Other parts representing other form fields may be
// extracted as well. One can even extract more than one
// file.
// Do something with image. E.g. move to some folder.
String destFilename = tempFolder + "/" + filename;
MoveFile.moveToFolder(inputStream, destFilename);
// Point to e.g. a pipeline in your sitemap.
String url = "....";
// Create response.
Map<String, Object> map = new HashMap<String, Object>();
return URLResponseBuilder.newInstance(url, map).build();
}
private String getFilename(BodyPart bodyPart)
{
return bodyPart.getContentDisposition().getFileName();
}
private boolean isImage(BodyPart bodyPart)
{
return bodyPart.getMediaType().getType().indexOf("image") != -1;
}
private InputStream getInputStream(BodyPart bodyPart)
{
return ((BodyPartEntity)bodyPart.getEntity()).getInputStream();
}
}
</code>
You need to include in your pom.xml the following dependency:
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-multipart</artifactId>
<version>1.8</version>
</dependency>
Feel free to use this code for your own purposes.
Best regards,
--
Andre H. Juffer | Phone: +358-8-553 1161
Biocenter Oulu and | Fax: +358-8-553-1141
Department of Biochemistry | Email: andre.juffer@oulu.fi
University of Oulu, Finland | WWW: www.biochem.oulu.fi/Biocomputing/
StruBioCat | WWW: www.strubiocat.oulu.fi
Triacle Biocomputing | WWW: www.triacle-bc.com
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org