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.RenderRequest;
20  import javax.portlet.RenderResponse;
21  import javax.servlet.ServletRequest;
22  import javax.servlet.http.HttpServletRequest;
23  
24  /***
25   * StrutsPortletURL
26   * 
27   * @author <a href="mailto:ate@douma.nu">Ate Douma</a>
28   * @version $Id: StrutsPortletURL.java 510753 2007-02-23 01:28:50Z ate $
29   */
30  public class StrutsPortletURL
31  {
32      public static final String PAGE = "_spage";
33      public static final String ORIGIN = "_sorig";
34      public static final String KEEP_RENDER_ATTRIBUTES = "_kra";
35      
36      public static String getPageURL(ServletRequest request)
37      {
38          return (String)request.getAttribute(StrutsPortlet.PAGE_URL);
39      }
40      public static String getOriginURL(ServletRequest request)
41      {
42          return (String)request.getAttribute(StrutsPortlet.ORIGIN_URL);
43      }
44      private static PortletURL createPortletURL(ServletRequest request,
45              String pageURL, boolean actionURL)
46      {
47          RenderResponse renderResponse = (RenderResponse) request
48                  .getAttribute("javax.portlet.response");
49          PortletURL portletURL;
50          if (actionURL)
51              portletURL = renderResponse.createActionURL();
52          else
53              portletURL = renderResponse.createRenderURL();
54          if (request instanceof HttpServletRequest)
55          {
56              String contextPath = ((HttpServletRequest) request)
57                      .getContextPath();
58              if (pageURL.startsWith(contextPath))
59                  pageURL = pageURL.substring(contextPath.length());
60          }
61          if (actionURL)
62          {
63              portletURL.setParameter(PAGE, pageURL.replaceAll("&amp;","&"));
64              String originURL = request.getParameter(PAGE);
65              if (originURL != null)
66                  portletURL.setParameter(ORIGIN, originURL);
67          }
68          else
69          {
70              RenderRequest renderRequest = (RenderRequest)request.getAttribute("javax.portlet.request");
71              portletURL.setParameter(PAGE+renderRequest.getPortletMode().toString(), pageURL.replaceAll("&amp;","&"));
72          }
73          return portletURL;
74      }
75      public static PortletURL createRenderURL(ServletRequest request,
76              String pageURL)
77      {
78          return createPortletURL(request, pageURL, false);
79      }
80      public static PortletURL createActionURL(ServletRequest request,
81              String pageURL)
82      {
83          return createPortletURL(request, pageURL, true);
84      }
85  }