Save This Page
Home » mojarra-1.2_09-b02-FCS-source » com.sun.faces.context » [javadoc | source]
    1   /*
    2    * $Id: ExternalContextImpl.java,v 1.64.4.2 2008/03/10 16:36:37 rlubke Exp $
    3    */
    4   
    5   /*
    6    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
    7    * 
    8    * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
    9    * 
   10    * The contents of this file are subject to the terms of either the GNU
   11    * General Public License Version 2 only ("GPL") or the Common Development
   12    * and Distribution License("CDDL") (collectively, the "License").  You
   13    * may not use this file except in compliance with the License. You can obtain
   14    * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
   15    * or glassfish/bootstrap/legal/LICENSE.txt.  See the License for the specific
   16    * language governing permissions and limitations under the License.
   17    * 
   18    * When distributing the software, include this License Header Notice in each
   19    * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
   20    * Sun designates this particular file as subject to the "Classpath" exception
   21    * as provided by Sun in the GPL Version 2 section of the License file that
   22    * accompanied this code.  If applicable, add the following below the License
   23    * Header, with the fields enclosed by brackets [] replaced by your own
   24    * identifying information: "Portions Copyrighted [year]
   25    * [name of copyright owner]"
   26    * 
   27    * Contributor(s):
   28    * 
   29    * If you wish your version of this file to be governed by only the CDDL or
   30    * only the GPL Version 2, indicate your decision by adding "[Contributor]
   31    * elects to include this software in this distribution under the [CDDL or GPL
   32    * Version 2] license."  If you don't indicate a single choice of license, a
   33    * recipient has the option to distribute your version of this file under
   34    * either the CDDL, the GPL Version 2 or to extend the choice of license to
   35    * its licensees as provided above.  However, if you add GPL Version 2 code
   36    * and therefore, elected the GPL Version 2 license, then the option applies
   37    * only if the new code is made subject to such option by the copyright
   38    * holder.
   39    */
   40   
   41   package com.sun.faces.context;
   42   
   43   import javax.faces.FacesException;
   44   import javax.faces.context.ExternalContext;
   45   import javax.faces.context.FacesContext;
   46   import javax.servlet.RequestDispatcher;
   47   import javax.servlet.ServletContext;
   48   import javax.servlet.ServletException;
   49   import javax.servlet.ServletRequest;
   50   import javax.servlet.ServletResponse;
   51   import javax.servlet.http.Cookie;
   52   import javax.servlet.http.HttpServletRequest;
   53   import javax.servlet.http.HttpServletResponse;
   54   import javax.servlet.http.HttpSession;
   55   
   56   import java.io.IOException;
   57   import java.io.InputStream;
   58   import java.io.UnsupportedEncodingException;
   59   import java.net.MalformedURLException;
   60   import java.net.URL;
   61   import java.util.AbstractCollection;
   62   import java.util.AbstractMap;
   63   import java.util.AbstractSet;
   64   import java.util.ArrayList;
   65   import java.util.Arrays;
   66   import java.util.Collection;
   67   import java.util.Collections;
   68   import java.util.Enumeration;
   69   import java.util.HashMap;
   70   import java.util.Iterator;
   71   import java.util.List;
   72   import java.util.Locale;
   73   import java.util.Map;
   74   import java.util.NoSuchElementException;
   75   import java.util.Set;
   76   
   77   import com.sun.faces.config.WebConfiguration;
   78   import com.sun.faces.config.WebConfiguration.BooleanWebContextInitParameter;
   79   import com.sun.faces.util.MessageUtils;
   80   import com.sun.faces.util.TypedCollections;
   81   import com.sun.faces.util.Util;
   82   import com.sun.faces.util.RequestStateManager;
   83   
   84   /**
   85    * <p>This implementation of {@link ExternalContext} is specific to the
   86    * servlet implementation.
   87    *
   88    * @author Brendan Murray
   89    * @version $Id: ExternalContextImpl.java,v 1.64.4.2 2008/03/10 16:36:37 rlubke Exp $
   90    */
   91   public class ExternalContextImpl extends ExternalContext {
   92   
   93       private ServletContext servletContext = null;
   94       private ServletRequest request = null;
   95       private ServletResponse response = null;
   96   
   97       private Map<String,Object> applicationMap = null;
   98       private Map<String,Object> sessionMap = null;
   99       private Map<String,Object> requestMap = null;
  100       private Map<String,String> requestParameterMap = null;
  101       private Map<String,String[]> requestParameterValuesMap = null;
  102       private Map<String,String> requestHeaderMap = null;
  103       private Map<String,String[]> requestHeaderValuesMap = null;
  104       private Map<String,Object> cookieMap = null;
  105       private Map<String,String> initParameterMap = null;
  106   
  107       static final Class theUnmodifiableMapClass =
  108           Collections.unmodifiableMap(new HashMap<Object,Object>()).getClass();
  109       
  110       public ExternalContextImpl(ServletContext sc, ServletRequest request,
  111                                  ServletResponse response) {
  112   
  113           // Validate the incoming parameters
  114           try {
  115               Util.parameterNonNull(sc);
  116               Util.parameterNonNull(request);
  117               Util.parameterNonNull(response);
  118           } catch (Exception e) {
  119               throw new FacesException(
  120                   MessageUtils.getExceptionMessageString(
  121                       MessageUtils.FACES_CONTEXT_CONSTRUCTION_ERROR_MESSAGE_ID));
  122           }
  123   
  124           // Save references to our context, request, and response
  125           this.servletContext = sc;
  126           this.request = request;        
  127           this.response = response;
  128           WebConfiguration config = WebConfiguration.getInstance(sc);
  129           if (config
  130                 .isOptionEnabled(BooleanWebContextInitParameter.SendPoweredByHeader)) {
  131               ((HttpServletResponse) response)
  132                     .addHeader("X-Powered-By", "JSF/1.2");
  133           }
  134           
  135       }
  136   
  137   
  138       public Object getSession(boolean create) {
  139           return (((HttpServletRequest) request).getSession(create));
  140       }
  141   
  142   
  143       public Object getContext() {
  144           return this.servletContext;
  145       }
  146   
  147   
  148       public Object getRequest() {
  149           return this.request;
  150       }
  151   
  152       public void setRequest(Object request) {
  153           if (request instanceof ServletRequest) {
  154               this.request = (ServletRequest) request;
  155               requestHeaderMap = null;
  156               requestHeaderValuesMap = null;
  157               requestHeaderValuesMap = null;
  158               requestMap = null;
  159               requestParameterMap = null;
  160               requestParameterValuesMap = null;
  161           }
  162       }
  163       
  164       public void setRequestCharacterEncoding(String encoding) throws UnsupportedEncodingException {
  165           request.setCharacterEncoding(encoding);
  166       }
  167   
  168       public Object getResponse() {
  169           return this.response;
  170       }
  171   
  172       public void setResponse(Object response) {
  173   	if (response instanceof ServletResponse) {
  174   	    this.response = (ServletResponse) response;
  175   	}
  176       }
  177       
  178       public void setResponseCharacterEncoding(String encoding) {
  179           response.setCharacterEncoding(encoding);
  180       }
  181   
  182   
  183       @SuppressWarnings("unchecked")
  184       public Map<String,Object> getApplicationMap() {
  185           if (applicationMap == null) {
  186               applicationMap = new ApplicationMap(servletContext);
  187           }
  188           return applicationMap;
  189       }
  190   
  191   
  192       @SuppressWarnings("unchecked")
  193       public Map<String,Object> getSessionMap() {
  194           if (sessionMap == null) {
  195               sessionMap = new SessionMap((HttpServletRequest) request);
  196           }
  197           return sessionMap;
  198       }
  199   
  200   
  201       @SuppressWarnings("unchecked")
  202       public Map<String,Object> getRequestMap() {
  203           if (requestMap == null) {
  204               requestMap = new RequestMap(this.request);
  205           }
  206           return requestMap;
  207       }
  208   
  209   
  210       public Map<String,String> getRequestHeaderMap() {
  211           if (null == requestHeaderMap) {
  212               requestHeaderMap = 
  213                   Collections.unmodifiableMap(
  214                       new RequestHeaderMap((HttpServletRequest) request));                                                      
  215           }
  216           return requestHeaderMap;
  217       }
  218   
  219   
  220       public Map<String,String[]> getRequestHeaderValuesMap() {
  221           if (null == requestHeaderValuesMap) {
  222               requestHeaderValuesMap = 
  223                   Collections.unmodifiableMap(
  224                       new RequestHeaderValuesMap((HttpServletRequest) request));
  225           }
  226           return requestHeaderValuesMap;
  227       }
  228   
  229   
  230       public Map<String,Object> getRequestCookieMap() {
  231           if (null == cookieMap) {
  232               cookieMap =
  233                   Collections.unmodifiableMap(
  234                       new RequestCookieMap((HttpServletRequest) request));
  235           }
  236           return cookieMap;
  237       }
  238   
  239   
  240       public Map<String,String> getInitParameterMap() {
  241           if (null == initParameterMap) {
  242               initParameterMap = 
  243                   Collections.unmodifiableMap(
  244                       new InitParameterMap(servletContext));
  245           }
  246           return initParameterMap;
  247       }
  248   
  249   
  250       public Map<String,String> getRequestParameterMap() {
  251           if (null == requestParameterMap) {
  252               requestParameterMap = 
  253                   Collections.unmodifiableMap(
  254                       new RequestParameterMap(request));
  255           }
  256           return requestParameterMap;
  257       }
  258   
  259   
  260       public Map<String,String[]> getRequestParameterValuesMap() {
  261           if (null == requestParameterValuesMap) {
  262               requestParameterValuesMap = 
  263                   Collections.unmodifiableMap(
  264                       new RequestParameterValuesMap(request));
  265           }
  266           return requestParameterValuesMap;
  267       }
  268   
  269   
  270       public Iterator<String> getRequestParameterNames() {
  271           final Enumeration namEnum = request.getParameterNames();
  272   
  273           return new Iterator<String>() {
  274               public boolean hasNext() {
  275                   return namEnum.hasMoreElements();
  276               }
  277   
  278   
  279               public String next() {
  280                   return (String) namEnum.nextElement();
  281               }
  282   
  283   
  284               public void remove() {
  285                   throw new UnsupportedOperationException();
  286               }
  287           };
  288       }
  289   
  290   
  291       public Locale getRequestLocale() {
  292           return request.getLocale();
  293       }
  294   
  295   
  296       public Iterator<Locale> getRequestLocales() {
  297           return (new LocalesIterator(request.getLocales()));
  298       }
  299   
  300   
  301       public String getRequestPathInfo() {
  302           return (((HttpServletRequest) request).getPathInfo());
  303       }
  304   
  305   
  306       public Cookie[] getRequestCookies() {
  307           return (((HttpServletRequest) request).getCookies());
  308       }
  309   
  310   
  311       public String getRequestContextPath() {
  312           return (((HttpServletRequest) request).getContextPath());
  313       }
  314   
  315   
  316       public String getRequestServletPath() {
  317           return (((HttpServletRequest) request).getServletPath());
  318       }
  319       
  320       public String getRequestCharacterEncoding() {
  321           return (request.getCharacterEncoding());
  322       }
  323   
  324        
  325       public String getRequestContentType() {
  326           return (request.getContentType());
  327       }
  328       
  329       public String getResponseCharacterEncoding() {
  330           return (response.getCharacterEncoding());
  331       }
  332       
  333       public String getResponseContentType() {
  334           return (response.getContentType());
  335       }
  336   
  337       /**
  338        * <p>Manage attributes associated with the <code>ServletContext</code>
  339        * instance associated with the current request.</p>
  340        */
  341       public String getInitParameter(String name) {
  342           return servletContext.getInitParameter(name);
  343       }
  344   
  345   
  346       public Set<String> getResourcePaths(String path) {
  347           return TypedCollections.dynamicallyCastSet(servletContext.getResourcePaths(path), String.class);
  348       }
  349   
  350   
  351       public InputStream getResourceAsStream(String path) {
  352           return servletContext.getResourceAsStream(path);
  353       }
  354   
  355   
  356       public URL getResource(String path) {
  357           URL url;
  358           try {
  359               url = servletContext.getResource(path);
  360           } catch (MalformedURLException e) {
  361               return null;
  362           }
  363           return url;
  364       }
  365   
  366   
  367       /**
  368        * <p>Force any URL that causes an action to work within a portal/portlet.
  369        * This causes the URL to have the required redirection for the specific
  370        * portal to be included</p>
  371        *
  372        * @param sb The input URL to be reformatted
  373        */
  374       public String encodeActionURL(String sb) {
  375           return ((HttpServletResponse) response).encodeURL(sb);
  376       }
  377   
  378   
  379       /**
  380        * <p>Force any URL that references a resource to work within a
  381        * portal/portlet. This causes the URL to have the required
  382        * redirection for the specific portal to be included. In reality,
  383        * it simply returns an absolute URL.</p>
  384        *
  385        * @param sb The input URL to be reformatted
  386        */
  387       public String encodeResourceURL(String sb) {
  388           return ((HttpServletResponse) response).encodeURL(sb);
  389       }
  390   
  391   
  392       public String encodeNamespace(String aValue) {
  393           return aValue; // Do nothing for servlets
  394       }
  395   
  396   
  397       public String encodeURL(String url) {
  398           return ((HttpServletResponse) response).encodeURL(url);
  399       }
  400   
  401       public void dispatch(String requestURI) throws IOException, FacesException {
  402           RequestDispatcher requestDispatcher = request.getRequestDispatcher(
  403               requestURI);
  404           if (requestDispatcher == null) {
  405               ((HttpServletResponse) response).
  406                       sendError(HttpServletResponse.SC_NOT_FOUND);
  407               return;
  408           }
  409           try {
  410               requestDispatcher.forward(this.request, this.response);
  411           } catch (IOException ioe) {
  412               // e.printStackTrace();
  413               throw ioe;
  414           } catch (ServletException se) {
  415               throw new FacesException(se);
  416           }
  417       }
  418   
  419   
  420       public void redirect(String requestURI) throws IOException {
  421           ((HttpServletResponse) response).sendRedirect(requestURI);
  422           FacesContext.getCurrentInstance().responseComplete();
  423       }
  424   
  425   
  426       public void log(String message) {
  427           servletContext.log(message);
  428       }
  429   
  430   
  431       public void log(String message, Throwable throwable) {
  432           servletContext.log(message, throwable);
  433       }
  434   
  435   
  436       public String getAuthType() {
  437           return ((HttpServletRequest) request).getAuthType();
  438       }
  439   
  440   
  441       public String getRemoteUser() {
  442           return ((HttpServletRequest) request).getRemoteUser();
  443       }
  444   
  445   
  446       public java.security.Principal getUserPrincipal() {
  447           return ((HttpServletRequest) request).getUserPrincipal();
  448       }
  449   
  450   
  451       public boolean isUserInRole(String role) {
  452           return ((HttpServletRequest) request).isUserInRole(role);
  453       }
  454   
  455   
  456       private static class LocalesIterator implements Iterator<Locale> {
  457   
  458           public LocalesIterator(Enumeration locales) {
  459               this.locales = locales;
  460           }
  461   
  462   
  463           private Enumeration locales;
  464   
  465   
  466           public boolean hasNext() {
  467               return locales.hasMoreElements();
  468           }
  469   
  470   
  471           public Locale next() {
  472               return (Locale) locales.nextElement();
  473           }
  474   
  475   
  476           public void remove() {
  477               throw new UnsupportedOperationException();
  478           }
  479   
  480       }
  481   }
  482   
  483   abstract class BaseContextMap<V> extends AbstractMap<String,V> {
  484   
  485       private Set<Map.Entry<String, V>> entrySet;
  486       private Set<String> keySet;
  487       private Collection<V> values;
  488       
  489       // Supported by maps if overridden
  490       @Override
  491       public void clear() {
  492           throw new UnsupportedOperationException();
  493       }
  494   
  495   
  496       // Supported by maps if overridden
  497       @Override
  498       public void putAll(Map t) {
  499           throw new UnsupportedOperationException();
  500       }
  501   
  502       @Override
  503       public Set<Map.Entry<String, V>> entrySet() {
  504           if (entrySet == null) {
  505               entrySet = new EntrySet();
  506           }
  507   
  508           return entrySet;
  509       }
  510   
  511       @Override
  512       public Set<String> keySet() {
  513           if (keySet == null) {
  514               keySet = new KeySet();
  515           }
  516   
  517           return keySet;
  518       }
  519   
  520       @Override
  521       public Collection<V> values() {
  522           if (values == null) {
  523               values = new ValueCollection();
  524           }
  525   
  526           return values;
  527       }
  528   
  529   
  530       // Supported by maps if overridden
  531       @Override
  532       public V remove(Object key) {
  533           throw new UnsupportedOperationException();
  534       }
  535   
  536       protected boolean removeKey(Object key) {
  537           return (this.remove(key) != null);
  538       }
  539   
  540       protected boolean removeValue(Object value) {
  541           boolean valueRemoved = false;
  542           if (value == null) {
  543               return false;
  544           }
  545           if (containsValue(value)) {
  546               for (Iterator i = entrySet().iterator(); i.hasNext(); ) {
  547                   Map.Entry e = (Map.Entry) i.next();
  548                   if (value.equals(e.getValue())) {                    
  549                       valueRemoved = (remove(e.getKey()) != null);
  550                   }
  551               }
  552           }
  553           return valueRemoved;
  554       }
  555   
  556       protected abstract Iterator<Map.Entry<String, V>> getEntryIterator();
  557       protected abstract Iterator<String> getKeyIterator();
  558       protected abstract Iterator<V> getValueIterator();
  559   
  560       abstract class BaseSet<E> extends AbstractSet<E> {
  561   
  562           @Override
  563           public int size() {
  564               int size = 0;
  565               for (Iterator<E> i = iterator(); i.hasNext(); size++) {
  566                   i.next();
  567               }
  568               return size;
  569           }
  570   
  571       }
  572   
  573       class EntrySet extends BaseSet<Map.Entry<String, V>> {
  574   
  575           @Override
  576           public Iterator<Map.Entry<String, V>> iterator() {
  577               return getEntryIterator();
  578           }
  579   
  580           @Override
  581           public boolean remove(Object o) {
  582               return o instanceof Map.Entry
  583                      && removeKey(((Map.Entry) o).getKey());
  584           }
  585   
  586       }
  587   
  588       class KeySet extends BaseSet<String> {
  589   
  590           @Override
  591           public Iterator<String> iterator() {
  592               return getKeyIterator();
  593           }
  594   
  595   
  596           @Override
  597           public boolean contains(Object o) {
  598               return BaseContextMap.this.containsKey(o);
  599           }
  600   
  601           @Override
  602           public boolean remove(Object o) {
  603               return o instanceof String && removeKey(o);
  604           }
  605       }
  606   
  607       class ValueCollection extends AbstractCollection<V> {
  608   
  609           @Override
  610           public int size() {
  611               int size = 0;
  612               for (Iterator i = iterator(); i.hasNext(); size++) {
  613                   i.next();
  614               }
  615               return size;
  616           }
  617   
  618           @Override
  619           public Iterator<V> iterator() {
  620               return getValueIterator();
  621           }
  622   
  623           @Override
  624           public boolean remove(Object o) {
  625               return removeValue(o);
  626           }
  627       }
  628   
  629       abstract class BaseIterator<E> implements Iterator<E> {
  630   
  631           protected Enumeration e;
  632           protected String currentKey;
  633           protected boolean removeCalled = false;
  634   
  635           BaseIterator(Enumeration e) {
  636               this.e = e;
  637           }
  638   
  639           public boolean hasNext() {
  640               return e.hasMoreElements();
  641           }
  642   
  643           public String nextKey() {
  644               removeCalled = false;
  645               currentKey = (String) e.nextElement();
  646               return currentKey;
  647           }
  648       }
  649   
  650       class EntryIterator extends BaseIterator<Map.Entry<String,V>> {
  651   
  652           EntryIterator(Enumeration e) {
  653               super(e);
  654           }
  655   
  656           public void remove() {
  657               if (currentKey != null && !removeCalled) {
  658                   removeCalled = true;
  659                   removeKey(currentKey);
  660               } else {
  661                   throw new IllegalStateException();
  662               }
  663           }
  664   
  665           public Map.Entry<String,V> next() {
  666               nextKey();
  667               return new Entry<V>(currentKey, get(currentKey));
  668           }
  669       }
  670   
  671        class KeyIterator extends BaseIterator<String> {
  672   
  673           KeyIterator(Enumeration e) {
  674               super(e);
  675           }
  676   
  677           public void remove() {
  678               if (currentKey != null && !removeCalled) {
  679                   removeCalled = true;
  680                   removeKey(currentKey);
  681               } else {
  682                   throw new IllegalStateException();
  683               }
  684           }
  685   
  686           public String next() {
  687               return nextKey();
  688           }
  689       }
  690   
  691       class ValueIterator extends BaseIterator<V> {
  692   
  693           ValueIterator(Enumeration e) {
  694               super(e);
  695           }
  696   
  697           public void remove() {
  698               if (currentKey != null && !removeCalled) {
  699                   removeCalled = true;
  700                   removeValue(get(currentKey));
  701               } else {
  702                   throw new IllegalStateException();
  703               }
  704           }
  705   
  706           public V next() {
  707               nextKey();
  708               return get(currentKey);
  709           }
  710       }
  711   
  712   
  713       static class Entry<V> implements Map.Entry<String,V> {
  714   
  715           // immutable Entry
  716           private final String key;
  717           private final V value;
  718   
  719   
  720           Entry(String key, V value) {
  721               this.key = key;
  722               this.value = value;
  723           }
  724   
  725   
  726           public String getKey() {
  727               return key;
  728           }
  729   
  730   
  731           public V getValue() {
  732               return value;
  733           }
  734   
  735   
  736           // No support of setting the value
  737           public V setValue(V value) {
  738               throw new UnsupportedOperationException();
  739           }
  740   
  741           @Override
  742           public int hashCode() {
  743               return ((key == null ? 0 : key.hashCode()) ^
  744                   (value == null ? 0 : value.hashCode()));
  745           }
  746   
  747           @Override
  748           public boolean equals(Object obj) {
  749               if (obj == null || !(obj instanceof Map.Entry)) {
  750                   return false;
  751               }
  752   
  753               Map.Entry input = (Map.Entry) obj;
  754               Object inputKey = input.getKey();
  755               Object inputValue = input.getValue();
  756   
  757               if (inputKey == key ||
  758                   (inputKey != null && inputKey.equals(key))) {
  759                   if (inputValue == value ||
  760                       (inputValue != null && inputValue.equals(value))) {
  761                       return true;
  762                   }
  763               }
  764               return false;
  765           }
  766       }
  767   }
  768   
  769   abstract class StringArrayValuesMap extends BaseContextMap<String[]> {
  770   
  771       @Override
  772       public boolean equals(Object obj) {
  773           
  774           if (obj == null || 
  775               !(obj.getClass() == ExternalContextImpl.theUnmodifiableMapClass)) {
  776               return false;
  777           }
  778           Map objMap = (Map) obj;
  779           
  780           if (this.size() != objMap.size()) {
  781               return false;
  782           }
  783           String[] thisKeys = keySet().toArray(new String[this.size()]);
  784           Object[] objKeys = objMap.keySet().toArray();
  785           
  786           Arrays.sort(thisKeys);
  787           Arrays.sort(objKeys);
  788           
  789           if (!(Arrays.equals(thisKeys, objKeys))) {
  790               return false;
  791           } else {
  792               for (Object key : thisKeys) {
  793                   Object[] thisVal = this.get(key);
  794                   Object[] objVal = (Object[]) objMap.get(key);
  795                   if (!(Arrays.equals(thisVal, objVal))) {
  796                       return false;
  797                   }
  798               }
  799           }
  800           
  801           return true;        
  802           
  803       }
  804       
  805       protected int hashCode(Object someObject) {        
  806           int hashCode = 7 * someObject.hashCode();
  807            for (Object o : entrySet()) {             
  808                Map.Entry entry = (Map.Entry) o;             
  809                hashCode += entry.getKey().hashCode();
  810                hashCode +=
  811                      (Arrays.hashCode((Object[]) entry.getValue()));
  812            }
  813           return hashCode;
  814       }
  815   
  816       @Override
  817       public boolean containsValue(Object value) {
  818                   
  819           if (value == null || !value.getClass().isArray()) {
  820               return false;
  821           }
  822           
  823           Set entrySet = entrySet();
  824           for (Object anEntrySet : entrySet) {
  825               Map.Entry entry = (Map.Entry) anEntrySet;
  826               // values will be arrays
  827               if (Arrays.equals((Object[]) value, (Object[]) entry.getValue())) {
  828                   return true;
  829               }
  830           }
  831           return false;
  832       }
  833   }
  834   
  835   class ApplicationMap extends BaseContextMap {
  836   
  837       private final ServletContext servletContext;
  838   
  839       ApplicationMap(ServletContext servletContext) {
  840           this.servletContext = servletContext;
  841       }
  842   
  843       @Override
  844       public void clear() {
  845           for (Enumeration e = servletContext.getAttributeNames();
  846                e.hasMoreElements(); ) {
  847               servletContext.removeAttribute((String) e.nextElement());
  848           }
  849       }
  850   
  851       // Supported by maps if overridden
  852       @Override
  853       public void putAll(Map t) {
  854           for (Iterator i = t.entrySet().iterator(); i.hasNext(); ) {
  855               Map.Entry entry = (Map.Entry) i.next();
  856               servletContext.setAttribute((String) entry.getKey(),
  857                                           entry.getValue());
  858           }
  859       }
  860   
  861       @Override
  862       public Object get(Object key) {
  863           Util.notNull("key", key);
  864           return servletContext.getAttribute(key.toString());
  865       }
  866   
  867       @Override
  868       public Object put(Object key, Object value) {
  869           Util.notNull("key", key);
  870           Object result = servletContext.getAttribute(key.toString());
  871           servletContext.setAttribute(key.toString(), value);
  872           return (result);
  873       }
  874   
  875       @Override
  876       public Object remove(Object key) {
  877           if (key == null) {
  878               return null;
  879           }
  880           String keyString = key.toString();
  881           Object result = servletContext.getAttribute(keyString);
  882           servletContext.removeAttribute(keyString);
  883           return (result);
  884       }
  885   
  886   
  887       @Override
  888       public boolean containsKey(Object key) {
  889           return (servletContext.getAttribute(key.toString()) != null);
  890       }
  891   
  892       @Override
  893       public boolean equals(Object obj) {
  894           return !(obj == null || !(obj instanceof ApplicationMap))
  895                      && super.equals(obj);
  896       }
  897   
  898       @Override
  899       public int hashCode() {
  900           int hashCode = 7 * servletContext.hashCode();
  901           for (Iterator i = entrySet().iterator(); i.hasNext(); ) {
  902               hashCode += i.next().hashCode();
  903           }
  904           return hashCode;
  905       }
  906   
  907       // --------------------------------------------- Methods from BaseContextMap
  908   
  909   
  910       @SuppressWarnings("unchecked")
  911       protected Iterator<Map.Entry<String, Object>> getEntryIterator() {
  912           return new EntryIterator(servletContext.getAttributeNames());
  913       }
  914   
  915       @SuppressWarnings("unchecked")
  916       protected Iterator<String> getKeyIterator() {
  917           return new KeyIterator(servletContext.getAttributeNames());
  918       }
  919   
  920       @SuppressWarnings("unchecked")
  921       protected Iterator<Object> getValueIterator() {
  922           return new ValueIterator(servletContext.getAttributeNames());
  923       }
  924   
  925   } // END ApplicationMap
  926   
  927   class SessionMap extends BaseContextMap {
  928   
  929       private final HttpServletRequest request;
  930   
  931       SessionMap(HttpServletRequest request) {
  932           this.request = request;
  933       }
  934   
  935       @Override
  936       public void clear() {
  937           HttpSession session = getSession(false);
  938           if (session != null) {
  939               for (Enumeration e = session.getAttributeNames();
  940                    e.hasMoreElements();) {
  941                   String name = (String) e.nextElement();
  942                   session.removeAttribute(name);
  943               }
  944           }
  945       }
  946   
  947       // Supported by maps if overridden
  948       @Override
  949       public void putAll(Map t) {
  950           HttpSession session = getSession(true);
  951           for (Iterator i = t.entrySet().iterator(); i.hasNext(); ) {
  952               Map.Entry entry = (Map.Entry) i.next();
  953               session.setAttribute((String) entry.getKey(),
  954                                    entry.getValue());
  955           }
  956       }
  957   
  958       @Override
  959       public Object get(Object key) {
  960           Util.notNull("key", key);
  961           HttpSession session = getSession(false);
  962           return ((session != null) ? session.getAttribute(key.toString()) : null);
  963   
  964       }
  965   
  966   
  967       public Object put(Object key, Object value) {
  968           Util.notNull("key", key);
  969           HttpSession session = getSession(true);
  970           Object result = session.getAttribute(key.toString());
  971           session.setAttribute(key.toString(), value);
  972           return (result);
  973       }
  974   
  975       @Override
  976       public Object remove(Object key) {
  977           if (key == null) {
  978               return null;
  979           }
  980           HttpSession session = getSession(false);
  981           if (session != null) {
  982               String keyString = key.toString();
  983               Object result = session.getAttribute(keyString);
  984               session.removeAttribute(keyString);
  985               return (result);
  986           }
  987           return null;
  988       }
  989   
  990   
  991       @Override
  992       public boolean containsKey(Object key) {
  993           HttpSession session = getSession(false);
  994           return ((session != null)
  995                   && session.getAttribute(key.toString()) != null);
  996       }
  997   
  998       @Override
  999       public boolean equals(Object obj) {
 1000           return !(obj == null || !(obj instanceof SessionMap))
 1001                  && super.equals(obj);
 1002       }
 1003   
 1004   
 1005       private HttpSession getSession(boolean createNew) {
 1006           return request.getSession(createNew);
 1007       }
 1008   
 1009       @Override
 1010       public int hashCode() {
 1011           HttpSession session = getSession(false);
 1012           int hashCode =
 1013                 7 * ((session != null) ? session.hashCode() : super.hashCode());
 1014           if (session != null) {
 1015               for (Iterator i = entrySet().iterator(); i.hasNext();) {
 1016                   hashCode += i.next().hashCode();
 1017               }
 1018           }
 1019           return hashCode;
 1020       }
 1021   
 1022       // --------------------------------------------- Methods from BaseContextMap
 1023   
 1024       @SuppressWarnings("unchecked")
 1025       protected Iterator<Map.Entry<String,Object>> getEntryIterator() {
 1026           HttpSession session = getSession(false);
 1027           return ((session != null)
 1028                   ? new EntryIterator(session.getAttributeNames())
 1029                   : Collections.emptyMap().entrySet().iterator());
 1030       }
 1031   
 1032       @SuppressWarnings("unchecked")
 1033       protected Iterator<String> getKeyIterator() {
 1034           HttpSession session = getSession(false);
 1035           return ((session != null)
 1036                   ? new KeyIterator(session.getAttributeNames())
 1037                   : Collections.emptyMap().entrySet().iterator());
 1038       }
 1039   
 1040       @SuppressWarnings("unchecked")
 1041       protected Iterator<Object> getValueIterator() {
 1042           HttpSession session = getSession(false);
 1043           return ((session != null)
 1044                   ? new ValueIterator(session.getAttributeNames())
 1045                   : Collections.emptyMap().entrySet().iterator());
 1046       }
 1047   
 1048   } // END SessionMap
 1049   
 1050   class RequestMap extends BaseContextMap {
 1051   
 1052       private final ServletRequest request;    
 1053   
 1054   
 1055       RequestMap(ServletRequest request) {
 1056           this.request = request;     
 1057       }
 1058   
 1059       @Override
 1060       public void clear() {
 1061           for (Enumeration e = request.getAttributeNames();
 1062                e.hasMoreElements(); ) {
 1063               request.removeAttribute((String) e.nextElement());
 1064           }
 1065       }
 1066   
 1067       // Supported by maps if overridden
 1068       @Override
 1069       public void putAll(Map t) {
 1070           for (Iterator i = t.entrySet().iterator(); i.hasNext(); ) {
 1071               Map.Entry entry = (Map.Entry) i.next();
 1072               request.setAttribute((String) entry.getKey(),
 1073                                           entry.getValue());
 1074           }
 1075       }
 1076   
 1077       @Override
 1078       public Object get(Object key) {
 1079           Util.notNull("key", key);
 1080           return request.getAttribute(key.toString());
 1081       }
 1082   
 1083   
 1084       public Object put(Object key, Object value) {
 1085           Util.notNull("key", key);
 1086           Object result = request.getAttribute(key.toString());
 1087           request.setAttribute(key.toString(), value);
 1088           return (result);
 1089       }
 1090   
 1091       @Override
 1092       public Object remove(Object key) {
 1093           Util.notNull("key", key);
 1094           String keyString = key.toString();
 1095           Object result = request.getAttribute(keyString);
 1096           request.removeAttribute(keyString);
 1097           return (result);
 1098       }
 1099   
 1100   
 1101       @Override
 1102       public boolean containsKey(Object key) {
 1103           return (request.getAttribute(key.toString()) != null);
 1104       }
 1105   
 1106       @Override
 1107       public boolean equals(Object obj) {
 1108           return !(obj == null || !(obj instanceof RequestMap))
 1109                  && super.equals(obj);
 1110       }
 1111   
 1112       @Override
 1113       public int hashCode() {
 1114           int hashCode = 7 * request.hashCode();
 1115           for (Iterator i = entrySet().iterator(); i.hasNext(); ) {
 1116               hashCode += i.next().hashCode();
 1117           }
 1118           return hashCode;
 1119       }
 1120   
 1121       // --------------------------------------------- Methods from BaseContextMap
 1122   
 1123       @SuppressWarnings("unchecked")
 1124       protected Iterator<Map.Entry<String,Object>> getEntryIterator() {
 1125           return new EntryIterator(request.getAttributeNames());
 1126       }
 1127   
 1128       @SuppressWarnings("unchecked")
 1129       protected Iterator<String> getKeyIterator() {
 1130           return new KeyIterator(request.getAttributeNames());
 1131       }
 1132   
 1133       @SuppressWarnings("unchecked")
 1134       protected Iterator<Object> getValueIterator() {
 1135           return new ValueIterator(request.getAttributeNames());
 1136       }
 1137   
 1138   } // END RequestMap
 1139   
 1140   class RequestParameterMap extends BaseContextMap<String> {
 1141   
 1142       private final ServletRequest request;
 1143   
 1144   
 1145       RequestParameterMap(ServletRequest request) {
 1146           this.request = request;
 1147       }
 1148   
 1149       @Override
 1150       public String get(Object key) {
 1151           Util.notNull("key", key);
 1152           return request.getParameter(key.toString());
 1153       }
 1154   
 1155       @Override
 1156       public Set<Map.Entry<String,String>> entrySet() {
 1157           return Collections.unmodifiableSet(super.entrySet());
 1158       }
 1159   
 1160       @Override
 1161       public Set<String> keySet() {
 1162           return Collections.unmodifiableSet(super.keySet());
 1163       }
 1164   
 1165       @Override
 1166       public Collection<String> values() {
 1167           return Collections.unmodifiableCollection(super.values());
 1168       }
 1169   
 1170   
 1171       @Override
 1172       public boolean containsKey(Object key) {
 1173           return (request.getParameter(key.toString()) != null);
 1174       }
 1175   
 1176       @Override
 1177       public boolean equals(Object obj) {
 1178           return !(obj == null ||
 1179                    !(obj.getClass()
 1180                      == ExternalContextImpl
 1181                          .theUnmodifiableMapClass)) && super.equals(obj);
 1182       }
 1183   
 1184       @Override
 1185       public int hashCode() {
 1186           int hashCode = 7 * request.hashCode();
 1187           for (Iterator i = entrySet().iterator(); i.hasNext(); ) {
 1188               hashCode += i.next().hashCode();
 1189           }
 1190           return hashCode;
 1191       }
 1192   
 1193       // --------------------------------------------- Methods from BaseContextMap
 1194   
 1195       protected Iterator<Map.Entry<String,String>> getEntryIterator() {
 1196           return new EntryIterator(request.getParameterNames());
 1197       }
 1198   
 1199       protected Iterator<String> getKeyIterator() {
 1200           return new KeyIterator(request.getParameterNames());
 1201       }
 1202   
 1203       protected Iterator<String> getValueIterator() {
 1204           return new ValueIterator(request.getParameterNames());
 1205       }
 1206   
 1207   } // END RequestParameterMap
 1208   
 1209   class RequestParameterValuesMap extends StringArrayValuesMap {
 1210   
 1211       private final ServletRequest request;
 1212   
 1213   
 1214       RequestParameterValuesMap(ServletRequest request) {
 1215           this.request = request;
 1216       }
 1217   
 1218   
 1219       @Override
 1220       public String[] get(Object key) {
 1221           Util.notNull("key", key);
 1222           return request.getParameterValues(key.toString());
 1223       }
 1224   
 1225   
 1226       @Override
 1227       public boolean containsKey(Object key) {
 1228           return (request.getParameterValues(key.toString()) != null);
 1229       }
 1230   
 1231       @Override
 1232       public Set<Map.Entry<String,String[]>> entrySet() {
 1233           return Collections.unmodifiableSet(super.entrySet());
 1234       }
 1235   
 1236       @Override
 1237       public Set<String> keySet() {
 1238           return Collections.unmodifiableSet(super.keySet());
 1239       }
 1240   
 1241       @Override
 1242       public Collection<String[]> values() {
 1243           return Collections.unmodifiableCollection(super.values());
 1244       }
 1245   
 1246       @Override
 1247       public int hashCode() {        
 1248           return hashCode(request);
 1249       }
 1250   
 1251   
 1252       // --------------------------------------------- Methods from BaseContextMap
 1253   
 1254       protected Iterator<Map.Entry<String, String[]>> getEntryIterator() {
 1255           return new EntryIterator(request.getParameterNames());
 1256       }
 1257   
 1258       protected Iterator<String> getKeyIterator() {
 1259           return new KeyIterator(request.getParameterNames());
 1260       }
 1261   
 1262       protected Iterator<String[]> getValueIterator() {
 1263           return new ValueIterator(request.getParameterNames());
 1264       }
 1265   
 1266   } // END RequestParameterValuesMap
 1267   
 1268   class RequestHeaderMap extends BaseContextMap<String> {
 1269   
 1270       private final HttpServletRequest request;
 1271   
 1272   
 1273       RequestHeaderMap(HttpServletRequest request) {
 1274           this.request = request;
 1275       }
 1276   
 1277       @Override
 1278       public String get(Object key) {
 1279           Util.notNull("key", key);
 1280           return (request.getHeader(key.toString()));
 1281       }
 1282   
 1283       @Override
 1284       public Set<Map.Entry<String,String>> entrySet() {
 1285           return Collections.unmodifiableSet(super.entrySet());
 1286       }
 1287   
 1288       @Override
 1289       public Set<String> keySet() {
 1290           return Collections.unmodifiableSet(super.keySet());
 1291       }
 1292   
 1293       @Override
 1294       public Collection<String> values() {
 1295           return Collections.unmodifiableCollection(super.values());
 1296       }
 1297   
 1298   
 1299       @Override
 1300       public boolean containsKey(Object key) {
 1301           return (request.getHeader(key.toString()) != null);
 1302       }
 1303   
 1304       @Override
 1305       public boolean equals(Object obj) {
 1306           return !(obj == null ||
 1307                    !(obj.getClass()
 1308                      == ExternalContextImpl
 1309                          .theUnmodifiableMapClass)) && super.equals(obj);
 1310       }
 1311   
 1312       @Override
 1313       public int hashCode() {
 1314           int hashCode = 7 * request.hashCode();
 1315           for (Iterator i = entrySet().iterator(); i.hasNext(); ) {
 1316               hashCode += i.next().hashCode();
 1317           }
 1318           return hashCode;
 1319       }
 1320   
 1321       // --------------------------------------------- Methods from BaseContextMap
 1322   
 1323       protected Iterator<Map.Entry<String,String>> getEntryIterator() {
 1324           return new EntryIterator(request.getHeaderNames());
 1325       }
 1326   
 1327       protected Iterator<String> getKeyIterator() {
 1328           return new KeyIterator(request.getHeaderNames());
 1329       }
 1330   
 1331       protected Iterator<String> getValueIterator() {
 1332           return new ValueIterator(request.getHeaderNames());
 1333       }
 1334   
 1335   } // END RequestHeaderMap
 1336   
 1337   class RequestHeaderValuesMap extends StringArrayValuesMap {
 1338   
 1339       private final HttpServletRequest request;
 1340   
 1341   
 1342       RequestHeaderValuesMap(HttpServletRequest request) {
 1343           this.request = request;
 1344       }
 1345   
 1346   
 1347       @Override
 1348       public boolean containsKey(Object key) {
 1349           return (request.getHeaders(key.toString()) != null);
 1350       }
 1351   
 1352       @Override
 1353       public String[] get(Object key) {
 1354           Util.notNull("key", key);
 1355           List<String> valuesList = new ArrayList<String>();
 1356           Enumeration valuesEnum = this.request.getHeaders(key.toString());
 1357           while (valuesEnum.hasMoreElements()) {
 1358               valuesList.add((String) valuesEnum.nextElement());
 1359           } 
 1360   
 1361           return valuesList.toArray(new String[valuesList.size()]); 
 1362       }
 1363   
 1364       @Override
 1365       public Set<Map.Entry<String,String[]>> entrySet() {
 1366           return Collections.unmodifiableSet(super.entrySet());
 1367       }
 1368   
 1369       @Override
 1370       public Set<String> keySet() {
 1371           return Collections.unmodifiableSet(super.keySet());
 1372       }
 1373   
 1374       @Override
 1375       public Collection<String[]> values() {
 1376           return Collections.unmodifiableCollection(super.values());
 1377       }        
 1378   
 1379       @Override
 1380       public int hashCode() {        
 1381           return hashCode(request);
 1382       }
 1383   
 1384       // --------------------------------------------- Methods from BaseContextMap
 1385   
 1386       protected Iterator<Map.Entry<String,String[]>> getEntryIterator() {
 1387           return new EntryIterator(request.getHeaderNames());
 1388       }
 1389   
 1390       protected Iterator<String> getKeyIterator() {
 1391           return new KeyIterator(request.getHeaderNames());
 1392       }
 1393   
 1394       protected Iterator<String[]> getValueIterator() {
 1395           return new ValueIterator(request.getHeaderNames());
 1396       }
 1397   
 1398   } // END RequestHeaderValuesMap
 1399   
 1400   class RequestCookieMap extends BaseContextMap<Object> {
 1401   
 1402       private final HttpServletRequest request;
 1403   
 1404   
 1405       RequestCookieMap(HttpServletRequest newRequest) {
 1406           this.request = newRequest;
 1407       }
 1408   
 1409       @Override
 1410       public Object get(Object key) {
 1411           Util.notNull("key", key);
 1412   
 1413           Cookie[] cookies = request.getCookies();
 1414           if (null == cookies) {
 1415               return null;
 1416           }
 1417   
 1418           String keyString = key.toString();
 1419           Object result = null;
 1420   
 1421           for (int i = 0; i < cookies.length; i++) {
 1422               if (cookies[i].getName().equals(keyString)) {
 1423                   result = cookies[i];
 1424                   break;
 1425               }
 1426           }
 1427           return result;
 1428       }
 1429   
 1430       @Override
 1431       public Set<Map.Entry<String,Object>> entrySet() {
 1432           return Collections.unmodifiableSet(super.entrySet());
 1433       }
 1434   
 1435       @Override
 1436       public Set<String> keySet() {
 1437           return Collections.unmodifiableSet(super.keySet());
 1438       }
 1439   
 1440       @Override
 1441       public Collection<Object> values() {
 1442           return Collections.unmodifiableCollection(super.values());
 1443       }
 1444   
 1445       @Override
 1446       public boolean equals(Object obj) {
 1447           return !(obj == null ||
 1448                    !(obj.getClass()
 1449                      == ExternalContextImpl
 1450                          .theUnmodifiableMapClass)) && super.equals(obj);
 1451       }
 1452   
 1453       @Override
 1454       public int hashCode() {
 1455           int hashCode = 7 * request.hashCode();
 1456           for (Iterator i = entrySet().iterator(); i.hasNext(); ) {
 1457               hashCode += i.next().hashCode();
 1458           }
 1459           return hashCode;
 1460       }
 1461   
 1462       // --------------------------------------------- Methods from BaseContextMap
 1463   
 1464   
 1465       protected Iterator<Map.Entry<String,Object>> getEntryIterator() {
 1466           return new EntryIterator(
 1467                   new CookieArrayEnumerator(request.getCookies()));
 1468       }
 1469   
 1470       protected Iterator<String> getKeyIterator() {
 1471           return new KeyIterator(
 1472                   new CookieArrayEnumerator(request.getCookies()));
 1473       }
 1474   
 1475       protected Iterator<Object> getValueIterator() {
 1476           return new ValueIterator(
 1477               new CookieArrayEnumerator(request.getCookies()));
 1478       }
 1479   
 1480       private static class CookieArrayEnumerator implements Enumeration {
 1481   
 1482           Cookie[] cookies;
 1483           int curIndex = -1;
 1484           int upperBound;
 1485   
 1486           public CookieArrayEnumerator(Cookie[] cookies) {
 1487               this.cookies = cookies;
 1488               upperBound = ((this.cookies != null) ? this.cookies.length : -1);
 1489           }
 1490   
 1491           public boolean hasMoreElements() {
 1492               return (curIndex + 2 <= upperBound);
 1493           }
 1494   
 1495           public Object nextElement() {
 1496               curIndex++;
 1497               if (curIndex < upperBound) {
 1498                   return cookies[curIndex].getName();
 1499               } else {
 1500                   throw new NoSuchElementException();
 1501               }
 1502           }
 1503       }
 1504   
 1505   } // END RequestCookiesMap
 1506   
 1507   class InitParameterMap extends BaseContextMap<String> {
 1508   
 1509       private final ServletContext servletContext;
 1510   
 1511   
 1512       InitParameterMap(ServletContext newServletContext) {
 1513           servletContext = newServletContext;
 1514       }
 1515   
 1516       @Override
 1517       public String get(Object key) {
 1518           Util.notNull("key", key);
 1519           String keyString = key.toString();
 1520           return servletContext.getInitParameter(keyString);
 1521       }
 1522   
 1523       @Override
 1524       public Set<Map.Entry<String,String>> entrySet() {
 1525           return Collections.unmodifiableSet(super.entrySet());
 1526       }
 1527   
 1528       @Override
 1529       public Set<String> keySet() {
 1530           return Collections.unmodifiableSet(super.keySet());
 1531       }
 1532   
 1533       @Override
 1534       public Collection<String> values() {
 1535           return Collections.unmodifiableCollection(super.values());
 1536       }
 1537   
 1538       @Override
 1539       public boolean containsKey(Object key) {
 1540           return (servletContext.getInitParameter(key.toString()) != null);
 1541       }
 1542   
 1543   
 1544   
 1545       public boolean equals(Object obj) {
 1546           return !(obj == null ||
 1547                    !(obj.getClass()
 1548                      == ExternalContextImpl
 1549                          .theUnmodifiableMapClass)) && super.equals(obj);
 1550       }
 1551   
 1552   
 1553   
 1554       public int hashCode() {
 1555           int hashCode = 7 * servletContext.hashCode();
 1556           for (Iterator i = entrySet().iterator(); i.hasNext(); ) {
 1557               hashCode += i.next().hashCode();
 1558           }
 1559           return hashCode;
 1560       }
 1561   
 1562       // --------------------------------------------- Methods from BaseContextMap
 1563   
 1564       protected Iterator<Map.Entry<String,String>> getEntryIterator() {
 1565           return new EntryIterator(servletContext.getInitParameterNames());
 1566       }
 1567   
 1568       protected Iterator<String> getKeyIterator() {
 1569           return new KeyIterator(servletContext.getInitParameterNames());
 1570       }
 1571   
 1572       protected Iterator<String> getValueIterator() {
 1573           return new ValueIterator(servletContext.getInitParameterNames());
 1574       }
 1575   
 1576   } // END InitParameterMap

Save This Page
Home » mojarra-1.2_09-b02-FCS-source » com.sun.faces.context » [javadoc | source]