/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.sling.servlets;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.apache.sling.servlets.helpers.HtmlRendererServlet;
import org.apache.sling.servlets.helpers.JsonRendererServlet;
import org.apache.sling.servlets.helpers.PlainTextRendererServlet;
import org.apache.sling.servlets.helpers.StreamRendererServlet;

/**
 * A SlingSafeMethodsServlet that renders the current Resource as simple HTML
 * 
 * @scr.component immediate="true" metatype="false"
 * @scr.service interface="javax.servlet.Servlet"
 * 
 * @scr.property name="service.description" value="Default GET Servlet"
 * @scr.property name="service.vendor" value="The Apache Software Foundation"
 * 
 * Use this as a default servlet for Sling
 * @scr.property name="sling.servlet.resourceTypes"
 *               value="sling/servlet/default"
 *               
 * Generic handler for all get requests
 * @scr.property name="sling.servlet.methods" value="GET"
 */
public class DefaultGetServlet extends SlingSafeMethodsServlet {

    private static final long serialVersionUID = -5815904221043005085L;

    private Map<String, Servlet> rendererMap = new HashMap<String, Servlet>();

    private Servlet streamerServlet;

    @Override
    public void init() throws ServletException {
        super.init();

        // Register renderer servlets
        setupServlet(rendererMap, HtmlRendererServlet.EXT_HTML,
            new HtmlRendererServlet());
        setupServlet(rendererMap, PlainTextRendererServlet.EXT_TXT,
            new PlainTextRendererServlet());
        setupServlet(rendererMap, JsonRendererServlet.EXT_JSON,
            new JsonRendererServlet());
        setupServlet(rendererMap, StreamRendererServlet.EXT_RES,
            new StreamRendererServlet());

        // use the servlet for rendering StreamRendererServlet.EXT_RES as the
        // streamer servlet
        streamerServlet = rendererMap.get(StreamRendererServlet.EXT_RES);
    }

    @Override
    protected void doGet(SlingHttpServletRequest request,
            SlingHttpServletResponse response) throws ServletException,
            IOException {

        // cannot handle the request for missing resources
        if (ResourceUtil.isNonExistingResource(request.getResource())) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND,
                "Resource not found at path " + request.getResource().getPath());
            return;
        }

        Servlet rendererServlet;
        String ext = request.getRequestPathInfo().getExtension();
        if (ext == null) {
            rendererServlet = streamerServlet;
        } else {
            rendererServlet = rendererMap.get(ext);
        }

        // fail if we should not just stream or we cannot support the ext.
        if (rendererServlet == null) {
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                "No renderer for extension='" + ext + "'");
            return;
        }

        rendererServlet.service(request, response);
    }

    @Override
    public void destroy() {

        for (Servlet servlet : rendererMap.values()) {
            try {
                servlet.destroy();
            } catch (Throwable t) {
                // TODO: log
            }
        }

        streamerServlet = null;
        rendererMap.clear();

        super.destroy();
    }

    private void setupServlet(Map<String, Servlet> rendererMap, String key,
            Servlet servlet) {
        try {
            servlet.init(getServletConfig());
            rendererMap.put(key, servlet);
        } catch (Throwable t) {
            // TODO: log
        }
    }
}

@@ -1133,16 +1133,80 @@
 sponse;%0A
+import org.apache.sling.api.resource.ResourceNotFoundException;%0A
 import o
@@ -3163,32 +3163,167 @@
 XT_RES);%0A    %7D%0A%0A
+    /**%0A     * @throws ResourceNotFoundException if the resource of the request is a non%0A     *             existing resource.%0A     */%0A
     @Override%0A  
@@ -3625,108 +3625,60 @@
     
-response.sendError(HttpServletResponse.SC_NOT_FOUND,%0A                %22Resource not found at path %22 +
+throw new ResourceNotFoundException(%0A               
  req
@@ -3705,37 +3705,38 @@
 etPath()
-);%0A            return
+, %22No Resource found%22)
 ;%0A      

Throw ResourceNotFoundException instead of sending 404 itself if the request resource is a non existing resource