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.taglib;
    18
    19  import javax.servlet.http.HttpServletRequest;
    20  import javax.servlet.jsp.PageContext;
    21
    22  import org.apache.portals.bridges.struts.PortletServlet;
    23  import org.apache.portals.bridges.struts.StrutsPortlet;
    24  import org.apache.portals.bridges.struts.StrutsPortletURL;
    25  import org.apache.portals.bridges.struts.config.StrutsPortletConfig;
    26  import org.apache.portals.bridges.struts.config.PortletURLTypes; // javadoc
    27
    28  /***
    29   * Utility class providing common Struts Bridge Tags functionality.
    30   *
    31   * @author <a href="mailto:ate@douma.nu">Ate Douma</a>
    32   * @version $Id: TagsSupport.java 517068 2007-03-12 01:44:37Z ate $
    33   */
    34  class TagsSupport
    35  {
    36      /***
    37       * Private constructor as this class isn't supposed to be instantiated.
    38       *
    39       */
    40      private TagsSupport(){}
    41
    42      /***
    43       * Resolves a, possibly relative, url to a context relative one for use within a Portlet context.
    44       * <p>
    45       * The url parameter may contain relative (../) elements.
    46       * </p>
    47       * @param pageContext the JSP pageContext
    48       * @param url the url to resolve
    49       * @return the resolved url
    50       */
    51      public static String getContextRelativeURL(PageContext pageContext, String url, boolean addContextPath)
    52      {
    53          if ( !url.startsWith("/"))
    54          {
    55              String newUrl = url;
    56              String currentPath = (String)pageContext.getRequest().getAttribute(StrutsPortlet.PAGE_URL);
    57              if ( addContextPath )
    58              {
    59                  currentPath = ((HttpServletRequest)pageContext.getRequest()).getContextPath() + currentPath;
    60              }
    61              if ( addContextPath || currentPath.length() > 1 /* keep "/" */)
    62              {
    63                  currentPath = currentPath.substring(0,currentPath.lastIndexOf('/'));
    64              }
    65              if ( currentPath.length() == 0 )
    66              {
    67                  currentPath = "/";
    68              }
    69              while ( currentPath.length() > 0 )
    70              {
    71                  if ( newUrl.startsWith("../"))
    72                  {
    73                      currentPath = currentPath.substring(0, currentPath.lastIndexOf('/'));
    74                      newUrl = newUrl.substring(3);
    75                  }
    76                  else
    77                  {
    78                      break;
    79                  }
    80              }
    81              if ( currentPath.length() > 1 )
    82              {
    83                  url = currentPath + "/" + newUrl;
    84              }
    85              else
    86              {
    87                  url = "/" + newUrl;
    88              }
    89          }
    90          return url;
    91      }
    92
    93      /***
    94       * Creates an action or render PortletURL, or a ResourceURL.
    95       * <p>
    96       * The url parameter is first {@link #getContextRelativeURL(PageContext, String) resolved}
    97       * to an context relative url.<br/>
    98       * Then, a prefixed contextPath is removed from the resulting url.<br/>
    99       * If the type parameter is specified (not null), the type of url created is based on its value.<br/>
    100      * Otherwise, {@link PortletURLTypes#getType(String)} is used to determine which
    101      * type of url must be created.
    102      * </p>
    103      * @param pageContext the JSP pageContext
    104      * @param url the url to resolve
    105      * @param type indicated which type of url must be created
    106      * @return an action or render PortletURL, or a ResourceURL
    107      */
    108     public static String getURL(PageContext pageContext, String url, PortletURLTypes.URLType type)
    109     {
    110         url = getContextRelativeURL(pageContext,url,false);
    111         String contextPath = ((HttpServletRequest)pageContext.getRequest()).getContextPath();
    112         if (url.startsWith(contextPath + "/"))
    113         {
    114             url = url.substring(contextPath.length());
    115         }
    116
    117         if ( type == null )
    118         {
    119             StrutsPortletConfig strutsPortletConfig = (StrutsPortletConfig)pageContext.getAttribute(StrutsPortlet.STRUTS_PORTLET_CONFIG,PageContext.APPLICATION_SCOPE);
    120             type = strutsPortletConfig.getPortletURLTypes().getType(url);
    121         }
    122
    123         if ( type.equals(PortletURLTypes.URLType.ACTION) )
    124         {
    125             return StrutsPortletURL.createActionURL(pageContext.getRequest(),url).toString();
    126         }
    127         else if ( type.equals(PortletURLTypes.URLType.RENDER) )
    128         {
    129             return StrutsPortletURL.createRenderURL(pageContext.getRequest(),url).toString();
    130         }
    131         else // type.equals(PortletURLTypes.URLType.RESOURCE)
    132         {
    133             if ( url.startsWith("/"))
    134             {
    135                 return contextPath + url;
    136             }
    137             return contextPath + "/" + url;
    138         }
    139     }
    140
    141     /***
    142      * Replaces the action url as generated by the struts:form tag with an action PortletURL.
    143      * @param pageContext the JSP pageContext
    144      * @param formStartElement the formStartElement as generated by the struts:form tag
    145      * @return the formStartElement containing an action PortletURL
    146      */
    147     public static String getFormTagRenderFormStartElement(PageContext pageContext, String formStartElement)
    148     {
    149         if ( PortletServlet.isPortletRequest(pageContext.getRequest()))
    150         {
    151             int actionURLStart = formStartElement.indexOf("action=") + 8;
    152             int actionURLEnd = formStartElement.indexOf('"', actionURLStart);
    153             String actionURL = formStartElement.substring(actionURLStart,
    154                     actionURLEnd);
    155             formStartElement = formStartElement.substring(0, actionURLStart)
    156                     + StrutsPortletURL.createActionURL(pageContext.getRequest(),
    157                             actionURL).toString()
    158                     + formStartElement.substring(actionURLEnd);
    159         }
    160         return formStartElement;
    161     }
    162 }