View Javadoc

1   /**
2    * Copyright (C) 2005-2009 Alfresco Software Limited.
3    *
4    * This file is part of the Spring Surf Extension project.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.springframework.extensions.surf.util;
20  
21  import java.util.Enumeration;
22  
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpServletResponse;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.springframework.extensions.surf.util.FakeHttpServletResponse;
29  import org.springframework.extensions.surf.util.WrappedHttpServletRequest;
30  
31  /**
32   * Convenience functions for managing overlay state
33   * 
34   * @author muzquiano
35   */
36  public class OverlayUtil
37  {
38      private static Log logger = LogFactory.getLog(OverlayUtil.class);
39  
40      /**
41       * Performs a wrapped include of a resource and writes results to a buffer
42       * 
43       * @param request
44       * @param realResponse
45       * @param buffer
46       * @param path
47       */
48      public static void include(HttpServletRequest request, HttpServletResponse realResponse, StringBuilder buffer,
49              String path)
50      {
51          WrappedHttpServletRequest wrappedRequest = new WrappedHttpServletRequest(
52                  request);
53          FakeHttpServletResponse fakeResponse = new FakeHttpServletResponse(realResponse, 
54                  false);
55  
56          try
57          {
58              // do the include
59              request.getRequestDispatcher(path).include(wrappedRequest,
60                      fakeResponse);
61  
62              // write to buffer
63              buffer.append(fakeResponse.getContentAsString());
64  
65              // append a line feed / carriage return
66              buffer.append("\r\n");
67          }
68          catch (Exception ex)
69          {
70              logger.warn("Unable to include '" + path + "', " + ex.getMessage());
71          }
72      }
73  
74      /**
75       * Gets a cached resource from the user session
76       * 
77       * @param request
78       * @param key
79       * @return
80       */
81      public static StringBuilder getCachedResource(HttpServletRequest request,
82              String key)
83      {
84          return (StringBuilder) request.getSession().getAttribute(
85                  "CACHED_RESOURCE_" + key);
86      }
87  
88      /**
89       * Caches a resource into the user session
90       * 
91       * @param request
92       * @param key
93       * @param buffer
94       */
95      public static void setCachedResource(HttpServletRequest request,
96              String key, StringBuilder buffer)
97      {
98          request.getSession().setAttribute("CACHED_RESOURCE_" + key, buffer);
99      }
100 
101     /**
102      * Removes a cached resource from the user session
103      * 
104      * @param request
105      *            the request
106      * @param key
107      *            the key
108      */
109     public static void removeCachedResource(HttpServletRequest request,
110             String key)
111     {
112         request.getSession().removeAttribute("CACHED_RESOURCE_" + key);
113     }
114 
115     /**
116      * Removes cached resources that whose start with the given string
117      * 
118      * @param request
119      * @param car
120      */
121     public static void removeCachedResources(HttpServletRequest request,
122             String key)
123     {
124         Enumeration en = request.getSession().getAttributeNames();
125         while (en.hasMoreElements())
126         {
127             String attributeName = (String) en.nextElement();
128             if (attributeName.startsWith("CACHED_RESOURCE_" + key))
129             {
130                 request.getSession().removeAttribute(attributeName);
131             }
132         }
133     }
134 
135     /**
136      * Returns the host port for the web studio application
137      * 
138      * @return
139      */
140     public static String getWebStudioHostPort(HttpServletRequest request)
141     {
142         String url = request.getScheme();
143         url += "://";
144         url += request.getServerName();
145         if (request.getServerPort() != 80)
146         {
147             url += ":" + request.getServerPort();
148         }
149         url += request.getContextPath();
150 
151         return url;
152     }
153 
154     /**
155      * Constructs a browser-friendly path to the web studio relative path
156      * 
157      * @param request
158      * @param relativePath
159      * @return
160      */
161     public static String getWebStudioURL(HttpServletRequest request,
162             String relativePath)
163     {
164         return getWebStudioHostPort(request) + relativePath;
165     }
166 
167     /**
168      * Returns the context path of the original web application
169      * 
170      * @param request
171      * @return
172      */
173     public static String getOriginalContextPath(HttpServletRequest request)
174     {
175         String contextPath = request.getParameter("contextPath");
176         if (contextPath == null)
177         {
178             contextPath = "/";
179         }
180 
181         return contextPath;
182     }
183 
184     /**
185      * Constructs a browser-friendly path to the original webapp relative path
186      * 
187      * @param request
188      * @param relativePath
189      * @return
190      */
191     public static String getOriginalURL(HttpServletRequest request,
192             String relativePath)
193     {
194         return getOriginalContextPath(request) + relativePath;
195     }
196 
197 }