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