/*
 * 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.felix.httplite.server;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import javax.servlet.http.HttpServletResponse;

import org.apache.felix.httplite.osgi.Logger;
import org.apache.felix.httplite.osgi.ServiceRegistration;
import org.apache.felix.httplite.osgi.ServiceRegistrationHandler;
import org.apache.felix.httplite.servlet.HttpConstants;
import org.apache.felix.httplite.servlet.HttpServletRequestImpl;
import org.apache.felix.httplite.servlet.HttpServletResponseImpl;
import org.osgi.service.http.HttpContext;

/**
 * Handles resource processing.  
 * 
 * Encapsulates the logic in OSGI Service Platform Release 4 Compendium Version 4.2 Section 102.3
 *
 */
public class ResourceHandler implements ServiceRegistrationHandler
{

    private static final String INDEX_HTML = "index.html";
	private final HttpServletRequestImpl m_request;
    private final HttpServletResponseImpl m_response;

    private final HttpContext m_httpContext;
    private final String m_name;
    private final String m_alias;
	private final Logger m_logger;
    private final int m_aliasIndex;

    /**
     * @param req HttpRequest
     * @param res HttpResponse
     * @param resource ServiceRegistration
     * @param logger Log reference
     */
    public ResourceHandler(final HttpServletRequestImpl req, final HttpServletResponseImpl res, final ServiceRegistration resource, final Logger logger)
    {
        if (resource.isServlet())
        {
            throw new IllegalStateException(
                "Invalid state, ResourceHandler constructed with a Servlet.");
        }

        this.m_request = req;
        this.m_response = res;
        this.m_httpContext = resource.getContext();
        this.m_name = resource.getName();
        this.m_alias = resource.getAlias();  
        this.m_aliasIndex = m_alias.length() + 1;
        this.m_logger = logger;
    }

    /* (non-Javadoc)
     * @see org.apache.felix.http.lightweight.osgi.ServiceRegistrationHandler#process(boolean)
     */
    public void handle(final boolean close) throws IOException
    {
        if (!m_request.getMethod().equals(HttpConstants.GET_REQUEST)
            && !m_request.getMethod().equals(HttpConstants.HEAD_REQUEST))
        {

            //POST, PUT, DELETE operations not valid on resources.
        	m_logger.log(Logger.LOG_WARNING, "Ignored client " + m_request.getMethod() + " on static resource.");
            return;
        }

        if (m_httpContext.handleSecurity(m_request, m_response))
        {
            String resourceName = getResourceName(m_request.getRequestURI());
            
            URL resource = null;
            
            if (resourceName.endsWith("/"))
            {
            	m_logger.log(Logger.LOG_DEBUG, "Appending " + INDEX_HTML + " to request " + resourceName);
            	resource = m_httpContext.getResource(resourceName + INDEX_HTML);
            } 
            else 
            {
            	resource = m_httpContext.getResource(resourceName);	
            }
           
            if (resource == null)
            {
            	m_logger.log(Logger.LOG_INFO, "Returning HTTP 404 for request for " + resourceName);
            	m_response.sendError(HttpServletResponse.SC_NOT_FOUND);
            	return;
            }

            InputStream inputStream = resource.openStream();
            m_response.setContentType(m_httpContext.getMimeType(resourceName));

            m_response.writeToOutputStream(inputStream, close);
        }
    }

    /**
     * @param path String
     * @return resource name at given path.
     */
    private String getResourceName(final String path)
    {
        //Ensure that the path begins at root.
        String rpath = path;
        if (!path.startsWith( "/" ))
        {
            rpath = "/" + path;
        }
        
        return m_name + "/" + rpath.substring(m_aliasIndex);
    }
}

@@ -2676,12 +2676,8 @@
 th()
- + 1
 ;%0A  
@@ -4484,84 +4484,8 @@
   %7B%0A
-        //Ensure that the path begins at root.%0A        String rpath = path;%0A
     
@@ -4492,17 +4492,16 @@
     if (
-!
 path.sta
@@ -4515,16 +4515,17 @@
 ( %22/%22 ))
+ 
 %0A       
@@ -4544,25 +4544,52 @@
    r
-path = %22/%22 + path
+eturn  m_name + path.substring(m_aliasIndex)
 ;%0A  
@@ -4587,32 +4587,38 @@
 ndex);%0A        %7D
+ else 
 %0A        %0A      
@@ -4602,33 +4602,38 @@
 %7D else %0A        
-%0A
+%7B%0A    
         return m
@@ -4646,17 +4646,16 @@
 + %22/%22 + 
-r
 path.sub
@@ -4672,20 +4672,37 @@
 liasIndex);%0A
+        %7D       %0A
     %7D%0A%7D%0A

fix regression in resource path resolution code in test cases.