1 3 package org.contineo.actions.rest; 4 5 import java.io.FileNotFoundException ; 6 import java.io.IOException ; 7 8 import javax.servlet.http.HttpServletResponse ; 9 10 import org.contineo.actions.documan.util.DownloadDocUtil; 11 import org.contineo.admin.Menu; 12 import org.contineo.admin.dao.MenuDAO; 13 import org.contineo.apis.rest.HttpStatusCodes; 14 import org.contineo.documan.Document; 15 import org.contineo.documan.dao.DocumentDAO; 16 17 21 public class GetDocumentRESTAction extends RESTAction { 22 23 private final int thisDocId; 24 25 26 private final String thisVerId; 27 28 29 public GetDocumentRESTAction(String p_schemaName, String requestedRESTUrl, 30 String p_userName, HttpServletResponse p_response, 31 int p_docId, String p_docVerId) { 32 super(p_schemaName, requestedRESTUrl, p_userName, p_response, "GET REST/document/XX: download doc XX"); 33 thisDocId = p_docId; 34 if (p_docVerId == null || p_docVerId.equalsIgnoreCase("")) 35 thisVerId = null; 36 else 37 thisVerId = p_docVerId; 38 39 processRequest(); 40 } 41 42 @Override 43 protected void processRequest() { 44 if (thisVerId != null) { 46 try { 47 new Double (thisVerId); 48 } catch (NumberFormatException ex) { 49 setHttpStatusCode(HttpStatusCodes.BAD_REQUEST); 50 return; 51 } 52 } 53 54 MenuDAO menuDao = new MenuDAO(); 56 Menu thisMenu = menuDao.findByPrimaryKey(thisDocId); 57 DocumentDAO ddao = new DocumentDAO(); 58 Document thisDoc = ddao.findByMenuId(thisDocId); 59 if (thisMenu == null || thisDoc == null) { 60 setHttpStatusCode(HttpStatusCodes.NOT_FOUND); 61 return; 62 } 63 64 if (!menuDao.isReadEnable(thisDocId, userName)) { 66 setHttpStatusCode(HttpStatusCodes.FORBIDDEN); 67 return; 68 } 69 70 try { 72 DownloadDocUtil.downloadDocument(response, thisDocId, thisVerId); 73 } catch (FileNotFoundException e) { 74 setHttpStatusCode(HttpStatusCodes.NOT_FOUND); 75 return; 76 } catch (IOException e) { 77 setHttpStatusCode(HttpStatusCodes.BAD_REQUEST); 78 return; 79 } 80 81 setHttpStatusCode(HttpStatusCodes.OK); 83 return; 84 } 85 } | Popular Tags |