1 19 20 package com.sslexplorer.server.jetty; 21 22 import java.io.IOException ; 23 import java.util.ArrayList ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 import org.mortbay.http.HttpException; 30 import org.mortbay.http.HttpRequest; 31 import org.mortbay.http.HttpResponse; 32 import org.mortbay.http.handler.AbstractHttpHandler; 33 34 import com.sslexplorer.boot.RequestHandler; 35 import com.sslexplorer.boot.RequestHandlerException; 36 37 46 public class RequestHandlerAdapter extends AbstractHttpHandler { 47 48 50 static Log log = LogFactory.getLog(RequestHandlerAdapter.class); 51 52 54 private static final long serialVersionUID = 4682392114545977296L; 55 56 58 private List requestHandlers = new ArrayList (); 59 60 72 public void registerRequestHandler(RequestHandler requestHandler) { 73 if (log.isInfoEnabled()) 74 log.info("Registering request handler " + requestHandler.getClass().getName()); 75 requestHandlers.add(requestHandler); 76 } 77 78 89 public void deregisterRequestHandler(RequestHandler requestHandler) { 90 if (log.isInfoEnabled()) 91 log.info("De-registering request handler " + requestHandler.getClass().getName()); 92 requestHandlers.remove(requestHandler); 93 } 94 95 102 public void handle(String pathInContext, String pathParams, HttpRequest request, HttpResponse response) throws HttpException, 103 IOException { 104 if (log.isDebugEnabled()) 105 log.debug("Request for " + pathInContext); 106 for (Iterator i = requestHandlers.iterator(); i.hasNext();) { 107 try { 108 request.setCharacterEncoding(System.getProperty("sslexplorer.encoding", "UTF-8"), false); 109 RequestHandler handler = (RequestHandler) i.next(); 110 if (handler.handle(pathInContext, pathParams, new RequestAdapter(request), new ResponseAdapter(response))) { 111 request.setHandled(true); 112 break; 113 } 114 } catch (RequestHandlerException e) { 115 log.error("Failed to handle request. Status code " + e.getCode()); 116 throw new HttpException(e.getCode(), e.getMessage()); 117 } 118 } 119 } 120 } 121 | Popular Tags |