1 45 package org.openejb.webadmin.httpd; 46 47 import java.io.FileNotFoundException ; 48 import java.io.IOException ; 49 import java.io.InputStream ; 50 import java.io.OutputStream ; 51 import java.net.MalformedURLException ; 52 import java.net.URL ; 53 import java.net.URLConnection ; 54 import java.util.ArrayList ; 55 import java.util.StringTokenizer ; 56 57 import javax.ejb.SessionContext ; 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 69 public class DefaultHttpBean implements HttpBean { 70 71 72 private static final URL [] PATH = getSearchPath(); 73 74 75 private SessionContext context; 76 77 78 private static URL [] getSearchPath(){ 79 ArrayList path = new ArrayList (); 80 81 try { 82 URL base = SystemInstance.get().getBase().getDirectory().toURL(); 84 URL home = SystemInstance.get().getHome().getDirectory().toURL(); 85 86 87 if (!base.sameFile(home)){ 88 path.add(new URL (base,"htdocs/")); 89 } 90 path.add(new URL (home,"htdocs/")); 91 path.add(new URL ("resource:/htdocs/")); 92 path.add(new URL ("resource:/openejb/webadmin/")); 93 } catch (Exception e) { 94 e.printStackTrace(); 96 } 97 98 return (URL []) path.toArray(new URL [0]); 99 } 100 101 102 public void ejbCreate() {} 103 104 109 public void onMessage(HttpRequest request, HttpResponse response) throws java.io.IOException { 110 InputStream in = null; 111 OutputStream out = null; 112 try { 114 String file = request.getURI().getFile(); 115 String ext = (file.indexOf('.') == -1) ? null : file.substring(file.indexOf('.')); 116 117 if (ext != null) { 118 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 resource = findResource(request.getURI().getFile()); 143 HttpResponseImpl res = (HttpResponseImpl)response; 144 res.setContent(resource); 145 146 } catch (java.io.FileNotFoundException e) { 147 do404(request, response); 148 149 } catch (java.io.IOException e) { 150 do500(request, response, e.getMessage()); 151 } finally { 152 if (in != null) in.close(); 153 } 154 } 155 156 private URLConnection findResource(String fileName) throws FileNotFoundException , IOException { 157 if (fileName.startsWith("/")){ 158 fileName = fileName.substring(1); 159 } 160 161 for (int i = 0; i < PATH.length; i++) { 162 try { 163 URL base = PATH[i]; 164 URL resource = new URL (base, fileName); 165 URLConnection conn = resource.openConnection(); 166 if (resource.openConnection().getContentLength() > 0){ 167 return conn; 168 } 169 } catch (MalformedURLException e) { 170 } catch (FileNotFoundException e) { 171 } 172 } 173 throw new FileNotFoundException ("Cannot locate resource: "+fileName); 174 } 175 179 public void do404(HttpRequest request, HttpResponse response) { 180 response.reset(404, "Object not found."); 181 java.io.PrintWriter 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 202 public void do500(HttpRequest request, HttpResponse response, String message) { 203 response.reset(500, "Internal Server Error."); 204 java.io.PrintWriter 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 msg = new StringTokenizer (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 227 public void ejbActivate() throws javax.ejb.EJBException , java.rmi.RemoteException {} 228 229 235 public void ejbPassivate() throws javax.ejb.EJBException , java.rmi.RemoteException {} 236 237 241 public void ejbRemove() throws javax.ejb.EJBException , java.rmi.RemoteException {} 242 243 248 public void setSessionContext(javax.ejb.SessionContext sessionContext) 249 throws javax.ejb.EJBException , java.rmi.RemoteException { 250 this.context = sessionContext; 251 } 252 } 253 | Popular Tags |