All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ch.qos.logback.access.servlet.TeeHttpServletRequest Maven / Gradle / Ivy

There is a newer version: 1.5.16
Show newest version
/**
 * Logback: the reliable, generic, fast and flexible logging framework.
 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
 *
 * This program and the accompanying materials are dual-licensed under
 * either the terms of the Eclipse Public License v1.0 as published by
 * the Eclipse Foundation
 *
 *   or (per the licensee's choosing)
 *
 * under the terms of the GNU Lesser General Public License version 2.1
 * as published by the Free Software Foundation.
 */
package ch.qos.logback.access.servlet;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

import static ch.qos.logback.access.AccessConstants.LB_INPUT_BUFFER;

/**
 * As the "tee" program on Unix, duplicate the request's input stream.
 *
 * @author Ceki Gülcü
 */
class TeeHttpServletRequest extends HttpServletRequestWrapper {

    private TeeServletInputStream inStream;
    private BufferedReader reader;
    boolean postedParametersMode = false;

    TeeHttpServletRequest(HttpServletRequest request) {
        super(request);
        // we can't access the input stream and access the request parameters
        // at the same time
        if (Util.isFormUrlEncoded(request)) {
            postedParametersMode = true;
        } else {
            inStream = new TeeServletInputStream(request);
            // add the contents of the input buffer as an attribute of the request
            request.setAttribute(LB_INPUT_BUFFER, inStream.getInputBuffer());
            reader = new BufferedReader(new InputStreamReader(inStream));
        }

    }

    byte[] getInputBuffer() {
        if (postedParametersMode) {
            throw new IllegalStateException("Call disallowed in postedParametersMode");
        }
        return inStream.getInputBuffer();
    }

    @Override
    public ServletInputStream getInputStream() throws IOException {
        if (!postedParametersMode) {
            return inStream;
        } else {
            return super.getInputStream();
        }
    }

    //

    @Override
    public BufferedReader getReader() throws IOException {
        if (!postedParametersMode) {
            return reader;
        } else {
            return super.getReader();
        }
    }

    public boolean isPostedParametersMode() {
        return postedParametersMode;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy