1 package usecase.post; 2 3 import common.http.interceptor.InterceptableServlet; 4 import usecase.auth.AuthorizationConstraints; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.annotation.MultipartConfig; 8 import javax.servlet.annotation.WebServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 import java.io.IOException; 12 13 import static usecase.auth.AuthorizationConstraints.Types.REQUIRE_AUTHENTICATION; 14 15 /** 16 * Classe che permette di modificare un post. 17 */ 18 @WebServlet("/editpost") 19 @MultipartConfig 20 @AuthorizationConstraints(REQUIRE_AUTHENTICATION) 21 class EditPostServlet extends InterceptableServlet { 22 //Funzionalità disabilitata 23 24 @Override 25 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 26 resp.sendError(HttpServletResponse.SC_FORBIDDEN, "Funzionalità disabilitata temporaneamente"); 27 } 28 29 30 /*@Inject private PostService service; 31 32 private static final String EDIT_POST_PAGE = "/WEB-INF/views/section/edit-post.jsp"; 33 34 @Override 35 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 36 //check necessario? 37 ParameterConverter converter = new ParameterConverter(request); 38 int postId = converter.getIntParameter("id").orElse(0); 39 PostPage post = service.getPost(postId); 40 request.setAttribute("post", post); 41 request.getRequestDispatcher(EDIT_POST_PAGE).forward(request, response); 42 } 43 44 @Override 45 @ForwardOnError(EDIT_POST_PAGE) 46 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 47 ParameterConverter converter = new ParameterConverter(request); 48 String title = request.getParameter("title"); 49 String type = request.getParameter("type"); 50 Post.Type postType = type != null && type.equalsIgnoreCase("text") ? Post.Type.TEXT : Post.Type.IMG; 51 Part picture = request.getPart("picture"); 52 String content = request.getParameter("content"); 53 54 PostEditDTO postToEdit = new PostEditDTO(title,content,postType); 55 //in caso di immagine il content sarà null infatti non sarà utilizzato nel servizio EditPostIMG 56 57 int postId = converter.getIntParameter("id").orElse(0); 58 if(postType == Post.Type.TEXT){ 59 service.editPost(postToEdit,postId); 60 }else{ 61 BufferedInputStream buff = new BufferedInputStream(picture.getInputStream()); 62 service.editPost(postToEdit,postId,buff); 63 } 64 response.sendRedirect(getServletContext().getContextPath() + "/post/" + postId); 65 } 66 */ 67 }