1 import javax.servlet.*;
2 import javax.servlet.http.*;
3
4 import java.io.*;
5 import java.util.*;
6 /***
7 Servlet to process jnlp files before serving to client - allows values to be filled in
8 based on url-request parameters
9 <p>
10 from <a href="http://lopica.sourceforge.net/faq.html">http://lopica.sourceforge.net/faq.html</a>
11 */
12 public class JnlpServlet extends HttpServlet
13 {
14 private HashMap jnlpMap = new HashMap();
15 private ArrayList matchList;
16 private boolean parseQueryString;
17
18 public void init( ServletConfig config ) throws ServletException
19 {
20 super.init( config );
21
22 this.initMatchList();
23
24 this.parseQueryString =
25 config.getInitParameter( "parse.querystring" ).equalsIgnoreCase( "true" ) ? true : false;
26
27 config.getServletContext().setAttribute( "DynamicJNLPServlet", this );
28 }
29
30 private void initMatchList()
31 {
32 this.matchList = new ArrayList();
33 String toMatch = getServletConfig().getInitParameter( "match.list" );
34
35 StringTokenizer tok = new StringTokenizer( toMatch, "|" );
36 while( tok.hasMoreTokens() )
37 this.matchList.add( tok.nextToken() );
38 }
39
40 private String getPropertyXML( String name, String value )
41 {
42 return "<property name=\"" + name + "\" value=\"" + value + "\"/>\n";
43 }
44
45 public void clearCache()
46 {
47 this.jnlpMap.clear();
48 }
49
50 private StringBuffer getJnlpFromDisk( HttpServletRequest req, String match ) throws IOException
51 {
52 String uriSansContext = req.getRequestURI().substring(
53 req.getRequestURI().indexOf( req.getContextPath() ) + req.getContextPath().length() );
54
55 String filepath = this.getServletContext().getRealPath( uriSansContext );
56
57
58
59
60 if( this.jnlpMap.containsKey( filepath ) )
61 return (StringBuffer) this.jnlpMap.get( filepath );
62
63 int ch;
64 StringBuffer sb = new StringBuffer();
65 BufferedReader br = new BufferedReader( new FileReader( filepath ) );
66
67 while( ( ch = br.read() ) > -1 )
68 sb.append( (char) ch );
69
70
71 br.close();
72
73 this.replaceCodebase( req, sb );
74 this.replaceDisplayName( match, sb );
75 this.replaceHref( req, sb );
76
77 this.jnlpMap.put( match, sb );
78
79 System.out.println( "[" + new Date() + "] Found JNLP Template" );
80 System.out.println( " URL: " + HttpUtils.getRequestURL( req ) );
81 System.out.println( " Match: " + match );
82 System.out.println( " File: " + filepath );
83
84 return sb;
85 }
86
87 private void replaceCodebase( HttpServletRequest req, StringBuffer sb )
88 {
89 int start = sb.toString().indexOf( "$$codebase" );
90 int length = "$$codebase".length();
91
92 String url = HttpUtils.getRequestURL( req ).toString();
93
94 String codebase = url.substring( 0, url.lastIndexOf( "/" ) );
95
96 sb.replace( start, start+length, codebase );
97 }
98
99 private void replaceDisplayName( String matchName, StringBuffer sb )
100 {
101 int start = sb.toString().indexOf( "$$title" );
102 int length = "$$title".length();
103
104 String name = this.getServletConfig().getInitParameter( matchName + ".title" );
105
106 sb.replace( start, start+length, name );
107 }
108
109 private void replaceProperties( HttpServletRequest req, StringBuffer sb )
110 {
111 if( sb.toString().indexOf( "$$properties" ) < 0 )
112 return;
113
114
115 StringBuffer propXmlBuffer = new StringBuffer();
116 Enumeration enum = req.getParameterNames();
117 while( enum.hasMoreElements() )
118 {
119 String name = (String) enum.nextElement();
120 propXmlBuffer.append( this.getPropertyXML( name, req.getParameter( name ) ) );
121 }
122
123 int start = sb.toString().indexOf( "$$properties" );
124 int length = "$$properties".length();
125
126 sb.replace( start, start+length, propXmlBuffer.toString() );
127 }
128
129 private void replaceHref( HttpServletRequest req, StringBuffer sb )
130 {
131 int start = sb.toString().indexOf( "$$href" );
132 int length = "$$href".length();
133
134 String url = HttpUtils.getRequestURL( req ).toString();
135
136 String href = url.substring( url.lastIndexOf( "/" ) + 1 );
137
138 sb.replace( start, start+length, href );
139 }
140
141 private String getJnlp( HttpServletRequest req ) throws IOException
142 {
143 String matchName = "", url = HttpUtils.getRequestURL( req ).toString();
144 boolean foundMatch = false;
145
146 for( int i = 0; i < this.matchList.size(); i++ )
147 {
148 matchName = (String) this.matchList.get( i );
149 if( url.indexOf( matchName ) > -1 )
150 {
151 foundMatch = true;
152 break;
153 }
154 }
155
156 if( !foundMatch )
157 return null;
158
159 StringBuffer jnlpBuffer = this.getJnlpFromDisk( req, matchName );
160
161 if( this.parseQueryString )
162 {
163 StringBuffer tmpBuffer = new StringBuffer( jnlpBuffer.toString() );
164 this.replaceProperties( req, tmpBuffer );
165 return tmpBuffer.toString();
166 }
167 else
168 return jnlpBuffer.toString();
169 }
170
171 protected void service( HttpServletRequest req, HttpServletResponse res )
172 throws ServletException, IOException
173 {
174 try
175 {
176 String jnlp = this.getJnlp( req );
177 if( jnlp == null )
178 res.sendError( HttpServletResponse.SC_NOT_FOUND );
179 else
180 {
181 res.setContentType( "application/x-java-jnlp" );
182 res.getWriter().print( jnlp );
183 res.flushBuffer();
184 }
185 }
186 catch( IOException e )
187 {
188 System.err.println( "DynamicJNLPServlet Error: " + e.getMessage() );
189 res.sendError( HttpServletResponse.SC_NOT_FOUND );
190 }
191 }
192 }