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: [email protected]
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: [email protected]
For additional commands, e-mail: [email protected]