001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.webadmin.httpd;
017:
018: import java.io.FileNotFoundException;
019: import java.io.IOException;
020: import java.io.InputStream;
021: import java.io.OutputStream;
022: import java.net.MalformedURLException;
023: import java.net.URL;
024: import java.net.URLConnection;
025: import java.util.ArrayList;
026: import java.util.StringTokenizer;
027:
028: import javax.ejb.SessionContext;
029: import javax.ejb.Stateless;
030: import javax.ejb.RemoteHome;
031:
032: import org.apache.openejb.webadmin.HttpBean;
033: import org.apache.openejb.webadmin.HttpRequest;
034: import org.apache.openejb.webadmin.HttpResponse;
035: import org.apache.openejb.webadmin.HttpHome;
036: import org.apache.openejb.loader.SystemInstance;
037:
038: /** This is a webadmin bean which has default functionality such as genderating
039: * error pages and setting page content.
040: * @author <a href="mailto:david.blevins@visi.com">David Blevins</a>
041: */
042: @Stateless(name="httpd/DefaultBean")
043: @RemoteHome(HttpHome.class)
044: public class DefaultHttpBean implements HttpBean {
045:
046: /** The path in which to look for files. */
047: private static final URL[] PATH = getSearchPath();
048:
049: /** the ejb session context */
050: private SessionContext context;
051:
052: private static URL[] getSearchPath() {
053: ArrayList path = new ArrayList();
054:
055: try {
056: //OpenEJB Home and Base folders
057: URL base = SystemInstance.get().getBase().getDirectory()
058: .toURL();
059: URL home = SystemInstance.get().getHome().getDirectory()
060: .toURL();
061:
062: ClassLoader classLoader = Thread.currentThread()
063: .getContextClassLoader();
064:
065: if (!base.sameFile(home)) {
066: path.add(new URL(base, "htdocs/"));
067: }
068: path.add(new URL(home, "htdocs/"));
069: path.add(classLoader.getResource("/htdocs/"));
070: path.add(classLoader.getResource("/openejb/webadmin/"));
071: } catch (Exception e) {
072: // TODO: 1: We should never get an exception here
073: e.printStackTrace();
074: }
075:
076: return (URL[]) path.toArray(new URL[0]);
077: }
078:
079: /** Creates a new instance */
080: public void ejbCreate() {
081: }
082:
083: /** the main processing part of the this bean
084: * @param request the http request object
085: * @param response the http response object
086: * @throws IOException if an exception is thrown
087: */
088: public void onMessage(HttpRequest request, HttpResponse response)
089: throws java.io.IOException {
090: InputStream in = null;
091: OutputStream out = null;
092: // Internationalize this
093: try {
094: String file = request.getURI().getFile();
095: String ext = (file.indexOf('.') == -1) ? null : file
096: .substring(file.indexOf('.'));
097:
098: if (ext != null) {
099: //resolve the content type
100: if (ext.equalsIgnoreCase(".gif")) {
101: response.setContentType("image/gif");
102: } else if (ext.equalsIgnoreCase(".jpeg")
103: || ext.equalsIgnoreCase(".jpg")) {
104: response.setContentType("image/jpeg");
105: } else if (ext.equalsIgnoreCase(".png")) {
106: response.setContentType("image/png");
107: } else if (ext.equalsIgnoreCase(".css")) {
108: response.setContentType("text/css");
109: } else if (ext.equalsIgnoreCase(".js")) {
110: response.setContentType("text/javascript");
111: } else if (ext.equalsIgnoreCase(".txt")) {
112: response.setContentType("text/plain");
113: } else if (ext.equalsIgnoreCase(".java")) {
114: response.setContentType("text/plain");
115: } else if (ext.equalsIgnoreCase(".xml")) {
116: response.setContentType("text/plain");
117: } else if (ext.equalsIgnoreCase(".zip")) {
118: response.setContentType("application/zip");
119: }
120: }
121:
122: URLConnection resource = findResource(request.getURI()
123: .getFile());
124: HttpResponseImpl res = (HttpResponseImpl) response;
125: res.setContent(resource);
126:
127: } catch (java.io.FileNotFoundException e) {
128: do404(request, response);
129:
130: } catch (java.io.IOException e) {
131: do500(request, response, e.getMessage());
132: } finally {
133: if (in != null)
134: in.close();
135: }
136: }
137:
138: private URLConnection findResource(String fileName)
139: throws FileNotFoundException, IOException {
140: if (fileName.startsWith("/")) {
141: fileName = fileName.substring(1);
142: }
143:
144: for (int i = 0; i < PATH.length; i++) {
145: try {
146: URL base = PATH[i];
147: URL resource = new URL(base, fileName);
148: URLConnection conn = resource.openConnection();
149: if (resource.openConnection().getContentLength() > 0) {
150: return conn;
151: }
152: } catch (MalformedURLException e) {
153: } catch (FileNotFoundException e) {
154: }
155: }
156: throw new FileNotFoundException("Cannot locate resource: "
157: + fileName);
158: }
159:
160: /** Creates a "Page not found" error screen
161: * @param request the HTTP request object
162: * @param response the HTTP response object
163: */
164: public void do404(HttpRequest request, HttpResponse response) {
165: response.reset(404, "Object not found.");
166: java.io.PrintWriter body = response.getPrintWriter();
167:
168: body
169: .println("<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">");
170: body.println("<HTML><HEAD>");
171: body.println("<TITLE>404 Not Found</TITLE>");
172: body.println("</HEAD><BODY>");
173: body.println("<H1>Not Found</H1>");
174: body.println("The requested URL <font color=\"red\">"
175: + request.getURI().getFile()
176: + "</font> was not found on this server.<P>");
177: body.println("<HR>");
178: body.println("<ADDRESS>" + response.getServerName()
179: + "</ADDRESS>");
180: body.println("</BODY></HTML>");
181: }
182:
183: /** Creates and "Internal Server Error" page
184: * @param request the HTTP request object
185: * @param response the HTTP response object
186: * @param message the message to be sent back to the browser
187: */
188: public void do500(HttpRequest request, HttpResponse response,
189: String message) {
190: response.reset(500, "Internal Server Error.");
191: java.io.PrintWriter body = response.getPrintWriter();
192: body.println("<html>");
193: body.println("<body>");
194: body.println("<h3>Internal Server Error</h3>");
195: body.println("<br><br>");
196:
197: if (message != null) {
198: StringTokenizer msg = new StringTokenizer(message, "\n\r");
199: while (msg.hasMoreTokens()) {
200: body.print(msg.nextToken());
201: body.println("<br>");
202: }
203: }
204:
205: body.println("</body>");
206: body.println("</html>");
207: }
208:
209: /** called on a stateful sessionbean after the bean is
210: * deserialized from storage and put back into use.
211: * @throws javax.ejb.EJBException if an exeption is thrown
212: * @throws java.rmi.RemoteException if an exception is thrown
213: */
214: public void ejbActivate() throws javax.ejb.EJBException,
215: java.rmi.RemoteException {
216: }
217:
218: /** called on a stateful sessionbean before the bean is
219: * removed from memory and serialized to a temporary store.
220: * This method is never called on a stateless sessionbean
221: * @throws javax.ejb.EJBException if an exception is thrown
222: * @throws java.rmi.RemoteException if an exception is thrown
223: */
224: public void ejbPassivate() throws javax.ejb.EJBException,
225: java.rmi.RemoteException {
226: }
227:
228: /** called by the ejb container when this bean is about to be garbage collected
229: * @throws javax.ejb.EJBException if an exception is thrown
230: * @throws java.rmi.RemoteException if an exception is thrown
231: */
232: public void ejbRemove() throws javax.ejb.EJBException,
233: java.rmi.RemoteException {
234: }
235:
236: /** sets the session context for this bean
237: * @param sessionContext the session context to be set
238: * @throws javax.ejb.EJBException if an exception is thrown
239: * @throws java.rmi.RemoteException if an exception is thrown
240: */
241: public void setSessionContext(
242: javax.ejb.SessionContext sessionContext)
243: throws javax.ejb.EJBException, java.rmi.RemoteException {
244: this.context = sessionContext;
245: }
246: }
|