001/* 002 * (C) Copyright 2006-2009 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * 018 * $Id$ 019 */ 020 021package org.nuxeo.ecm.platform.error.web; 022 023import java.io.PrintWriter; 024import java.io.StringWriter; 025 026import javax.ws.rs.GET; 027import javax.ws.rs.Path; 028import javax.ws.rs.Produces; 029import javax.ws.rs.core.MediaType; 030import javax.ws.rs.core.Response; 031 032import org.nuxeo.ecm.core.api.DocumentSecurityException; 033import org.nuxeo.ecm.core.api.NuxeoException; 034import org.nuxeo.ecm.webengine.model.WebObject; 035import org.nuxeo.ecm.webengine.model.impl.ModuleRoot; 036 037@Path("/error") 038@WebObject(type = "error") 039@Produces("text/html; charset=UTF-8") 040public class WebengineError extends ModuleRoot { 041 042 /** 043 * Default view 044 */ 045 @GET 046 public Object doGet() { 047 return getView("index"); 048 } 049 050 @Path("nuxeoException") 051 public Object getNuxeoException() { 052 throw new NuxeoException("Nuxeo exception"); 053 } 054 055 @Path("checkedError") 056 public Object getCheckedError() throws Exception { 057 throw new Exception("CheckedError in webengine"); 058 } 059 060 @Path("uncheckedError") 061 public Object getUncheckedError() { 062 throw new NullPointerException("UncheckedError in webengine"); 063 } 064 065 @Path("securityError") 066 public Object getSecurityError() throws DocumentSecurityException { 067 throw new DocumentSecurityException("Security error in webengine"); 068 } 069 070 public Object handleError(Throwable t) { 071 StringWriter sw = new StringWriter(); 072 PrintWriter pw = new PrintWriter(sw); 073 pw.println("<html>"); 074 pw.println("<head><title>WebEbgine Error Test</title></head>"); 075 pw.println("<body>"); 076 pw.println("WEBENGINE HANDLED ERROR: "); 077 t.printStackTrace(pw); 078 pw.println("</body>"); 079 pw.println("</html>"); 080 pw.close(); 081 return Response.status(500).type(MediaType.TEXT_HTML_TYPE).entity(sw.toString()).build(); 082 } 083 084}