001/*
002 * (C) Copyright 2017 Nuxeo (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 *     Florent Guillaume
018 */
019package org.nuxeo.ecm.platform.error.web;
020
021import java.io.IOException;
022
023import javax.servlet.http.HttpServlet;
024import javax.servlet.http.HttpServletRequest;
025import javax.servlet.http.HttpServletResponse;
026
027import org.apache.commons.io.IOUtils;
028
029/**
030 * Simple servlet that throws an error.
031 *
032 * @since 9.2
033 */
034public class ErrorServlet extends HttpServlet {
035
036    private static final long serialVersionUID = 1L;
037
038    @Override
039    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
040        String uri = request.getRequestURI();
041        if (uri.contains("/gos")) {
042            // getting the OutputStream is incompatible with getting the Writer,
043            // which is something the error page generation may want to do
044            response.getOutputStream();
045        }
046        if (uri.contains("/npe")) {
047            throw new NullPointerException("test error");
048        }
049        response.setContentType("text/html");
050        String text = "<html><p>ok</p></html>";
051        IOUtils.write(text, response.getOutputStream(), "UTF-8");
052    }
053
054}