|  1 |     | 
     | 
  |  2 |     | 
     | 
  |  3 |     | 
     | 
  |  4 |     | 
     | 
  |  5 |     | 
     | 
  |  6 |     | 
     | 
  |  7 |     | 
     | 
  |  8 |     | 
     | 
  |  9 |     | 
     | 
  |  10 |     | 
     | 
  |  11 |     | 
     | 
  |  12 |     | 
     | 
  |  13 |     | 
     | 
  |  14 |     | 
     | 
  |  15 |     | 
     | 
  |  16 |     | 
   package org.kuali.rice.kew.notes.web;  | 
  |  17 |     | 
     | 
  |  18 |     | 
   import org.apache.log4j.Logger;  | 
  |  19 |     | 
   import org.kuali.rice.core.framework.services.CoreFrameworkServiceLocator;  | 
  |  20 |     | 
   import org.kuali.rice.kew.doctype.SecuritySession;  | 
  |  21 |     | 
   import org.kuali.rice.kew.exception.WorkflowRuntimeException;  | 
  |  22 |     | 
   import org.kuali.rice.kew.notes.Attachment;  | 
  |  23 |     | 
   import org.kuali.rice.kew.notes.service.NoteService;  | 
  |  24 |     | 
   import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;  | 
  |  25 |     | 
   import org.kuali.rice.kew.service.KEWServiceLocator;  | 
  |  26 |     | 
   import org.kuali.rice.kew.util.KEWConstants;  | 
  |  27 |     | 
   import org.kuali.rice.kns.UserSession;  | 
  |  28 |     | 
   import org.kuali.rice.kns.util.KNSConstants;  | 
  |  29 |     | 
     | 
  |  30 |     | 
   import javax.servlet.ServletException;  | 
  |  31 |     | 
   import javax.servlet.http.HttpServlet;  | 
  |  32 |     | 
   import javax.servlet.http.HttpServletRequest;  | 
  |  33 |     | 
   import javax.servlet.http.HttpServletResponse;  | 
  |  34 |     | 
   import java.io.BufferedInputStream;  | 
  |  35 |     | 
   import java.io.BufferedOutputStream;  | 
  |  36 |     | 
   import java.io.File;  | 
  |  37 |     | 
   import java.io.FileInputStream;  | 
  |  38 |     | 
   import java.io.IOException;  | 
  |  39 |     | 
   import java.io.OutputStream;  | 
  |  40 |     | 
     | 
  |  41 |     | 
     | 
  |  42 |     | 
     | 
  |  43 |     | 
     | 
  |  44 |     | 
     | 
  |  45 |     | 
     | 
  |  46 |     | 
     | 
  |  47 |     | 
     | 
  |  48 |     | 
     | 
  |  49 |    0 |    public class AttachmentServlet extends HttpServlet { | 
  |  50 |     | 
             | 
  |  51 |     | 
           private static final long serialVersionUID = -1918858512573502697L;  | 
  |  52 |     | 
           public static final String ATTACHMENT_ID_KEY = "attachmentId";  | 
  |  53 |     | 
     | 
  |  54 |     | 
             | 
  |  55 |     | 
             | 
  |  56 |    0 |            private static final Logger LOG = Logger.getLogger(AttachmentServlet.class);  | 
  |  57 |     | 
                             | 
  |  58 |     | 
           @Override  | 
  |  59 |     | 
           protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | 
  |  60 |    0 |                    Long attachmentId = new Long(request.getParameter(ATTACHMENT_ID_KEY));  | 
  |  61 |    0 |                    if (attachmentId == null) { | 
  |  62 |    0 |                            throw new ServletException("No 'attachmentId' was specified."); | 
  |  63 |     | 
                   }  | 
  |  64 |     | 
                     | 
  |  65 |    0 |                    boolean secureChecks = true;  | 
  |  66 |    0 |                    String secureAttachmentsParam = null;  | 
  |  67 |     | 
                   try { | 
  |  68 |    0 |                            secureAttachmentsParam = CoreFrameworkServiceLocator.getParameterService().getParameterValueAsString(KEWConstants.KEW_NAMESPACE, "All", KEWConstants.SECURE_ATTACHMENTS_PARAM);  | 
  |  69 |    0 |                    } catch (Exception e) { | 
  |  70 |    0 |                            LOG.info("Attempted to retrieve parameter value, but could not. Defaulting to unsecured attachment retrieval. " + e.getMessage()); | 
  |  71 |    0 |                    }  | 
  |  72 |    0 |                    if (secureAttachmentsParam != null && secureAttachmentsParam.equals("N")) { | 
  |  73 |    0 |                            secureChecks = false;  | 
  |  74 |     | 
                   }  | 
  |  75 |     | 
                   try { | 
  |  76 |    0 |                            UserSession userSession = (UserSession) request.getSession().getAttribute(KNSConstants.USER_SESSION_KEY);  | 
  |  77 |    0 |                            if (userSession != null) { | 
  |  78 |     | 
                                     | 
  |  79 |    0 |                                    NoteService noteService = KEWServiceLocator.getNoteService();   | 
  |  80 |    0 |                                    Attachment attachment = noteService.findAttachment(attachmentId);  | 
  |  81 |    0 |                                    File file = noteService.findAttachmentFile(attachment);  | 
  |  82 |     | 
                                     | 
  |  83 |    0 |                                    DocumentRouteHeaderValue routeHeader = KEWServiceLocator.getRouteHeaderService().getRouteHeader(noteService.getNoteByNoteId(attachment.getNoteId()).getRouteHeaderId());  | 
  |  84 |     | 
                                     | 
  |  85 |    0 |                                    if(!secureChecks || routeHeader != null){ | 
  |  86 |    0 |                                            boolean authorized = KEWServiceLocator.getDocumentSecurityService().routeLogAuthorized(userSession, routeHeader, new SecuritySession(userSession));  | 
  |  87 |     | 
                                             | 
  |  88 |    0 |                                            if(!secureChecks || authorized){ | 
  |  89 |    0 |                                                    response.setContentLength((int)file.length());  | 
  |  90 |    0 |                                                    response.setContentType(attachment.getMimeType());  | 
  |  91 |    0 |                                                    response.setHeader("Content-disposition", "attachment; filename="+attachment.getFileName()); | 
  |  92 |    0 |                                                    FileInputStream attachmentFile = new FileInputStream(file);  | 
  |  93 |    0 |                                                    BufferedInputStream inputStream = new BufferedInputStream(attachmentFile);  | 
  |  94 |    0 |                                                    OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());  | 
  |  95 |     | 
     | 
  |  96 |     | 
                                                   try { | 
  |  97 |     | 
                                                           int c;  | 
  |  98 |    0 |                                                            while ((c = inputStream.read()) != -1) { | 
  |  99 |    0 |                                                                    outputStream.write(c);  | 
  |  100 |     | 
                                                           }  | 
  |  101 |     | 
                                                   } finally { | 
  |  102 |    0 |                                                            inputStream.close();  | 
  |  103 |    0 |                                                    }  | 
  |  104 |    0 |                                                    outputStream.close();  | 
  |  105 |    0 |                                            } else { | 
  |  106 |    0 |                                                    LOG.error("Attempt to access attachmentId:"+ attachmentId + " from routeHeaderId:" + routeHeader.getRouteHeaderId() + " from unauthorized user: " + userSession.getPrincipalId()); | 
  |  107 |    0 |                                                    response.sendError(HttpServletResponse.SC_FORBIDDEN);  | 
  |  108 |    0 |                                                    return;  | 
  |  109 |     | 
                                           }  | 
  |  110 |    0 |                                    } else { | 
  |  111 |    0 |                                            LOG.error("Caught Null Pointer trying to determine routeHeader for requested attachmentId:" + attachmentId); | 
  |  112 |    0 |                                            response.sendError(HttpServletResponse.SC_NOT_FOUND);  | 
  |  113 |    0 |                                            return;  | 
  |  114 |     | 
                                   }  | 
  |  115 |    0 |                            } else { | 
  |  116 |    0 |                                    LOG.error("Attempt to access attachmentId:" + attachmentId + " with invalid UserSession"); | 
  |  117 |    0 |                                    response.sendError(HttpServletResponse.SC_BAD_REQUEST);  | 
  |  118 |    0 |                                    return;  | 
  |  119 |     | 
                           }  | 
  |  120 |    0 |                    } catch (Exception e) { | 
  |  121 |    0 |                            LOG.error("Problem retrieving requested attachmentId:" + attachmentId, e); | 
  |  122 |    0 |                            throw new WorkflowRuntimeException(e);  | 
  |  123 |    0 |                    }  | 
  |  124 |    0 |            }  | 
  |  125 |     | 
           @Override  | 
  |  126 |     | 
           protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | 
  |  127 |    0 |                    doPost(request, response);  | 
  |  128 |    0 |            }          | 
  |  129 |     | 
             | 
  |  130 |     | 
   }  |