<commit_before>/* Copyright 2007 <NAME>.
 *
 * Licensed 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.ops4j.pax.web.service.jetty.internal;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.mortbay.jetty.HttpConnection;
import org.mortbay.resource.Resource;
import org.osgi.service.http.HttpContext;

class ResourceServlet
    extends HttpServlet
{

    private final HttpContext m_httpContext;
    private final String m_contextName;
    private final String m_alias;
    private final String m_name;

    ResourceServlet( final HttpContext httpContext,
                     final String contextName,
                     final String alias,
                     final String name )
    {
        m_httpContext = httpContext;
        m_contextName = "/" + contextName;
        m_alias = alias;
        if( "/".equals( name ) )
        {
            m_name = "";
        }
        else
        {
            m_name = name;
        }
    }

    protected void doGet( final HttpServletRequest request, final HttpServletResponse response )
        throws ServletException, IOException
    {
        String mapping;
        if( m_contextName.equals( m_alias ) )
        {
            mapping = m_name + request.getRequestURI();
        }
        else
        {
            mapping = request.getRequestURI().replaceFirst( m_contextName, "/" );
            mapping = mapping.replaceFirst( m_alias, m_name );
        }
        final URL url = m_httpContext.getResource( mapping );
        if( url == null )
        {
            response.sendError( HttpServletResponse.SC_NOT_FOUND );
            return;
        }

        final Resource resource = Resource.newResource( url, false );
        if( !resource.exists() )
        {
            response.sendError( HttpServletResponse.SC_NOT_FOUND );
            return;
        }
        if( resource.isDirectory() )
        {
            response.sendError( HttpServletResponse.SC_FORBIDDEN );
            return;
        }

        String mimeType = m_httpContext.getMimeType( mapping );
        if( mimeType == null )
        {
            try
            {
                mimeType = url.openConnection().getContentType();
            }
            catch( IOException ignore )
            {
                // we do not care about such an exception as the fact that we are using also the connection for
                // finding the mime type is just a "nice to have" not an requirement<commit_msg>[PAXWEB-197] read and write eTag to cache resources<commit_after>/* Copyright 2007 <NAME>.
 *
 * Licensed 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.ops4j.pax.web.service.jetty.internal;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.mortbay.jetty.HttpConnection;
import org.mortbay.resource.Resource;
import org.osgi.service.http.HttpContext;

class ResourceServlet
    extends HttpServlet
{

    //header constants
    private static final String IFNONEMATCH_HEADER = "If-None-Match";
    private static final String ETAG = "ETag";

    private final HttpContext m_httpContext;
    private final String m_contextName;
    private final String m_alias;
    private final String m_name;

    ResourceServlet( final HttpContext httpContext,
                     final String contextName,
                     final String alias,
                     final String name )
    {
        m_httpContext = httpContext;
        m_contextName = "/" + contextName;
        m_alias = alias;
        if( "/".equals( name ) )
        {
            m_name = "";
        }
        else
        {
            m_name = name;
        }
    }

    protected void doGet( final HttpServletRequest request, final HttpServletResponse response )
        throws ServletException, IOException
    {
        String mapping;
        if( m_contextName.equals( m_alias ) )
        {
            mapping = m_name + request.getRequestURI();
        }
        else
        {
            mapping = request.getRequestURI().replaceFirst( m_contextName, "/" );
            mapping = mapping.replaceFirst( m_alias, m_name );
        }
        final URL url = m_httpContext.getResource( mapping );
        if( url == null )
        {
            response.sendError( HttpServletResponse.SC_NOT_FOUND );
            return;
        }

        final Resource resource = Resource.newResource( url, false );
        if( !resource.exists() )
        {
            response.sendError( HttpServletResponse.SC_NOT_FOUND );
            return;
        }
        if( resource.isDirectory() )
        {
            response.sendError( HttpServletResponse.SC_FORBIDDEN );
            return;
        }

        //if the request contains an etag and its the same for the resource, we deliver a NOT MODIFIED response
        String eTag = String.valueOf(resource.hashCode());
        if ((request.getHeader(IFNONEMATCH_HEADER) != null) && (eTag.equals(request.getHeader(IFNONEMATCH_HEADER)))) {
            response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
            return;
        }
        //set the etag
        response.setHeader(ETAG, eTag);

        String mimeType = m_httpContext.getMimeType( mapping );
        if( mimeType == null )
        {
            try
            {
                mimeType = url.openConnection().getContentType();
            }
            catch( IOException ignore )
            {
                // we do not care about such an exception as the fact that we are using also the connection for
                // finding the mime type is just a "nice to have" not an requirement
{'fraction_non_alphanumeric': 0.08034433285509325, 'fraction_numerical': 0.0030129124820659973, 'mean_word_length': 2.181652213601095, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1120899', 'n_tokens_mistral': 1827, 'n_tokens_neox': 1786, 'n_words': 553}