1 19 20 package com.sslexplorer.agent; 21 22 import java.util.Iterator ; 23 import java.util.List ; 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 ("Request for list of resources of an unknown type (" + resourceTypeId + ")"); 56 } 57 List resources = ResourceUtil.getGrantedResource(agent.getSession(), resourceType); 58 Resource resource; 59 ByteArrayWriter response = new ByteArrayWriter(); 60 response.writeInt(resources.size()); 61 for (Iterator 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 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 ("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 ("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 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 |