io.trino.gateway.proxyserver.wrapper.MultiReadHttpServletRequest Maven / Gradle / Ivy
/*
* 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.trino.gateway.proxyserver.wrapper;
import jakarta.servlet.ReadListener;
import jakarta.servlet.ServletInputStream;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletRequestWrapper;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MultiReadHttpServletRequest
extends HttpServletRequestWrapper
{
private final byte[] content;
private final Map headerMap = new HashMap<>();
public static void copy(InputStream in, OutputStream out)
throws IOException
{
byte[] buffer = new byte[1024];
while (true) {
int bytesRead = in.read(buffer);
if (bytesRead == -1) {
break;
}
out.write(buffer, 0, bytesRead);
}
}
public MultiReadHttpServletRequest(HttpServletRequest request)
throws IOException
{
super(request);
ByteArrayOutputStream bodyInOutputStream = new ByteArrayOutputStream();
copy(request.getInputStream(), bodyInOutputStream);
content = bodyInOutputStream.toByteArray();
}
/**
* add a header with given name and value.
*/
public void addHeader(String name, String value)
{
headerMap.put(name, value);
}
@Override
public String getHeader(String name)
{
String headerValue = super.getHeader(name);
if (headerMap.containsKey(name)) {
headerValue = headerMap.get(name);
}
return headerValue;
}
/**
* get the header names.
*/
@Override
public Enumeration getHeaderNames()
{
List names = Collections.list(super.getHeaderNames());
for (String name : headerMap.keySet()) {
names.add(name);
}
return Collections.enumeration(names);
}
@Override
public Enumeration getHeaders(String name)
{
List values = Collections.list(super.getHeaders(name));
if (headerMap.containsKey(name)) {
values.add(headerMap.get(name));
}
return Collections.enumeration(values);
}
@Override
public ServletInputStream getInputStream()
throws IOException
{
final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(content);
return new ServletInputStream()
{
@Override
public boolean isFinished()
{
return false;
}
@Override
public boolean isReady()
{
return false;
}
@Override
public void setReadListener(ReadListener readListener) {}
@Override
public int read()
throws IOException
{
return byteArrayInputStream.read();
}
};
}
@Override
public BufferedReader getReader()
throws IOException
{
return new BufferedReader(new InputStreamReader(this.getInputStream()));
}
}