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.webapp.filter;
20  
21  import javax.faces.FacesException;
22  import javax.faces.context.ExternalContext;
23  import javax.faces.context.FacesContext;
24  import javax.faces.event.PhaseEvent;
25  import javax.faces.event.PhaseId;
26  import javax.faces.event.PhaseListener;
27  import javax.servlet.ServletContext;
28  import javax.servlet.http.HttpServletRequest;
29  import javax.servlet.http.HttpServletResponse;
30  
31  import org.apache.commons.logging.Log;
32  import org.apache.commons.logging.LogFactory;
33  import org.apache.myfaces.renderkit.html.util.AddResource;
34  import org.apache.myfaces.renderkit.html.util.AddResourceFactory;
35  import org.apache.myfaces.tomahawk.util.ExternalContextUtils;
36  
37  /**
38   * This listener is used for serve resources, as a replacement of 
39   * ExtensionsFilter serve resources feature.
40   * <p>
41   * The idea is map FacesServlet to org.apache.myfaces.RESOURCE_VIRTUAL_PATH
42   * (Default is "/faces/myFacesExtensionResource), so this
43   * listener can receive the request.
44   * </p>
45   * 
46   * @author Martin Marinschek (latest modification by $Author: lu4242 $)
47   * @version $Revision: 685725 $ $Date: 2008-08-13 18:14:31 -0500 (Wed, 13 Aug 2008) $
48   */
49  public class ServeResourcePhaseListener implements PhaseListener {
50  
51      private Log log = LogFactory.getLog(ServeResourcePhaseListener.class);
52  
53      public static final String DOFILTER_CALLED = "org.apache.myfaces.component.html.util.ExtensionFilter.doFilterCalled";
54  
55      public void afterPhase(PhaseEvent event) {
56      }
57  
58      public void beforePhase(PhaseEvent event) {
59          if(event.getPhaseId()==PhaseId.RESTORE_VIEW || event.getPhaseId()==PhaseId.RENDER_RESPONSE) {
60  
61              FacesContext fc = event.getFacesContext();
62              ExternalContext externalContext = event.getFacesContext().getExternalContext();
63  
64              if(externalContext.getRequestMap().get(DOFILTER_CALLED)!=null)
65              {
66                  //we have already been called (before-restore-view, and we are now in render-response),
67                  // no need to do everything again...
68                  return;
69              }
70  
71              externalContext.getRequestMap().put(DOFILTER_CALLED,"true");
72  
73  
74              //Use ExternalContextUtils to find if this is a portled request
75              //if(externalContext.getRequest() instanceof PortletRequest) {            
76              if(ExternalContextUtils.getRequestType(externalContext).isPortlet()) {
77                  //we are in portlet-world! in portlet 1.0 (JSR-168), we cannot do anything here, but
78                  //TODO in portlet 2.0 (JSR-286), we will write the resource to the stream here if we
79                  //get a resource-request (resource-requests are only available in 286)
80                  if(log.isDebugEnabled()) {
81                      log.debug("We are in portlet-space, but we cannot do anything here in JSR-168 - " +
82                              "for resource-serving, our resource-servlet has to be registered.");
83                  }
84              }
85              else if(externalContext.getResponse() instanceof HttpServletResponse) {
86  
87                  HttpServletResponse response = (HttpServletResponse) fc.getExternalContext().getResponse();
88                  HttpServletRequest request = (HttpServletRequest) fc.getExternalContext().getRequest();
89                  ServletContext context = (ServletContext) fc.getExternalContext().getContext();
90  
91                  // Serve resources
92                  AddResource addResource;
93  
94                  try
95                  {
96                      addResource= AddResourceFactory.getInstance(request, context);
97                      if( addResource.isResourceUri(context, request ) ){
98                          addResource.serveResource(context, request, response);
99                          event.getFacesContext().responseComplete();
100                         return;
101                     }
102                 }
103                 catch(Throwable th)
104                 {
105                     log.error("Exception wile retrieving addResource",th);
106                     throw new FacesException(th);
107                 }
108             }
109             else {
110                 if(log.isDebugEnabled()) {
111                     log.debug("Response of type : "+(
112                             externalContext.getResponse()==null?"null":externalContext.getResponse().getClass().getName())+" not handled so far.");
113                 }
114             }
115         }
116     }
117 
118     public PhaseId getPhaseId() {
119         return PhaseId.ANY_PHASE;
120     }
121 }