KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > rm > view > servlet > HarmoniseXMLServlet


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.rm.view.servlet;
20 import java.io.*;
21 import java.util.logging.*;
22
23 import javax.servlet.*;
24 import javax.servlet.http.*;
25 import javax.xml.parsers.*;
26
27 import org.openharmonise.rm.config.*;
28 import org.openharmonise.rm.dsi.*;
29 import org.openharmonise.rm.factory.*;
30 import org.openharmonise.rm.publishing.*;
31 import org.openharmonise.rm.resources.*;
32 import org.openharmonise.rm.resources.publishing.*;
33 import org.openharmonise.rm.view.servlet.utils.*;
34 import org.w3c.dom.*;
35
36
37 /**
38  * A servlet which returns XML.
39  *
40  * @author Michael Bell
41  * @version $Revision: 1.2 $
42  *
43  */

44
45 public class HarmoniseXMLServlet extends HarmoniseServlet {
46     
47     /**
48      * <code>String</code> constant for the configuration parameter that sets
49      * the maximum file size for upload
50      */

51     static final String JavaDoc MAXFILESIZE_PNAME = "MAX_UPLOADED_FILESIZE_MB";
52     
53     /**
54      * <code>String</code> constant for the configuration parameter that sets
55      * the directory which uploaded files are uploaded to
56      */

57     static final String JavaDoc TEMPDIR_PNAME = "UPLOADED_TEMPFILE_DIR";
58     
59     /**
60      * Logger for this class
61      */

62     private static final Logger m_logger = Logger.getLogger(HarmoniseXMLServlet.class.getName());
63
64     /**
65      * Constructs a new <code>HarmoniseXMLServlet</code>.
66      *
67      */

68     public HarmoniseXMLServlet() {
69     }
70
71     /* (non-Javadoc)
72      * @see javax.servlet.Servlet#init(javax.servlet.ServletConfig)
73      */

74     public void init(ServletConfig config) throws ServletException {
75         super.init(config);
76
77         try {
78             m_dbintrf = DataStoreInterfaceFactory.getDataStoreInterface();
79         } catch (Exception JavaDoc e) {
80             m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
81         }
82     }
83
84
85     /* (non-Javadoc)
86      * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
87      */

88     public void doGet(HttpServletRequest request, HttpServletResponse response)
89         throws ServletException, IOException {
90         doPost(request, response);
91     }
92
93
94     /* (non-Javadoc)
95      * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
96      */

97     public void doPost(
98         HttpServletRequest request,
99         HttpServletResponse response)
100         throws ServletException, IOException {
101         OutputStream out = response.getOutputStream();
102
103         try {
104             HttpRequestManager req_mgr = null;
105
106             int max_filesize_mb =
107                 ConfigSettings.getIntProperty(
108                     MAXFILESIZE_PNAME,
109                     "1");
110             int m_MAX_FILESIZE = max_filesize_mb * 1024 * 1024;
111
112             String JavaDoc m_TEMP_FILE_DIR =
113                 ConfigSettings.getProperty(
114                     TEMPDIR_PNAME,
115                     "c:\\Harmonise\\temp");
116
117             
118             req_mgr = new HttpRequestManager(request, m_MAX_FILESIZE, m_TEMP_FILE_DIR);
119
120             String JavaDoc sState = req_mgr.getStringFromReqReader();
121
122             org.w3c.dom.Document JavaDoc xml_doc =
123                 DocumentBuilderFactory
124                     .newInstance()
125                     .newDocumentBuilder()
126                     .parse(
127                     new org.xml.sax.InputSource JavaDoc(new StringReader(sState)));
128
129             State state = new State(xml_doc, this.m_dbintrf);
130
131             NodeList nodes = state.getElementsByTagName(WebPage.TAG_PAGE);
132             int nPage = 1;
133
134             if (nodes.getLength() > 0) {
135                 nPage =
136                     Integer.parseInt(
137                         ((Element) nodes.item(0)).getAttribute(
138                             AbstractObject.ATTRIB_ID));
139             }
140
141             org.w3c.dom.Document JavaDoc xml = null;
142
143             WebPageEngine pageEngine = null;
144             WebPage page = null;
145             String JavaDoc sXMLfile = "Filename not known";
146             String JavaDoc sXSLfile = "Filename not known";
147
148             if (true) {
149                 try {
150
151                     
152                     String JavaDoc sSessionId = state.getSessionId();
153
154                     if ((sSessionId == null) || (sSessionId.length() == 0)) {
155                         //Create a WebPageEngine with a brand new session
156
pageEngine = new WebPageEngine(this.m_dbintrf);
157                         
158                     } else {
159                         //Create a WebPageEngine with the current session
160
pageEngine =
161                             WebPageEngineCache.getInstance(
162                                 m_dbintrf).getWebPageEngine(
163                                 sSessionId);
164                         
165                     }
166
167                     page =
168                         (WebPage) HarmoniseObjectFactory.instantiateHarmoniseObject(
169                             this.m_dbintrf,
170                             WebPage.class.getName(),
171                             nPage);
172
173                     xml = pageEngine.createXML(page, state);
174
175                     if (xml == null && m_logger.isLoggable(Level.FINE)) {
176                         m_logger.logp(Level.FINE,this.getClass().getName(),"doPost", "Null XML document returned.");
177                     }
178
179                     pageEngine.render(page, state, out);
180
181                 } catch (Exception JavaDoc e) {
182                     m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
183                     throw e;
184                 }
185             }
186
187             out.flush();
188
189         } catch (Exception JavaDoc e) {
190             m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
191         } finally {
192             out.flush();
193             out.close();
194         }
195
196     }
197 }
198
Popular Tags