1 19 20 21 package com.sslexplorer.agent; 22 23 import java.io.IOException ; 24 import java.util.Enumeration ; 25 import java.util.HashMap ; 26 import java.util.Map ; 27 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 31 import com.sslexplorer.boot.HttpConstants; 32 import com.sslexplorer.boot.RequestHandler; 33 import com.sslexplorer.boot.RequestHandlerException; 34 import com.sslexplorer.boot.RequestHandlerRequest; 35 import com.sslexplorer.boot.RequestHandlerResponse; 36 import com.sslexplorer.properties.Property; 37 import com.sslexplorer.properties.impl.profile.ProfilePropertyKey; 38 import com.sslexplorer.security.User; 39 40 public class AgentRequestHandler implements RequestHandler { 41 42 Log log = LogFactory.getLog(AgentRequestHandler.class); 43 44 public boolean handle(String pathInContext, String pathParams, 45 RequestHandlerRequest request, RequestHandlerResponse response) 46 throws IOException , RequestHandlerException { 47 48 49 if(request.getPath().startsWith("/agent")) { 50 51 String type = (String ) request.getParameters().get("agentType"); 52 53 if(type!=null) { 54 55 56 User user = AgentManager.getInstance().authenticate(type, request); 57 58 if(user == null) { 59 if(request.getField("Authorization") != null || request.getParameters().get("ticket") != null) { 60 61 if(log.isDebugEnabled()) 63 log.debug("Authentication failed. Sending 403 - FORBIDDEN"); 64 response.sendError(HttpConstants.RESP_403_FORBIDDEN, "Access forbidden"); 65 } 66 else { 67 if(log.isDebugEnabled()) 69 log.debug("Authentication required. Sending 401 - Unauthorized"); 70 response.setField("WWW-Authenticate", "Basic realm=\"SSL-Explorer Agent\""); 71 response.sendError(HttpConstants.RESP_401_UNAUTHORIZED, "Unauthorized"); 72 73 } 74 75 } else { 76 77 AgentTunnel agent = null; 78 try { 79 agent = AgentManager.getInstance().createAgent(request.getRemoteHost(), user, type, request); 80 81 86 int timeoutMs = Property.getPropertyInt(new ProfilePropertyKey("client.heartbeat.interval", agent.getSession())); 87 request.setTunnel(agent, (timeoutMs * 2)); 88 89 } catch (AgentException e) { 90 log.error("Could not create agent tunnel of type " + type, e); 91 response.sendError(HttpConstants.RESP_500_INTERNAL_SERVER_ERROR, e.getMessage()); 92 } 93 94 return true; 95 } 96 } else { 97 log.error("Agent did not sent agentType parameter in request"); 99 response.sendError(HttpConstants.RESP_403_FORBIDDEN, "Incorrect request"); 100 } 101 102 return true; 103 104 } else { 105 return false; 106 } 107 } 108 109 110 @SuppressWarnings ( { "unchecked" }) 111 private static Map getParameters(RequestHandlerRequest request) { 112 Map parameters = new HashMap (request.getParameters()); 113 for (Enumeration fieldNames = request.getFieldNames(); fieldNames.hasMoreElements();) { 114 String fieldName = (String ) fieldNames.nextElement(); 115 Enumeration fieldValues = request.getFieldValues(fieldName); 116 if (fieldValues.hasMoreElements()) 117 parameters.put(fieldName, fieldValues.nextElement()); 118 } 119 return parameters; 120 } 121 } | Popular Tags |