Clover coverage report - Code Coverage for tapestry-portlet release 4.0-alpha-2
Coverage timestamp: Thu May 5 2005 10:02:47 EDT
file stats: LOC: 223   Methods: 10
NCLOC: 142   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
PortletExceptionPresenter.java 0% 0% 0% 0%
coverage
 1   
 // Copyright 2005 The Apache Software Foundation
 2   
 //
 3   
 // Licensed under the Apache License, Version 2.0 (the "License");
 4   
 // you may not use this file except in compliance with the License.
 5   
 // You may obtain a copy of the License at
 6   
 //
 7   
 //     http://www.apache.org/licenses/LICENSE-2.0
 8   
 //
 9   
 // Unless required by applicable law or agreed to in writing, software
 10   
 // distributed under the License is distributed on an "AS IS" BASIS,
 11   
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12   
 // See the License for the specific language governing permissions and
 13   
 // limitations under the License.
 14   
 
 15   
 package org.apache.tapestry.portlet;
 16   
 
 17   
 import java.io.CharArrayWriter;
 18   
 import java.io.IOException;
 19   
 import java.io.PrintWriter;
 20   
 
 21   
 import javax.portlet.ActionResponse;
 22   
 
 23   
 import org.apache.hivemind.ApplicationRuntimeException;
 24   
 import org.apache.tapestry.IMarkupWriter;
 25   
 import org.apache.tapestry.IRequestCycle;
 26   
 import org.apache.tapestry.describe.RenderStrategy;
 27   
 import org.apache.tapestry.error.ErrorMessages;
 28   
 import org.apache.tapestry.error.ExceptionPresenter;
 29   
 import org.apache.tapestry.error.RequestExceptionReporter;
 30   
 import org.apache.tapestry.markup.AsciiMarkupFilter;
 31   
 import org.apache.tapestry.markup.MarkupWriterImpl;
 32   
 import org.apache.tapestry.services.ServiceConstants;
 33   
 import org.apache.tapestry.util.ContentType;
 34   
 import org.apache.tapestry.util.exception.ExceptionAnalyzer;
 35   
 import org.apache.tapestry.util.exception.ExceptionDescription;
 36   
 import org.apache.tapestry.util.exception.ExceptionProperty;
 37   
 import org.apache.tapestry.web.WebRequest;
 38   
 import org.apache.tapestry.web.WebResponse;
 39   
 
 40   
 /**
 41   
  * Service used to present a runtime exception to the user. This is very tricky in the Portlet world
 42   
  * because of the split between the action and render requests (much more likely to get an error
 43   
  * during the action request than during the render request, but both are possible).
 44   
  * <p>
 45   
  * During an action request, this code will render the HTML markup for the exception into a buffer
 46   
  * that is stored as persistent attribute in the portal session.
 47   
  * 
 48   
  * @author Howard M. Lewis Ship
 49   
  * @since 4.0
 50   
  */
 51   
 public class PortletExceptionPresenter implements ExceptionPresenter
 52   
 {
 53   
     private PortletRequestGlobals _globals;
 54   
 
 55   
     private RenderStrategy _renderStrategy;
 56   
 
 57   
     private WebRequest _request;
 58   
 
 59   
     private RequestExceptionReporter _requestExceptionReporter;
 60   
 
 61   
     private WebResponse _response;
 62   
 
 63  0
     public void presentException(IRequestCycle cycle, Throwable cause)
 64   
     {
 65  0
         try
 66   
         {
 67  0
             if (_globals.isRenderRequest())
 68  0
                 reportRenderRequestException(cycle, cause);
 69   
             else
 70  0
                 reportActionRequestException(cycle, cause);
 71   
         }
 72   
         catch (Exception ex)
 73   
         {
 74   
             // Worst case scenario. The exception page itself is broken, leaving
 75   
             // us with no option but to write the cause to the output.
 76   
 
 77   
             // Also, write the exception thrown when redendering the exception
 78   
             // page, so that can get fixed as well.
 79   
 
 80  0
             _requestExceptionReporter.reportRequestException(PortletMessages
 81   
                     .errorReportingException(ex), ex);
 82   
 
 83   
             // And throw the exception.
 84   
 
 85  0
             throw new ApplicationRuntimeException(ex.getMessage(), ex);
 86   
         }
 87   
 
 88  0
         _requestExceptionReporter.reportRequestException(ErrorMessages
 89   
                 .unableToProcessClientRequest(cause), cause);
 90   
     }
 91   
 
 92  0
     private void reportActionRequestException(IRequestCycle cycle, Throwable cause)
 93   
     {
 94  0
         CharArrayWriter caw = new CharArrayWriter();
 95  0
         PrintWriter pw = new PrintWriter(caw);
 96   
 
 97  0
         IMarkupWriter writer = new MarkupWriterImpl("text/html", pw, new AsciiMarkupFilter());
 98   
 
 99  0
         writeException(writer, cycle, cause);
 100   
 
 101  0
         writer.close();
 102   
 
 103  0
         String markup = caw.toString();
 104   
 
 105  0
         _request.getSession(true).setAttribute(
 106   
                 PortletConstants.PORTLET_EXCEPTION_MARKUP_ATTRIBUTE,
 107   
                 markup);
 108   
 
 109  0
         ActionResponse response = _globals.getActionResponse();
 110   
 
 111  0
         response.setRenderParameter(ServiceConstants.SERVICE, PortletConstants.EXCEPTION_SERVICE);
 112   
     }
 113   
 
 114  0
     private void reportRenderRequestException(IRequestCycle cycle, Throwable cause)
 115   
             throws IOException
 116   
     {
 117  0
         PrintWriter pw = _response.getPrintWriter(new ContentType("text/html"));
 118   
 
 119  0
         IMarkupWriter writer = new MarkupWriterImpl("text/html", pw, new AsciiMarkupFilter());
 120   
 
 121  0
         writeException(writer, cycle, cause);
 122   
     }
 123   
 
 124  0
     public void setGlobals(PortletRequestGlobals globals)
 125   
     {
 126  0
         _globals = globals;
 127   
     }
 128   
 
 129  0
     public void setRenderStrategy(RenderStrategy renderStrategy)
 130   
     {
 131  0
         _renderStrategy = renderStrategy;
 132   
     }
 133   
 
 134  0
     public void setRequest(WebRequest request)
 135   
     {
 136  0
         _request = request;
 137   
     }
 138   
 
 139  0
     public void setRequestExceptionReporter(RequestExceptionReporter requestExceptionReporter)
 140   
     {
 141  0
         _requestExceptionReporter = requestExceptionReporter;
 142   
     }
 143   
 
 144  0
     public void setResponse(WebResponse response)
 145   
     {
 146  0
         _response = response;
 147   
     }
 148   
 
 149  0
     private void writeException(IMarkupWriter writer, IRequestCycle cycle,
 150   
             ExceptionDescription exception, boolean showStackTrace)
 151   
     {
 152  0
         writer.begin("div");
 153  0
         writer.attribute("class", "portlet-section-header");
 154  0
         writer.print(exception.getExceptionClassName());
 155  0
         writer.end();
 156  0
         writer.println();
 157   
 
 158  0
         writer.begin("div");
 159  0
         writer.attribute("class", "portlet-msg-error");
 160  0
         writer.print(exception.getMessage());
 161  0
         writer.end();
 162  0
         writer.println();
 163   
 
 164  0
         ExceptionProperty[] properties = exception.getProperties();
 165   
 
 166  0
         if (properties.length > 0)
 167   
         {
 168   
 
 169  0
             writer.begin("table");
 170  0
             writer.attribute("class", "portlet-section-subheader");
 171   
 
 172  0
             for (int i = 0; i < properties.length; i++)
 173   
             {
 174  0
                 writer.begin("tr");
 175   
 
 176  0
                 writer.attribute("class", i % 2 == 0 ? "portlet-section-body"
 177   
                         : "portlet-section-alternate");
 178   
 
 179  0
                 writer.begin("th");
 180  0
                 writer.print(properties[i].getName());
 181  0
                 writer.end();
 182  0
                 writer.println();
 183   
 
 184  0
                 writer.begin("td");
 185   
 
 186  0
                 _renderStrategy.renderObject(properties[i].getValue(), writer, cycle);
 187  0
                 writer.end("tr");
 188  0
                 writer.println();
 189   
             }
 190   
 
 191  0
             writer.end();
 192  0
             writer.println();
 193   
         }
 194   
 
 195  0
         if (!showStackTrace)
 196  0
             return;
 197   
 
 198  0
         writer.begin("ul");
 199   
 
 200  0
         String[] trace = exception.getStackTrace();
 201   
 
 202  0
         for (int i = 0; i < trace.length; i++)
 203   
         {
 204  0
             writer.begin("li");
 205  0
             writer.print(trace[i]);
 206  0
             writer.end();
 207  0
             writer.println();
 208   
         }
 209   
 
 210  0
         writer.end();
 211  0
         writer.println();
 212   
 
 213   
     }
 214   
 
 215  0
     private void writeException(IMarkupWriter writer, IRequestCycle cycle, Throwable cause)
 216   
     {
 217  0
         ExceptionDescription[] exceptions = new ExceptionAnalyzer().analyze(cause);
 218   
 
 219  0
         for (int i = 0; i < exceptions.length; i++)
 220  0
             writeException(writer, cycle, exceptions[i], i + 1 == exceptions.length);
 221   
     }
 222   
 
 223   
 }