KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > webadmin > httpd > DefaultHttpBean


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "OpenEJB" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of The OpenEJB Group. For written permission,
18  * please contact dev@openejb.org.
19  *
20  * 4. Products derived from this Software may not be called "OpenEJB"
21  * nor may "OpenEJB" appear in their names without prior written
22  * permission of The OpenEJB Group. OpenEJB is a registered
23  * trademark of The OpenEJB Group.
24  *
25  * 5. Due credit should be given to the OpenEJB Project
26  * (http://www.openejb.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE OPENEJB GROUP AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * THE OPENEJB GROUP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2001 (C) The OpenEJB Group. All Rights Reserved.
42  *
43  * $Id: DefaultHttpBean.java 1988 2005-07-09 08:51:00Z dblevins $
44  */

45 package org.openejb.webadmin.httpd;
46
47 import java.io.FileNotFoundException JavaDoc;
48 import java.io.IOException JavaDoc;
49 import java.io.InputStream JavaDoc;
50 import java.io.OutputStream JavaDoc;
51 import java.net.MalformedURLException JavaDoc;
52 import java.net.URL JavaDoc;
53 import java.net.URLConnection JavaDoc;
54 import java.util.ArrayList JavaDoc;
55 import java.util.StringTokenizer JavaDoc;
56
57 import javax.ejb.SessionContext JavaDoc;
58
59 import org.openejb.webadmin.HttpBean;
60 import org.openejb.webadmin.HttpRequest;
61 import org.openejb.webadmin.HttpResponse;
62 import org.openejb.util.FileUtils;
63 import org.openejb.loader.SystemInstance;
64
65 /** This is a webadmin bean which has default functionality such as genderating
66  * error pages and setting page content.
67  * @author <a HREF="mailto:david.blevins@visi.com">David Blevins</a>
68  */

69 public class DefaultHttpBean implements HttpBean {
70
71     /** The path in which to look for files. */
72     private static final URL JavaDoc[] PATH = getSearchPath();
73     
74     /** the ejb session context */
75     private SessionContext JavaDoc context;
76
77     
78     private static URL JavaDoc[] getSearchPath(){
79         ArrayList JavaDoc path = new ArrayList JavaDoc();
80
81         try {
82             //OpenEJB Home and Base folders
83
URL JavaDoc base = SystemInstance.get().getBase().getDirectory().toURL();
84             URL JavaDoc home = SystemInstance.get().getHome().getDirectory().toURL();
85
86             
87             if (!base.sameFile(home)){
88                 path.add(new URL JavaDoc(base,"htdocs/"));
89             }
90             path.add(new URL JavaDoc(home,"htdocs/"));
91             path.add(new URL JavaDoc("resource:/htdocs/"));
92             path.add(new URL JavaDoc("resource:/openejb/webadmin/"));
93            } catch (Exception JavaDoc e) {
94             // TODO: 1: We should never get an exception here
95
e.printStackTrace();
96         }
97         
98         return (URL JavaDoc[]) path.toArray(new URL JavaDoc[0]);
99     }
100
101     /** Creates a new instance */
102     public void ejbCreate() {}
103
104     /** the main processing part of the this bean
105      * @param request the http request object
106      * @param response the http response object
107      * @throws IOException if an exception is thrown
108      */

109     public void onMessage(HttpRequest request, HttpResponse response) throws java.io.IOException JavaDoc {
110         InputStream JavaDoc in = null;
111         OutputStream JavaDoc out = null;
112         // Internationalize this
113
try {
114             String JavaDoc file = request.getURI().getFile();
115             String JavaDoc ext = (file.indexOf('.') == -1) ? null : file.substring(file.indexOf('.'));
116
117             if (ext != null) {
118                 //resolve the content type
119
if (ext.equalsIgnoreCase(".gif")) {
120                     response.setContentType("image/gif");
121                 } else if (ext.equalsIgnoreCase(".jpeg") || ext.equalsIgnoreCase(".jpg")) {
122                     response.setContentType("image/jpeg");
123                 } else if (ext.equalsIgnoreCase(".png")) {
124                     response.setContentType("image/png");
125                 } else if (ext.equalsIgnoreCase(".css")) {
126                     response.setContentType("text/css");
127                 } else if (ext.equalsIgnoreCase(".js")) {
128                     response.setContentType("text/javascript");
129                 } else if (ext.equalsIgnoreCase(".txt")) {
130                     response.setContentType("text/plain");
131                 } else if (ext.equalsIgnoreCase(".java")) {
132                     response.setContentType("text/plain");
133                 } else if (ext.equalsIgnoreCase(".xml")) {
134                     response.setContentType("text/plain");
135                 } else if (ext.equalsIgnoreCase(".zip")) {
136                     response.setContentType("application/zip");
137                 }
138             }
139
140             
141             
142             URLConnection JavaDoc resource = findResource(request.getURI().getFile());
143             HttpResponseImpl res = (HttpResponseImpl)response;
144             res.setContent(resource);
145
146         } catch (java.io.FileNotFoundException JavaDoc e) {
147             do404(request, response);
148
149         } catch (java.io.IOException JavaDoc e) {
150             do500(request, response, e.getMessage());
151         } finally {
152             if (in != null) in.close();
153         }
154     }
155
156     private URLConnection JavaDoc findResource(String JavaDoc fileName) throws FileNotFoundException JavaDoc, IOException JavaDoc{
157         if (fileName.startsWith("/")){
158             fileName = fileName.substring(1);
159         }
160         
161         for (int i = 0; i < PATH.length; i++) {
162             try {
163                 URL JavaDoc base = PATH[i];
164                 URL JavaDoc resource = new URL JavaDoc(base, fileName);
165                 URLConnection JavaDoc conn = resource.openConnection();
166                 if (resource.openConnection().getContentLength() > 0){
167                     return conn;
168                 }
169             } catch (MalformedURLException JavaDoc e) {
170             } catch (FileNotFoundException JavaDoc e) {
171             }
172         }
173         throw new FileNotFoundException JavaDoc("Cannot locate resource: "+fileName);
174     }
175     /** Creates a "Page not found" error screen
176      * @param request the HTTP request object
177      * @param response the HTTP response object
178      */

179     public void do404(HttpRequest request, HttpResponse response) {
180         response.reset(404, "Object not found.");
181         java.io.PrintWriter JavaDoc body = response.getPrintWriter();
182
183         body.println("<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">");
184         body.println("<HTML><HEAD>");
185         body.println("<TITLE>404 Not Found</TITLE>");
186         body.println("</HEAD><BODY>");
187         body.println("<H1>Not Found</H1>");
188         body.println(
189             "The requested URL <font color=\"red\">"
190                 + request.getURI().getFile()
191                 + "</font> was not found on this server.<P>");
192         body.println("<HR>");
193         body.println("<ADDRESS>" + response.getServerName() + "</ADDRESS>");
194         body.println("</BODY></HTML>");
195     }
196
197     /** Creates and "Internal Server Error" page
198      * @param request the HTTP request object
199      * @param response the HTTP response object
200      * @param message the message to be sent back to the browser
201      */

202     public void do500(HttpRequest request, HttpResponse response, String JavaDoc message) {
203         response.reset(500, "Internal Server Error.");
204         java.io.PrintWriter JavaDoc body = response.getPrintWriter();
205         body.println("<html>");
206         body.println("<body>");
207         body.println("<h3>Internal Server Error</h3>");
208         body.println("<br><br>");
209
210         if (message != null) {
211             StringTokenizer JavaDoc msg = new StringTokenizer JavaDoc(message, "\n\r");
212             while (msg.hasMoreTokens()) {
213                 body.print(msg.nextToken());
214                 body.println("<br>");
215             }
216         }
217
218         body.println("</body>");
219         body.println("</html>");
220     }
221
222     /** called on a stateful sessionbean after the bean is
223      * deserialized from storage and put back into use.
224      * @throws EJBException if an exeption is thrown
225      * @throws RemoteException if an exception is thrown
226      */

227     public void ejbActivate() throws javax.ejb.EJBException JavaDoc, java.rmi.RemoteException JavaDoc {}
228
229     /** called on a stateful sessionbean before the bean is
230      * removed from memory and serialized to a temporary store.
231      * This method is never called on a stateless sessionbean
232      * @throws EJBException if an exception is thrown
233      * @throws RemoteException if an exception is thrown
234      */

235     public void ejbPassivate() throws javax.ejb.EJBException JavaDoc, java.rmi.RemoteException JavaDoc {}
236
237     /** called by the ejb container when this bean is about to be garbage collected
238      * @throws EJBException if an exception is thrown
239      * @throws RemoteException if an exception is thrown
240      */

241     public void ejbRemove() throws javax.ejb.EJBException JavaDoc, java.rmi.RemoteException JavaDoc {}
242
243     /** sets the session context for this bean
244      * @param sessionContext the session context to be set
245      * @throws EJBException if an exception is thrown
246      * @throws RemoteException if an exception is thrown
247      */

248     public void setSessionContext(javax.ejb.SessionContext JavaDoc sessionContext)
249         throws javax.ejb.EJBException JavaDoc, java.rmi.RemoteException JavaDoc {
250         this.context = sessionContext;
251     }
252 }
253
Popular Tags