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;
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, this method must copy its locale
100                     // and renderKitId to this new view root
101                     newViewRoot.setLocale(oldViewRoot.getLocale());
102                     newViewRoot.setRenderKitId(oldViewRoot.getRenderKitId());
103                 }
104                 
105                 // TODO: VALIDATE - The spec is silent on the following line, but I feel bad if I don't set it
106                 newViewRoot.setViewId(viewId);
107     
108                 return newViewRoot;
109             }
110             catch (InvalidViewIdException e)
111             {
112                 // If no viewId could be identified, or the viewId is exactly equal to the servlet mapping, 
113                 // send the response error code SC_NOT_FOUND with a suitable message to the client.
114                 sendSourceNotFound(context, e.getMessage());
115                 
116                 // TODO: VALIDATE - Spec is silent on the return value when an error was sent
117                 return null;
118             }
119         }
120     }
121     
122     /**
123      * Calculates the effective view identifier for the specified raw view identifier.
124      * 
125      * @param context le current FacesContext
126      * @param viewId the raw view identifier
127      * 
128      * @return the effective view identifier
129      */
130     protected String calculateViewId(FacesContext context, String viewId)
131     {
132         if (viewId != null)
133         {
134             throw new InvalidViewIdException();
135         }
136         return null;
137     }
138     
139     protected void sendSourceNotFound(FacesContext context, String message)
140     {
141         HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
142         try
143         {
144             context.responseComplete();
145             response.sendError(HttpServletResponse.SC_NOT_FOUND, message);
146         }
147         catch (IOException ioe)
148         {
149             throw new FacesException(ioe);
150         }
151     }
152     
153     /**
154      * Hook the passed instance on UIViewRoot, storing into facesContext attribute map,
155      * so the next call to createView() will return that value.
156      * 
157      * @param context
158      * @param root
159      */
160     public static void hookCreateView(FacesContext context, UIViewRoot root)
161     {
162         context.getAttributes().put(DUMMY_VIEW_CREATE_HOOK, root);
163     }
164 
165     /**
166      * Hook the passed instance on UIViewRoot, storing into facesContext attribute map,
167      * so the next call to createView() will return that value.
168      * 
169      * @param context
170      * @param root
171      */
172     public static void hookRestoreView(FacesContext context, UIViewRoot root)
173     {
174         context.getAttributes().put(DUMMY_VIEW_RESTORE_HOOK, root);
175     }
176 
177     @Override
178     public BeanInfo getComponentMetadata(FacesContext context,
179             Resource componentResource)
180     {
181         return null;
182     }
183 
184     @Override
185     public Resource getScriptComponentResource(FacesContext context,
186             Resource componentResource)
187     {
188         return null;
189     }
190 
191     @Override
192     public StateManagementStrategy getStateManagementStrategy(
193             FacesContext context, String viewId)
194     {
195         return null;
196     }
197 
198     @Override
199     public ViewMetadata getViewMetadata(FacesContext context, String viewId)
200     {
201         return null;
202     }
203 
204     @Override
205     public void renderView(FacesContext context, UIViewRoot view)
206             throws IOException
207     {
208         checkNull(context, "context");
209         checkNull(view, "view");
210         
211         ExternalContext externalContext = context.getExternalContext();
212         ResponseWriter responseWriter = context.getResponseWriter();
213         if (responseWriter == null)
214         {
215             RenderKitFactory renderFactory = (RenderKitFactory) FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY);
216             RenderKit renderKit = renderFactory.getRenderKit(context, view.getRenderKitId());
217 
218             responseWriter = renderKit.createResponseWriter(externalContext.getResponseOutputWriter(), 
219                     null, externalContext.getRequestCharacterEncoding());
220             context.setResponseWriter(responseWriter);
221         }
222 
223         // Now we actually render the document
224         // Call startDocument() on the ResponseWriter.
225         responseWriter.startDocument();
226 
227         view.encodeAll(context);
228         
229         // Call endDocument() on the ResponseWriter
230         responseWriter.endDocument();
231     
232         responseWriter.flush();
233     }
234 
235     @Override
236     public UIViewRoot restoreView(FacesContext context, String viewId)
237     {
238         checkNull(context, "context");
239         //checkNull(viewId, "viewId");
240 
241         if (context.getAttributes().containsKey(DUMMY_VIEW_RESTORE_HOOK))
242         {
243             UIViewRoot root = (UIViewRoot) context.getAttributes().remove(DUMMY_VIEW_RESTORE_HOOK);
244             root.setViewId(viewId);
245             return root;
246         }
247         else
248         {
249             Application application = context.getApplication();
250             
251             ViewHandler applicationViewHandler = application.getViewHandler();
252             
253             String renderKitId = applicationViewHandler.calculateRenderKitId(context);
254     
255             UIViewRoot viewRoot = application.getStateManager().restoreView(context, viewId, renderKitId);
256     
257             return viewRoot;
258         }
259     }
260     
261     protected void checkNull(final Object o, final String param)
262     {
263         if (o == null)
264         {
265             throw new NullPointerException(param + " can not be null.");
266         }
267     }
268 }