1 package com.sslexplorer.agent.channels; 2 3 import java.io.File ; 4 import java.io.FileInputStream ; 5 import java.io.IOException ; 6 7 import org.apache.commons.logging.Log; 8 import org.apache.commons.logging.LogFactory; 9 10 import com.maverick.multiplex.Channel; 11 import com.maverick.multiplex.ChannelOpenException; 12 import com.maverick.util.ByteArrayReader; 13 import com.sslexplorer.agent.AgentTunnel; 14 import com.sslexplorer.boot.Util; 15 import com.sslexplorer.extensions.ExtensionDescriptor; 16 import com.sslexplorer.extensions.store.ExtensionStore; 17 import com.sslexplorer.policyframework.LaunchSession; 18 import com.sslexplorer.policyframework.LaunchSessionFactory; 19 20 26 public class ApplicationFileChannel extends Channel implements Runnable { 27 final static Log log = LogFactory.getLog(ApplicationFileChannel.class); 28 29 32 public static final String CHANNEL_TYPE = "applicationFile@3sp.com"; 33 34 String name; 35 String launchId; 36 String filename; 37 File file; 38 FileInputStream in; 39 Thread thread; 40 AgentTunnel agent; 41 42 48 public ApplicationFileChannel(AgentTunnel agent) { 49 super(CHANNEL_TYPE, 32768, 0); 50 this.agent = agent; 51 } 52 53 56 @Override 57 public byte[] open(byte[] data) throws IOException , ChannelOpenException { 58 59 try { 60 ByteArrayReader reader = new ByteArrayReader(data); 61 name = reader.readString(); 62 launchId = reader.readString(); 63 filename = reader.readString(); 64 65 LaunchSession launchSession = LaunchSessionFactory.getInstance().getLaunchSession(launchId); 66 if (launchSession == null) { 67 throw new ChannelOpenException(ChannelOpenException.CHANNEL_REFUSED, "No launch session with ID " + launchId + ", cannot read file " + filename + " for " + name); 68 } 69 70 launchSession.checkAccessRights(null, agent.getSession()); 71 72 ExtensionDescriptor descriptor = ExtensionStore.getInstance().getExtensionDescriptor(name); 73 if (!descriptor.containsFile(filename)) 74 throw new ChannelOpenException(ChannelOpenException.CHANNEL_REFUSED, "Application does not contain file " + filename + " in extension " + name); 75 76 this.file = descriptor.getFile(filename); 77 this.in = new FileInputStream (file); 78 79 } catch (Exception e) { 80 throw new ChannelOpenException(ChannelOpenException.CHANNEL_REFUSED, e.getMessage()); 81 } 82 83 return null; 84 85 } 86 87 90 public void onChannelData(byte[] buf, int off, int len) { 91 } 92 93 96 @Override 97 public byte[] create() throws IOException { 98 return null; 99 } 100 101 104 @Override 105 public void onChannelOpen(byte[] data) { 106 thread = new Thread (this); 107 thread.start(); 108 } 109 110 113 @Override 114 public void onChannelClose() { 115 117 } 118 119 122 public void run() { 123 try { 124 Util.copy(in, getOutputStream()); 125 } catch (Exception ex) { 126 log.error("Failed to copy file contents.", ex); 127 } finally { 128 close(); 129 } 130 } 131 132 } 133 | Popular Tags |