View Javadoc

1   /*
2    * Copyright 2000-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.portals.bridges.struts;
17  
18  import javax.portlet.PortletURL;
19  import javax.portlet.RenderResponse;
20  import javax.servlet.ServletRequest;
21  import javax.servlet.http.HttpServletRequest;
22  
23  /***
24   * StrutsPortletURL
25   * 
26   * @author <a href="mailto:ate@douma.nu">Ate Douma</a>
27   * @version $Id: StrutsPortletURL.java 306954 2005-10-07 01:14:47 +0200 (Fri, 07 Oct 2005) ate $
28   */
29  public class StrutsPortletURL
30  {
31      public static final String PAGE = "_spage";
32      public static final String ORIGIN = "_sorig";
33      public static final String KEEP_RENDER_ATTRIBUTES = "_kra";
34      
35      public static String getPageURL(ServletRequest request)
36      {
37          return request.getParameter(PAGE);
38      }
39      public static String getOriginURL(ServletRequest request)
40      {
41          return (String)request.getAttribute(StrutsPortlet.ORIGIN_URL);
42      }
43      private static PortletURL createPortletURL(ServletRequest request,
44              String pageURL, boolean actionURL)
45      {
46          RenderResponse renderResponse = (RenderResponse) request
47                  .getAttribute("javax.portlet.response");
48          PortletURL portletURL;
49          if (actionURL)
50              portletURL = renderResponse.createActionURL();
51          else
52              portletURL = renderResponse.createRenderURL();
53          if (request instanceof HttpServletRequest)
54          {
55              String contextPath = ((HttpServletRequest) request)
56                      .getContextPath();
57              if (pageURL.startsWith(contextPath))
58                  pageURL = pageURL.substring(contextPath.length());
59          }
60          portletURL.setParameter(PAGE, pageURL.replaceAll("&amp;","&"));
61          if (actionURL)
62          {
63              String originURL = request.getParameter(PAGE);
64              if (originURL != null)
65                  portletURL.setParameter(ORIGIN, originURL);
66          }
67          return portletURL;
68      }
69      public static PortletURL createRenderURL(ServletRequest request,
70              String pageURL)
71      {
72          return createPortletURL(request, pageURL, false);
73      }
74      public static PortletURL createActionURL(ServletRequest request,
75              String pageURL)
76      {
77          return createPortletURL(request, pageURL, true);
78      }
79  }