KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > core > MessageResourceLoaderServlet


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.core;
21
22 import java.io.ByteArrayInputStream JavaDoc;
23 import java.io.ByteArrayOutputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26
27 import javax.servlet.ServletException JavaDoc;
28 import javax.servlet.http.HttpServlet JavaDoc;
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30 import javax.servlet.http.HttpServletResponse JavaDoc;
31
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34 import org.apache.struts.action.ActionForm;
35 import org.apache.struts.action.ActionMapping;
36
37 import com.sslexplorer.boot.ContextHolder;
38 import com.sslexplorer.boot.Util;
39 import com.sslexplorer.security.SessionInfo;
40
41 /**
42  * A network class loader servlet that loads classes and returns them
43  * to the client via HTTP.
44  *
45  * Note, this currently on allows ApplicationResources*.properties
46  * resources to be loaded (for use by the agent suite).
47  *
48  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
49  */

50 public class MessageResourceLoaderServlet extends HttpServlet JavaDoc {
51     final static Log log = LogFactory.getLog(MessageResourceLoaderServlet.class);
52
53     public void service(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc {
54         String JavaDoc name = request.getPathInfo().substring(1);
55         if (name.equals("")) {
56             log.error("No name supplied to the class loader servlet.");
57             response.sendError(500);
58             return;
59         }
60         int idx = name.lastIndexOf('/');
61         String JavaDoc basename = name;
62         if(idx != -1) {
63             basename = name.substring(idx + 1);
64         }
65         if(!basename.startsWith("ApplicationResources") || ( !basename.endsWith(".properties") && !basename.endsWith(".class"))) {
66             log.debug("Attempt to load something other that a resource bundle via the class loader servlet.");
67             response.sendError(500);
68             return;
69         }
70         
71         /* This is a hack to get around the problem where we never get
72          * get a request for the default language resources when they are
73          * in a properties file. This is because we use a class loader on
74          * the client end to retrieve the resources.
75          */

76         
77         if(basename.endsWith(".class")) {
78             basename = basename.substring(0, basename.length() - 6) + ".properties";
79             name = name.substring(0, name.length() - 6) + ".properties";
80         }
81         
82         /*
83          * Load into byte array so we get the content length before sending on
84          * to the client
85          */

86         ByteArrayOutputStream JavaDoc bout = new ByteArrayOutputStream JavaDoc();
87         InputStream JavaDoc in = ContextHolder.getContext().getContextLoader().getResourceAsStream(name);
88         if (in == null) {
89             response.setContentType("text/plain");
90             response.sendError(404, "Class not found");
91         } else {
92             try {
93                 Util.copy(in, bout);
94             }
95             finally {
96                 bout.close();
97                 in.close();
98             }
99             response.setContentType("text/plain");
100             response.setContentLength(bout.size());
101             response.setStatus(200);
102             ByteArrayInputStream JavaDoc bin = new ByteArrayInputStream JavaDoc(bout.toByteArray());
103             sendFile(bin, bout.size(), response);
104         }
105     }
106
107     private void sendFile(InputStream JavaDoc in, long length, HttpServletResponse JavaDoc response) throws IOException JavaDoc {
108         response.setHeader("Content-Type", "text/plain");
109         response.setHeader("Content-Length", String.valueOf(length));
110         response.setContentLength((int) length);
111         Util.noCache(response);
112         try {
113             Util.copy(in, response.getOutputStream());
114             response.getOutputStream().flush();
115         } catch (IOException JavaDoc ex) {
116         } finally {
117             Util.closeStream(in);
118             Util.closeStream(response.getOutputStream());
119         }
120
121     }
122
123     public int getNavigationContext(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
124         return SessionInfo.ALL_CONTEXTS;
125     }
126
127 }
Popular Tags