2 * ============LICENSE_START===================================================
3 * SPARKY (AAI UI service)
4 * ============================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
8 * ============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=====================================================
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
26 package org.openecomp.sparky.dal.servlet;
28 import com.google.common.primitives.Bytes;
30 import java.io.ByteArrayInputStream;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.util.Arrays;
35 import javax.servlet.ReadListener;
36 import javax.servlet.ServletInputStream;
37 import javax.servlet.http.HttpServletRequest;
38 import javax.servlet.http.HttpServletRequestWrapper;
41 * The Class ResettableStreamHttpServletRequest.
43 public class ResettableStreamHttpServletRequest extends HttpServletRequestWrapper {
45 private byte[] requestBody = new byte[0];
46 private boolean bufferFilled = false;
49 * Constructs a request object wrapping the given request.
51 * @param request The request to wrap
52 * @throws IllegalArgumentException if the request is null
54 public ResettableStreamHttpServletRequest(HttpServletRequest request) {
61 * @return Bytes with the request body contents.
62 * @throws IOException In case stream reqding fails.
64 public byte[] getRequestBody() throws IOException {
66 return Arrays.copyOf(requestBody, requestBody.length);
69 InputStream inputStream = super.getInputStream();
71 byte[] buffer = new byte[102400];
74 while ((bytesRead = inputStream.read(buffer)) != -1) {
75 requestBody = Bytes.concat(this.requestBody, Arrays.copyOfRange(buffer, 0, bytesRead));
84 public ServletInputStream getInputStream() throws IOException {
85 return new CustomServletInputStream(getRequestBody());
89 * The Class CustomServletInputStream.
91 private static class CustomServletInputStream extends ServletInputStream {
93 private ByteArrayInputStream buffer;
96 * Instantiates a new custom servlet input stream.
98 * @param contents the contents
100 public CustomServletInputStream(byte[] contents) {
101 this.buffer = new ByteArrayInputStream(contents);
105 * @see java.io.InputStream#read()
108 public int read() throws IOException {
109 return buffer.read();
113 public boolean isFinished() {
114 return buffer.available() == 0;
118 public boolean isReady() {
123 public void setReadListener(ReadListener arg0) {
124 throw new RuntimeException("Not implemented");