KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > agent > AgentRequestHandler


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20
21 package com.sslexplorer.agent;
22
23 import java.io.IOException JavaDoc;
24 import java.util.Enumeration JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Map JavaDoc;
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 JavaDoc pathInContext, String JavaDoc pathParams,
45             RequestHandlerRequest request, RequestHandlerResponse response)
46             throws IOException JavaDoc, RequestHandlerException {
47
48
49         if(request.getPath().startsWith("/agent")) {
50             
51             String JavaDoc type = (String JavaDoc) 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                         // Authentication failed
62
if(log.isDebugEnabled())
63                             log.debug("Authentication failed. Sending 403 - FORBIDDEN");
64                         response.sendError(HttpConstants.RESP_403_FORBIDDEN, "Access forbidden");
65                     }
66                     else {
67                         // Authentication required
68
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                         /**
82                          * LDP - We set the socket timeout to twice the value of the
83                          * keep-alive interval. This means we should only ever timeout
84                          * if the agent becomes unresponsive.
85                          */

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                 // No credentials to authenticate
98
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 JavaDoc( { "unchecked" })
111     private static Map JavaDoc getParameters(RequestHandlerRequest request) {
112         Map JavaDoc parameters = new HashMap JavaDoc(request.getParameters());
113         for (Enumeration JavaDoc fieldNames = request.getFieldNames(); fieldNames.hasMoreElements();) {
114             String JavaDoc fieldName = (String JavaDoc) fieldNames.nextElement();
115             Enumeration JavaDoc fieldValues = request.getFieldValues(fieldName);
116             if (fieldValues.hasMoreElements())
117                 parameters.put(fieldName, fieldValues.nextElement());
118         }
119         return parameters;
120     }
121 }
Popular Tags