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.WebApplicationException;
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.WebException;
035import org.nuxeo.ecm.webengine.model.WebObject;
036import org.nuxeo.ecm.webengine.model.impl.ModuleRoot;
037
038@Path("/error")
039@WebObject(type = "error")
040@Produces("text/html; charset=UTF-8")
041public class WebengineError extends ModuleRoot {
042
043    /**
044     * Default view
045     */
046    @GET
047    public Object doGet() {
048        return getView("index");
049    }
050
051    @Path("webException")
052    public Object getWebException() {
053        throw new WebException("Web exception");
054    }
055
056    @Path("checkedError")
057    public Object getCheckedError() {
058        throw new NuxeoException("CheckedError in webengine");
059    }
060
061    @Path("uncheckedError")
062    public Object getUncheckedError() {
063        throw new NullPointerException("UncheckedError in webengine");
064    }
065
066    @Path("securityError")
067    public Object getSecurityError() throws DocumentSecurityException {
068        throw new DocumentSecurityException("Security error in webengine");
069    }
070
071    public Object handleError(WebApplicationException e) {
072        StringWriter sw = new StringWriter();
073        PrintWriter pw = new PrintWriter(sw);
074        pw.println("<html>");
075        pw.println("<head><title>WebEbgine Error Test</title></head>");
076        pw.println("<body>");
077        pw.println("WEBENGINE HANDLED ERROR: ");
078        e.printStackTrace(pw);
079        pw.println("</body>");
080        pw.println("</html>");
081        pw.close();
082        return Response.status(500).entity(sw.toString()).build();
083    }
084
085}