Search in sources :

Example 1 with Pipeline

use of org.apache.catalina.Pipeline in project Payara by payara.

the class Request method setContext.

/**
 * Set the Context within which this Request is being processed.  This
 * must be called as soon as the appropriate Context is identified, because
 * it identifies the value to be returned by <code>getContextPath()</code>,
 * and thus enables parsing of the request URI.
 *
 * @param context The newly associated Context
 */
@Override
public void setContext(Context context) {
    this.context = context;
    if (context != null) {
        this.servletContext = context.getServletContext();
        Pipeline p = context.getParent().getPipeline();
        if (p != null) {
            hostValve = p.getBasic();
        }
        try {
            String reqEncoding = this.servletContext.getRequestCharacterEncoding();
            if (reqEncoding != null) {
                setCharacterEncoding(reqEncoding);
            }
            String resEncoding = this.servletContext.getResponseCharacterEncoding();
            if (resEncoding != null) {
                getResponse().getResponse().setCharacterEncoding(resEncoding);
            }
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }
    // START GlassFish 896
    initSessionTracker();
// END GlassFish 896
}
Also used : UnsupportedEncodingException(java.io.UnsupportedEncodingException) Pipeline(org.apache.catalina.Pipeline)

Example 2 with Pipeline

use of org.apache.catalina.Pipeline in project tomcat70 by apache.

the class ValveBase method getObjectNameKeyProperties.

// -------------------- JMX and Registration  --------------------
@Override
public String getObjectNameKeyProperties() {
    StringBuilder name = new StringBuilder("type=Valve");
    Container container = getContainer();
    name.append(MBeanUtils.getContainerKeyProperties(container));
    int seq = 0;
    // Pipeline may not be present in unit testing
    Pipeline p = container.getPipeline();
    if (p != null) {
        for (Valve valve : p.getValves()) {
            // Skip null valves
            if (valve == null) {
                continue;
            }
            // Only compare valves in pipeline until we find this valve
            if (valve == this) {
                break;
            }
            if (valve.getClass() == this.getClass()) {
                // Duplicate valve earlier in pipeline
                // increment sequence number
                seq++;
            }
        }
    }
    if (seq > 0) {
        name.append(",seq=");
        name.append(seq);
    }
    String className = this.getClass().getName();
    int period = className.lastIndexOf('.');
    if (period >= 0) {
        className = className.substring(period + 1);
    }
    name.append(",name=");
    name.append(className);
    return name.toString();
}
Also used : Container(org.apache.catalina.Container) Valve(org.apache.catalina.Valve) Pipeline(org.apache.catalina.Pipeline)

Example 3 with Pipeline

use of org.apache.catalina.Pipeline in project tomcat by apache.

the class ContextConfig method configureStart.

/**
 * Process a "contextConfig" event for this Context.
 */
protected synchronized void configureStart() {
    if (log.isDebugEnabled()) {
        log.debug(sm.getString("contextConfig.start"));
    }
    if (log.isDebugEnabled()) {
        log.debug(sm.getString("contextConfig.xmlSettings", context.getName(), Boolean.valueOf(context.getXmlValidation()), Boolean.valueOf(context.getXmlNamespaceAware())));
    }
    webConfig();
    if (!context.getIgnoreAnnotations()) {
        applicationAnnotationsConfig();
    }
    if (ok) {
        validateSecurityRoles();
    }
    // Configure an authenticator if we need one
    if (ok) {
        authenticatorConfig();
    }
    // Dump the contents of this pipeline if requested
    if (log.isDebugEnabled()) {
        log.debug("Pipeline Configuration:");
        Pipeline pipeline = context.getPipeline();
        Valve[] valves = null;
        if (pipeline != null) {
            valves = pipeline.getValves();
        }
        if (valves != null) {
            for (Valve valve : valves) {
                log.debug("  " + valve.getClass().getName());
            }
        }
        log.debug("======================");
    }
    // Make our application available if no problems were encountered
    if (ok) {
        context.setConfigured(true);
    } else {
        log.error(sm.getString("contextConfig.unavailable"));
        context.setConfigured(false);
    }
}
Also used : Valve(org.apache.catalina.Valve) Pipeline(org.apache.catalina.Pipeline)

Example 4 with Pipeline

use of org.apache.catalina.Pipeline in project tomcat by apache.

the class RewriteValve method invoke.

@Override
public void invoke(Request request, Response response) throws IOException, ServletException {
    if (!getEnabled() || rules == null || rules.length == 0) {
        getNext().invoke(request, response);
        return;
    }
    if (Boolean.TRUE.equals(invoked.get())) {
        try {
            getNext().invoke(request, response);
        } finally {
            invoked.set(null);
        }
        return;
    }
    try {
        Resolver resolver = new ResolverImpl(request);
        invoked.set(Boolean.TRUE);
        // As long as MB isn't a char sequence or affiliated, this has to be
        // converted to a string
        Charset uriCharset = request.getConnector().getURICharset();
        String originalQueryStringEncoded = request.getQueryString();
        MessageBytes urlMB = context ? request.getRequestPathMB() : request.getDecodedRequestURIMB();
        urlMB.toChars();
        CharSequence urlDecoded = urlMB.getCharChunk();
        CharSequence host = request.getServerName();
        boolean rewritten = false;
        boolean done = false;
        boolean qsa = false;
        boolean qsd = false;
        for (int i = 0; i < rules.length; i++) {
            RewriteRule rule = rules[i];
            CharSequence test = (rule.isHost()) ? host : urlDecoded;
            CharSequence newtest = rule.evaluate(test, resolver);
            if (newtest != null && !test.equals(newtest.toString())) {
                if (containerLog.isDebugEnabled()) {
                    containerLog.debug("Rewrote " + test + " as " + newtest + " with rule pattern " + rule.getPatternString());
                }
                if (rule.isHost()) {
                    host = newtest;
                } else {
                    urlDecoded = newtest;
                }
                rewritten = true;
            }
            // Check QSA before the final reply
            if (!qsa && newtest != null && rule.isQsappend()) {
                qsa = true;
            }
            if (!qsa && newtest != null && rule.isQsdiscard()) {
                qsd = true;
            }
            // - forbidden
            if (rule.isForbidden() && newtest != null) {
                response.sendError(HttpServletResponse.SC_FORBIDDEN);
                done = true;
                break;
            }
            // - gone
            if (rule.isGone() && newtest != null) {
                response.sendError(HttpServletResponse.SC_GONE);
                done = true;
                break;
            }
            // - redirect (code)
            if (rule.isRedirect() && newtest != null) {
                // Append the query string to the url if there is one and it
                // hasn't been rewritten
                String urlStringDecoded = urlDecoded.toString();
                int index = urlStringDecoded.indexOf('?');
                String rewrittenQueryStringDecoded;
                if (index == -1) {
                    rewrittenQueryStringDecoded = null;
                } else {
                    rewrittenQueryStringDecoded = urlStringDecoded.substring(index + 1);
                    urlStringDecoded = urlStringDecoded.substring(0, index);
                }
                StringBuilder urlStringEncoded = new StringBuilder(URLEncoder.DEFAULT.encode(urlStringDecoded, uriCharset));
                if (!qsd && originalQueryStringEncoded != null && originalQueryStringEncoded.length() > 0) {
                    if (rewrittenQueryStringDecoded == null) {
                        urlStringEncoded.append('?');
                        urlStringEncoded.append(originalQueryStringEncoded);
                    } else {
                        if (qsa) {
                            // if qsa is specified append the query
                            urlStringEncoded.append('?');
                            urlStringEncoded.append(URLEncoder.QUERY.encode(rewrittenQueryStringDecoded, uriCharset));
                            urlStringEncoded.append('&');
                            urlStringEncoded.append(originalQueryStringEncoded);
                        } else if (index == urlStringEncoded.length() - 1) {
                            // if the ? is the last character delete it, its only purpose was to
                            // prevent the rewrite module from appending the query string
                            urlStringEncoded.deleteCharAt(index);
                        } else {
                            urlStringEncoded.append('?');
                            urlStringEncoded.append(URLEncoder.QUERY.encode(rewrittenQueryStringDecoded, uriCharset));
                        }
                    }
                } else if (rewrittenQueryStringDecoded != null) {
                    urlStringEncoded.append('?');
                    urlStringEncoded.append(URLEncoder.QUERY.encode(rewrittenQueryStringDecoded, uriCharset));
                }
                // 3. the url isn't absolute
                if (context && urlStringEncoded.charAt(0) == '/' && !UriUtil.hasScheme(urlStringEncoded)) {
                    urlStringEncoded.insert(0, request.getContext().getEncodedPath());
                }
                if (rule.isNoescape()) {
                    response.sendRedirect(UDecoder.URLDecode(urlStringEncoded.toString(), uriCharset));
                } else {
                    response.sendRedirect(urlStringEncoded.toString());
                }
                response.setStatus(rule.getRedirectCode());
                done = true;
                break;
            }
            // - cookie
            if (rule.isCookie() && newtest != null) {
                Cookie cookie = new Cookie(rule.getCookieName(), rule.getCookieResult());
                cookie.setDomain(rule.getCookieDomain());
                cookie.setMaxAge(rule.getCookieLifetime());
                cookie.setPath(rule.getCookiePath());
                cookie.setSecure(rule.isCookieSecure());
                cookie.setHttpOnly(rule.isCookieHttpOnly());
                response.addCookie(cookie);
            }
            // - env (note: this sets a request attribute)
            if (rule.isEnv() && newtest != null) {
                for (int j = 0; j < rule.getEnvSize(); j++) {
                    request.setAttribute(rule.getEnvName(j), rule.getEnvResult(j));
                }
            }
            // to do that)
            if (rule.isType() && newtest != null) {
                request.setContentType(rule.getTypeValue());
            }
            // - chain (skip remaining chained rules if this one does not match)
            if (rule.isChain() && newtest == null) {
                for (int j = i; j < rules.length; j++) {
                    if (!rules[j].isChain()) {
                        i = j;
                        break;
                    }
                }
                continue;
            }
            // - last (stop rewriting here)
            if (rule.isLast() && newtest != null) {
                break;
            }
            // - next (redo again)
            if (rule.isNext() && newtest != null) {
                i = 0;
                continue;
            }
            // - skip (n rules)
            if (newtest != null) {
                i += rule.getSkip();
            }
        }
        if (rewritten) {
            if (!done) {
                // See if we need to replace the query string
                String urlStringDecoded = urlDecoded.toString();
                String queryStringDecoded = null;
                int queryIndex = urlStringDecoded.indexOf('?');
                if (queryIndex != -1) {
                    queryStringDecoded = urlStringDecoded.substring(queryIndex + 1);
                    urlStringDecoded = urlStringDecoded.substring(0, queryIndex);
                }
                // Save the current context path before re-writing starts
                String contextPath = null;
                if (context) {
                    contextPath = request.getContextPath();
                }
                // Populated the encoded (i.e. undecoded) requestURI
                request.getCoyoteRequest().requestURI().setString(null);
                CharChunk chunk = request.getCoyoteRequest().requestURI().getCharChunk();
                chunk.recycle();
                if (context) {
                    // This is neither decoded nor normalized
                    chunk.append(contextPath);
                }
                chunk.append(URLEncoder.DEFAULT.encode(urlStringDecoded, uriCharset));
                request.getCoyoteRequest().requestURI().toChars();
                // Decoded and normalized URI
                // Rewriting may have denormalized the URL
                urlStringDecoded = RequestUtil.normalize(urlStringDecoded);
                request.getCoyoteRequest().decodedURI().setString(null);
                chunk = request.getCoyoteRequest().decodedURI().getCharChunk();
                chunk.recycle();
                if (context) {
                    // This is decoded and normalized
                    chunk.append(request.getServletContext().getContextPath());
                }
                chunk.append(urlStringDecoded);
                request.getCoyoteRequest().decodedURI().toChars();
                // Set the new Query if there is one
                if (queryStringDecoded != null) {
                    request.getCoyoteRequest().queryString().setString(null);
                    chunk = request.getCoyoteRequest().queryString().getCharChunk();
                    chunk.recycle();
                    chunk.append(URLEncoder.QUERY.encode(queryStringDecoded, uriCharset));
                    if (qsa && originalQueryStringEncoded != null && originalQueryStringEncoded.length() > 0) {
                        chunk.append('&');
                        chunk.append(originalQueryStringEncoded);
                    }
                    if (!chunk.isNull()) {
                        request.getCoyoteRequest().queryString().toChars();
                    }
                }
                // Set the new host if it changed
                if (!host.equals(request.getServerName())) {
                    request.getCoyoteRequest().serverName().setString(null);
                    chunk = request.getCoyoteRequest().serverName().getCharChunk();
                    chunk.recycle();
                    chunk.append(host.toString());
                    request.getCoyoteRequest().serverName().toChars();
                }
                request.getMappingData().recycle();
                // Reinvoke the whole request recursively
                Connector connector = request.getConnector();
                try {
                    if (!connector.getProtocolHandler().getAdapter().prepare(request.getCoyoteRequest(), response.getCoyoteResponse())) {
                        return;
                    }
                } catch (Exception e) {
                // This doesn't actually happen in the Catalina adapter implementation
                }
                Pipeline pipeline = connector.getService().getContainer().getPipeline();
                request.setAsyncSupported(pipeline.isAsyncSupported());
                pipeline.getFirst().invoke(request, response);
            }
        } else {
            getNext().invoke(request, response);
        }
    } finally {
        invoked.set(null);
    }
}
Also used : Cookie(jakarta.servlet.http.Cookie) Connector(org.apache.catalina.connector.Connector) Charset(java.nio.charset.Charset) MessageBytes(org.apache.tomcat.util.buf.MessageBytes) ServletException(jakarta.servlet.ServletException) LifecycleException(org.apache.catalina.LifecycleException) IOException(java.io.IOException) Pipeline(org.apache.catalina.Pipeline) CharChunk(org.apache.tomcat.util.buf.CharChunk)

Example 5 with Pipeline

use of org.apache.catalina.Pipeline in project tomcat by apache.

the class ValveBase method getObjectNameKeyProperties.

// -------------------- JMX and Registration  --------------------
@Override
public String getObjectNameKeyProperties() {
    StringBuilder name = new StringBuilder("type=Valve");
    Container container = getContainer();
    name.append(container.getMBeanKeyProperties());
    int seq = 0;
    // Pipeline may not be present in unit testing
    Pipeline p = container.getPipeline();
    if (p != null) {
        for (Valve valve : p.getValves()) {
            // Skip null valves
            if (valve == null) {
                continue;
            }
            // Only compare valves in pipeline until we find this valve
            if (valve == this) {
                break;
            }
            if (valve.getClass() == this.getClass()) {
                // Duplicate valve earlier in pipeline
                // increment sequence number
                seq++;
            }
        }
    }
    if (seq > 0) {
        name.append(",seq=");
        name.append(seq);
    }
    String className = this.getClass().getName();
    int period = className.lastIndexOf('.');
    if (period >= 0) {
        className = className.substring(period + 1);
    }
    name.append(",name=");
    name.append(className);
    return name.toString();
}
Also used : Container(org.apache.catalina.Container) Valve(org.apache.catalina.Valve) Pipeline(org.apache.catalina.Pipeline)

Aggregations

Pipeline (org.apache.catalina.Pipeline)13 Valve (org.apache.catalina.Valve)10 IOException (java.io.IOException)4 LifecycleException (org.apache.catalina.LifecycleException)4 Container (org.apache.catalina.Container)3 Context (org.apache.catalina.Context)2 Lifecycle (org.apache.catalina.Lifecycle)2 Realm (org.apache.catalina.Realm)2 ContainerBase (org.apache.catalina.core.ContainerBase)2 StandardContext (org.apache.catalina.core.StandardContext)2 SecurityConstraint (org.apache.catalina.deploy.SecurityConstraint)2 LoginConfig (org.apache.tomcat.util.descriptor.web.LoginConfig)2 WebBeansContext (org.apache.webbeans.config.WebBeansContext)2 ServletException (jakarta.servlet.ServletException)1 Cookie (jakarta.servlet.http.Cookie)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 FileWriter (java.io.FileWriter)1 InputStream (java.io.InputStream)1