1 19 20 package com.sslexplorer.extensions.actions; 21 22 import java.io.ByteArrayInputStream ; 23 import java.io.File ; 24 import java.io.FileInputStream ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 28 import javax.servlet.http.HttpServletRequest ; 29 import javax.servlet.http.HttpServletResponse ; 30 31 import org.apache.commons.logging.Log; 32 import org.apache.commons.logging.LogFactory; 33 import org.apache.struts.action.ActionForm; 34 import org.apache.struts.action.ActionForward; 35 import org.apache.struts.action.ActionMapping; 36 37 import com.sslexplorer.boot.ContextKey; 38 import com.sslexplorer.boot.KeyStoreManager; 39 import com.sslexplorer.boot.Util; 40 import com.sslexplorer.core.CoreException; 41 import com.sslexplorer.core.actions.XMLOutputAction; 42 import com.sslexplorer.core.filters.GZIPResponseWrapper; 43 import com.sslexplorer.extensions.ExtensionDescriptor; 44 import com.sslexplorer.extensions.store.ExtensionStore; 45 import com.sslexplorer.properties.Property; 46 import com.sslexplorer.security.Constants; 47 import com.sslexplorer.security.LogonControllerFactory; 48 import com.sslexplorer.security.SessionInfo; 49 50 public class GetApplicationFileAction extends XMLOutputAction { 51 final static Log log = LogFactory.getLog(GetApplicationFileAction.class); 52 53 public GetApplicationFileAction() { 54 } 55 56 public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 57 throws Exception { 58 if(response instanceof GZIPResponseWrapper) { 59 ((GZIPResponseWrapper)response).setCompress(false); 60 } 61 62 request.setAttribute(Constants.REQ_ATTR_COMPRESS, Boolean.FALSE); 63 String application = request.getParameter("name"); 64 String file = request.getParameter("file").replace('\\', '/'); 65 66 String ticket = request.getParameter("ticket"); 67 68 if (file.equalsIgnoreCase("sslexplorer.cert")) { 69 processApplicationCertRequest(application, ticket, response); 70 } else { 71 processApplicationFileRequest(application, file, ticket, request, response); 72 } 73 return null; 74 } 75 76 protected void processApplicationCertRequest(String application, String ticket, HttpServletResponse response) 77 78 throws Exception { 79 80 byte[] cert = KeyStoreManager.getInstance(KeyStoreManager.DEFAULT_KEY_STORE) 81 .getCertificate(Property.getProperty(new ContextKey("webServer.alias"))) 82 .getEncoded(); 83 84 87 SessionInfo sessionInfo = LogonControllerFactory.getInstance().getAuthorizationTicket(ticket); 88 89 if (sessionInfo == null) 90 throw new CoreException(0, ""); 91 92 sendFile(new ByteArrayInputStream (cert), cert.length, response); 93 } 94 95 protected void processApplicationFileRequest(String application, String file, String ticket, HttpServletRequest request, 96 HttpServletResponse response) 97 98 throws Exception { 99 100 103 SessionInfo sessionInfo = LogonControllerFactory.getInstance().getSessionInfo(request); 104 if (sessionInfo == null) { 105 109 sessionInfo = LogonControllerFactory.getInstance().getAuthorizationTicket(ticket); 110 } 111 112 if (sessionInfo == null) 113 throw new CoreException(0, ""); 114 115 ExtensionDescriptor app = ExtensionStore.getInstance().getExtensionDescriptor(application); 116 117 if (app == null) 118 app = ExtensionStore.getInstance().getAgentApplication(); 119 120 if (!app.containsFile(file)) { 121 log.error("SSL-Explorer Agent requested a file that does not exist (" + file + ")."); 122 sendError(file + " not found", response); 123 } else { 124 sendFile(app.getFile(file), response); 125 } 126 } 127 128 private void sendFile(File file, HttpServletResponse response) throws IOException { 129 130 sendFile(new FileInputStream (file), file.length(), response); 131 132 } 133 134 private void sendFile(InputStream in, long length, HttpServletResponse response) throws IOException { 135 Util.noCache(response); 136 137 response.setHeader("Content-type", "application/octet-stream"); 138 response.setContentLength((int) length); 139 try { 140 141 Util.copy(in, response.getOutputStream()); 142 143 } catch (IOException ex) { 144 } finally { 145 Util.closeStream(in); 146 Util.closeStream(response.getOutputStream()); 147 } 148 149 } 150 151 } | Popular Tags |