1 3 package org.contineo.actions.rest; 4 5 import java.io.IOException ; 6 import java.io.InputStream ; 7 import java.util.Iterator ; 8 import java.util.List ; 9 10 import javax.servlet.http.HttpServletRequest ; 11 import javax.servlet.http.HttpServletResponse ; 12 13 import org.apache.commons.fileupload.FileItem; 14 import org.apache.commons.fileupload.FileItemFactory; 15 import org.apache.commons.fileupload.FileUploadException; 16 import org.apache.commons.fileupload.disk.DiskFileItemFactory; 17 import org.apache.commons.fileupload.servlet.ServletFileUpload; 18 import org.contineo.admin.Menu; 19 import org.contineo.admin.dao.MenuDAO; 20 import org.contineo.apis.rest.HttpStatusCodes; 21 import org.contineo.core.XMLBean; 22 import org.contineo.documan.CheckinDocUtil; 23 import org.contineo.documan.Document; 24 import org.contineo.documan.Version; 25 import org.contineo.documan.Version.VERSION_TYPE; 26 import org.contineo.documan.dao.DocumentDAO; 27 import org.jdom.Element; 28 29 33 public class PostDocumentRESTAction extends RESTAction { 34 35 private final int thisMenuId; 36 37 38 private int thisDocId; 39 40 41 private final HttpServletRequest request; 42 43 private InputStream xmlInfoFile = null; 44 private InputStream newDocFile = null; 45 private String newDocName; 46 private Version.VERSION_TYPE versionType; 47 private String versionDescr; 48 49 50 public PostDocumentRESTAction(String p_schemaName, String requestedRESTUrl, 51 String p_userName, HttpServletResponse p_response, int p_menuId, HttpServletRequest p_request) { 52 super(p_schemaName, requestedRESTUrl, p_userName, p_response, "POST REST/document/XX: checkin doc XX"); 53 thisMenuId = p_menuId; 54 request = p_request; 55 56 processRequest(); 57 } 58 59 @Override 60 protected void processRequest() { 61 MenuDAO menuDao = new MenuDAO(); 63 Menu thisMenu = menuDao.findByPrimaryKey(thisMenuId); 64 DocumentDAO ddao = new DocumentDAO(); 65 Document thisDoc = ddao.findByMenuId(thisMenuId); 66 if (thisMenu == null || thisDoc == null) { 67 setHttpStatusCode(HttpStatusCodes.NOT_FOUND); 68 return; 69 } 70 thisDocId = thisDoc.getDocId(); 71 72 if (!menuDao.isWriteEnable(thisMenuId, userName)) { 74 setHttpStatusCode(HttpStatusCodes.FORBIDDEN); 75 return; 76 } 77 78 if (thisDoc.getDocStatus() == Document.DOC_CHECKED_IN) { 80 setHttpStatusCode(HttpStatusCodes.FORBIDDEN); 81 return; 82 } 83 84 boolean isMultiPart = ServletFileUpload.isMultipartContent(request); 89 if (isMultiPart == false) { 90 setHttpStatusCode(HttpStatusCodes.BAD_REQUEST); 91 return; 92 } 93 94 if (extractUploadedFiles() == false) { 98 setHttpStatusCode(HttpStatusCodes.BAD_REQUEST); 99 return; 100 } 101 102 if (parseXMLFile() == false) { 104 setHttpStatusCode(HttpStatusCodes.BAD_REQUEST); 105 return; 106 } 107 108 try { 110 CheckinDocUtil.checkinDocument(thisDocId, newDocFile, newDocName, userName, versionType, versionDescr); 111 } catch (Exception e) { 112 setHttpStatusCode(HttpStatusCodes.INTERNAL_SERVER_ERROR); 113 addLogMessage("Something failed internally while trying to check in the document with the doc id " 114 + thisDocId + ". " + e.getMessage()); 115 return; 116 } 117 return; 118 } 119 120 124 private boolean parseXMLFile() { 125 try { 126 XMLBean xmlBean = new XMLBean(xmlInfoFile); 128 Element root = xmlBean.getChild("versionInfo"); 129 130 Element descrElement = root.getChild("description"); 132 versionDescr = xmlBean.getText(descrElement); 133 134 Element verTypeElement = root.getChild("versionType"); 136 String verTypeStr = xmlBean.getText(verTypeElement); 137 if (verTypeStr.equals("newMajorVersion")) 138 versionType = VERSION_TYPE.NEW_RELEASE; 139 else if (verTypeStr.equals("newSubVersion")) 140 versionType = VERSION_TYPE.NEW_SUBVERSION; 141 else if (verTypeStr.equals("oldVersion")) 142 versionType = VERSION_TYPE.OLD_VERSION; 143 else 144 return false; 145 } catch (Exception ex) { 146 return false; 147 } 148 149 return true; 150 } 151 152 157 private boolean extractUploadedFiles() { 158 FileItemFactory factory = new DiskFileItemFactory(); 159 ServletFileUpload upload = new ServletFileUpload(factory); 160 List items = null; 161 162 try { 163 items = upload.parseRequest(request); 164 } catch (FileUploadException e) { 165 return false; 166 } 167 168 if (items.size() > 2) 170 return false; 171 172 Iterator iter = items.iterator(); 173 while (iter.hasNext()) { 174 FileItem fileItem = (FileItem) iter.next(); 175 if (fileItem.isFormField()) { 176 return false; 180 } else { 181 try { 182 String itemName = fileItem.getFieldName(); 183 if (itemName.equals("contineoCheckInInfo.xml") && xmlInfoFile == null) { 184 xmlInfoFile = fileItem.getInputStream(); 185 } else if (newDocFile == null) { 186 newDocFile = fileItem.getInputStream(); 187 newDocName = itemName; 188 } else { 189 return false; 191 } 192 } catch (IOException e) { 193 return false; 194 } 195 } 196 } 197 if (xmlInfoFile == null || newDocFile == null) 199 return false; 200 else 201 return true; 202 } 203 } | Popular Tags |