1 package com.sslexplorer.server.jetty; 2 3 import java.io.IOException ; 4 import java.util.ArrayList ; 5 6 import org.mortbay.http.HttpContext; 7 import org.mortbay.http.HttpException; 8 import org.mortbay.http.HttpHandler; 9 import org.mortbay.http.HttpRequest; 10 import org.mortbay.http.HttpResponse; 11 12 import com.sslexplorer.boot.ContextHolder; 13 import com.sslexplorer.boot.RequestHandler; 14 import com.sslexplorer.boot.RequestHandlerException; 15 16 17 28 public class HTTPRedirectHandler implements HttpHandler { 29 30 HttpContext context; 31 32 static ArrayList <RequestHandler> handlers = new ArrayList <RequestHandler>(); 33 34 35 public static void registerHandler(RequestHandler handler) { 36 handlers.add(handler); 37 } 38 39 public static void unregisterHandler(RequestHandler handler) { 40 handlers.remove(handler); 41 } 42 45 public HTTPRedirectHandler() { 46 } 47 48 51 public void initialize(HttpContext context) { 52 this.context = context; 53 } 54 55 58 public boolean isStarted() { 59 return true; 60 } 61 62 65 public void stop() { 66 67 } 68 69 72 public void start() { 73 74 } 75 76 79 public String getName() { 80 return "SECURE"; 81 } 82 83 86 public HttpContext getHttpContext() { 87 return context; 88 } 89 90 93 public void handle(String pathInContext, 94 String str, 95 HttpRequest request, 96 HttpResponse response) throws IOException { 97 handle(pathInContext, request, response); 98 } 99 107 public void handle(String pathInContext, 108 HttpRequest request, 109 HttpResponse response) throws IOException { 110 111 for(RequestHandler handler : handlers) { 112 try { 113 request.setCharacterEncoding(System.getProperty("sslexplorer.encoding", "UTF-8"), false); 114 if (handler.handle(pathInContext, "", new RequestAdapter(request), new ResponseAdapter(response))) { 115 request.setHandled(true); 116 return; 117 } 118 } catch (RequestHandlerException e) { 119 throw new HttpException(e.getCode(), e.getMessage()); 120 } 121 122 } 123 124 if(!request.isConfidential()) { 125 int sslPort = ContextHolder.getContext().getPort(); 126 response.sendRedirect("https://" + request.getHost() + (sslPort > 0 && sslPort!=443 ? ":" + sslPort : "") + request.getEncodedPath()); 127 request.setHandled(true); 128 } 129 } 130 } 131 | Popular Tags |