View Javadoc

1   /*
2    * Copyright 2006-2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * A servlet which can be used to retrieve attachments from Notes.
46   * 
47   * @author Kuali Rice Team (rice.collab@kuali.org)
48   */
49  public class AttachmentServlet extends HttpServlet {
50  	
51  	private static final long serialVersionUID = -1918858512573502697L;
52  	public static final String ATTACHMENT_ID_KEY = "attachmentId";
53  
54  	// TODO This should probably be put into KEWConstants when contributed back
55  	// to Rice 1.0.3
56  	private static final Logger LOG = Logger.getLogger(AttachmentServlet.class);
57  			
58  	@Override
59  	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
60  		Long attachmentId = new Long(request.getParameter(ATTACHMENT_ID_KEY));
61  		if (attachmentId == null) {
62  			throw new ServletException("No 'attachmentId' was specified.");
63  		}
64  		
65  		boolean secureChecks = true;
66  		String secureAttachmentsParam = null;
67  		try {
68  			secureAttachmentsParam = CoreFrameworkServiceLocator.getParameterService().getParameterValueAsString(KEWConstants.KEW_NAMESPACE, "All", KEWConstants.SECURE_ATTACHMENTS_PARAM);
69  		} catch (Exception e) {
70  			LOG.info("Attempted to retrieve parameter value, but could not. Defaulting to unsecured attachment retrieval. " + e.getMessage());
71  		}
72  		if (secureAttachmentsParam != null && secureAttachmentsParam.equals("N")) {
73  			secureChecks = false;
74  		}
75  		try {
76  			UserSession userSession = (UserSession) request.getSession().getAttribute(KNSConstants.USER_SESSION_KEY);
77  			if (userSession != null) {// If we can get a valid userSession object off the Http request...
78  				
79  				NoteService noteService = KEWServiceLocator.getNoteService(); 
80  				Attachment attachment = noteService.findAttachment(attachmentId);
81  				File file = noteService.findAttachmentFile(attachment);
82  				
83  				DocumentRouteHeaderValue routeHeader = KEWServiceLocator.getRouteHeaderService().getRouteHeader(noteService.getNoteByNoteId(attachment.getNoteId()).getDocumentId());
84  				
85  				if(!secureChecks || routeHeader != null){// If we can get a valid routeHeader based on the requested attachment ID
86  					boolean authorized = KEWServiceLocator.getDocumentSecurityService().routeLogAuthorized(userSession, routeHeader, new SecuritySession(userSession));
87  					
88  					if(!secureChecks || authorized){// If this user can see this document, they can get the attachment(s)
89  						response.setContentLength((int)file.length());
90  						response.setContentType(attachment.getMimeType());
91  						response.setHeader("Content-disposition", "attachment; filename="+attachment.getFileName());
92  						FileInputStream attachmentFile = new FileInputStream(file);
93  						BufferedInputStream inputStream = new BufferedInputStream(attachmentFile);
94  						OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
95  
96  						try {
97  							int c;
98  							while ((c = inputStream.read()) != -1) {
99  								outputStream.write(c);
100 							}
101 						} finally {
102 							inputStream.close();
103 						}
104 						outputStream.close();
105 					} else {// Throw a forbidden page back, they were not approved by DocumentSecurityService
106 						LOG.error("Attempt to access attachmentId:"+ attachmentId + " from documentId:" + routeHeader.getDocumentId() + " from unauthorized user: " + userSession.getPrincipalId());
107 						response.sendError(HttpServletResponse.SC_FORBIDDEN);
108 						return;
109 					}
110 				} else {// Throw a not found, couldn't get a valid routeHeader
111 					LOG.error("Caught Null Pointer trying to determine routeHeader for requested attachmentId:" + attachmentId);
112 					response.sendError(HttpServletResponse.SC_NOT_FOUND);
113 					return;
114 				}
115 			} else {// Throw a bad request, we couldn't find a valid user session
116 				LOG.error("Attempt to access attachmentId:" + attachmentId + " with invalid UserSession");
117 				response.sendError(HttpServletResponse.SC_BAD_REQUEST);
118 				return;
119 			}
120 		} catch (Exception e) {// Catch any error, log it. Send a not found, and throw up the exception.
121 			LOG.error("Problem retrieving requested attachmentId:" + attachmentId, e);
122 			throw new WorkflowRuntimeException(e);
123 		}
124 	}
125 	@Override
126 	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
127 		doPost(request, response);
128 	}	
129 	
130 }