View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.component.html.util;
20  
21  import org.apache.commons.lang.builder.EqualsBuilder;
22  import org.apache.commons.lang.builder.HashCodeBuilder;
23  import org.apache.myfaces.renderkit.html.util.ResourceHandler;
24  
25  import javax.faces.FacesException;
26  import javax.faces.context.FacesContext;
27  import java.io.UnsupportedEncodingException;
28  import java.net.URLEncoder;
29  import java.util.Iterator;
30  import java.util.Map;
31  
32  /**
33   * @author Mathias Broekelmann
34   *
35   */
36  public class ParameterResourceHandler implements ResourceHandler
37  {
38      private final Map _parameters;
39      private final Class _resourceLoaderClass;
40  
41      private Integer _hashCode;
42  
43      /**
44       * @param resourceLoaderClass
45       * @param parameters
46       */
47      public ParameterResourceHandler(Class resourceLoaderClass, Map parameters)
48      {
49          _resourceLoaderClass = resourceLoaderClass;
50          _parameters = parameters;
51      }
52  
53      /**
54       * @see org.apache.myfaces.renderkit.html.util.ResourceHandler#getResourceLoaderClass()
55       */
56      public Class getResourceLoaderClass()
57      {
58          return _resourceLoaderClass;
59      }
60  
61      /**
62       * @see org.apache.myfaces.renderkit.html.util.ResourceHandler#getResourceUri(javax.faces.context.FacesContext)
63       */
64      public String getResourceUri(FacesContext context)
65      {
66          if (_parameters != null && !_parameters.isEmpty())
67          {
68              StringBuffer sb = new StringBuffer();
69              sb.append("?");
70              for (Iterator iter = _parameters.entrySet().iterator(); iter.hasNext();)
71              {
72                  Map.Entry entry = (Map.Entry) iter.next();
73                  sb.append(entry.getKey());
74                  sb.append("=");
75                  if (entry.getValue() != null)
76                  {
77                      try
78                      {
79                          // encode the value to make it safe to be passed through the url
80                          // the best we can do here is to use the same encoding than the response writer
81                          String encoding = context.getResponseWriter().getCharacterEncoding();
82                          if (encoding == null)
83                          {
84                              // or fallback to UTF-8 (which makes the most sense)
85                              encoding = "UTF-8";
86                          }
87                          sb.append(URLEncoder.encode(entry.getValue().toString(), encoding));
88                      }
89                      catch (UnsupportedEncodingException e)
90                      {
91                          throw new FacesException(e);
92                      }
93                  }
94                  if (iter.hasNext())
95                  {
96                      sb.append("&");
97                  }
98              }
99              return sb.toString();
100         }
101         return null;
102     }
103 
104     /**
105      * @see java.lang.Object#equals(java.lang.Object)
106      */
107     public boolean equals(Object obj)
108     {
109         if (obj == null)
110         {
111             return false;
112         }
113         if (obj == this)
114         {
115             return true;
116         }
117         if (obj instanceof ParameterResourceHandler)
118         {
119             ParameterResourceHandler other = (ParameterResourceHandler) obj;
120             return new EqualsBuilder().append(_parameters, other._parameters).isEquals();
121         }
122         return false;
123     }
124 
125     /**
126      * @see java.lang.Object#hashCode()
127      */
128     public int hashCode()
129     {
130         if (_hashCode == null)
131         {
132             _hashCode = new Integer(new HashCodeBuilder().append(_parameters).toHashCode());
133         }
134         return _hashCode.intValue();
135     }
136 }