KickJava   Java API By Example, From Geeks To Geeks.

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


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 package com.sslexplorer.agent;
21
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27
28 import com.maverick.multiplex.MultiplexedConnection;
29 import com.maverick.multiplex.Request;
30 import com.maverick.multiplex.RequestHandler;
31 import com.maverick.util.ByteArrayReader;
32 import com.maverick.util.ByteArrayWriter;
33 import com.sslexplorer.policyframework.LaunchSession;
34 import com.sslexplorer.policyframework.LaunchSessionFactory;
35 import com.sslexplorer.policyframework.LaunchSessionManager;
36 import com.sslexplorer.policyframework.Policy;
37 import com.sslexplorer.policyframework.PolicyDatabaseFactory;
38 import com.sslexplorer.policyframework.Resource;
39 import com.sslexplorer.policyframework.ResourceType;
40 import com.sslexplorer.policyframework.ResourceUtil;
41
42 public class ResourceRequestHandler implements RequestHandler {
43
44     static Log log = LogFactory.getLog(ResourceRequestHandler.class);
45
46     public boolean processRequest(Request request, MultiplexedConnection connection) {
47         AgentTunnel agent = (AgentTunnel) connection;
48         if (request.getRequestName().equals("getResources@3sp.com")) {
49             try {
50                 if(request.getRequestData()!=null) {
51                     ByteArrayReader bar = new ByteArrayReader(request.getRequestData());
52                     int resourceTypeId = (int)bar.readInt();
53                     ResourceType resourceType = PolicyDatabaseFactory.getInstance().getResourceType(resourceTypeId);
54                     if(resourceType == null) {
55                         throw new Exception JavaDoc("Request for list of resources of an unknown type (" + resourceTypeId + ")");
56                     }
57                     List JavaDoc resources = ResourceUtil.getGrantedResource(agent.getSession(), resourceType);
58                     Resource resource;
59                     ByteArrayWriter response = new ByteArrayWriter();
60                     response.writeInt(resources.size());
61                     for (Iterator JavaDoc it = resources.iterator(); it.hasNext();) {
62                         resource = (Resource) it.next();
63                         response.writeInt(resource.getResourceId());
64                         response.writeString(resource.getResourceDisplayName());
65                     }
66                     request.setRequestData(response.toByteArray());
67                     return true;
68                 }
69             } catch (Exception JavaDoc e) {
70                 log.error("Failed to get resources.", e);
71             }
72
73         }
74         else if (request.getRequestName().equals("setupResourceLaunch@3sp.com")) {
75             try {
76                 if(request.getRequestData()!=null) {
77                     ByteArrayReader bar = new ByteArrayReader(request.getRequestData());
78                     int resourceTypeId = (int)bar.readInt();
79                     ResourceType resourceType = PolicyDatabaseFactory.getInstance().getResourceType(resourceTypeId);
80                     if(resourceType == null) {
81                         throw new Exception JavaDoc("Request for launch of resource of an unknown type (" + resourceTypeId + ")");
82                     }
83                     int id = (int)bar.readInt();
84                     Resource resource = resourceType.getResourceById(id);
85                     if (resource == null) {
86                         throw new Exception JavaDoc("No resource with ID " + id + " and type of " + resourceTypeId);
87                     }
88                     Policy policy = LaunchSessionManager.getLaunchRequestPolicy(null, agent.getSession(), resource);
89                     if (resource.sessionPasswordRequired(agent.getSession())) {
90                         log.error("Resource requires session password. Not yet supported.");
91                         return false;
92                     } else {
93                         LaunchSession launchSession = LaunchSessionFactory.getInstance().createLaunchSession(agent.getSession(),
94                             resource,
95                             policy);
96                         ByteArrayWriter baw = new ByteArrayWriter();
97                         baw.writeString(launchSession.getId());
98                         request.setRequestData(baw.toByteArray());
99                         return true;
100                     }
101                 }
102             } catch (Exception JavaDoc e) {
103                 log.error("Failed to setup resource launch.", e);
104                 return false;
105             }
106         }
107         return false;
108     }
109
110     public void postReply(MultiplexedConnection connection) {
111     }
112 }
113
Popular Tags