// remove photo api @Override @DELETE @Path("/remove") @Produces(MediaType.APPLICATION_XML) @Transactional(propagation = Propagation.NESTED) public Response removePhoto(Integer photoId, @Context HttpServletRequest request) { try { Photo photo = photoDao.load(photoId); Token tokenObj = (Token) request.getAttribute("token"); User user = tokenObj.getUser(); if (user.equals(photo.getUser())) { new File(photo.getPath()).delete(); // delete the file photoDao.delete(photo); } GetPhotoResponseDTO dto = buildDTO(200, null); dto.setImageId(photoId); return Response.status(200).entity(dto).build(); } catch (HibernateException e) { e.printStackTrace(); return Response.status(500).entity(buildDTO(500, null)).build(); } }
// find uploaded photo api @Override @GET @Path("/my_photos") @Produces(MediaType.APPLICATION_XML) @Transactional(propagation = Propagation.NESTED) public Response getMyImages(HttpServletRequest request) { try { Token tokenObj = (Token) request.getAttribute("token"); Integer page = Integer.parseInt(request.getParameter("page")); Integer limit = Integer.parseInt(request.getParameter("limit")); // if(tokenObj == null) return Response.status(401).entity(buildDTO(401, null)).build(); User user = tokenObj.getUser(); List<Photo> photos = photoDao.getPhotoByUser(user, PagingUtil.doPaging(page, limit), limit); List<String> imageIds = buildImageStringList(photos); return Response.status(200).entity(buildDTO(200, imageIds)).build(); } catch (HibernateException e) { e.printStackTrace(); return Response.status(500).entity(buildDTO(500, null)).build(); } catch (FileNotFoundException e) { e.printStackTrace(); return Response.status(500).entity(buildDTO(500, null)).build(); } catch (IOException e) { e.printStackTrace(); return Response.status(500).entity(buildDTO(500, null)).build(); } }
@Override @PUT @Path("/like/{id}") @Transactional(propagation = Propagation.NESTED) public Response likePhoto(@PathParam("id") Integer id, @Context HttpServletRequest request) { try { Token tokenObj = (Token) request.getAttribute("token"); Photo photo = photoDao.load(id); User likeGiver = tokenObj.getUser(); LikeInfor likeInfor = likeInforDao.findLikeInfor(likeGiver, photo); if (likeInfor != null) { if (likeInfor.isStatus()) { likeInfor.setStatus(false); } else { likeInfor.setStatus(true); } } else { likeInfor = new LikeInfor(); likeInfor.setPhoto(photo); likeInfor.setUser(likeGiver); likeInforDao.save(likeInfor); } return Response.status(200).entity(buildDTO(200, null)).build(); } catch (HibernateException e) { e.printStackTrace(); return Response.status(500).entity(buildDTO(500, null)).build(); } }
// uplaod api @Override @POST @Path("/upload") @Consumes(MediaType.MULTIPART_FORM_DATA) @Produces(MediaType.APPLICATION_XML) @Transactional(propagation = Propagation.NESTED) public Response saveImage(@Context HttpServletRequest request) { String comment = null; // Token tokenObj = (Token) request.getAttribute("token"); // byte[] image = null; InputStream is = null; BufferedOutputStream out = null; String path = null; try { ServletFileUpload servletFileUpload = new ServletFileUpload(); FileItemIterator fileItemIterator = servletFileUpload.getItemIterator(request); while (fileItemIterator.hasNext()) { FileItemStream fileItemStream = fileItemIterator.next(); if ("comment".equals(fileItemStream.getFieldName())) { StringWriter stringWriter = new StringWriter(); IOUtils.copy(fileItemStream.openStream(), stringWriter, "utf-8"); comment = stringWriter.toString(); } else if ("content".equals(fileItemStream.getFieldName())) { path = createPath(); // save image content // path = ("/Users/aoden/Desktop/aaaaaa.jpg"); is = fileItemStream.openStream(); out = new BufferedOutputStream(new FileOutputStream(path)); int data = -1; while ((data = is.read()) != -1) { out.write(data); } } } Token tokenObj = (Token) request.getAttribute("token"); Photo photo = new Photo(); photo.setComment(comment); photo.setPath(path); photo.setUser(tokenObj.getUser()); photoDao.save(photo); return Response.status(500).entity(buildDTO(200, null)).build(); // User user = tokenObj.getUser(); } catch (HibernateException e) { e.printStackTrace(); return Response.status(500).entity(buildDTO(500, null)).build(); } catch (FileUploadException e) { e.printStackTrace(); return Response.status(500).entity(buildDTO(500, null)).build(); } catch (IOException e) { e.printStackTrace(); return Response.status(500).entity(buildDTO(500, null)).build(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); return Response.status(500).entity(buildDTO(500, null)).build(); } finally { // close the streams if (is != null) try { is.close(); } catch (IOException e) { e.printStackTrace(); } if (out != null) try { out.close(); } catch (IOException e) { e.printStackTrace(); } } }