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.ServletRequest; // javadoc
    20  import javax.servlet.jsp.JspException;
    21
    22  import org.apache.portals.bridges.struts.PortletServlet; // javadoc
    23  import org.apache.portals.bridges.struts.config.PortletURLTypes;
    24
    25  /***
    26   * Supports the Struts html:link tag to be used within a Portlet context.
    27   *
    28   * @author <a href="mailto:ate@douma.nu">Ate Douma</a>
    29   * @version $Id: LinkTag.java 517068 2007-03-12 01:44:37Z ate $
    30   */
    31  public class LinkTag extends org.apache.struts.taglib.html.LinkTag
    32  {
    33      /***
    34       * Indicates which type of a url must be generated: action, render or resource.
    35       * <p>If not specified, the type will be determined by
    36       * {@link PortletURLTypes#getType(String)}</p>.
    37       */
    38      protected PortletURLTypes.URLType urlType = null;
    39
    40      /***
    41       * @return "true" if an ActionURL must be rendered
    42       */
    43      public String getActionURL()
    44      {
    45          return urlType != null && urlType.equals(PortletURLTypes.URLType.ACTION) ? "true" : "false";
    46      }
    47
    48      /***
    49       * Render an ActionURL when set to "true"
    50       * @param value "true" renders an ActionURL
    51       */
    52      public void setActionURL(String value)
    53      {
    54          this.urlType = value != null && value.equalsIgnoreCase("true") ? PortletURLTypes.URLType.ACTION : null;
    55      }
    56
    57      public String getRenderURL()
    58      {
    59          return urlType != null && urlType.equals(PortletURLTypes.URLType.RENDER) ? "true" : "false";
    60      }
    61
    62      /***
    63       * Render a RenderURL when set to "true"
    64       * @param value "true" renders a RenderURL
    65       */
    66      public void setRenderURL(String value)
    67      {
    68          this.urlType = value != null && value.equalsIgnoreCase("true") ? PortletURLTypes.URLType.RENDER : null;
    69      }
    70
    71      public String getResourceURL()
    72      {
    73          return urlType != null && urlType.equals(PortletURLTypes.URLType.RESOURCE) ? "true" : "false";
    74      }
    75
    76      /***
    77       * Render a ResourceURL when set to "true"
    78       * @param value "true" renders a ResourceURL
    79       */
    80      public void setResourceURL(String value)
    81      {
    82          this.urlType = value != null && value.equalsIgnoreCase("true") ? PortletURLTypes.URLType.RESOURCE : null;
    83      }
    84
    85      /***
    86       * Generates a PortletURL or a ResourceURL for the link when in the context of a
    87       * {@link PortletServlet#isPortletRequest(ServletRequest) PortletRequest}, otherwise
    88       * the default behaviour is maintained.
    89       * @return the link url
    90       * @exception JspException if a JSP exception has occurred
    91       */
    92      protected String calculateURL() throws JspException
    93      {
    94          if ( PortletServlet.isPortletRequest(pageContext.getRequest() ))
    95          {
    96              String url = super.calculateURL();
    97
    98              // process embedded anchor
    99              String anchor = null;
    100             int hash = url.indexOf('#');
    101             if ( hash > -1 )
    102             {
    103                 // save embedded anchor to be appended later and strip it from the url
    104                 anchor = url.substring(hash);
    105                 url = url.substring(0,hash);
    106             }
    107
    108             url = TagsSupport.getURL(pageContext, url, urlType);
    109
    110             if ( anchor != null )
    111             {
    112                 url = url + anchor;
    113             }
    114             return url;
    115         }
    116         else
    117         {
    118             return super.calculateURL();
    119         }
    120     }
    121
    122     public void release() {
    123
    124         super.release();
    125         urlType = null;
    126     }
    127 }