View Javadoc

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