View Javadoc

1   /*
2    * $Id$
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  package org.apache.struts2;
22  
23  import com.opensymphony.xwork2.ActionContext;
24  import org.apache.struts2.views.util.UrlHelper;
25  
26  import javax.servlet.Servlet;
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.http.HttpServletResponse;
29  import javax.servlet.jsp.HttpJspPage;
30  import java.util.Map;
31  
32  /***
33   * Maintains a cache of jsp locations -> servlet instances for those jsps. When a jsp is requested
34   * from the cache, the cache will block if the jsp was not compiled already, and wait for the compilation
35   */
36  public abstract class JSPRuntime {
37      //maps from jsp path -> pagelet
38      protected static final ServletCache servletCache = new ServletCache();
39  
40      public static void clearCache() {
41          servletCache.clear();
42      }
43  
44      public static void handle(String location) throws Exception {
45          handle(location, false);
46      }
47  
48      public static void handle(String location, boolean flush) throws Exception {
49          final HttpServletResponse response = ServletActionContext.getResponse();
50          final HttpServletRequest request = ServletActionContext.getRequest();
51  
52          int i = location.indexOf("?");
53          if (i > 0) {
54              //extract params from the url and add them to the request
55              Map parameters = ActionContext.getContext().getParameters();
56              String query = location.substring(i + 1);
57              Map queryParams = UrlHelper.parseQueryString(query, true);
58              if (queryParams != null && !queryParams.isEmpty())
59                  parameters.putAll(queryParams);
60              location = location.substring(0, i);
61          }
62  
63          Servlet servlet = servletCache.get(location);
64          HttpJspPage page = (HttpJspPage) servlet;
65  
66          page._jspService(request, response);
67          if (flush)
68              response.flushBuffer();
69      }
70  }