1 23 24 package com.sun.enterprise.appclient.jws; 25 26 import com.sun.enterprise.appclient.AppClientInfo; 27 import com.sun.enterprise.appclient.MainWithModuleSupport; 28 import com.sun.logging.LogDomains; 29 import java.io.PrintWriter ; 30 import java.io.UnsupportedEncodingException ; 31 import java.net.URLDecoder ; 32 import java.util.Date ; 33 import java.util.Properties ; 34 import java.util.logging.Level ; 35 import java.util.logging.Logger ; 36 import javax.servlet.ServletRequest ; 37 import javax.servlet.ServletResponse ; 38 import javax.servlet.ServletException ; 39 import javax.servlet.http.HttpServletRequest ; 40 import javax.servlet.http.HttpServletRequestWrapper ; 41 import javax.servlet.http.HttpServletResponse ; 42 43 44 45 67 public class JWSSystemServlet extends org.apache.catalina.servlets.DefaultServlet { 68 69 private static final String ARG_QUERY_PARAM_NAME = "arg"; 70 private static final String PROP_QUERY_PARAM_NAME = "prop"; 71 private static final String VMARG_QUERY_PARAM_NAME = "vmarg"; 72 73 private static final String LAST_MODIFIED_HEADER_NAME = "Last-Modified"; 74 private static final String DATE_HEADER_NAME = "Date"; 75 76 private static final String GET_METHOD_NAME = "GET"; 77 78 private final String lineSep = System.getProperty("line.separator"); 79 80 private AppclientJWSSupportInfo jwsInfo = null; 81 82 private Logger _logger=LogDomains.getLogger(NamingConventions.JWS_LOGGER); 83 84 85 86 public JWSSystemServlet() { 87 } 88 89 92 public void init() throws ServletException { 93 super.init(); 94 try { 95 98 jwsInfo = AppclientJWSSupportInfo.getInstance(); 99 } catch (Throwable thr) { 100 throw new ServletException (thr); 101 } 102 } 103 104 111 public void service(ServletRequest request, ServletResponse response) throws ServletException { 112 if ( ! (request instanceof HttpServletRequest ) || ! (response instanceof HttpServletResponse )) { 113 throw new ServletException ("Expected HttpServletRequest and HttpServletResponse but received " + request.getClass().getName() + " and " + response.getClass().getName()); 114 } 115 116 boolean isFine = _logger.isLoggable(Level.FINE); 117 118 HttpServletRequest req = (HttpServletRequest ) request; 119 HttpServletResponse resp = (HttpServletResponse ) response; 120 121 125 Content content = jwsInfo.getContent(req); 126 if (content == null) { 127 try { 128 if (isFine) { 129 _logger.fine("JWSSystemServlet: content not found for request: method=" + req.getMethod() + ", pathInfo=" + req.getPathInfo()); 130 } 131 132 resp.sendError(resp.SC_NOT_FOUND, req.getPathInfo()); 133 } catch (Throwable thr) { 134 throw new ServletException ("Error attempting to return not-found response", thr); 135 } 136 } else { 137 141 if (content instanceof StaticContent) { 142 processStaticContent(req, resp, content); 143 } else { 144 processDynamicContent(req, resp, content); 145 } 146 } 147 148 } 149 150 159 private void processStaticContent(HttpServletRequest req, HttpServletResponse resp, Content content) throws ServletException { 160 try { 161 StaticContent sc = (StaticContent) content; 162 163 169 HttpServletRequest wrappedRequest = new JWSSystemServletRequestWrapper(req, sc.getRelativeURI()); 170 if (_logger.isLoggable(Level.FINE)) { 171 _logger.fine("JWSSystemServlet: serving static content: method=" + wrappedRequest.getMethod() + ", mapped path=" + wrappedRequest.getPathInfo()); 172 } 173 174 177 super.service(wrappedRequest, resp); 178 } catch (Throwable thr) { 179 throw new ServletException ("Error processing static content for path " + content.getPath(), thr); 180 } 181 } 182 183 190 private void processDynamicContent(HttpServletRequest req, HttpServletResponse resp, Content content) throws ServletException { 191 try { 192 DynamicContent dc = (DynamicContent) content; 193 194 203 Properties requestPlaceholders = prepareRequestPlaceholders(req, dc); 204 205 boolean isGetMethod = req.getMethod().equals(GET_METHOD_NAME); 206 207 DynamicContent.Instance instance = dc.findInstance(requestPlaceholders, isGetMethod ); 208 209 214 if (instance != null) { 215 String responseText = instance.getText(); 216 Date instanceTimestamp = instance.getTimestamp(); 217 resp.setDateHeader(LAST_MODIFIED_HEADER_NAME, instanceTimestamp.getTime()); 218 resp.setDateHeader(DATE_HEADER_NAME, System.currentTimeMillis()); 219 resp.setContentType(dc.getMimeType()); 220 resp.setStatus(resp.SC_OK); 221 224 if (isGetMethod) { 225 PrintWriter pw = resp.getWriter(); 226 pw.println(responseText); 227 pw.flush(); 228 } 229 if (_logger.isLoggable(Level.FINE)) { 230 _logger.fine("JWSSystemServlet: serving dynamic content: method=" + req.getMethod() + 231 ", pathInfo=" + req.getPathInfo() + 232 ", queryString=" + req.getQueryString() + 233 ", isGetMethod=" + isGetMethod + 234 ", content timestamp=" + instanceTimestamp + 235 ", content=" + responseText 236 ); 237 } 238 } else { 239 247 resp.setDateHeader(LAST_MODIFIED_HEADER_NAME, System.currentTimeMillis()); 248 resp.setDateHeader(DATE_HEADER_NAME, System.currentTimeMillis()); 249 resp.setContentType(dc.getMimeType()); 250 resp.setStatus(resp.SC_OK); 251 } 252 } catch (Throwable thr) { 253 throw new ServletException ("Error processing dynamic content for path " + content.getPath(), thr); 254 } 255 } 256 257 264 private Properties prepareRequestPlaceholders( 265 HttpServletRequest request, 266 DynamicContent content) throws ServletException { 267 Properties answer = new Properties (); 268 269 answer.setProperty("request.scheme", request.getScheme()); 270 answer.setProperty("request.host", request.getLocalName()); 271 answer.setProperty("request.port", Integer.toString(request.getLocalPort())); 272 273 277 278 String queryString = request.getQueryString(); 279 StringBuilder queryStringPropValue = new StringBuilder (); 280 if (queryString != null && queryString.length() > 0) { 281 queryStringPropValue.append("?").append(queryString); 282 } 283 answer.setProperty("request.web.app.context.root", NamingConventions.SYSTEM_WEBAPP_URL); 284 answer.setProperty("request.path", request.getPathInfo()); 285 answer.setProperty("request.query.string", queryStringPropValue.toString()); 286 287 answer.setProperty("security.setting", content.getJNLPSecuritySetting()); 288 289 294 ContentOrigin origin = content.getOrigin(); 295 answer.setProperty("appclient.user.code.is.signed", Boolean.toString(content.requiresElevatedPermissions())); 296 297 processQueryParameters(queryString, answer); 298 299 return answer; 300 } 301 302 private void processQueryParameters(String queryString, Properties answer) throws ServletException { 303 if (queryString == null) { 304 queryString = ""; 305 } 306 String [] queryParams = null; 307 try { 308 queryParams = URLDecoder.decode(queryString, "UTF-8").split("&"); 309 } catch (UnsupportedEncodingException uee) { 310 throw new ServletException ("Error decoding query string", uee); 311 } 312 313 QueryParams arguments = new ArgQueryParams(); 314 QueryParams properties = new PropQueryParams(); 315 QueryParams vmArguments = new VMArgQueryParams(); 316 QueryParams [] paramTypes = new QueryParams[] {arguments, properties, vmArguments}; 317 318 for (String param : queryParams) { 319 for (QueryParams qpType : paramTypes) { 320 if (qpType.processParameter(param)) { 321 break; 322 } 323 } 324 } 325 326 answer.setProperty("request.arguments", arguments.toString()); 327 answer.setProperty("request.properties", properties.toString()); 328 answer.setProperty("request.vmargs", vmArguments.toString()); 329 } 330 331 private abstract class QueryParams { 332 private String prefix; 333 334 protected QueryParams(String prefix) { 335 this.prefix = prefix; 336 } 337 338 private boolean handles(String prefix) { 339 return prefix.equals(this.prefix); 340 } 341 342 protected abstract void processValue(String value); 343 344 public abstract String toString(); 345 346 public boolean processParameter(String param) { 347 boolean result = false; 348 int equalsSign = param.indexOf("="); 349 String value = ""; 350 String prefix; 351 if (equalsSign != -1) { 352 prefix = param.substring(0, equalsSign); 353 } else { 354 prefix = param; 355 } 356 if (handles(prefix)) { 357 result = true; 358 if ((equalsSign + 1) < param.length()) { 359 value = param.substring(equalsSign + 1); 360 } 361 processValue(value); 362 } 363 return result; 364 } 365 } 366 367 private class ArgQueryParams extends QueryParams { 368 private StringBuilder arguments = new StringBuilder (); 369 370 public ArgQueryParams() { 371 super(ARG_QUERY_PARAM_NAME); 372 } 373 374 public void processValue(String value) { 375 if (value.length() == 0) { 376 value = "#missing#"; 377 } 378 arguments.append("<argument>").append(value).append("</argument>").append(lineSep); 379 } 380 381 public String toString() { 382 return arguments.toString(); 383 } 384 } 385 386 private class PropQueryParams extends QueryParams { 387 private StringBuilder properties = new StringBuilder (); 388 389 public PropQueryParams() { 390 super(PROP_QUERY_PARAM_NAME); 391 } 392 393 public void processValue(String value) { 394 if (value.length() > 0) { 395 int equalsSign = value.indexOf('='); 396 String propValue = ""; 397 String propName; 398 if (equalsSign > 0) { 399 propName = value.substring(0, equalsSign); 400 if ((equalsSign + 1) < value.length()) { 401 propValue = value.substring(equalsSign + 1); 402 } 403 properties.append("<property name=\"" + propName + "\" value=\"" + propValue + "\"/>").append(lineSep); 404 } 405 } 406 } 407 408 public String toString() { 409 return properties.toString(); 410 } 411 412 } 413 414 private class VMArgQueryParams extends QueryParams { 415 private StringBuilder vmArgs = new StringBuilder (); 416 417 public VMArgQueryParams() { 418 super(VMARG_QUERY_PARAM_NAME); 419 } 420 421 public void processValue(String value) { 422 vmArgs.append(value).append(" "); 423 } 424 425 public String toString() { 426 return vmArgs.length() > 0 ? " java-vm=args=\"" + vmArgs.toString() + "\"" : ""; 427 } 428 } 429 430 431 } 432 | Popular Tags |