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;
    23  import org.apache.portals.bridges.struts.config.PortletURLTypes; //javadoc
    24  import org.apache.strutsel.taglib.utils.EvalHelper;
    25  // javadoc
    26
    27  /***
    28   * Supports the Struts html-el:link tag to be used within a Portlet context.
    29   *
    30   * @author <a href="mailto:ate@douma.nu">Ate Douma</a>
    31   * @version $Id: ELLinkTag.java 517068 2007-03-12 01:44:37Z ate $
    32   */
    33  public class ELLinkTag extends org.apache.strutsel.taglib.html.ELLinkTag
    34  {
    35      /***
    36       * Indicates which type of a url must be generated: action, render or resource.
    37       * <p>If not specified, the type will be determined by
    38       * {@link PortletURLTypes#getType(String)}</p>.
    39       */
    40      protected PortletURLTypes.URLType urlType = null;
    41
    42      /***
    43       * @return "true" if an ActionURL must be rendered
    44       */
    45      public String getActionURL()
    46      {
    47          return urlType != null && urlType.equals(PortletURLTypes.URLType.ACTION) ? "true" : "false";
    48      }
    49
    50      private String actionURL;
    51      /***
    52       * Render an ActionURL when set to "true"
    53       * <p>
    54       * Supports jstl expression language.
    55       * </p>
    56       * @param actionURL when (evaluated to) "true" renders an ActionURL
    57       */
    58      public void setActionURL(String actionURL)
    59      {
    60          // delay evaluation of urlType to doStartTag
    61          this.actionURL = actionURL;
    62      }
    63
    64      public String getRenderURL()
    65      {
    66          return urlType != null && urlType.equals(PortletURLTypes.URLType.RENDER) ? "true" : "false";
    67      }
    68
    69      private String renderURL;
    70      /***
    71       * Render a RenderURL when set to "true"
    72       * <p>
    73       * Supports jstl expression language.
    74       * </p>
    75       * @param renderURL when (evaluated to) "true" renders a RenderURL
    76       */
    77      public void setRenderURL(String renderURL)
    78      {
    79          // delay evaluation of urlType to doStartTag
    80          this.renderURL = renderURL;
    81      }
    82
    83      public String getResourceURL()
    84      {
    85          return urlType != null && urlType.equals(PortletURLTypes.URLType.RESOURCE) ? "true" : "false";
    86      }
    87
    88      private String resourceURL;
    89
    90      /***
    91       * Render a ResourceURL when set to "true"
    92       * <p>
    93       * Supports jstl expression language.
    94       * </p>
    95       * @param resourceURL when (evaluated to) "true" renders a ResourceURL
    96       */
    97      public void setResourceURL(String resourceURL)
    98      {
    99          // delay evaluation of urlType to doStartTag
    100         this.resourceURL = resourceURL;
    101     }
    102
    103     /***
    104      * Generates a PortletURL or a ResourceURL for the link when in the context of a
    105      * {@link PortletServlet#isPortletRequest(ServletRequest) PortletRequest}, otherwise
    106      * the default behaviour is maintained.
    107      * @return the link url
    108      * @exception JspException if a JSP exception has occurred
    109      */
    110     protected String calculateURL() throws JspException
    111     {
    112         if ( PortletServlet.isPortletRequest(pageContext.getRequest() ))
    113         {
    114             String url = super.calculateURL();
    115
    116             // process embedded anchor
    117             String anchor = null;
    118             int hash = url.indexOf('#');
    119             if ( hash > -1 )
    120             {
    121                 // save embedded anchor to be appended later and strip it from the url
    122                 anchor = url.substring(hash);
    123                 url = url.substring(0,hash);
    124             }
    125
    126             url = TagsSupport.getURL(pageContext, url, urlType);
    127
    128             if ( anchor != null )
    129             {
    130                 url = url + anchor;
    131             }
    132             return url;
    133         }
    134         else
    135         {
    136             return super.calculateURL();
    137         }
    138     }
    139
    140     public int doStartTag() throws JspException {
    141         evaluateExpressions();
    142         return(super.doStartTag());
    143     }
    144
    145     /***
    146      * Resolve the {@link #actionURL}, {@link #renderURL} and {@link #resourceURL} attributes
    147      * using the Struts JSTL expression evaluation engine ({@link EvalHelper}).
    148      * @exception JspException if a JSP exception has occurred
    149      */
    150     private void evaluateExpressions() throws JspException {
    151         Boolean value;
    152
    153         value = EvalHelper.evalBoolean("actionURL", actionURL,this, pageContext);
    154         if ( value != null && value.booleanValue() )
    155         {
    156             urlType = PortletURLTypes.URLType.ACTION;
    157         }
    158         if ( urlType == null )
    159         {
    160             value = EvalHelper.evalBoolean("renderURL", renderURL,this, pageContext);
    161             if ( value != null && value.booleanValue() )
    162             {
    163                 urlType = PortletURLTypes.URLType.RENDER;
    164             }
    165         }
    166         if ( urlType == null )
    167         {
    168             value = EvalHelper.evalBoolean("resourceURL", resourceURL,this, pageContext);
    169             if ( value != null && value.booleanValue() )
    170             {
    171                 urlType = PortletURLTypes.URLType.RESOURCE;
    172             }
    173         }
    174     }
    175
    176     public void release() {
    177
    178         super.release();
    179         urlType = null;
    180         actionURL = null;
    181         renderURL = null;
    182         resourceURL = null;
    183     }
    184 }