View Javadoc

1   // 
2   // Copyright (c) 2003, 2004, Caltha - Gajda, Krzewski, Mach, Potempski Sp.J. 
3   // All rights reserved. 
4   // 
5   // Redistribution and use in source and binary forms, with or without modification,  
6   // are permitted provided that the following conditions are met: 
7   //  
8   // * Redistributions of source code must retain the above copyright notice,  
9   //   this list of conditions and the following disclaimer. 
10  // * Redistributions in binary form must reproduce the above copyright notice,  
11  //   this list of conditions and the following disclaimer in the documentation  
12  //   and/or other materials provided with the distribution. 
13  // * Neither the name of the Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.  
14  //   nor the names of its contributors may be used to endorse or promote products  
15  //   derived from this software without specific prior written permission. 
16  // 
17  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"  
18  // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED  
19  // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
20  // IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,  
21  // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,  
22  // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
23  // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
24  // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)  
25  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE  
26  // POSSIBILITY OF SUCH DAMAGE. 
27  //
28  package org.objectledge.web.mvc;
29  
30  import java.io.IOException;
31  import java.io.PrintWriter;
32  
33  import org.objectledge.context.Context;
34  import org.objectledge.pipeline.ErrorHandlingPipeline;
35  import org.objectledge.pipeline.ProcessingException;
36  import org.objectledge.pipeline.Valve;
37  import org.objectledge.utils.StackTrace;
38  import org.objectledge.utils.StringUtils;
39  import org.objectledge.web.HttpContext;
40  
41  /***
42   * Pipeline component for putting the MVC view building results to the response.
43   * 
44   * <p>
45   * The build results are only put into the response if no direct response has been triggered.
46   * </p>
47   * 
48   * @author <a href="mailto:dgajda@caltha.pl">Damian Gajda</a>
49   * @version $Id: MVCResultsValve.java,v 1.9 2005/07/22 17:25:50 pablo Exp $
50   */
51  public class MVCResultsValve 
52      implements Valve
53  {
54  	/***
55  	 * Run view building starting from a view builder chosen in request parameters.
56       * 
57       * @param context used application context 
58       * @throws ProcessingException if the processing fails.
59  	 */
60  	public void process(Context context)
61          throws ProcessingException
62  	{
63  		HttpContext httpContext = HttpContext.getHttpContext(context);
64  		if(!httpContext.getDirectResponse())
65  		{
66              MVCContext mvcContext = MVCContext.getMVCContext(context);
67              String result = mvcContext.getBuildResult();
68  			try
69  			{
70  				if(result == null)
71  				{
72                      Exception e = (Exception)context.
73                          getAttribute(ErrorHandlingPipeline.PIPELINE_EXCEPTION);
74                      if(e != null)
75                      {
76                          result = new StackTrace(e).toString();
77                      }
78                      else
79                      {
80                          result = "no processing result was set in mvcContext "
81                                   +"- check the pipeline configuration";
82                      }
83  				}
84  				httpContext.getResponse().setContentLength(
85  			       	StringUtils.getByteCount(result, httpContext.getEncoding()));
86  				PrintWriter out = httpContext.getPrintWriter();
87  				out.write(result);
88  				out.flush();
89  			}
90              ///CLOVER:OFF
91  			catch(IOException e)
92  			{
93  				throw new ProcessingException("Cannot write the response",e);
94  			}
95              ///CLOVER:ON
96  		}
97  	}
98  }