KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > extensions > actions > GetApplicationFileAction


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.extensions.actions;
21
22 import java.io.ByteArrayInputStream JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileInputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27
28 import javax.servlet.http.HttpServletRequest JavaDoc;
29 import javax.servlet.http.HttpServletResponse JavaDoc;
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 JavaDoc request, HttpServletResponse JavaDoc response)
57                     throws Exception JavaDoc {
58         if(response instanceof GZIPResponseWrapper) {
59             ((GZIPResponseWrapper)response).setCompress(false);
60         }
61
62         request.setAttribute(Constants.REQ_ATTR_COMPRESS, Boolean.FALSE);
63         String JavaDoc application = request.getParameter("name");
64         String JavaDoc file = request.getParameter("file").replace('\\', '/');
65
66         String JavaDoc 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 JavaDoc application, String JavaDoc ticket, HttpServletResponse JavaDoc response)
77
78     throws Exception JavaDoc {
79
80         byte[] cert = KeyStoreManager.getInstance(KeyStoreManager.DEFAULT_KEY_STORE)
81                         .getCertificate(Property.getProperty(new ContextKey("webServer.alias")))
82                         .getEncoded();
83
84         /**
85          * If the ticket is a pending VPN session ticket (Agent) then process
86          */

87         SessionInfo sessionInfo = LogonControllerFactory.getInstance().getAuthorizationTicket(ticket);
88
89         if (sessionInfo == null)
90             throw new CoreException(0, "");
91
92         sendFile(new ByteArrayInputStream JavaDoc(cert), cert.length, response);
93     }
94
95     protected void processApplicationFileRequest(String JavaDoc application, String JavaDoc file, String JavaDoc ticket, HttpServletRequest JavaDoc request,
96                                                     HttpServletResponse JavaDoc response)
97
98     throws Exception JavaDoc {
99
100         /*
101          * We may already have session info
102          */

103         SessionInfo sessionInfo = LogonControllerFactory.getInstance().getSessionInfo(request);
104         if (sessionInfo == null) {
105             /**
106              * If the ticket is a pending VPN session ticket (Agent) then
107              * process
108              */

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 JavaDoc file, HttpServletResponse JavaDoc response) throws IOException JavaDoc {
129
130         sendFile(new FileInputStream JavaDoc(file), file.length(), response);
131
132     }
133
134     private void sendFile(InputStream JavaDoc in, long length, HttpServletResponse JavaDoc response) throws IOException JavaDoc {
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 JavaDoc ex) {
144         } finally {
145             Util.closeStream(in);
146             Util.closeStream(response.getOutputStream());
147         }
148
149     }
150
151 }
Popular Tags