1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.student.core.document.ui.server.upload; |
17 | |
|
18 | |
import java.io.ByteArrayOutputStream; |
19 | |
import java.io.IOException; |
20 | |
import java.io.InputStream; |
21 | |
import java.io.PrintWriter; |
22 | |
|
23 | |
import javax.servlet.ServletContext; |
24 | |
import javax.servlet.ServletException; |
25 | |
import javax.servlet.ServletOutputStream; |
26 | |
import javax.servlet.http.HttpServlet; |
27 | |
import javax.servlet.http.HttpServletRequest; |
28 | |
import javax.servlet.http.HttpServletResponse; |
29 | |
import javax.servlet.http.HttpSession; |
30 | |
|
31 | |
import org.apache.commons.codec.binary.Base64; |
32 | |
import org.apache.commons.fileupload.FileItemIterator; |
33 | |
import org.apache.commons.fileupload.FileItemStream; |
34 | |
import org.apache.commons.fileupload.ProgressListener; |
35 | |
import org.apache.commons.fileupload.servlet.ServletFileUpload; |
36 | |
import org.apache.commons.fileupload.util.Streams; |
37 | |
import org.apache.commons.io.FilenameUtils; |
38 | |
import org.apache.log4j.Logger; |
39 | |
import org.kuali.student.common.dto.RichTextInfo; |
40 | |
import org.kuali.student.common.dto.DtoConstants.DtoState; |
41 | |
import org.kuali.student.common.ui.client.dto.FileStatus; |
42 | |
import org.kuali.student.common.ui.client.dto.UploadStatus; |
43 | |
import org.kuali.student.common.ui.client.dto.FileStatus.FileTransferStatus; |
44 | |
import org.kuali.student.common.ui.client.dto.UploadStatus.UploadTransferStatus; |
45 | |
import org.kuali.student.core.document.dto.DocumentBinaryInfo; |
46 | |
import org.kuali.student.core.document.dto.DocumentInfo; |
47 | |
import org.kuali.student.core.document.dto.RefDocRelationInfo; |
48 | |
import org.kuali.student.core.document.service.DocumentService; |
49 | |
|
50 | 0 | public class UploadServlet extends HttpServlet{ |
51 | 0 | final Logger LOG = Logger.getLogger(UploadServlet.class); |
52 | |
private static final long serialVersionUID = 1L; |
53 | |
DocumentService documentService; |
54 | |
|
55 | 0 | private class DocumentProgressListener implements ProgressListener{ |
56 | |
|
57 | 0 | private long kiloBytes = -1; |
58 | |
private String uploadId; |
59 | 0 | private int currentItem = 1; |
60 | 0 | private boolean uploadFinished = false; |
61 | |
private HttpSession session; |
62 | |
|
63 | 0 | public DocumentProgressListener(String uploadId, HttpSession session){ |
64 | 0 | this.uploadId = uploadId; |
65 | 0 | this.session = session; |
66 | 0 | } |
67 | |
|
68 | |
public void update(long pBytesRead, long pContentLength, int pItems) { |
69 | 0 | UploadStatus status = (UploadStatus) (session.getAttribute(uploadId)); |
70 | 0 | if(status.getTotalSize() == null && pContentLength != -1){ |
71 | 0 | status.setTotalSize(pContentLength); |
72 | |
} |
73 | |
|
74 | 0 | if(!uploadFinished && pBytesRead == pContentLength){ |
75 | 0 | status.setItemsRead(status.getItemsRead()+ 1); |
76 | 0 | status.setStatus(UploadTransferStatus.UPLOAD_FINISHED); |
77 | 0 | uploadFinished = true; |
78 | |
} |
79 | |
|
80 | |
|
81 | 0 | long kBytes = pBytesRead / 1024; |
82 | 0 | if (kiloBytes == kBytes) { |
83 | 0 | return; |
84 | |
} |
85 | 0 | kiloBytes = kBytes; |
86 | |
|
87 | 0 | status.setProgress(pBytesRead); |
88 | 0 | if(pItems > currentItem){ |
89 | 0 | status.setItemsRead(status.getItemsRead()+ 1); |
90 | |
} |
91 | |
|
92 | 0 | } |
93 | |
} |
94 | |
|
95 | |
String data; |
96 | |
|
97 | |
@Override |
98 | |
protected void doPost(HttpServletRequest request, HttpServletResponse response) |
99 | |
throws ServletException, IOException { |
100 | 0 | UploadStatus status = new UploadStatus(); |
101 | 0 | request.getSession().setAttribute(request.getParameter("uploadId"), status); |
102 | |
try { |
103 | |
|
104 | 0 | ServletFileUpload upload = new ServletFileUpload(); |
105 | 0 | upload.setProgressListener(new DocumentProgressListener(request.getParameter("uploadId"), request.getSession())); |
106 | 0 | FileItemIterator iter = upload.getItemIterator(request); |
107 | 0 | DocumentInfo info = new DocumentInfo(); |
108 | |
|
109 | 0 | boolean fileError = false; |
110 | 0 | int currentItem = 0; |
111 | 0 | while (iter.hasNext()) { |
112 | 0 | FileItemStream item = iter.next(); |
113 | 0 | String name = item.getFieldName(); |
114 | 0 | InputStream stream = item.openStream(); |
115 | 0 | if (item.isFormField()) { |
116 | 0 | String value = Streams.asString(stream); |
117 | 0 | if(name.equals("documentDescription")){ |
118 | 0 | RichTextInfo text = new RichTextInfo(); |
119 | 0 | text.setPlain(value); |
120 | 0 | info.setDesc(text); |
121 | |
} |
122 | 0 | } else { |
123 | 0 | String fullFileName = item.getName(); |
124 | 0 | if (fullFileName != null) { |
125 | 0 | String filename = FilenameUtils.getName(fullFileName); |
126 | 0 | FileStatus fileStatus = new FileStatus(); |
127 | 0 | fileStatus.setFileName(filename); |
128 | 0 | status.getFileStatusList().add(currentItem, fileStatus); |
129 | 0 | DocumentBinaryInfo bInfo = new DocumentBinaryInfo(); |
130 | 0 | ByteArrayOutputStream bytes = new ByteArrayOutputStream(); |
131 | 0 | byte[] buffer = new byte[4096]; |
132 | |
while (true) { |
133 | 0 | int read = stream.read(buffer); |
134 | 0 | if (read == -1) { |
135 | 0 | break; |
136 | |
} else { |
137 | 0 | bytes.write(buffer, 0, read); |
138 | 0 | fileStatus.setProgress(fileStatus.getProgress() + read); |
139 | |
} |
140 | 0 | } |
141 | 0 | bytes.flush(); |
142 | 0 | fileStatus.setStatus(FileTransferStatus.UPLOAD_FINISHED); |
143 | 0 | bInfo.setBinary(new String(Base64.encodeBase64(bytes.toByteArray()))); |
144 | |
|
145 | 0 | info.setDocumentBinaryInfo(bInfo); |
146 | 0 | info.setState(DtoState.ACTIVE.toString()); |
147 | 0 | info.setFileName(filename); |
148 | |
|
149 | 0 | int extSeperator = filename.lastIndexOf("."); |
150 | 0 | info.setName(filename.substring(0, extSeperator)); |
151 | |
|
152 | |
|
153 | 0 | String type = "documentType." + filename.substring(extSeperator + 1); |
154 | 0 | info.setType(type); |
155 | 0 | } |
156 | |
else{ |
157 | |
|
158 | 0 | status.setStatus(UploadTransferStatus.ERROR); |
159 | |
} |
160 | |
} |
161 | |
|
162 | 0 | if(info.getDesc() != null && info.getDocumentBinaryInfo() != null && info.getType() != null){ |
163 | |
|
164 | 0 | FileStatus fileStatus = status.getFileStatusList().get(currentItem); |
165 | |
try{ |
166 | 0 | DocumentInfo createdDoc = documentService.createDocument(info.getType(), "documentCategory.proposal", info); |
167 | 0 | fileStatus.setStatus(FileTransferStatus.COMMIT_FINISHED); |
168 | 0 | if(createdDoc != null){ |
169 | 0 | status.getCreatedDocIds().add(createdDoc.getId()); |
170 | 0 | fileStatus.setDocId(createdDoc.getId()); |
171 | |
} |
172 | |
|
173 | 0 | RefDocRelationInfo relationInfo = new RefDocRelationInfo(); |
174 | 0 | relationInfo.setState(DtoState.ACTIVE.toString()); |
175 | 0 | relationInfo.setDesc(info.getDesc()); |
176 | 0 | relationInfo.setRefObjectId(request.getParameter("referenceId")); |
177 | 0 | relationInfo.setRefObjectTypeKey(request.getParameter("refObjectTypeKey")); |
178 | 0 | relationInfo.setTitle(info.getFileName()); |
179 | 0 | relationInfo.setDocumentId(createdDoc.getId()); |
180 | 0 | relationInfo.setType(request.getParameter("refDocRelationTypeKey")); |
181 | 0 | documentService.createRefDocRelation(relationInfo.getRefObjectTypeKey(),relationInfo.getRefObjectId(),relationInfo.getDocumentId(),relationInfo.getType(), relationInfo); |
182 | 0 | }catch(Exception e){ |
183 | 0 | fileError = true; |
184 | 0 | LOG.error(e); |
185 | 0 | fileStatus.setStatus(FileTransferStatus.ERROR); |
186 | 0 | } |
187 | 0 | info = new DocumentInfo(); |
188 | 0 | currentItem++; |
189 | |
} |
190 | 0 | } |
191 | |
|
192 | 0 | if(fileError){ |
193 | 0 | status.setStatus(UploadTransferStatus.ERROR); |
194 | |
} |
195 | |
else{ |
196 | 0 | status.setStatus(UploadTransferStatus.COMMIT_FINISHED); |
197 | |
} |
198 | |
|
199 | 0 | } catch (Exception e) { |
200 | 0 | status.setStatus(UploadTransferStatus.ERROR); |
201 | 0 | LOG.error(e); |
202 | 0 | } |
203 | |
|
204 | 0 | } |
205 | |
|
206 | |
@Override |
207 | |
protected void doGet(HttpServletRequest request, HttpServletResponse response) |
208 | |
throws ServletException, IOException { |
209 | 0 | DocumentInfo info = null; |
210 | |
try { |
211 | 0 | info = documentService.getDocument(request.getParameter("docId")); |
212 | 0 | } catch (Exception e) { |
213 | 0 | LOG.error(e); |
214 | 0 | } |
215 | |
|
216 | 0 | if(info != null && info.getDocumentBinaryInfo() != null && info.getDocumentBinaryInfo().getBinary() != null && |
217 | |
!(info.getDocumentBinaryInfo().getBinary().isEmpty())){ |
218 | |
|
219 | 0 | ServletOutputStream op = response.getOutputStream(); |
220 | |
try { |
221 | |
|
222 | 0 | byte[] fileBytes = Base64.decodeBase64(info.getDocumentBinaryInfo().getBinary().getBytes()); |
223 | 0 | int length = fileBytes.length; |
224 | |
|
225 | 0 | ServletContext context = getServletConfig().getServletContext(); |
226 | 0 | String mimetype = context.getMimeType(info.getFileName()); |
227 | |
|
228 | 0 | response.setContentType( (mimetype != null) ? mimetype : "application/octet-stream" ); |
229 | 0 | response.setContentLength(length); |
230 | 0 | response.setHeader( "Content-Disposition", "attachment; filename=\"" + info.getFileName() + "\"" ); |
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | 0 | op.write(fileBytes,0,length); |
236 | 0 | } catch (Exception e) { |
237 | 0 | LOG.error(e); |
238 | |
} |
239 | |
finally{ |
240 | 0 | op.flush(); |
241 | 0 | op.close(); |
242 | 0 | } |
243 | |
|
244 | 0 | } |
245 | |
else{ |
246 | 0 | response.setContentType("text/html"); |
247 | 0 | PrintWriter out = response.getWriter(); |
248 | 0 | out.println("Sorry, the file could not be retrieved. It may not exist, or the server could not be contacted."); |
249 | |
} |
250 | |
|
251 | 0 | } |
252 | |
|
253 | |
public DocumentService getDocumentService() { |
254 | 0 | return documentService; |
255 | |
} |
256 | |
|
257 | |
public void setDocumentService(DocumentService documentService) { |
258 | 0 | this.documentService = documentService; |
259 | 0 | } |
260 | |
|
261 | |
} |