org.apache.catalina.core.StandardContextValve.java Source code

Java tutorial

Introduction

Here is the source code for org.apache.catalina.core.StandardContextValve.java

Source

/*
 * 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.catalina.core;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;

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

import org.apache.catalina.Wrapper;
import org.apache.catalina.comet.CometEvent;
import org.apache.catalina.connector.Request;
import org.apache.catalina.connector.Response;
import org.apache.catalina.valves.ValveBase;
import org.apache.commons.lang3.StringUtils;
import org.apache.tomcat.util.buf.MessageBytes;

import cn.how2j.lazycat.pojo.Tomcat;
import cn.how2j.lazycat.pojo.WebApp;
import cn.how2j.lazycat.util.CatalinaUtil;
import cn.how2j.lazycat.util.LastVisitManager;
import cn.how2j.lazycat.util.MapperUtil;

/**
 * Valve that implements the default basic behavior for the
 * <code>StandardContext</code> container implementation.
 * <p>
 * <b>USAGE CONSTRAINT</b>: This implementation is likely to be useful only when
 * processing HTTP requests.
 *
 * @author Craig R. McClanahan
 */
final class StandardContextValve extends ValveBase {

    public StandardContextValve() {
        super(true);
    }

    /**
     * The descriptive information related to this implementation.
     */
    private static final String info = "org.apache.catalina.core.StandardContextValve/1.0";

    /**
     * Return descriptive information about this Valve implementation.
     */
    @Override
    public String getInfo() {
        return (info);
    }

    /**
     * Select the appropriate child Wrapper to process this request, based on
     * the specified request URI. If no matching Wrapper can be found, return an
     * appropriate HTTP error.
     *
     * @param request
     *            Request to be processed
     * @param response
     *            Response to be produced
     *
     * @exception IOException
     *                if an input/output error occurred
     * @exception ServletException
     *                if a servlet error occurred
     */
    @Override
    public final void invoke(Request request, Response response) throws IOException, ServletException {

        StringBuffer sb = new StringBuffer();

        Map map = request.getParameterMap();
        Set<String> keySet = map.keySet();
        for (String key : keySet) {
            String[] values = (String[]) map.get(key);
            for (String value : values) {
                sb.append("&" + key + "=" + value);
            }
        }

        String param = "";
        if (0 != sb.length())
            param = "?" + sb.toString().substring(1, sb.toString().length());

        StandardContext context = (StandardContext) this.getContainer();
        StandardHost host = (StandardHost) context.getParent();
        int port = CatalinaUtil.getPort(host);
        List<Tomcat> ts = MapperUtil.tomcatMapper.list();
        for (Tomcat t : ts) {
            if (t.getPort() == port) {

                List<WebApp> ws = MapperUtil.webAppMapper.listByTomcat(t);
                for (WebApp w : ws) {
                    if (w.nameMatch(context)) {
                        LastVisitManager.put(w.getId(), request.getRequestURI() + param);
                    }
                }

            }
        }

        //      StandardHost host = (StandardHost) this.getContainer();
        //      int port= CatalinaUtil.getPort(host);

        // Disallow any direct access to resources under WEB-INF or META-INF
        MessageBytes requestPathMB = request.getRequestPathMB();
        if ((requestPathMB.startsWithIgnoreCase("/META-INF/", 0)) || (requestPathMB.equalsIgnoreCase("/META-INF"))
                || (requestPathMB.startsWithIgnoreCase("/WEB-INF/", 0))
                || (requestPathMB.equalsIgnoreCase("/WEB-INF"))) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }

        // Select the Wrapper to be used for this Request
        Wrapper wrapper = request.getWrapper();
        if (wrapper == null || wrapper.isUnavailable()) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }

        // Acknowledge the request
        try {
            response.sendAcknowledgement();
        } catch (IOException ioe) {
            container.getLogger().error(sm.getString("standardContextValve.acknowledgeException"), ioe);
            request.setAttribute(RequestDispatcher.ERROR_EXCEPTION, ioe);
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            return;
        }

        if (request.isAsyncSupported()) {
            request.setAsyncSupported(wrapper.getPipeline().isAsyncSupported());
        }
        wrapper.getPipeline().getFirst().invoke(request, response);
    }

    /**
     * Select the appropriate child Wrapper to process this request, based on
     * the specified request URI. If no matching Wrapper can be found, return an
     * appropriate HTTP error.
     *
     * @param request
     *            Request to be processed
     * @param response
     *            Response to be produced
     * @param event
     *
     * @exception IOException
     *                if an input/output error occurred
     * @exception ServletException
     *                if a servlet error occurred
     */
    @Override
    public final void event(Request request, Response response, CometEvent event)
            throws IOException, ServletException {

        // Select the Wrapper to be used for this Request
        Wrapper wrapper = request.getWrapper();

        wrapper.getPipeline().getFirst().event(request, response, event);
    }
}