001: /*
002: * Copyright 2001-2006 C:1 Financial Services GmbH
003: *
004: * This software is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License Version 2.1, as published by the Free Software Foundation.
007: *
008: * This software is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public
014: * License along with this library; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
016: */
017:
018: package de.finix.contelligent.servlet;
019:
020: import java.io.IOException;
021:
022: import javax.servlet.RequestDispatcher;
023: import javax.servlet.ServletException;
024: import javax.servlet.http.HttpServlet;
025: import javax.servlet.http.HttpServletRequest;
026: import javax.servlet.http.HttpServletResponse;
027:
028: import de.finix.contelligent.logging.LoggingService;
029:
030: /**
031: * This servlet forwards to an URL defined via HTTP-request-parameter
032: * {@link #PARAM_REALURL}. This can be used to force the browser to use a
033: * specific name when downloading a resource.
034: *
035: * @web:servlet name="Forward Servlet" description="Request Forwarding servlet"
036: * @web:servlet-mapping url-pattern="/forward/*"
037: */
038: public class ForwardServlet extends HttpServlet {
039: final static org.apache.log4j.Logger log = LoggingService
040: .getLogger(ForwardServlet.class);
041:
042: final static public String PARAM_REALURL = "realURL";
043:
044: final static public String PARAM_MIME = "mime";
045:
046: final static public String PARAM_DOWNLOAD = "download";
047:
048: final protected void doPost(HttpServletRequest request,
049: HttpServletResponse response) throws ServletException,
050: IOException {
051: this .doGet(request, response);
052: }
053:
054: /**
055: * Reads request parameter {@link PARAM_REALURL} and forwards to it.
056: *
057: * @param request
058: * a <code>HttpServletRequest</code> value
059: * @param response
060: * a <code>HttpServletResponse</code> value
061: * @exception javax.servlet.ServletException
062: * if an error occurs
063: * @exception java.io.IOException
064: * if an error occurs
065: */
066: final protected void doGet(HttpServletRequest request,
067: HttpServletResponse response) throws ServletException,
068: IOException {
069: String realURL = request.getParameter(PARAM_REALURL);
070: String mime = request.getParameter(PARAM_MIME);
071: String download = request.getParameter(PARAM_DOWNLOAD);
072:
073: if (realURL != null) {
074: if (log.isDebugEnabled()) {
075: log.debug("Got URL '" + realURL + "' from parameter '"
076: + PARAM_REALURL + "' ...");
077: }
078: // remove webapp prefix or else we get no request-dispatcher
079: // XXX: this will fail if we use the 'needsSecureTransfer' feature
080: // because then
081: // components return their path including protocol, host, etc. (rs,
082: // 2002/12/06)
083: String webapp = request.getContextPath(); // starts with '/' but
084: // doesn't end with one
085: if (realURL.startsWith(webapp + '/')) {
086: realURL = realURL.substring(webapp.length());
087: log.debug("... removed webapp prefix (" + webapp
088: + ") from url ...");
089: }
090: if (log.isDebugEnabled()) {
091: log
092: .debug("... trying to get request-dispatcher for path '"
093: + realURL + "' ...");
094: }
095: RequestDispatcher dispatcher = getServletContext()
096: .getRequestDispatcher(realURL);
097: if (dispatcher == null) {
098: log
099: .error("Could not get a RequestDispatcher for path '"
100: + realURL + "'!");
101: response.sendError(HttpServletResponse.SC_NOT_FOUND,
102: "URL '" + realURL + "' not found!");
103: }
104:
105: if (mime != null) {
106: try {
107: if (download != null) {
108: if (download.equals("1")) {
109: String dlFile = request.getPathInfo()
110: .substring(1); // Ohne
111: // Slash
112: // log.info("PathInfo: "+dlFile);
113: response.addHeader("Content-disposition",
114: "attachment; filename=" + dlFile);
115: }
116: }
117:
118: if (log.isDebugEnabled()) {
119: log.debug("Force content-type to '" + mime
120: + "'");
121: }
122: response.setContentType(mime);
123: ForwardResponse forwardResponse = new ForwardResponse(
124: response);
125: dispatcher.forward(request, forwardResponse);
126: } catch (Exception e) {
127: log.error("Exception while forwarding request.", e);
128: }
129: } else
130: dispatcher.forward(request, response);
131:
132: } else {
133: log.error("Parameter '" + PARAM_REALURL + "' missing!");
134: response.sendError(HttpServletResponse.SC_BAD_REQUEST,
135: "parameter '" + PARAM_REALURL + "' missing!");
136: }
137: }
138:
139: }
|