001: /*
002: * This file is part of the WfMOpen project.
003: * Copyright (C) 2001-2004 Danet GmbH (www.danet.de), GS-AN.
004: * All rights reserved.
005: *
006: * This program is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published by
008: * the Free Software Foundation; either version 2 of the License, or
009: * (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
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * $Id: ResourceServlet.java,v 1.2 2006/09/29 12:32:08 drmlipp Exp $
021: *
022: * $Log: ResourceServlet.java,v $
023: * Revision 1.2 2006/09/29 12:32:08 drmlipp
024: * Consistently using WfMOpen as projct name now.
025: *
026: * Revision 1.1.1.1 2004/08/18 15:17:35 drmlipp
027: * Update to 1.2
028: *
029: * Revision 1.3 2004/03/24 15:15:02 lipp
030: * Added development support feature.
031: *
032: * Revision 1.2 2004/03/15 12:52:33 lipp
033: * Changed resource retrieval.
034: *
035: * Revision 1.1 2004/03/04 13:37:45 lipp
036: * Moved resource servlet up.
037: *
038: * Revision 1.3 2004/03/01 21:15:01 lipp
039: * Returns result now.
040: *
041: * Revision 1.2 2004/02/27 14:39:00 lipp
042: * Progressing.
043: *
044: * Revision 1.1 2004/02/26 15:46:44 lipp
045: * Renamed package.
046: *
047: * Revision 1.1 2004/02/25 16:51:58 lipp
048: * Getting started.
049: *
050: */
051: package de.danet.an.util.web;
052:
053: import java.io.File;
054: import java.io.IOException;
055: import java.io.InputStream;
056: import java.io.OutputStream;
057:
058: import java.net.URL;
059: import java.net.URLConnection;
060:
061: import javax.servlet.ServletConfig;
062: import javax.servlet.ServletContext;
063: import javax.servlet.ServletException;
064: import javax.servlet.http.HttpServlet;
065: import javax.servlet.http.HttpServletRequest;
066: import javax.servlet.http.HttpServletResponse;
067:
068: /**
069: * This class provides a servlet for accessing resources. The servlet
070: * uses the extra path info, prepends a configurable prefix, and looks
071: * up a resource with the resulting name. The prefix defaults to
072: * "<code>web-resource</code>". It can be altered by setting the
073: * initialization parameter "<code>lookupPrefix</code>".<P>
074: *
075: * If the requested resource is found, the resource (i.e. its content)
076: * is returned. The content type of the response is derived from the
077: * resource's suffix. If the resource cannot be found, error 404 is
078: * returned.<P>
079: *
080: * To facilitate development, an initialization parameter
081: * "<code>resourceDirectory</code>" may be set. If set, the servlet
082: * will first look for the requested resource in the specified
083: * directory (not prepending the lookup prefix!).
084: *
085: * @author <a href="mailto:mnl@mnl.de">Michael N. Lipp</a>
086: * @version $Revision: 1.2 $
087: */
088:
089: public class ResourceServlet extends HttpServlet {
090:
091: private static final org.apache.commons.logging.Log logger = org.apache.commons.logging.LogFactory
092: .getLog(ResourceServlet.class);
093:
094: // getServletConfig does not work, let's remember it ourselves
095: private ServletConfig myServletConfig = null;
096: private String lookupPrefix = "web-resource";
097: private String resourceDirectory = null;
098:
099: /**
100: * Default init method.
101: *
102: * @param servletConfig a <code>ServletConfig</code> value
103: * @exception ServletException if an error occurs
104: */
105: public void init(ServletConfig servletConfig)
106: throws ServletException {
107: myServletConfig = servletConfig;
108: String prefix = servletConfig.getInitParameter("lookupPrefix");
109: if (prefix != null) {
110: lookupPrefix = prefix;
111: }
112: resourceDirectory = servletConfig
113: .getInitParameter("resourceDirectory");
114: }
115:
116: /**
117: * Lookup the requested resource and return it.
118: *
119: * @param request a <code>HttpServletRequest</code> value
120: * @param response a <code>HttpServletResponse</code> value
121: * @exception ServletException if an error occurs
122: * @exception IOException if an error occurs
123: */
124: public void doGet(HttpServletRequest request,
125: HttpServletResponse response) throws ServletException,
126: IOException {
127: String xtra = request.getPathInfo();
128: if (xtra == null || xtra.length() == 0) {
129: response.sendError(HttpServletResponse.SC_NOT_FOUND,
130: "No resource specified");
131: return;
132: }
133: String res = "/" + lookupPrefix + xtra;
134: ServletContext ctx = myServletConfig.getServletContext();
135: URL resUrl = null;
136: if (resourceDirectory != null) {
137: File file = new File(resourceDirectory + xtra);
138: logger.debug("Trying request for \"" + xtra + "\" as \""
139: + file + "\"");
140: if (file.exists()) {
141: logger.debug("Request for \"" + xtra
142: + "\" resolved as \"" + file + "\"");
143: resUrl = file.toURL();
144: }
145: }
146: if (resUrl == null) {
147: if (logger.isDebugEnabled()) {
148: logger.debug("Request for \"" + xtra
149: + "\", will be resolved as \"" + res + "\"");
150: }
151: resUrl = ctx.getResource(res);
152: }
153: if (resUrl == null) {
154: response.sendError(HttpServletResponse.SC_NOT_FOUND,
155: "No such resource: " + res);
156: return;
157: }
158: if (logger.isDebugEnabled()) {
159: logger.debug("Retrieving from \"" + resUrl.toExternalForm()
160: + "\"");
161: }
162: URLConnection conn = resUrl.openConnection();
163: int length = conn.getContentLength();
164: if (logger.isDebugEnabled()) {
165: logger.debug("Content length of \""
166: + resUrl.toExternalForm() + "\" is " + length);
167: }
168: if (length >= 0) {
169: response.setContentLength(length);
170: }
171: String mimeType = ctx.getMimeType(res);
172: if (mimeType != null) {
173: if (logger.isDebugEnabled()) {
174: logger.debug("Mime type of response is " + mimeType);
175: }
176: response.setContentType(mimeType);
177: }
178: InputStream in = conn.getInputStream();
179: OutputStream out = response.getOutputStream();
180: byte[] buf = new byte[2048];
181: while (true) {
182: int cnt = in.read(buf);
183: if (cnt < 0) {
184: break;
185: }
186: out.write(buf, 0, cnt);
187: }
188: out.close();
189: }
190:
191: }
|