001: /*
002: * argun 1.0
003: * Web 2.0 delivery framework
004: * Copyright (C) 2007 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.web;
024:
025: import java.io.IOException;
026: import java.util.Collection;
027:
028: import javax.servlet.ServletConfig;
029: import javax.servlet.ServletException;
030: import javax.servlet.http.HttpServlet;
031: import javax.servlet.http.HttpServletRequest;
032: import javax.servlet.http.HttpServletResponse;
033:
034: import org.apache.log4j.Logger;
035:
036: import biz.hammurapi.convert.CompositeConverter;
037: import biz.hammurapi.eval.EvaluationException;
038: import biz.hammurapi.eval.Evaluator;
039: import biz.hammurapi.metrics.MeasurementCategoryFactory;
040: import biz.hammurapi.metrics.TimeIntervalCategory;
041: import biz.hammurapi.xml.dom.CompositeDomSerializer;
042:
043: /**
044: * Invokes methods of request or session attributes and then forwards request to url tail.
045: * @web:servlet name="InvocationServlet" display-name="InvocationServlet" load-on-startup="1"
046: * @web:servlet-mapping url-pattern="/invoke/*"
047: */
048: public class ForwardingExpandingServlet extends HttpServlet {
049:
050: private static final Logger logger = Logger
051: .getLogger(ForwardingExpandingServlet.class);
052: private static final TimeIntervalCategory tic = MeasurementCategoryFactory
053: .getTimeIntervalCategory(ForwardingExpandingServlet.class);
054:
055: /**
056: * Override this method to return custom serializer if needed.
057: * @return DomSerializer
058: */
059: protected CompositeDomSerializer getDomSerializer() {
060: return CompositeDomSerializer.getThreadInstance();
061: }
062:
063: public void init(ServletConfig config) throws ServletException {
064: logger.info("Initialization");
065: super .init(config);
066: }
067:
068: /**
069: * Override this method if you need to supply some specific converter
070: * @return converter
071: */
072: protected CompositeConverter getConverter() {
073: return CompositeConverter.getDefaultConverter();
074: }
075:
076: protected void service(HttpServletRequest request,
077: HttpServletResponse response) throws ServletException,
078: IOException {
079: long start = tic.getTime();
080: String pathInfo = request.getPathInfo();
081: logger.debug("Serving " + pathInfo);
082: try {
083: if (pathInfo == null || "/".equals(pathInfo)) {
084: response.sendError(404, "Invalid path: " + pathInfo);
085: return;
086: }
087:
088: String[] path = pathInfo.split("/", 5);
089:
090: if (path.length < 2) {
091: response.sendError(404, "Invalid path: " + pathInfo);
092: return;
093: }
094:
095: CompositeConverter converter = CompositeConverter
096: .getDefaultConverter();
097: Evaluator evaluator = new Evaluator(path[1], null,
098: converter);
099: Collection res = evaluator.evaluate(new RequestContext(
100: request));
101:
102: if (path.length == 2) {
103: return;
104: }
105:
106: if (path.length != 5) {
107: response.sendError(404, "Invalid path: " + pathInfo);
108: return;
109: }
110:
111: if ("session".equals(path[2])) {
112: request.getSession().setAttribute(path[3], res);
113: } else if ("request".equals(path[2])) {
114: request.setAttribute(path[3], res);
115: } else {
116: response.sendError(404, "Invalid path: " + pathInfo);
117: return;
118: }
119:
120: getServletContext().getRequestDispatcher(path[4]).forward(
121: request, response);
122: } catch (EvaluationException e) {
123: throw new ServletException(e);
124: } finally {
125: tic.addInterval(pathInfo, start);
126: }
127: }
128: }
|