cocoon-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Andre Juffer <>
Subject Re: [cocoon3] @Context Request request
Date Fri, 04 Nov 2011 15:02:26 GMT
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:


import org.apache.cocoon.configuration.Settings;

import com.sun.jersey.multipart.*;

import java.util.*;

  * @author Andr&#233; Juffer, Triacle Biocomputing
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
     @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 =;
             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();


You need to include in your pom.xml the following 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:
University of Oulu, Finland  | WWW:
StruBioCat                   | WWW:
Triacle Biocomputing         | WWW:

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message