001/*
002 * Licensed to DuraSpace under one or more contributor license agreements.
003 * See the NOTICE file distributed with this work for additional information
004 * regarding copyright ownership.
005 *
006 * DuraSpace licenses this file to you under the Apache License,
007 * Version 2.0 (the "License"); you may not use this file except in
008 * compliance with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.fcrepo.auth.webac;
019
020import java.io.BufferedReader;
021import java.io.ByteArrayInputStream;
022import java.io.IOException;
023import java.io.InputStreamReader;
024
025import javax.servlet.ReadListener;
026import javax.servlet.ServletInputStream;
027import javax.servlet.ServletRequest;
028import javax.servlet.http.HttpServletRequest;
029import javax.servlet.http.HttpServletRequestWrapper;
030
031import org.apache.commons.io.IOUtils;
032
033/**
034 * An extension of HttpServletRequestWrapper that caches the InputStream as
035 * byte array and overrides the getInputStream to return a new InputStream
036 * object each time based on the cached byte array.
037 * 
038 * @author mohideen
039 */
040public class CachedHttpRequest extends HttpServletRequestWrapper {
041
042    private byte[] cachedContent;
043
044    private BufferedReader reader;
045
046    /**
047     * Create a new CachedSparqlRequest for the given servlet request.
048     * @param request the original servlet request
049     */
050    public CachedHttpRequest(final ServletRequest request) {
051        super((HttpServletRequest) request);
052    }
053
054    @Override
055    public ServletInputStream getInputStream() throws IOException {
056        if (getRequest().getInputStream() != null) {
057            if (this.cachedContent == null) {
058                this.cachedContent = IOUtils.toByteArray(getRequest().getInputStream());
059            }
060            return new CustomServletInputStream(cachedContent);
061        }
062        return null;
063    }
064
065    @Override
066    public BufferedReader getReader() throws IOException {
067        if (this.reader == null) {
068            this.reader = new BufferedReader(new InputStreamReader(getInputStream(),
069                    getCharacterEncoding()));
070        }
071        return this.reader;
072    }
073
074    private static class CustomServletInputStream extends ServletInputStream {
075
076        private final ByteArrayInputStream buffer;
077
078        public CustomServletInputStream(final byte[] contents) {
079            this.buffer = new ByteArrayInputStream(contents);
080        }
081
082        @Override
083        public int read() {
084            return buffer.read();
085        }
086
087        @Override
088        public boolean isFinished() {
089            return buffer.available() == 0;
090        }
091
092        @Override
093        public boolean isReady() {
094            return true;
095        }
096
097        @Override
098        public void setReadListener(final ReadListener listener) {
099            throw new RuntimeException("Not implemented");
100        }
101    }
102}