1 19 20 package com.sslexplorer.applications; 21 22 import java.util.HashMap ; 23 import java.util.Properties ; 24 25 import org.apache.commons.logging.Log; 26 import org.apache.commons.logging.LogFactory; 27 28 import com.maverick.multiplex.Channel; 29 import com.maverick.multiplex.MultiplexedConnection; 30 import com.maverick.multiplex.Request; 31 import com.maverick.multiplex.RequestHandler; 32 import com.maverick.util.ByteArrayReader; 33 import com.maverick.util.ByteArrayWriter; 34 import com.sslexplorer.agent.AbstractResourceService; 35 import com.sslexplorer.agent.AgentService; 36 import com.sslexplorer.agent.AgentTunnel; 37 import com.sslexplorer.agent.channels.ApplicationFileChannel; 38 import com.sslexplorer.core.CoreAttributeConstants; 39 import com.sslexplorer.core.CoreEvent; 40 import com.sslexplorer.core.CoreServlet; 41 import com.sslexplorer.extensions.ExtensionDescriptor; 42 import com.sslexplorer.extensions.ExtensionParser; 43 import com.sslexplorer.extensions.ExtensionBundle.ExtensionBundleStatus; 44 import com.sslexplorer.extensions.store.ExtensionStore; 45 import com.sslexplorer.policyframework.LaunchSession; 46 import com.sslexplorer.policyframework.LaunchSessionFactory; 47 import com.sslexplorer.policyframework.LaunchSessionManager; 48 import com.sslexplorer.policyframework.Policy; 49 import com.sslexplorer.policyframework.Resource; 50 import com.sslexplorer.policyframework.ResourceAccessEvent; 51 52 58 public class ApplicationService extends AbstractResourceService implements RequestHandler { 59 60 final static Log log = LogFactory.getLog(ApplicationService.class); 61 62 65 public ApplicationService() { 66 super(ApplicationsPlugin.APPLICATION_SHORTCUT_RESOURCE_TYPE, new int[] { 67 ApplicationShortcutEventConstants.CREATE_APPLICATION_SHORTCUT, 68 ApplicationShortcutEventConstants.REMOVE_APPLICATION_SHORTCUT, 69 ApplicationShortcutEventConstants.UPDATE_APPLICATION_SHORTCUT 70 }); 71 } 72 73 76 public void performStartup(AgentTunnel agent) { 77 } 78 79 88 public Request launchApplication(LaunchSession launchSession) throws Exception { 89 ApplicationShortcut shortcut = (ApplicationShortcut) launchSession.getResource(); 90 ByteArrayWriter msg = new ByteArrayWriter(); 91 92 ExtensionDescriptor descriptor = ExtensionStore.getInstance().getExtensionDescriptor(shortcut.getApplication()); 94 if(((ApplicationLauncherType)descriptor.getExtensionType()).isServerSide()) { 95 msg.writeBoolean(true); 96 97 98 try { 100 if(descriptor.getApplicationBundle().getStatus() != ExtensionBundleStatus.ACTIVATED) { 101 throw new Exception ("Extension bundle " + descriptor.getApplicationBundle().getId() +" is not activated, cannot launch applicaiton."); 102 } 103 104 ((ApplicationLauncherType) descriptor.getExtensionType()).launch(new HashMap <String , String >(), 105 descriptor, 106 shortcut, 107 null, 108 launchSession, 109 null, 110 null); 111 112 CoreServlet.getServlet().fireCoreEvent(new ResourceAccessEvent(this, 113 ApplicationShortcutEventConstants.APPLICATION_SHORTCUT_LAUNCHED, 114 launchSession.getResource(), 115 launchSession.getPolicy(), 116 launchSession.getSession(), 117 CoreEvent.STATE_SUCCESSFUL).addAttribute(CoreAttributeConstants.EVENT_ATTR_APPLICATION_NAME, 118 descriptor.getName()).addAttribute(CoreAttributeConstants.EVENT_ATTR_APPLICATION_ID, descriptor.getId())); 119 120 } catch (Exception ex) { 121 CoreServlet.getServlet().fireCoreEvent(new ResourceAccessEvent(this, 122 ApplicationShortcutEventConstants.APPLICATION_SHORTCUT_LAUNCHED, 123 launchSession.getSession(), 124 ex).addAttribute(CoreAttributeConstants.EVENT_ATTR_APPLICATION_NAME, descriptor.getName()) 125 .addAttribute(CoreAttributeConstants.EVENT_ATTR_APPLICATION_ID, descriptor.getId())); 126 throw ex; 127 128 } 129 } 130 else { 131 msg.writeBoolean(false); 132 msg.writeString(shortcut.getApplication()); 133 msg.writeInt(shortcut.getResourceId()); 134 msg.writeString(launchSession.getId()); 135 msg.writeString(ExtensionParser.processApplicationParameters(launchSession, 136 new Properties (), 137 shortcut.getParameters(), 138 shortcut.getApplication())); 139 } 140 141 return new Request("launchApplication@3sp.com", msg.toByteArray()); 142 } 143 144 147 public Channel createChannel(MultiplexedConnection connection, String type) { 148 if (type.equals(ApplicationFileChannel.CHANNEL_TYPE)) { 149 return new ApplicationFileChannel((AgentTunnel)connection); 150 } else 151 return null; 152 } 153 154 157 public boolean processRequest(Request request, MultiplexedConnection connection) { 158 159 AgentTunnel agent = (AgentTunnel) connection; 160 161 if (request.getRequestName().equals("setupAndLaunchApplication@3sp.com") && request.getRequestData()!=null) { 162 163 try { 164 ByteArrayReader reader = new ByteArrayReader(request.getRequestData()); 165 int id = (int)reader.readInt(); 166 Resource resource = ApplicationsPlugin.APPLICATION_SHORTCUT_RESOURCE_TYPE.getResourceById(id); 167 if (resource == null) { 168 throw new Exception ("No resource with ID " + id); 169 } 170 Policy policy = LaunchSessionManager.getLaunchRequestPolicy(null, agent.getSession(), resource); 171 if (resource.sessionPasswordRequired(agent.getSession())) { 172 return true; 174 } else { 175 LaunchSession launchSession = LaunchSessionFactory.getInstance().createLaunchSession(agent.getSession(), 176 resource, 177 policy); 178 launchSession.checkAccessRights(null, agent.getSession()); 179 180 ApplicationShortcut shortcut = (ApplicationShortcut) launchSession.getResource(); 181 ExtensionDescriptor descriptor = ExtensionStore.getInstance().getExtensionDescriptor(shortcut.getApplication()); 182 183 Request newRequest = launchApplication(launchSession); 184 request.setRequestData(newRequest.getRequestData()); 185 186 CoreServlet.getServlet().fireCoreEvent(new ResourceAccessEvent(this, 187 ApplicationShortcutEventConstants.APPLICATION_SHORTCUT_LAUNCHED, 188 launchSession.getResource(), 189 launchSession.getPolicy(), 190 launchSession.getSession(), 191 CoreEvent.STATE_SUCCESSFUL).addAttribute(CoreAttributeConstants.EVENT_ATTR_APPLICATION_NAME, 192 descriptor.getName()).addAttribute(CoreAttributeConstants.EVENT_ATTR_APPLICATION_ID, descriptor.getId())); 193 194 } 195 return true; 196 } catch (Exception e) { 197 e.printStackTrace(); 198 return false; 199 } 200 } 201 return false; 202 } 203 204 207 public void postReply(MultiplexedConnection connection) { 208 } 209 210 213 public void initializeTunnel(AgentTunnel tunnel) { 214 tunnel.registerRequestHandler("setupAndLaunchApplication@3sp.com", this); 215 } 216 } 217 | Popular Tags |