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

com.powsybl.afs.server.io.GzippedRequestWrapper Maven / Gradle / Ivy

/**
 * Copyright (c) 2019, RTE (http://www.rte-france.com)
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package com.powsybl.afs.server.io;

import jakarta.servlet.ReadListener;
import jakarta.servlet.ServletInputStream;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletRequestWrapper;
import java.io.IOException;
import java.util.zip.GZIPInputStream;

/**
 * Wraps a request to gunzip its content.
 *
 * @author Sylvain Leclerc {@literal }
 */
public class GzippedRequestWrapper extends HttpServletRequestWrapper {

    private ServletInputStream in;

    public GzippedRequestWrapper(final HttpServletRequest request) {
        super(request);
    }

    @Override
    public ServletInputStream getInputStream() throws IOException {
        if (in == null) {
            in = new GzippedServletInputStream(getRequest().getInputStream());
        }
        return in;
    }

    private static final class GzippedServletInputStream extends ServletInputStream {

        private final ServletInputStream delegate;
        private final GZIPInputStream gzipIn;

        private GzippedServletInputStream(ServletInputStream delegate) throws IOException {
            this.delegate = delegate;
            this.gzipIn = new GZIPInputStream(delegate);
        }

        @Override
        public int read() throws IOException {
            return gzipIn.read();
        }

        @Override
        public int read(byte[] b) throws IOException {
            return gzipIn.read(b);
        }

        @Override
        public int read(byte[] b, int off, int len) throws IOException {
            return gzipIn.read(b, off, len);
        }

        @Override
        public void close() throws IOException {
            gzipIn.close();
        }

        @Override
        public boolean isFinished() {
            return delegate.isFinished();
        }

        @Override
        public boolean isReady() {
            return delegate.isReady();
        }

        @Override
        public void setReadListener(ReadListener listener) {
            delegate.setReadListener(listener);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy