001: /*
002: * Copyright (C) 2004 TiongHiang Lee
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: * Email: thlee@onemindsoft.org
019: */
020:
021: package org.onemind.jxp.servlet;
022:
023: import java.io.*;
024: import java.util.*;
025: import java.util.Map;
026: import java.util.Properties;
027: import java.util.logging.Level;
028: import java.util.logging.Logger;
029: import javax.servlet.ServletConfig;
030: import javax.servlet.ServletException;
031: import javax.servlet.http.*;
032: import org.apache.commons.fileupload.DiskFileUpload;
033: import org.onemind.commons.java.util.*;
034: import org.onemind.jxp.*;
035: import org.xml.sax.SAXException;
036:
037: /**
038: * TODO comment
039: * @author TiongHiang Lee (thlee@onemindsoft.org)
040: *
041: */
042: public class JxpServlet extends HttpServlet {
043:
044: /** the logger **/
045: private static final Logger _logger = Logger
046: .getLogger(JxpServlet.class.getName());
047:
048: /** jxp configuration * */
049: private Properties _config;
050:
051: /** the processor **/
052: private JxpProcessor _processor;
053:
054: /** the configuration file **/
055: private String _configFile;
056:
057: /** the upload **/
058: private DiskFileUpload _uploadRepository;
059:
060: /**
061: * {@inheritDoc}
062: */
063: protected void doGet(HttpServletRequest req,
064: HttpServletResponse resp) throws ServletException,
065: IOException {
066: doPost(req, resp);
067: }
068:
069: /**
070: * Return the config
071: * @return the config
072: */
073: private Properties getConfig() {
074: return _config;
075: }
076:
077: /**
078: * Get the processor
079: * @param req the request
080: * @return the processor
081: * @throws Exception
082: */
083: private JxpProcessor getProcessor(HttpServletRequest req)
084: throws Exception {
085: if (_processor == null) {
086: synchronized (this ) {
087: if (_processor != null) {
088: return _processor;
089: }
090: if (StringUtils.isNullOrEmpty(_configFile)) {
091: _processor = new JxpProcessor(new JxpContext(
092: new FilePageSource(req.getRealPath("/"))));
093: } else {
094: _processor = JxpFactory.getProcessor(_configFile);
095: }
096:
097: }
098: }
099: return _processor;
100: }
101:
102: /**
103: * {@inheritDoc}
104: */
105: protected void doPost(HttpServletRequest req,
106: HttpServletResponse resp) throws ServletException,
107: IOException {
108: Map env = ServletUtils
109: .getServletEnvironment(getServletConfig());
110: env.putAll(ServletUtils.getRequestEnvironment(req));
111: env.putAll(ServletUtils.getExtraRequestEnvironment(req));
112: Map form = new HashMap();
113:
114: String uri = req.getRequestURI();
115: if (uri.endsWith("JxpServlet")) {
116: dumpDebugInfo(req, resp, env);
117: } else {
118: resp.setContentType("text/html");
119: resp.setStatus(200, "OK");
120: Writer writer = resp.getWriter();
121: env.put("form", form);
122: env.put("req", req);
123: env.put("resp", resp);
124: String uripath = StringUtils.substringBeforeLast(uri, "/");
125: if (uripath == null) {
126: uripath = "/";
127: }
128: env.put("uripath", uripath);
129: try {
130: form.putAll(ServletUtils.getRequestParameters(req,
131: _uploadRepository));
132: JxpProcessor processor = getProcessor(req);
133: processor.process(uri, writer, env);
134: } catch (JxpPageNotFoundException pnfe) {
135: _logger.info("Error processing uri" + uripath + ": "
136: + pnfe.getMessage());
137: writer.write(pnfe.getMessage());
138: } catch (JxpPageSourceException jpse) {
139: _logger.info("Error processing uri" + uripath + ": "
140: + jpse.getMessage());
141: writer.write(jpse.getMessage());
142: } catch (Exception e) {
143: _logger.info("Error processing uri" + uripath);
144: e.printStackTrace(new PrintWriter(writer));
145: } finally {
146: writer.flush();
147: }
148: }
149: }
150:
151: /**
152: * Output the debugging information
153: * @param req the request
154: * @param resp the resoibse
155: * @param env the environment
156: * @throws IOException io exceptions
157: */
158: private void dumpDebugInfo(HttpServletRequest req,
159: HttpServletResponse resp, Map env) throws IOException {
160: resp.setContentType("text/html");
161: resp.setStatus(200, "OK");
162: PrintWriter output = resp.getWriter();
163: output.write("<html><body><pre>");
164: MapUtils.dump(env, output);
165: output.write("</pre></body></html>");
166: output.flush();
167: output.close();
168: return;
169: }
170:
171: /**
172: * {@inheritDoc}
173: */
174: public void init(ServletConfig config) throws ServletException {
175: super .init(config);
176: LogUtils.initLoggingFromClassPath();
177: _configFile = config.getInitParameter("config");
178: // create a new file upload handler
179: _uploadRepository = new DiskFileUpload();
180: // Set upload parameters
181: _uploadRepository.setRepositoryPath(System
182: .getProperty("java.io.tmpdir"));
183: _uploadRepository.setSizeThreshold(1000000); //file > 1 meg write to disk
184: _uploadRepository.setSizeMax(-1); //no maximum size
185:
186: }
187: }
|