Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2014 Red Hat, Inc., and individual contributors
   * as indicated by the @author tags.
   *
   * 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 io.undertow.servlet.handlers;
 
 import java.util.List;
 
 
All the information that servlet needs to attach to the exchange.

This is all stored under this class, rather than using individual attachments, as this approach has significant performance advantages.

The ServletInitialHandler also pushed this information to the CURRENT thread local, which allows it to be access even if the request or response have been wrapped with non-compliant wrapper classes.

Author(s):
Stuart Douglas
 
 public class ServletRequestContext {
 
     private static final RuntimePermission GET_CURRENT_REQUEST = new RuntimePermission("io.undertow.servlet.GET_CURRENT_REQUEST");
     private static final RuntimePermission SET_CURRENT_REQUEST = new RuntimePermission("io.undertow.servlet.SET_CURRENT_REQUEST");
 
     private static final ThreadLocal<ServletRequestContextCURRENT = new ThreadLocal<>();
 
     public static void setCurrentRequestContext(ServletRequestContext servletRequestContext) {
         if(System.getSecurityManager() != null) {
             AccessController.checkPermission();
         }
         .set(servletRequestContext);
     }
 
     public static void clearCurrentServletAttachments() {
         if(System.getSecurityManager() != null) {
             AccessController.checkPermission();
         }
         .remove();
     }
 
     public static ServletRequestContext requireCurrent() {
         if(System.getSecurityManager() != null) {
             AccessController.checkPermission();
         }
         ServletRequestContext attachments = .get();
         if (attachments == null) {
             throw ..noRequestActive();
         }
         return attachments;
     }
 
     public static ServletRequestContext current() {
         if(System.getSecurityManager() != null) {
             AccessController.checkPermission();
         }
         return .get();
     }
 
     public static final AttachmentKey<ServletRequestContextATTACHMENT_KEY = AttachmentKey.create(ServletRequestContext.class);
 
     private final Deployment deployment;
     private final HttpServletRequestImpl originalRequest;
     private final HttpServletResponseImpl originalResponse;
     private final ServletPathMatch originalServletPathMatch;
    private ServletChain currentServlet;
    private HttpSessionImpl session;
    private ServletContextImpl currentServletContext;

    
If this is true the request is running inside the context of ServletInitialHandler
    private boolean runningInsideHandler = false;
    private int errorCode = -1;
    private String errorMessage;
    public ServletRequestContext(final Deployment deploymentfinal HttpServletRequestImpl originalRequestfinal HttpServletResponseImpl originalResponsefinal ServletPathMatch originalServletPathMatch) {
        this. = deployment;
        this. = originalRequest;
        this. = originalResponse;
        this. = originalRequest;
        this. = originalResponse;
        this. = originalServletPathMatch;
        this. = deployment.getServletContext();
    }
    public Deployment getDeployment() {
        return ;
    }
    public ServletChain getCurrentServlet() {
        return ;
    }
    public void setCurrentServlet(ServletChain currentServlet) {
        this. = currentServlet;
    }
        return ;
    }
    public void setServletPathMatch(ServletPathMatch servletPathMatch) {
        this. = servletPathMatch;
    }
        return ;
    }
    public void setRequiredConstrains(List<SingleConstraintMatchrequiredConstrains) {
        this. = requiredConstrains;
    }
        return ;
    }
    public void setTransportGuarenteeType(TransportGuaranteeType transportGuarenteeType) {
        this. = transportGuarenteeType;
    }
        return ;
    }
    public void setServletResponse(ServletResponse servletResponse) {
        this. = servletResponse;
    }
    public ServletRequest getServletRequest() {
        return ;
    }
    public void setServletRequest(ServletRequest servletRequest) {
        this. = servletRequest;
    }
    public DispatcherType getDispatcherType() {
        return ;
    }
    public void setDispatcherType(DispatcherType dispatcherType) {
        this. = dispatcherType;
    }
        return ;
    }
        return ;
    }
    public HttpSessionImpl getSession() {
        return ;
    }
    public void setSession(final HttpSessionImpl session) {
        this. = session;
    }
    public HttpServerExchange getExchange() {
        return .getExchange();
    }
        return ;
    }
        return ;
    }
    public void setCurrentServletContext(ServletContextImpl currentServletContext) {
        this. = currentServletContext;
    }
    public boolean displayStackTraces() {
        if (mode == .) {
            return false;
        } else if (mode == .) {
            return true;
        } else {
            InetSocketAddress localAddress = getExchange().getSourceAddress();
            if(localAddress == null) {
                return false;
            }
            InetAddress address = localAddress.getAddress();
            if(address == null) {
                return false;
            }
            if(!address.isLoopbackAddress()) {
                return false;
            }
            return !getExchange().getRequestHeaders().contains(.);
        }
    }
    public void setError(int scString msg) {
        this. = sc;
        this. = msg;
    }
    public int getErrorCode() {
        return ;
    }
    public String getErrorMessage() {
        return ;
    }
    public boolean isRunningInsideHandler() {
        return ;
    }
    public void setRunningInsideHandler(boolean runningInsideHandler) {
        this. = runningInsideHandler;
    }
New to GrepCode? Check out our FAQ X