Skip to content

Instantly share code, notes, and snippets.

@saketkc
Created May 14, 2012 14:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saketkc/2694420 to your computer and use it in GitHub Desktop.
Save saketkc/2694420 to your computer and use it in GitHub Desktop.
FileUpload Rest Service
package com.starcamping.boundary.rest;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import com.google.common.collect.Lists;
import com.starcamping.control.FileStore;
import com.starcamping.control.services.FileService;
import com.starcamping.domain.rest.fileupload.File;
import com.starcamping.domain.rest.fileupload.FileMeta;
import com.starcamping.domain.rest.fileupload.FileUrl;
@Path("/images")
public class FileUpload {
@Inject
private FileService fileService;
@Inject
private FileStore fileStore;
@PersistenceContext
private EntityManager em;
@PostConstruct
@SuppressWarnings("unused")
private void postConstruct() {
fileService.initEm(em);
}
@GET
@Path("/url")
public Response getCallbackUrl(@Context HttpServletRequest req) {
return Response.ok(new FileUrl("http://localhost:8080/Star-Camping/rest/images")).build();
}
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
public void post(@Context HttpServletRequest req, @Context HttpServletResponse res) throws IOException, URISyntaxException, ServletException, FileUploadException {
String campingUuid = req.getParameter("campingID");
UUID uuid = UUID.randomUUID();
String randomUUIDString = uuid.toString();
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List<?> items = upload.parseRequest(req);
// Process the uploaded items
Iterator<?> iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
FileMeta meta = new FileMeta(item.getName(), item.getSize(), "http://localhost:8080/Star-Camping/rest/images/" + randomUUIDString, item.getContentType());
File file = new File(item.get(), randomUUIDString, meta);
fileStore.put(campingUuid, randomUUIDString, file);
}
res.sendRedirect("http://localhost:8080/Star-Camping/rest/images/" + randomUUIDString + "/meta?campingID=" + campingUuid);
}
@GET
public Response get(@Context HttpServletRequest req) {
String campingUuid = req.getParameter("campingID");
List<FileMeta> metas = new ArrayList<FileMeta>();
if (fileStore.getFiles(campingUuid) != null) {
for (Map.Entry<String, File> e : fileStore.getFiles(campingUuid).entrySet()) {
metas.add(e.getValue().getMeta());
}
}
GenericEntity<List<FileMeta>> entity = new GenericEntity<List<FileMeta>>(metas) {
};
return Response.ok(entity).build();
}
@GET
@Path("/{uuid}/meta")
public Response redirect(@Context HttpServletRequest req, @PathParam("uuid") String uuid) throws IOException {
String campingUuid = req.getParameter("campingID");
File file = fileStore.get(campingUuid, uuid);
if (file != null) {
FileMeta meta = file.getMeta();
List<FileMeta> metas = Lists.newArrayList(meta);
GenericEntity<List<FileMeta>> entity = new GenericEntity<List<FileMeta>>(metas) {
};
return Response.ok(entity).build();
} else {
return Response.status(Status.NOT_FOUND).build();
}
}
@GET
@Path("/{uuid}")
public Response serve(@PathParam("uuid") String uuid, @Context HttpServletResponse response) throws IOException {
File file = fileService.findByUuid(uuid);
return Response.ok(file.getData(), file.getMeta().getContentType()).header("Content-Disposition", "attachment; filename=" + file.getMeta().getName()).build();
}
@DELETE
@Path("/{uuid}")
public Response delete(@Context HttpServletRequest req, @PathParam("uuid") String uuid) {
Status status;
String campingUuid = req.getParameter("campingID");
if (fileStore.remove(campingUuid, uuid)) {
status = Status.OK;
} else {
status = Status.NOT_FOUND;
}
return Response.status(status).build();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment