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.jsf;
    18
    19  import javax.faces.FactoryFinder;
    20  import javax.faces.application.Application;
    21  import javax.faces.application.ApplicationFactory;
    22  import javax.faces.application.FacesMessage;
    23  import javax.faces.component.UIViewRoot;
    24  import javax.faces.context.ExternalContext;
    25  import javax.faces.context.FacesContext;
    26  import javax.faces.context.ResponseStream;
    27  import javax.faces.context.ResponseWriter;
    28  import javax.faces.render.RenderKit;
    29  import javax.faces.render.RenderKitFactory;
    30  import javax.servlet.ServletContext;
    31  import javax.servlet.ServletRequest;
    32  import javax.servlet.ServletResponse;
    33  import java.util.*;
    34
    35  /***
    36   * @author Apache MyFaces team
    37   */
    38  public class ServletFacesContextImpl
    39          extends FacesContext {
    40      //~ Instance fields ----------------------------------------------------------------------------
    41
    42      private List _messageClientIds = null;
    43      private List                        _messages         = null;
    44      private Application _application;
    45      private ServletExternalContextImpl  _externalContext;
    46      private ResponseStream _responseStream   = null;
    47      private ResponseWriter _responseWriter   = null;
    48      private FacesMessage.Severity       _maximumSeverity  = null;
    49      private UIViewRoot _viewRoot;
    50      private boolean                     _renderResponse   = false;
    51      private boolean                     _responseComplete = false;
    52      private RenderKitFactory _renderKitFactory;
    53      private boolean                     _released = false;
    54
    55      //~ Constructors -------------------------------------------------------------------------------
    56
    57      public ServletFacesContextImpl(ServletContext servletContext,
    58                                     ServletRequest servletRequest,
    59                                     ServletResponse servletResponse)
    60      {
    61          this(new ServletExternalContextImpl(servletContext,
    62                                              servletRequest,
    63                                              servletResponse));
    64      }
    65
    66      private ServletFacesContextImpl(ServletExternalContextImpl externalContext)
    67      {
    68          _application = ((ApplicationFactory)FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY))
    69                  .getApplication();
    70          _renderKitFactory = (RenderKitFactory) FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY);
    71          _externalContext = externalContext;
    72          FacesContext.setCurrentInstance(this);  //protected method, therefore must be called from here
    73      }
    74
    75      //~ Methods ------------------------------------------------------------------------------------
    76
    77      public ExternalContext getExternalContext()
    78      {
    79          if (_released) {
    80              throw new IllegalStateException("FacesContext already released");
    81          }
    82          return (ExternalContext)_externalContext;
    83      }
    84
    85      public FacesMessage.Severity getMaximumSeverity()
    86      {
    87          if (_released) {
    88              throw new IllegalStateException("FacesContext already released");
    89          }
    90          return _maximumSeverity;
    91      }
    92
    93      public Iterator getMessages()
    94      {
    95          if (_released) {
    96              throw new IllegalStateException("FacesContext already released");
    97          }
    98          return (_messages != null) ? _messages.iterator() : Collections.EMPTY_LIST.iterator();
    99      }
    100
    101     public Application getApplication()
    102     {
    103         if (_released) {
    104             throw new IllegalStateException("FacesContext already released");
    105         }
    106
    107         return _application;
    108     }
    109
    110     public Iterator getClientIdsWithMessages()
    111     {
    112         if (_released) {
    113             throw new IllegalStateException("FacesContext already released");
    114         }
    115         if (_messages == null || _messages.isEmpty())
    116         {
    117             return NullIterator.instance();
    118         }
    119
    120         final Set uniqueClientIds = new LinkedHashSet(_messageClientIds);
    121         return uniqueClientIds.iterator();
    122     }
    123
    124     public Iterator getMessages(String clientId)
    125     {
    126         if (_released) {
    127             throw new IllegalStateException("FacesContext already released");
    128         }
    129         if (_messages == null)
    130         {
    131             return NullIterator.instance();
    132         }
    133
    134         List lst = new ArrayList();
    135         for (int i = 0; i < _messages.size(); i++)
    136         {
    137             Object savedClientId = _messageClientIds.get(i);
    138             if (clientId == null)
    139             {
    140                 if (savedClientId == null) lst.add(_messages.get(i));
    141             }
    142             else
    143             {
    144                 if (clientId.equals(savedClientId)) lst.add(_messages.get(i));
    145             }
    146         }
    147         return lst.iterator();
    148     }
    149
    150     public RenderKit getRenderKit()
    151     {
    152         if (getViewRoot() == null)
    153         {
    154             return null;
    155         }
    156
    157         String renderKitId = getViewRoot().getRenderKitId();
    158
    159         if (renderKitId == null)
    160         {
    161             return null;
    162         }
    163
    164         return _renderKitFactory.getRenderKit(this, renderKitId);
    165     }
    166
    167     public boolean getRenderResponse()
    168     {
    169         if (_released) {
    170             throw new IllegalStateException("FacesContext already released");
    171         }
    172         return _renderResponse;
    173     }
    174
    175     public boolean getResponseComplete()
    176     {
    177         if (_released) {
    178             throw new IllegalStateException("FacesContext already released");
    179         }
    180         return _responseComplete;
    181     }
    182
    183     public void setResponseStream(ResponseStream responseStream)
    184     {
    185         if (_released) {
    186             throw new IllegalStateException("FacesContext already released");
    187         }
    188         if (responseStream == null)
    189         {
    190             throw new NullPointerException("responseStream");
    191         }
    192         _responseStream = responseStream;
    193     }
    194
    195     public ResponseStream getResponseStream()
    196     {
    197         if (_released) {
    198             throw new IllegalStateException("FacesContext already released");
    199         }
    200         return _responseStream;
    201     }
    202
    203     public void setResponseWriter(ResponseWriter responseWriter)
    204     {
    205         if (_released) {
    206             throw new IllegalStateException("FacesContext already released");
    207         }
    208         if (responseWriter == null)
    209         {
    210             throw new NullPointerException("responseWriter");
    211         }
    212         _responseWriter = responseWriter;
    213     }
    214
    215     public ResponseWriter getResponseWriter()
    216     {
    217         if (_released) {
    218             throw new IllegalStateException("FacesContext already released");
    219         }
    220         return _responseWriter;
    221     }
    222
    223     public void setViewRoot(UIViewRoot viewRoot)
    224     {
    225         if (_released) {
    226             throw new IllegalStateException("FacesContext already released");
    227         }
    228         if (viewRoot == null)
    229         {
    230             throw new NullPointerException("viewRoot");
    231         }
    232         _viewRoot = viewRoot;
    233     }
    234
    235     public UIViewRoot getViewRoot()
    236     {
    237         if (_released) {
    238             throw new IllegalStateException("FacesContext already released");
    239         }
    240         return _viewRoot;
    241     }
    242
    243     public void addMessage(String clientId, FacesMessage message)
    244     {
    245         if (_released) {
    246             throw new IllegalStateException("FacesContext already released");
    247         }
    248         if (message == null)
    249         {
    250             throw new NullPointerException("message");
    251         }
    252
    253         if (_messages == null)
    254         {
    255             _messages             = new ArrayList();
    256             _messageClientIds     = new ArrayList();
    257         }
    258         _messages.add(message);
    259         _messageClientIds.add((clientId != null) ? clientId : null);
    260         FacesMessage.Severity serSeverity =  message.getSeverity();
    261         if (serSeverity != null) {
    262             if (_maximumSeverity == null)
    263             {
    264                 _maximumSeverity = serSeverity;
    265             }
    266             else if (serSeverity.compareTo(_maximumSeverity) > 0)
    267             {
    268                 _maximumSeverity = serSeverity;
    269             }
    270         }
    271     }
    272
    273     public boolean isReleased()
    274     {
    275        return _released;
    276     }
    277
    278     public void release()
    279     {
    280         if (_released) {
    281             throw new IllegalStateException("FacesContext already released");
    282         }
    283         if (_externalContext != null)
    284         {
    285             _externalContext.release();
    286             _externalContext = null;
    287         }
    288
    289         _messageClientIds     = null;
    290         _messages             = null;
    291         _application          = null;
    292         _responseStream       = null;
    293         _responseWriter       = null;
    294         _viewRoot             = null;
    295         _maximumSeverity      = null;
    296
    297         _released             = true;
    298         FacesContext.setCurrentInstance(null);
    299     }
    300
    301     public void renderResponse()
    302     {
    303         if (_released) {
    304             throw new IllegalStateException("FacesContext already released");
    305         }
    306         _renderResponse = true;
    307     }
    308
    309     public void responseComplete()
    310     {
    311         if (_released) {
    312             throw new IllegalStateException("FacesContext already released");
    313         }
    314         _responseComplete = true;
    315     }
    316
    317     // Portlet need to do this to change from ActionRequest/Response to
    318     // RenderRequest/Response
    319     public void setExternalContext(ServletExternalContextImpl extContext)
    320     {
    321         _externalContext = extContext;
    322         FacesContext.setCurrentInstance(this); //TODO: figure out if I really need to do this
    323     }
    324 }