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.mc.test.core.mock;
20  
21  import java.beans.BeanInfo;
22  import java.io.IOException;
23  
24  import javax.faces.FacesException;
25  import javax.faces.FactoryFinder;
26  import javax.faces.application.Application;
27  import javax.faces.application.Resource;
28  import javax.faces.application.ViewHandler;
29  import javax.faces.component.UIViewRoot;
30  import javax.faces.context.ExternalContext;
31  import javax.faces.context.FacesContext;
32  import javax.faces.context.ResponseWriter;
33  import javax.faces.render.RenderKit;
34  import javax.faces.render.RenderKitFactory;
35  import javax.faces.view.StateManagementStrategy;
36  import javax.faces.view.ViewDeclarationLanguage;
37  import javax.faces.view.ViewMetadata;
38  import javax.servlet.http.HttpServletResponse;
39  
40  import org.apache.myfaces.shared.application.InvalidViewIdException;
41  
42  /**
43   * It is a vdl that is just provided as fallback, to ensure if
44   * viewHandler.createView(context, null) is called, an instance
45   * is retrieved just like with jsp vdl, and it provides some methods
46   * to hook a view instance for createView() and restoreView() methods.
47   * 
48   * In this way, it is possible to create view instance programatically
49   * and simulate that it is generated by the vdl. 
50   * 
51   * @author Leonardo Uribe
52   *
53   */
54  public class MockDefaultViewDeclarationLanguage extends ViewDeclarationLanguage
55  {
56      public static final String DUMMY_VIEW_CREATE_HOOK = "oam.dummy.create.UIViewRoot";
57      public static final String DUMMY_VIEW_RESTORE_HOOK = "oam.dummy.restore.UIViewRoot";
58  
59      @Override
60      public void buildView(FacesContext context, UIViewRoot view)
61              throws IOException
62      {
63      }
64  
65      @Override
66      public UIViewRoot createView(FacesContext context, String viewId)
67      {
68          if (context.getAttributes().containsKey(DUMMY_VIEW_CREATE_HOOK))
69          {
70              UIViewRoot root = (UIViewRoot) context.getAttributes().remove(DUMMY_VIEW_CREATE_HOOK);
71              Application application = context.getApplication();
72              ViewHandler handler = application.getViewHandler();
73              root.setLocale(handler.calculateLocale(context));
74              root.setRenderKitId(handler.calculateRenderKitId(context));
75              root.setViewId(viewId);
76              return root;
77          }
78          else
79          {
80              try
81              {
82                  viewId = calculateViewId(context, viewId);
83                  
84                  Application application = context.getApplication();
85                  // Create a new UIViewRoot object instance using Application.createComponent(UIViewRoot.COMPONENT_TYPE).
86                  UIViewRoot newViewRoot = (UIViewRoot) application.createComponent(UIViewRoot.COMPONENT_TYPE);
87                  UIViewRoot oldViewRoot = context.getViewRoot();
88                  if (oldViewRoot == null)
89                  {
90                      // If not, this method must call calculateLocale() and calculateRenderKitId(), and store the results
91                      // as the values of the locale and renderKitId, proeprties, respectively, of the newly created
92                      // UIViewRoot.
93                      ViewHandler handler = application.getViewHandler();
94                      newViewRoot.setLocale(handler.calculateLocale(context));
95                      newViewRoot.setRenderKitId(handler.calculateRenderKitId(context));
96                  }
97                  else
98                  {
99                      // If there is an existing UIViewRoot available on the FacesContext, 
100                     //this method must copy its locale
101                     // and renderKitId to this new view root
102                     newViewRoot.setLocale(oldViewRoot.getLocale());
103                     newViewRoot.setRenderKitId(oldViewRoot.getRenderKitId());
104                 }
105                 
106                 // TODO: VALIDATE - The spec is silent on the following line, but I feel bad if I don't set it
107                 newViewRoot.setViewId(viewId);
108     
109                 return newViewRoot;
110             }
111             catch (InvalidViewIdException e)
112             {
113                 // If no viewId could be identified, or the viewId is exactly equal to the servlet mapping, 
114                 // send the response error code SC_NOT_FOUND with a suitable message to the client.
115                 sendSourceNotFound(context, e.getMessage());
116                 
117                 // TODO: VALIDATE - Spec is silent on the return value when an error was sent
118                 return null;
119             }
120         }
121     }
122     
123     /**
124      * Calculates the effective view identifier for the specified raw view identifier.
125      * 
126      * @param context le current FacesContext
127      * @param viewId the raw view identifier
128      * 
129      * @return the effective view identifier
130      */
131     protected String calculateViewId(FacesContext context, String viewId)
132     {
133         if (viewId != null)
134         {
135             throw new InvalidViewIdException();
136         }
137         return null;
138     }
139     
140     protected void sendSourceNotFound(FacesContext context, String message)
141     {
142         HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
143         try
144         {
145             context.responseComplete();
146             response.sendError(HttpServletResponse.SC_NOT_FOUND, message);
147         }
148         catch (IOException ioe)
149         {
150             throw new FacesException(ioe);
151         }
152     }
153     
154     /**
155      * Hook the passed instance on UIViewRoot, storing into facesContext attribute map,
156      * so the next call to createView() will return that value.
157      * 
158      * @param context
159      * @param root
160      */
161     public static void hookCreateView(FacesContext context, UIViewRoot root)
162     {
163         context.getAttributes().put(DUMMY_VIEW_CREATE_HOOK, root);
164     }
165 
166     /**
167      * Hook the passed instance on UIViewRoot, storing into facesContext attribute map,
168      * so the next call to createView() will return that value.
169      * 
170      * @param context
171      * @param root
172      */
173     public static void hookRestoreView(FacesContext context, UIViewRoot root)
174     {
175         context.getAttributes().put(DUMMY_VIEW_RESTORE_HOOK, root);
176     }
177 
178     @Override
179     public BeanInfo getComponentMetadata(FacesContext context,
180             Resource componentResource)
181     {
182         return null;
183     }
184 
185     @Override
186     public Resource getScriptComponentResource(FacesContext context,
187             Resource componentResource)
188     {
189         return null;
190     }
191 
192     @Override
193     public StateManagementStrategy getStateManagementStrategy(
194             FacesContext context, String viewId)
195     {
196         return null;
197     }
198 
199     @Override
200     public ViewMetadata getViewMetadata(FacesContext context, String viewId)
201     {
202         return null;
203     }
204 
205     @Override
206     public void renderView(FacesContext context, UIViewRoot view)
207             throws IOException
208     {
209         checkNull(context, "context");
210         checkNull(view, "view");
211         
212         ExternalContext externalContext = context.getExternalContext();
213         ResponseWriter responseWriter = context.getResponseWriter();
214         if (responseWriter == null)
215         {
216             RenderKitFactory renderFactory = (RenderKitFactory) 
217                 FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY);
218             RenderKit renderKit = renderFactory.getRenderKit(context, view.getRenderKitId());
219 
220             responseWriter = renderKit.createResponseWriter(externalContext.getResponseOutputWriter(), 
221                     null, externalContext.getRequestCharacterEncoding());
222             context.setResponseWriter(responseWriter);
223         }
224 
225         // Now we actually render the document
226         // Call startDocument() on the ResponseWriter.
227         responseWriter.startDocument();
228 
229         view.encodeAll(context);
230         
231         // Call endDocument() on the ResponseWriter
232         responseWriter.endDocument();
233     
234         responseWriter.flush();
235     }
236 
237     @Override
238     public UIViewRoot restoreView(FacesContext context, String viewId)
239     {
240         checkNull(context, "context");
241         //checkNull(viewId, "viewId");
242 
243         if (context.getAttributes().containsKey(DUMMY_VIEW_RESTORE_HOOK))
244         {
245             UIViewRoot root = (UIViewRoot) context.getAttributes().remove(DUMMY_VIEW_RESTORE_HOOK);
246             root.setViewId(viewId);
247             return root;
248         }
249         else
250         {
251             Application application = context.getApplication();
252             
253             ViewHandler applicationViewHandler = application.getViewHandler();
254             
255             String renderKitId = applicationViewHandler.calculateRenderKitId(context);
256     
257             UIViewRoot viewRoot = application.getStateManager().restoreView(context, viewId, renderKitId);
258     
259             return viewRoot;
260         }
261     }
262     
263     protected void checkNull(final Object o, final String param)
264     {
265         if (o == null)
266         {
267             throw new NullPointerException(param + " can not be null.");
268         }
269     }
270 }