KickJava   Java API By Example, From Geeks To Geeks.

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


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.PrintWriter JavaDoc;
7 import java.util.Collection JavaDoc;
8
9 import javax.servlet.http.HttpServletResponse JavaDoc;
10 import javax.xml.parsers.ParserConfigurationException JavaDoc;
11 import javax.xml.transform.TransformerConfigurationException JavaDoc;
12 import javax.xml.transform.TransformerException JavaDoc;
13
14 import org.contineo.admin.ExtMenu;
15 import org.contineo.admin.Menu;
16 import org.contineo.admin.dao.MenuDAO;
17 import org.contineo.apis.rest.HttpStatusCodes;
18 import org.contineo.documan.dao.DocumentDAO;
19 import org.w3c.dom.Document JavaDoc;
20 import org.w3c.dom.Element JavaDoc;
21
22 /**
23  * answers a GET request to a folder resource by returning the content of the folder as XML
24  * @author Sebastian Stein
25  */

26 public class GetFolderContentRESTAction extends RESTAction {
27     /** Id of the requested menu; -1 means not set */
28     private int thisMenuId = -1;
29     
30     /** true, if the menu is writeable for the given user */
31     private boolean thisMenuWriteable = false;
32     
33     /** name of the given menu */
34     private String JavaDoc thisMenuName;
35     
36     /** Id of the parent menu; -1 means not set */
37     private int parentMenuId = -1;
38     
39     /** name of parent menu */
40     private String JavaDoc parentMenuName;
41     
42     /** collection with all sub menus */
43     Collection JavaDoc<ExtMenu> subMenus = null;
44         
45     /** constructor sets some initial values and calls the processing of the request */
46     public GetFolderContentRESTAction(HttpServletResponse JavaDoc response, String JavaDoc userName, String JavaDoc requestedRESTUrl, int p_menuId) {
47         super("get-folder.xsd", requestedRESTUrl, userName, response, "GET REST/folder/XX: get content folder XX");
48         thisMenuId = p_menuId;
49         processRequest();
50     }
51
52     @Override JavaDoc
53     protected void processRequest() {
54         // check that we have valid input parameters
55
if (userName == null || userName.equals("") || thisMenuId == -1 || response == null) {
56             setHttpStatusCode(HttpStatusCodes.BAD_REQUEST);
57             return;
58         }
59         
60         // this object is a good util to handle all menu related tasks
61
MenuDAO menuDao = new MenuDAO();
62         
63         // check if we can find the menu
64
Menu thisMenu = menuDao.findByPrimaryKey(thisMenuId);
65         if (thisMenu == null) {
66             setHttpStatusCode(HttpStatusCodes.NOT_FOUND);
67             return;
68         }
69         
70         // check, if the user has at least read access to the menu
71
if (!menuDao.isReadEnable(thisMenuId, userName)) {
72             setHttpStatusCode(HttpStatusCodes.FORBIDDEN);
73             return;
74         }
75         
76         // get menu name
77
thisMenuName = thisMenu.getMenuText();
78         
79         // get Id of parent menu
80
parentMenuId = thisMenu.getMenuParent();
81         
82         // get name of parent menu
83
Menu parentMenu = menuDao.findByPrimaryKey(parentMenuId);
84         parentMenuName = parentMenu.getMenuText();
85         // extract if this menu is writeable
86
thisMenuWriteable = menuDao.isWriteEnable(thisMenuId, userName);
87         
88         // get all menus contained in this menu
89
subMenus = menuDao.getContainedMenus(thisMenuId, userName);
90         if (subMenus == null) {
91             setHttpStatusCode(HttpStatusCodes.NOT_FOUND);
92             return;
93         }
94         
95         // we have now all the data for the answer, we just have
96
// to create and write the XML document to the response
97
if (createXMLAnswer() == false) {
98             setHttpStatusCode(HttpStatusCodes.INTERNAL_SERVER_ERROR);
99             return;
100         } else {
101             // seems we were successful
102
setHttpStatusCode(HttpStatusCodes.OK);
103             return;
104         }
105     }
106
107     /**
108      * creates the XML document containing the menu content
109      * @return true, if successful; on any errors false
110      */

111     private boolean createXMLAnswer() {
112         PrintWriter JavaDoc responseWriter;
113         try {
114             responseWriter = response.getWriter();
115         } catch (IOException JavaDoc e) {
116             setHttpStatusCode(HttpStatusCodes.INTERNAL_SERVER_ERROR);
117             addLogMessage("Unable to get output object for sending XML answer to client: " + e.getMessage());
118             return false;
119         }
120         
121         // set correct content type
122
response.setContentType("text/xml;charset=utf-8");
123         boolean ret = true;
124         try {
125             // create XML document
126
Document JavaDoc xmlDoc = createXMLDocument();
127             
128             // root element
129
Element JavaDoc rootElement = createRootElement(xmlDoc, "folderContent", "folder", "get-folder.xsd");
130             
131             // header Element
132
Element JavaDoc headerRootElement = createChildElement(xmlDoc, "header", rootElement, ""); // <header xmlns="">
133
createTextNodeElement(xmlDoc, "id", headerRootElement, thisMenuId); // <id>29</id>
134
createTextNodeElement(xmlDoc, "name", headerRootElement, thisMenuName); // <name>thisFolder</name>
135
createTextNodeElement(xmlDoc, "writeable", headerRootElement, thisMenuWriteable); // <writeable>true</writeable>
136
createTextNodeElement(xmlDoc, "parentId", headerRootElement, parentMenuId); // <parentId>5</parentId>
137
createTextNodeElement(xmlDoc, "parentName", headerRootElement, parentMenuName); // <parentName>db.project</parentName>
138

139             // write sub-folders
140
for (ExtMenu menu : subMenus) {
141                 if (menu.getMenuType() == Menu.MENUTYPE_DIRECTORY) {
142                     writeSubFolderToXML(xmlDoc, rootElement, menu);
143                 }
144             }
145             
146             // write documents
147
for (ExtMenu menu : subMenus) {
148                 if (menu.getMenuType() == Menu.MENUTYPE_FILE) {
149                     writeDocumentToXML(xmlDoc, rootElement, menu);
150                 }
151             }
152             
153             // transform and write XML document
154
writeXMLDocument(xmlDoc, responseWriter);
155         } catch (ParserConfigurationException JavaDoc e) {
156             setHttpStatusCode(HttpStatusCodes.INTERNAL_SERVER_ERROR);
157             addLogMessage("While trying to create XML answer a ParserConfigurationException occured: " + e.getMessage());
158             ret = false;
159         } catch (TransformerConfigurationException JavaDoc e) {
160             setHttpStatusCode(HttpStatusCodes.INTERNAL_SERVER_ERROR);
161             addLogMessage("While trying to create XML answer a TransformerConfigurationException occured: " + e.getMessage());
162             ret = false;
163         } catch (TransformerException JavaDoc e) {
164             setHttpStatusCode(HttpStatusCodes.INTERNAL_SERVER_ERROR);
165             addLogMessage("While trying to create XML answer a TransformerException occured: " + e.getMessage());
166             ret = false;
167         } finally {
168             // finally write content to the response and close output stream
169
responseWriter.flush();
170             responseWriter.close();
171         }
172         
173         return ret;
174     }
175
176     /**
177      * creates the XML representation for the given document
178      * @param xmlDoc should be added to this XML document
179      * @param parentElement this is the parent element (here the root element of the XML document)
180      * @param document the document menu to be written as XML
181      */

182     private void writeDocumentToXML(Document JavaDoc xmlDoc, Element JavaDoc parentElement, ExtMenu document) {
183         Element JavaDoc documentRootElement = createChildElement(xmlDoc, "document", parentElement, ""); // <document xmlns="">
184
createTextNodeElement(xmlDoc, "name", documentRootElement, document.getMenuText()); // <name>Doc31.pdf</name>
185
createTextNodeElement(xmlDoc, "id", documentRootElement, document.getMenuId()); // <id>31</id>
186
createTextNodeElement(xmlDoc, "writeable", documentRootElement, document.getWritable()); // <writeable>true</writeable>
187

188         // <downloadURL>http://localhost:8080/contineo/rest/document/31</downloadURL>
189
createTextNodeElement(xmlDoc, "downloadURL", documentRootElement, localRESTUrl +
190                                             "document/" + document.getMenuId());
191         // <metadataURL>http://localhost:8080/contineo/rest/document/31/meta</metadataURL>
192
createTextNodeElement(xmlDoc, "metadataURL", documentRootElement, localRESTUrl +
193                                             "document/" + document.getMenuId() + "/meta");
194         
195         // depending whether the document is checked or in, we add a checkout/in URL
196
DocumentDAO ddao = new DocumentDAO();
197         org.contineo.documan.Document tmpDocument = ddao.findByMenuId(document.getMenuId());
198         if (tmpDocument.getDocStatus() == org.contineo.documan.Document.DOC_CHECKED_IN) {
199             // <checkoutURL>http://localhost:8080/contineo/rest/document/31/checkout</checkoutURL>
200
createTextNodeElement(xmlDoc, "checkoutURL", documentRootElement, localRESTUrl +
201                                             "document/" + document.getMenuId() + "/checkout");
202         } else {
203             // <checkinURL>http://localhost:8080/contineo/rest/document/31/checkin</checkinURL>
204
createTextNodeElement(xmlDoc, "checkinURL", documentRootElement, localRESTUrl +
205                                             "document/" + document.getMenuId() + "/checkin");
206         }
207     }
208
209     /**
210      * creates the XML representation for the given sub folder
211      * @param xmlDoc should be added to this XML document
212      * @param parentElement this is the parent element (here the root element of the XML document)
213      * @param folder the folder menu to be written as XML
214      */

215     private void writeSubFolderToXML(Document JavaDoc xmlDoc, Element JavaDoc parentElement, ExtMenu folder) {
216         Element JavaDoc subFolderRootElement = createChildElement(xmlDoc, "subfolder", parentElement, ""); // <subfolder xmlns="">
217
createTextNodeElement(xmlDoc, "id", subFolderRootElement, folder.getMenuId()); // <id>30</id>
218
createTextNodeElement(xmlDoc, "name", subFolderRootElement, folder.getMenuText()); // <name>Folder30</name>
219
createTextNodeElement(xmlDoc, "writeable", subFolderRootElement, folder.getWritable()); // <writeable>false</writeable>
220
createTextNodeElement(xmlDoc, "url", subFolderRootElement, localRESTUrl + "folder/" +
221                                                 folder.getMenuId()); // <url>http://localhost:8080/contineo/rest/folder/30</url>
222
}
223 }
Popular Tags