Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package io.undertow.servlet.util;
  
 
Saved servlet request.

Author(s):
Stuart Douglas
 
 public class SavedRequest implements Serializable {
 
     private static final String SESSION_KEY = SavedRequest.class.getName();
 
     private final byte[] data;
     private final int dataLength;
     private final HttpString method;
     private final String requestUri;
     private final HeaderMap headerMap;
 
     public SavedRequest(byte[] dataint dataLengthHttpString methodString requestUriHeaderMap headerMap) {
         this. = data;
         this. = dataLength;
         this. = method;
         this. = requestUri;
         this. = headerMap;
     }
 
     public static void trySaveRequest(final HttpServerExchange exchange) {
         int maxSize = exchange.getConnection().getUndertowOptions().get(., 16384);
         if (maxSize > 0) {
             //if this request has a body try and cache the response
             if (!exchange.isRequestComplete()) {
                 final long requestContentLength = exchange.getRequestContentLength();
                 if (requestContentLength > maxSize) {
                     ..debugf("Request to %s was to large to save"exchange.getRequestURI());
                     return;//failed to save the request, we just return
                 }
                 //TODO: we should really be used pooled buffers
                 //TODO: we should probably limit the number of saved requests at any given time
                 byte[] buffer = new byte[maxSize];
                 int read = 0;
                 int res = 0;
                 InputStream in = exchange.getInputStream();
                 try {
                     while ((res = in.read(bufferreadbuffer.length - read)) > 0) {
                         read += res;
                         if (read == maxSize) {
                             ..debugf("Request to %s was to large to save"exchange.getRequestURI());
                             return;//failed to save the request, we just return
                         }
                     }
                     HeaderMap headers = new HeaderMap();
                     for(HeaderValues entry : exchange.getRequestHeaders()) {
                         if(entry.getHeaderName().equals(.) ||
                                 entry.getHeaderName().equals(.) ||
                                 entry.getHeaderName().equals(.)) {
                             continue;
                         }
                         headers.putAll(entry.getHeaderName(), entry);
                     }
                     SavedRequest request = new SavedRequest(bufferreadexchange.getRequestMethod(), exchange.getRequestURI(), exchange.getRequestHeaders());
                     final ServletRequestContext sc = exchange.getAttachment(.);
                     HttpSessionImpl session = sc.getCurrentServletContext().getSession(exchangetrue);
                     Session underlyingSession;
                     if(System.getSecurityManager() == null) {
                         underlyingSession = session.getSession();
                     } else {
                         underlyingSession = AccessController.doPrivileged(new HttpSessionImpl.UnwrapSessionAction(session));
                     }
                     underlyingSession.setAttribute(request);
                 } catch (IOException e) {
                     ..ioException(e);
                 }
             }
         }
     }
 
     public static void tryRestoreRequest(final HttpServerExchange exchangeHttpSession session) {
         if(session instanceof HttpSessionImpl) {
 
            Session underlyingSession;
            if(System.getSecurityManager() == null) {
                underlyingSession = ((HttpSessionImplsession).getSession();
            } else {
                underlyingSession = AccessController.doPrivileged(new HttpSessionImpl.UnwrapSessionAction(session));
            }
            SavedRequest request = (SavedRequestunderlyingSession.getAttribute();
            if(request != null) {
                if(request.requestUri.equals(exchange.getRequestURI()) && exchange.isRequestComplete()) {
                    ..debugf("restoring request body for request to %s"request.requestUri);
                    exchange.setRequestMethod(request.method);
                    Connectors.ungetRequestBytes(exchangenew ImmediatePooled<ByteBuffer>(ByteBuffer.wrap(request.data, 0, request.dataLength)));
                    underlyingSession.removeAttribute();
                    //clear the existing header map of everything except the connection header
                    //TODO: are there other headers we should preserve?
                    Iterator<HeaderValuesheaderIterator = exchange.getRequestHeaders().iterator();
                    while (headerIterator.hasNext()) {
                        HeaderValues header = headerIterator.next();
                        if(!header.getHeaderName().equals(.)) {
                            headerIterator.remove();
                        }
                    }
                    for(HeaderValues header : request.headerMap) {
                        exchange.getRequestHeaders().putAll(header.getHeaderName(), header);
                    }
                }
            }
        }
    }
New to GrepCode? Check out our FAQ X