001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.rm.view.servlet;
020:
021: import java.io.*;
022: import java.util.logging.*;
023:
024: import javax.servlet.*;
025: import javax.servlet.http.*;
026: import javax.xml.parsers.*;
027:
028: import org.openharmonise.rm.config.*;
029: import org.openharmonise.rm.dsi.*;
030: import org.openharmonise.rm.factory.*;
031: import org.openharmonise.rm.publishing.*;
032: import org.openharmonise.rm.resources.*;
033: import org.openharmonise.rm.resources.publishing.*;
034: import org.openharmonise.rm.view.servlet.utils.*;
035: import org.w3c.dom.*;
036:
037: /**
038: * A servlet which returns XML.
039: *
040: * @author Michael Bell
041: * @version $Revision: 1.2 $
042: *
043: */
044:
045: public class HarmoniseXMLServlet extends HarmoniseServlet {
046:
047: /**
048: * <code>String</code> constant for the configuration parameter that sets
049: * the maximum file size for upload
050: */
051: static final String MAXFILESIZE_PNAME = "MAX_UPLOADED_FILESIZE_MB";
052:
053: /**
054: * <code>String</code> constant for the configuration parameter that sets
055: * the directory which uploaded files are uploaded to
056: */
057: static final String TEMPDIR_PNAME = "UPLOADED_TEMPFILE_DIR";
058:
059: /**
060: * Logger for this class
061: */
062: private static final Logger m_logger = Logger
063: .getLogger(HarmoniseXMLServlet.class.getName());
064:
065: /**
066: * Constructs a new <code>HarmoniseXMLServlet</code>.
067: *
068: */
069: public HarmoniseXMLServlet() {
070: }
071:
072: /* (non-Javadoc)
073: * @see javax.servlet.Servlet#init(javax.servlet.ServletConfig)
074: */
075: public void init(ServletConfig config) throws ServletException {
076: super .init(config);
077:
078: try {
079: m_dbintrf = DataStoreInterfaceFactory
080: .getDataStoreInterface();
081: } catch (Exception e) {
082: m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
083: }
084: }
085:
086: /* (non-Javadoc)
087: * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
088: */
089: public void doGet(HttpServletRequest request,
090: HttpServletResponse response) throws ServletException,
091: IOException {
092: doPost(request, response);
093: }
094:
095: /* (non-Javadoc)
096: * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
097: */
098: public void doPost(HttpServletRequest request,
099: HttpServletResponse response) throws ServletException,
100: IOException {
101: OutputStream out = response.getOutputStream();
102:
103: try {
104: HttpRequestManager req_mgr = null;
105:
106: int max_filesize_mb = ConfigSettings.getIntProperty(
107: MAXFILESIZE_PNAME, "1");
108: int m_MAX_FILESIZE = max_filesize_mb * 1024 * 1024;
109:
110: String m_TEMP_FILE_DIR = ConfigSettings.getProperty(
111: TEMPDIR_PNAME, "c:\\Harmonise\\temp");
112:
113: req_mgr = new HttpRequestManager(request, m_MAX_FILESIZE,
114: m_TEMP_FILE_DIR);
115:
116: String sState = req_mgr.getStringFromReqReader();
117:
118: org.w3c.dom.Document xml_doc = DocumentBuilderFactory
119: .newInstance().newDocumentBuilder().parse(
120: new org.xml.sax.InputSource(
121: new StringReader(sState)));
122:
123: State state = new State(xml_doc, this .m_dbintrf);
124:
125: NodeList nodes = state
126: .getElementsByTagName(WebPage.TAG_PAGE);
127: int nPage = 1;
128:
129: if (nodes.getLength() > 0) {
130: nPage = Integer.parseInt(((Element) nodes.item(0))
131: .getAttribute(AbstractObject.ATTRIB_ID));
132: }
133:
134: org.w3c.dom.Document xml = null;
135:
136: WebPageEngine pageEngine = null;
137: WebPage page = null;
138: String sXMLfile = "Filename not known";
139: String sXSLfile = "Filename not known";
140:
141: if (true) {
142: try {
143:
144: String sSessionId = state.getSessionId();
145:
146: if ((sSessionId == null)
147: || (sSessionId.length() == 0)) {
148: //Create a WebPageEngine with a brand new session
149: pageEngine = new WebPageEngine(this .m_dbintrf);
150:
151: } else {
152: //Create a WebPageEngine with the current session
153: pageEngine = WebPageEngineCache.getInstance(
154: m_dbintrf).getWebPageEngine(sSessionId);
155:
156: }
157:
158: page = (WebPage) HarmoniseObjectFactory
159: .instantiateHarmoniseObject(this .m_dbintrf,
160: WebPage.class.getName(), nPage);
161:
162: xml = pageEngine.createXML(page, state);
163:
164: if (xml == null && m_logger.isLoggable(Level.FINE)) {
165: m_logger.logp(Level.FINE, this .getClass()
166: .getName(), "doPost",
167: "Null XML document returned.");
168: }
169:
170: pageEngine.render(page, state, out);
171:
172: } catch (Exception e) {
173: m_logger.log(Level.WARNING,
174: e.getLocalizedMessage(), e);
175: throw e;
176: }
177: }
178:
179: out.flush();
180:
181: } catch (Exception e) {
182: m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
183: } finally {
184: out.flush();
185: out.close();
186: }
187:
188: }
189: }
|