| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.student.common.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.ui.client.dto.FileStatus; |
| 40 | |
import org.kuali.student.common.ui.client.dto.UploadStatus; |
| 41 | |
import org.kuali.student.common.ui.client.dto.FileStatus.FileTransferStatus; |
| 42 | |
import org.kuali.student.common.ui.client.dto.UploadStatus.UploadTransferStatus; |
| 43 | |
import org.kuali.student.core.document.dto.DocumentBinaryInfo; |
| 44 | |
import org.kuali.student.core.document.dto.DocumentInfo; |
| 45 | |
import org.kuali.student.core.document.dto.RefDocRelationInfo; |
| 46 | |
import org.kuali.student.core.document.service.DocumentService; |
| 47 | |
import org.kuali.student.core.dto.RichTextInfo; |
| 48 | |
import org.kuali.student.core.dto.DtoConstants.DtoState; |
| 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 | } |
| 123 | |
else { |
| 124 | 0 | String fullFileName = item.getName(); |
| 125 | 0 | if (fullFileName != null) { |
| 126 | 0 | String filename = FilenameUtils.getName(fullFileName); |
| 127 | 0 | FileStatus fileStatus = new FileStatus(); |
| 128 | 0 | fileStatus.setFileName(filename); |
| 129 | 0 | status.getFileStatusList().add(currentItem, fileStatus); |
| 130 | 0 | DocumentBinaryInfo bInfo = new DocumentBinaryInfo(); |
| 131 | 0 | ByteArrayOutputStream bytes = new ByteArrayOutputStream(); |
| 132 | 0 | byte[] buffer = new byte[4096]; |
| 133 | |
while (true) { |
| 134 | 0 | int read = stream.read(buffer); |
| 135 | 0 | if (read == -1) { |
| 136 | 0 | break; |
| 137 | |
} else { |
| 138 | 0 | bytes.write(buffer, 0, read); |
| 139 | 0 | fileStatus.setProgress(fileStatus.getProgress() + read); |
| 140 | |
} |
| 141 | 0 | } |
| 142 | 0 | bytes.flush(); |
| 143 | 0 | fileStatus.setStatus(FileTransferStatus.UPLOAD_FINISHED); |
| 144 | 0 | bInfo.setBinary(new String(Base64.encodeBase64(bytes.toByteArray()))); |
| 145 | |
|
| 146 | 0 | info.setDocumentBinaryInfo(bInfo); |
| 147 | 0 | info.setState(DtoState.ACTIVE.toString()); |
| 148 | 0 | info.setFileName(filename); |
| 149 | |
|
| 150 | 0 | int extSeperator = filename.lastIndexOf("."); |
| 151 | 0 | info.setName(filename.substring(0, extSeperator)); |
| 152 | |
|
| 153 | |
|
| 154 | 0 | String type = "documentType." + filename.substring(extSeperator + 1); |
| 155 | 0 | info.setType(type); |
| 156 | 0 | } |
| 157 | |
else{ |
| 158 | |
|
| 159 | 0 | status.setStatus(UploadTransferStatus.ERROR); |
| 160 | |
} |
| 161 | |
} |
| 162 | |
|
| 163 | 0 | if(info.getDesc() != null && info.getDocumentBinaryInfo() != null && info.getType() != null){ |
| 164 | |
|
| 165 | 0 | FileStatus fileStatus = status.getFileStatusList().get(currentItem); |
| 166 | |
try{ |
| 167 | 0 | DocumentInfo createdDoc = documentService.createDocument(info.getType(), "documentCategory.proposal", info); |
| 168 | 0 | fileStatus.setStatus(FileTransferStatus.COMMIT_FINISHED); |
| 169 | 0 | if(createdDoc != null){ |
| 170 | 0 | status.getCreatedDocIds().add(createdDoc.getId()); |
| 171 | 0 | fileStatus.setDocId(createdDoc.getId()); |
| 172 | |
} |
| 173 | |
|
| 174 | 0 | RefDocRelationInfo relationInfo = new RefDocRelationInfo(); |
| 175 | 0 | relationInfo.setState(DtoState.ACTIVE.toString()); |
| 176 | 0 | relationInfo.setDesc(info.getDesc()); |
| 177 | 0 | relationInfo.setRefObjectId(request.getParameter("referenceId")); |
| 178 | 0 | relationInfo.setRefObjectTypeKey(request.getParameter("refObjectTypeKey")); |
| 179 | 0 | relationInfo.setTitle(info.getFileName()); |
| 180 | 0 | relationInfo.setDocumentId(createdDoc.getId()); |
| 181 | 0 | relationInfo.setType(request.getParameter("refDocRelationTypeKey")); |
| 182 | 0 | documentService.createRefDocRelation(relationInfo.getRefObjectTypeKey(),relationInfo.getRefObjectId(),relationInfo.getDocumentId(),relationInfo.getType(), relationInfo); |
| 183 | |
} |
| 184 | 0 | catch(Exception e){ |
| 185 | 0 | fileError = true; |
| 186 | 0 | LOG.error(e); |
| 187 | 0 | fileStatus.setStatus(FileTransferStatus.ERROR); |
| 188 | 0 | } |
| 189 | 0 | info = new DocumentInfo(); |
| 190 | 0 | currentItem++; |
| 191 | |
} |
| 192 | 0 | } |
| 193 | |
|
| 194 | 0 | if(fileError){ |
| 195 | 0 | status.setStatus(UploadTransferStatus.ERROR); |
| 196 | |
} |
| 197 | |
else{ |
| 198 | 0 | status.setStatus(UploadTransferStatus.COMMIT_FINISHED); |
| 199 | |
} |
| 200 | |
|
| 201 | 0 | } catch (Exception e) { |
| 202 | 0 | status.setStatus(UploadTransferStatus.ERROR); |
| 203 | 0 | LOG.error(e); |
| 204 | 0 | } |
| 205 | |
|
| 206 | 0 | } |
| 207 | |
|
| 208 | |
@Override |
| 209 | |
protected void doGet(HttpServletRequest request, HttpServletResponse response) |
| 210 | |
throws ServletException, IOException { |
| 211 | 0 | DocumentInfo info = null; |
| 212 | |
try { |
| 213 | 0 | info = documentService.getDocument(request.getParameter("docId")); |
| 214 | 0 | } catch (Exception e) { |
| 215 | 0 | LOG.error(e); |
| 216 | 0 | } |
| 217 | |
|
| 218 | 0 | if(info != null && info.getDocumentBinaryInfo() != null && info.getDocumentBinaryInfo().getBinary() != null && |
| 219 | |
!(info.getDocumentBinaryInfo().getBinary().isEmpty())){ |
| 220 | |
|
| 221 | 0 | ServletOutputStream op = response.getOutputStream(); |
| 222 | |
try { |
| 223 | |
|
| 224 | 0 | byte[] fileBytes = Base64.decodeBase64(info.getDocumentBinaryInfo().getBinary().getBytes()); |
| 225 | 0 | int length = fileBytes.length; |
| 226 | |
|
| 227 | 0 | ServletContext context = getServletConfig().getServletContext(); |
| 228 | 0 | String mimetype = context.getMimeType(info.getFileName()); |
| 229 | |
|
| 230 | 0 | response.setContentType( (mimetype != null) ? mimetype : "application/octet-stream" ); |
| 231 | 0 | response.setContentLength(length); |
| 232 | 0 | response.setHeader( "Content-Disposition", "attachment; filename=\"" + info.getFileName() + "\"" ); |
| 233 | |
|
| 234 | |
|
| 235 | |
|
| 236 | |
|
| 237 | 0 | op.write(fileBytes,0,length); |
| 238 | 0 | } catch (Exception e) { |
| 239 | 0 | LOG.error(e); |
| 240 | |
} |
| 241 | |
finally{ |
| 242 | 0 | op.flush(); |
| 243 | 0 | op.close(); |
| 244 | 0 | } |
| 245 | |
|
| 246 | 0 | } |
| 247 | |
else{ |
| 248 | 0 | response.setContentType("text/html"); |
| 249 | 0 | PrintWriter out = response.getWriter(); |
| 250 | 0 | out.println("Sorry, the file could not be retrieved. It may not exist, or the server could not be contacted."); |
| 251 | |
} |
| 252 | |
|
| 253 | 0 | } |
| 254 | |
|
| 255 | |
public DocumentService getDocumentService() { |
| 256 | 0 | return documentService; |
| 257 | |
} |
| 258 | |
|
| 259 | |
public void setDocumentService(DocumentService documentService) { |
| 260 | 0 | this.documentService = documentService; |
| 261 | 0 | } |
| 262 | |
|
| 263 | |
} |