Home » openejb-3.1.2-src » org.apache » openejb » webadmin » httpd » [javadoc | source]

    1   /**
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *     http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   package org.apache.openejb.webadmin.httpd;
   18   
   19   import java.io.FileNotFoundException;
   20   import java.io.IOException;
   21   import java.io.InputStream;
   22   import java.io.OutputStream;
   23   import java.net.MalformedURLException;
   24   import java.net.URL;
   25   import java.net.URLConnection;
   26   import java.util.ArrayList;
   27   import java.util.StringTokenizer;
   28   
   29   import javax.ejb.SessionContext;
   30   import javax.ejb.Stateless;
   31   import javax.ejb.RemoteHome;
   32   
   33   import org.apache.openejb.webadmin.HttpBean;
   34   import org.apache.openejb.webadmin.HttpRequest;
   35   import org.apache.openejb.webadmin.HttpResponse;
   36   import org.apache.openejb.webadmin.HttpHome;
   37   import org.apache.openejb.loader.SystemInstance;
   38   
   39   /** This is a webadmin bean which has default functionality such as genderating
   40    * error pages and setting page content.
   41    * @author <a href="mailto:david.blevins@visi.com">David Blevins</a>
   42    */
   43   @Stateless(name = "httpd/DefaultBean")
   44   @RemoteHome(HttpHome.class)
   45   public class DefaultHttpBean implements HttpBean {
   46   
   47       /** The path in which to look for files. */
   48       private static final URL[] PATH = getSearchPath();
   49       
   50       /** the ejb session context */
   51   	private SessionContext context;
   52   
   53       
   54       private static URL[] getSearchPath(){
   55           ArrayList path = new ArrayList();
   56   
   57           try {
   58               //OpenEJB Home and Base folders
   59               URL base = SystemInstance.get().getBase().getDirectory().toURL();
   60               URL home = SystemInstance.get().getHome().getDirectory().toURL();
   61   
   62               ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
   63   
   64               if (!base.sameFile(home)) {
   65                   path.add(new URL(base, "htdocs/"));
   66               }
   67               path.add(new URL(home, "htdocs/"));
   68               path.add(classLoader.getResource("/htdocs/"));
   69               path.add(classLoader.getResource("/openejb/webadmin/"));
   70           } catch (Exception e) {
   71               // TODO: 1: We should never get an exception here
   72               e.printStackTrace();
   73           }
   74   
   75           return (URL[]) path.toArray(new URL[0]);
   76       }
   77   
   78   	/** Creates a new instance */
   79   	public void ejbCreate() {}
   80   
   81   	/** the main processing part of the this bean
   82   	 * @param request the http request object
   83   	 * @param response the http response object
   84   	 * @throws IOException if an exception is thrown
   85   	 */
   86   	public void onMessage(HttpRequest request, HttpResponse response) throws java.io.IOException {
   87           InputStream in = null;  
   88           OutputStream out = null;
   89   		// Internationalize this
   90   		try {
   91   			String file = request.getURI().getFile();
   92   			String ext = (file.indexOf('.') == -1) ? null : file.substring(file.indexOf('.'));
   93   
   94   			if (ext != null) {
   95   				//resolve the content type
   96   				if (ext.equalsIgnoreCase(".gif")) {
   97   					response.setContentType("image/gif");
   98   				} else if (ext.equalsIgnoreCase(".jpeg") || ext.equalsIgnoreCase(".jpg")) {
   99   					response.setContentType("image/jpeg");
  100   				} else if (ext.equalsIgnoreCase(".png")) {
  101   					response.setContentType("image/png");
  102   				} else if (ext.equalsIgnoreCase(".css")) {
  103   					response.setContentType("text/css");
  104   				} else if (ext.equalsIgnoreCase(".js")) {
  105   					response.setContentType("text/javascript");
  106   				} else if (ext.equalsIgnoreCase(".txt")) {
  107   					response.setContentType("text/plain");
  108   				} else if (ext.equalsIgnoreCase(".java")) {
  109   					response.setContentType("text/plain");
  110   				} else if (ext.equalsIgnoreCase(".xml")) {
  111   					response.setContentType("text/plain");
  112   				} else if (ext.equalsIgnoreCase(".zip")) {
  113   					response.setContentType("application/zip");
  114   				}
  115   			}
  116   
  117               
  118               
  119   			URLConnection resource = findResource(request.getURI().getFile());  
  120               HttpResponseImpl res = (HttpResponseImpl)response;
  121               res.setContent(resource);
  122   
  123           } catch (java.io.FileNotFoundException e) {
  124   			do404(request, response);
  125   
  126   		} catch (java.io.IOException e) {
  127   			do500(request, response, e.getMessage());
  128   		} finally {
  129               if (in != null) in.close();
  130           }
  131   	}
  132   
  133       private URLConnection findResource(String fileName) throws FileNotFoundException, IOException{
  134           if (fileName.startsWith("/")){
  135               fileName = fileName.substring(1);
  136           }
  137           
  138           for (int i = 0; i < PATH.length; i++) {
  139               try {
  140                   URL base = PATH[i];
  141                   URL resource = new URL(base, fileName);
  142                   URLConnection conn = resource.openConnection();
  143                   if (resource.openConnection().getContentLength() > 0){
  144                       return conn;
  145                   }
  146               } catch (MalformedURLException e) {
  147               } catch (FileNotFoundException e) {
  148               }
  149           }
  150           throw new FileNotFoundException("Cannot locate resource: "+fileName);
  151       }
  152   	/** Creates a "Page not found" error screen
  153   	 * @param request the HTTP request object
  154   	 * @param response the HTTP response object
  155   	 */
  156   	public void do404(HttpRequest request, HttpResponse response) {
  157   		response.reset(404, "Object not found.");
  158   		java.io.PrintWriter body = response.getPrintWriter();
  159   
  160   		body.println("<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">");
  161   		body.println("<HTML><HEAD>");
  162   		body.println("<TITLE>404 Not Found</TITLE>");
  163   		body.println("</HEAD><BODY>");
  164   		body.println("<H1>Not Found</H1>");
  165   		body.println(
  166   			"The requested URL <font color=\"red\">"
  167   				+ request.getURI().getFile()
  168   				+ "</font> was not found on this server.<P>");
  169   		body.println("<HR>");
  170   		body.println("<ADDRESS>" + response.getServerName() + "</ADDRESS>");
  171   		body.println("</BODY></HTML>");
  172   	}
  173   
  174   	/** Creates and "Internal Server Error" page
  175   	 * @param request the HTTP request object
  176   	 * @param response the HTTP response object
  177   	 * @param message the message to be sent back to the browser
  178   	 */
  179   	public void do500(HttpRequest request, HttpResponse response, String message) {
  180   		response.reset(500, "Internal Server Error.");
  181   		java.io.PrintWriter body = response.getPrintWriter();
  182   		body.println("<html>");
  183   		body.println("<body>");
  184   		body.println("<h3>Internal Server Error</h3>");
  185   		body.println("<br><br>");
  186   
  187   		if (message != null) {
  188   			StringTokenizer msg = new StringTokenizer(message, "\n\r");
  189   			while (msg.hasMoreTokens()) {
  190   				body.print(msg.nextToken());
  191   				body.println("<br>");
  192   			}
  193   		}
  194   
  195   		body.println("</body>");
  196   		body.println("</html>");
  197   	}
  198   
  199   	/** called on a stateful sessionbean after the bean is
  200   	 * deserialized from storage and put back into use.      
  201   	 * @throws javax.ejb.EJBException if an exeption is thrown
  202   	 * @throws java.rmi.RemoteException if an exception is thrown
  203   	 */
  204   	public void ejbActivate() throws javax.ejb.EJBException, java.rmi.RemoteException {}
  205   
  206   	/** called on a stateful sessionbean before the bean is 
  207   	 * removed from memory and serialized to a temporary store.  
  208   	 * This method is never called on a stateless sessionbean
  209   	 * @throws javax.ejb.EJBException if an exception is thrown
  210   	 * @throws java.rmi.RemoteException if an exception is thrown
  211   	 */
  212   	public void ejbPassivate() throws javax.ejb.EJBException, java.rmi.RemoteException {}
  213   
  214   	/** called by the ejb container when this bean is about to be garbage collected
  215   	 * @throws javax.ejb.EJBException if an exception is thrown
  216   	 * @throws java.rmi.RemoteException if an exception is thrown
  217   	 */
  218   	public void ejbRemove() throws javax.ejb.EJBException, java.rmi.RemoteException {}
  219   
  220   	/** sets the session context for this bean
  221   	 * @param sessionContext the session context to be set
  222   	 * @throws javax.ejb.EJBException if an exception is thrown
  223   	 * @throws java.rmi.RemoteException if an exception is thrown
  224   	 */
  225   	public void setSessionContext(javax.ejb.SessionContext sessionContext)
  226   		throws javax.ejb.EJBException, java.rmi.RemoteException {
  227   		this.context = sessionContext;
  228   	}
  229   }

Home » openejb-3.1.2-src » org.apache » openejb » webadmin » httpd » [javadoc | source]