2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017 Amdocs
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.onap.aai.sparky.dal.servlet;
25 import com.google.common.primitives.Bytes;
27 import java.io.ByteArrayInputStream;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.util.Arrays;
32 import javax.servlet.ReadListener;
33 import javax.servlet.ServletInputStream;
34 import javax.servlet.http.HttpServletRequest;
35 import javax.servlet.http.HttpServletRequestWrapper;
38 * The Class ResettableStreamHttpServletRequest.
40 public class ResettableStreamHttpServletRequest extends HttpServletRequestWrapper {
42 private byte[] requestBody = new byte[0];
43 private boolean bufferFilled = false;
46 * Constructs a request object wrapping the given request.
48 * @param request The request to wrap
49 * @throws IllegalArgumentException if the request is null
51 public ResettableStreamHttpServletRequest(HttpServletRequest request) {
58 * @return Bytes with the request body contents.
59 * @throws IOException In case stream reqding fails.
61 public byte[] getRequestBody() throws IOException {
63 return Arrays.copyOf(requestBody, requestBody.length);
66 InputStream inputStream = super.getInputStream();
68 byte[] buffer = new byte[102400];
71 while ((bytesRead = inputStream.read(buffer)) != -1) {
72 requestBody = Bytes.concat(this.requestBody, Arrays.copyOfRange(buffer, 0, bytesRead));
81 public ServletInputStream getInputStream() throws IOException {
82 return new CustomServletInputStream(getRequestBody());
86 * The Class CustomServletInputStream.
88 private static class CustomServletInputStream extends ServletInputStream {
90 private ByteArrayInputStream buffer;
93 * Instantiates a new custom servlet input stream.
95 * @param contents the contents
97 public CustomServletInputStream(byte[] contents) {
98 this.buffer = new ByteArrayInputStream(contents);
104 * @see java.io.InputStream#read()
107 public int read() throws IOException {
108 return buffer.read();
112 public boolean isFinished() {
113 return buffer.available() == 0;
117 public boolean isReady() {
122 public void setReadListener(ReadListener arg0) {
123 throw new RuntimeException("Not implemented");