KickJava   Java API By Example, From Geeks To Geeks.

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


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.FileNotFoundException JavaDoc;
6 import java.io.IOException JavaDoc;
7
8 import javax.servlet.http.HttpServletResponse JavaDoc;
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 /**
18  * answers a HTTP get request to a document resource by returning this document (download)
19  * @author Sebastian Stein
20  */

21 public class GetDocumentRESTAction extends RESTAction {
22     /** the id of the document to be returned */
23     private final int thisDocId;
24     
25     /** the version id of the document to be returned */
26     private final String JavaDoc thisVerId;
27
28     /** constructor sets some initial values and calls the processing of the request */
29     public GetDocumentRESTAction(String JavaDoc p_schemaName, String JavaDoc requestedRESTUrl,
30                                  String JavaDoc p_userName, HttpServletResponse JavaDoc p_response,
31                                  int p_docId, String JavaDoc 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 JavaDoc
43     protected void processRequest() {
44         // check that if a version Id was provided, it is some kind of number
45
if (thisVerId != null) {
46             try {
47                 new Double JavaDoc(thisVerId);
48             } catch (NumberFormatException JavaDoc ex) {
49                 setHttpStatusCode(HttpStatusCodes.BAD_REQUEST);
50                 return;
51             }
52         }
53         
54         // check if we can find the document
55
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         // check, if the user has at least read access to the document
65
if (!menuDao.isReadEnable(thisDocId, userName)) {
66             setHttpStatusCode(HttpStatusCodes.FORBIDDEN);
67             return;
68         }
69         
70         // download the document; if something goes wrong an exception is thrown
71
try {
72             DownloadDocUtil.downloadDocument(response, thisDocId, thisVerId);
73         } catch (FileNotFoundException JavaDoc e) {
74             setHttpStatusCode(HttpStatusCodes.NOT_FOUND);
75             return;
76         } catch (IOException JavaDoc e) {
77             setHttpStatusCode(HttpStatusCodes.BAD_REQUEST);
78             return;
79         }
80         
81         // seems we were successful
82
setHttpStatusCode(HttpStatusCodes.OK);
83         return;
84     }
85 }
Popular Tags