Created
January 26, 2011 00:50
-
-
Save pashields/796023 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.floop.rest.api; | |
import java.io.File; | |
import java.util.Iterator; | |
import java.util.List; | |
import org.apache.commons.fileupload.FileItem; | |
import org.apache.commons.fileupload.disk.DiskFileItemFactory; | |
import org.apache.http.HttpEntity; | |
import org.apache.http.HttpResponse; | |
import org.apache.http.client.HttpClient; | |
import org.apache.http.client.methods.HttpPost; | |
import org.apache.http.entity.mime.MultipartEntity; | |
import org.apache.http.entity.mime.content.InputStreamBody; | |
import org.apache.http.impl.client.DefaultHttpClient; | |
import org.restlet.data.MediaType; | |
import org.restlet.data.Status; | |
import org.restlet.ext.fileupload.RestletFileUpload; | |
import org.restlet.representation.FileRepresentation; | |
import org.restlet.representation.Representation; | |
import org.restlet.representation.StringRepresentation; | |
import org.restlet.resource.Post; | |
import org.restlet.resource.ServerResource; | |
public class UserPhotoResource extends ServerResource { | |
/** | |
* Accepts and processes a representation posted to the resource. As | |
* response, the content of the uploaded file is sent back the client. | |
*/ | |
@Post | |
public Representation accept(Representation entity) throws Exception { | |
Representation rep = null; | |
if (entity != null) { | |
if (MediaType.MULTIPART_FORM_DATA.equals(entity.getMediaType(), true)) { | |
String fileName = "/tmp/" | |
+ getRequest().getAttributes().get("userId"); | |
// The Apache FileUpload project parses HTTP requests which | |
// conform to RFC 1867, "Form-based File Upload in HTML". That | |
// is, if an HTTP request is submitted using the POST method, | |
// and with a content type of "multipart/form-data", then | |
// FileUpload can parse that request, and get all uploaded files | |
// as FileItem. | |
// 1/ Create a factory for disk-based file items | |
DiskFileItemFactory factory = new DiskFileItemFactory(); | |
factory.setSizeThreshold(1000240); | |
// 2/ Create a new file upload handler based on the Restlet | |
// FileUpload extension that will parse Restlet requests and | |
// generates FileItems. | |
RestletFileUpload upload = new RestletFileUpload(factory); | |
List<FileItem> items; | |
// 3/ Request is parsed by the handler which generates a | |
// list of FileItems | |
items = upload.parseRequest(getRequest()); | |
// Process only the uploaded item called "fileToUpload" and | |
// save it on disk | |
boolean found = false; | |
for (final Iterator<FileItem> it = items.iterator(); it.hasNext() | |
&& !found;) { | |
FileItem fi = it.next(); | |
if (fi.getFieldName().equals("fileToUpload")) { | |
found = true; | |
/* Pat's all new crazy */ | |
if (((String) getRequest().getAttributes().get("userId")) | |
.compareTo("foobar") != 0) { | |
MultipartEntity reqEntity = new MultipartEntity(); | |
reqEntity.addPart( | |
"avatar", | |
new InputStreamBody(fi.getInputStream(), fi | |
.getName())); | |
HttpClient httpclient = new DefaultHttpClient(); | |
HttpPost httppost = new HttpPost( | |
"http://localhost:8080/api/users/foobar/photo"); | |
httppost.setEntity(reqEntity); | |
HttpResponse response = httpclient.execute(httppost); | |
HttpEntity resEntity = response.getEntity(); | |
if (resEntity != null) { | |
resEntity.consumeContent(); | |
} | |
httpclient.getConnectionManager().shutdown(); | |
} | |
} | |
else { | |
File file = new File(fileName); | |
fi.write(file); | |
} | |
} | |
// Once handled, the content of the uploaded file is sent | |
// back to the client. | |
if (found) { | |
// Create a new representation based on disk file. | |
// The content is arbitrarily sent as plain text. | |
rep = new FileRepresentation(new File(fileName), | |
MediaType.TEXT_PLAIN, 0); | |
} else { | |
// Some problem occurs, sent back a simple line of text. | |
rep = new StringRepresentation("no file uploaded", | |
MediaType.TEXT_PLAIN); | |
} | |
} | |
} else { | |
// POST request with no entity. | |
setStatus(Status.CLIENT_ERROR_BAD_REQUEST); | |
} | |
return rep; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment