KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > contineo > actions > rest > PostDocumentRESTAction


1 /* License: GNU General Public License (GPL) version 2 from June 1991; but not any newer version!
2  */

3 package org.contineo.actions.rest;
4
5 import java.io.IOException JavaDoc;
6 import java.io.InputStream JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import java.util.List JavaDoc;
9
10 import javax.servlet.http.HttpServletRequest JavaDoc;
11 import javax.servlet.http.HttpServletResponse JavaDoc;
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 /**
30  * answers a HTTP POST request to checkin a document resource
31  * @author Sebastian Stein
32  */

33 public class PostDocumentRESTAction extends RESTAction {
34     /** id of the menu; this is not the docId */
35     private final int thisMenuId;
36     
37     /** id of the document the client requests the meta data of */
38     private int thisDocId;
39     
40     /** request object containing the uploaded file */
41     private final HttpServletRequest JavaDoc request;
42     
43     private InputStream JavaDoc xmlInfoFile = null;
44     private InputStream JavaDoc newDocFile = null;
45     private String JavaDoc newDocName;
46     private Version.VERSION_TYPE versionType;
47     private String JavaDoc versionDescr;
48     
49     /** constructor sets some initial values and calls the processing of the request */
50     public PostDocumentRESTAction(String JavaDoc p_schemaName, String JavaDoc requestedRESTUrl,
51             String JavaDoc p_userName, HttpServletResponse JavaDoc p_response, int p_menuId, HttpServletRequest JavaDoc 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 JavaDoc
60     protected void processRequest() {
61         // check if we can find the document
62
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         // check, if the user has write access to the document
73
if (!menuDao.isWriteEnable(thisMenuId, userName)) {
74             setHttpStatusCode(HttpStatusCodes.FORBIDDEN);
75             return;
76         }
77         
78         // the document must be checked out, otherwise we can not check it in
79
if (thisDoc.getDocStatus() == Document.DOC_CHECKED_IN) {
80             setHttpStatusCode(HttpStatusCodes.FORBIDDEN);
81             return;
82         }
83         
84         // now check that we have received a multipart request
85
//
86
// function will moved to another package in a future version of the library, but there
87
// is no way at the moment to do anything about this warning
88
boolean isMultiPart = ServletFileUpload.isMultipartContent(request);
89         if (isMultiPart == false) {
90             setHttpStatusCode(HttpStatusCodes.BAD_REQUEST);
91             return;
92         }
93         
94         // get the two files in the received message:
95
// - one file is an XML file describing the new version (post-version.xsd)
96
// - one file is the new version of the document
97
if (extractUploadedFiles() == false) {
98             setHttpStatusCode(HttpStatusCodes.BAD_REQUEST);
99             return;
100         }
101         
102         // now read the XML file to get the needed data for the new version
103
if (parseXMLFile() == false) {
104             setHttpStatusCode(HttpStatusCodes.BAD_REQUEST);
105             return;
106         }
107         
108         // we are now trying to checkin the file
109
try {
110             CheckinDocUtil.checkinDocument(thisDocId, newDocFile, newDocName, userName, versionType, versionDescr);
111         } catch (Exception JavaDoc 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     /**
121      * parses the received XML file and extracts all necessary information
122      * @return true, if the XML file contained all necessary information
123      */

124     private boolean parseXMLFile() {
125         try {
126             // get root element
127
XMLBean xmlBean = new XMLBean(xmlInfoFile);
128             Element root = xmlBean.getChild("versionInfo");
129             
130             // get version description text
131
Element descrElement = root.getChild("description");
132             versionDescr = xmlBean.getText(descrElement);
133
134             // get type of new version
135
Element verTypeElement = root.getChild("versionType");
136             String JavaDoc 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 JavaDoc ex) {
146             return false;
147         }
148         
149         return true;
150     }
151
152     /**
153      * extracts the XML file and the new document file from the multipart message
154      * and set internal input stream pointers pointing at them
155      * @return true, if the files could be successfully extracted from the request
156      */

157     private boolean extractUploadedFiles() {
158         FileItemFactory factory = new DiskFileItemFactory();
159         ServletFileUpload upload = new ServletFileUpload(factory);
160         List JavaDoc items = null;
161         
162         try {
163             items = upload.parseRequest(request);
164         } catch (FileUploadException e) {
165             return false;
166         }
167
168         // there should be exactly two files
169
if (items.size() > 2)
170             return false;
171         
172         Iterator JavaDoc iter = items.iterator();
173         while (iter.hasNext()) {
174             FileItem fileItem = (FileItem) iter.next();
175             if (fileItem.isFormField()) {
176                 // there should be no form fields in the request
177
// String name = fileItem.getFieldName();
178
// String value = fileItem.getString();
179
return false;
180             } else {
181                 try {
182                     String JavaDoc 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                         // if we get here, the message received is invalid
190
return false;
191                     }
192                 } catch (IOException JavaDoc e) {
193                     return false;
194                 }
195             }
196         }
197         // just to make sure, another check
198
if (xmlInfoFile == null || newDocFile == null)
199             return false;
200         else
201             return true;
202     }
203 }
Popular Tags