Initial commit for AAI-UI(sparky-backend)
[aai/sparky-be.git] / src / main / java / org / openecomp / sparky / dal / servlet / ResettableStreamHttpServletRequest.java
1 /**
2  * ============LICENSE_START===================================================
3  * SPARKY (AAI UI service)
4  * ============================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
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
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
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=====================================================
21  *
22  * ECOMP and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25
26 package org.openecomp.sparky.dal.servlet;
27
28 import com.google.common.primitives.Bytes;
29
30 import java.io.ByteArrayInputStream;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.util.Arrays;
34
35 import javax.servlet.ReadListener;
36 import javax.servlet.ServletInputStream;
37 import javax.servlet.http.HttpServletRequest;
38 import javax.servlet.http.HttpServletRequestWrapper;
39
40 /**
41  * The Class ResettableStreamHttpServletRequest.
42  */
43 public class ResettableStreamHttpServletRequest extends HttpServletRequestWrapper {
44
45   private byte[] requestBody = new byte[0];
46   private boolean bufferFilled = false;
47
48   /**
49    * Constructs a request object wrapping the given request.
50    *
51    * @param request The request to wrap
52    * @throws IllegalArgumentException if the request is null
53    */
54   public ResettableStreamHttpServletRequest(HttpServletRequest request) {
55     super(request);
56   }
57
58   /**
59    * Get request body.
60    * 
61    * @return Bytes with the request body contents.
62    * @throws IOException In case stream reqding fails.
63    */
64   public byte[] getRequestBody() throws IOException {
65     if (bufferFilled) {
66       return Arrays.copyOf(requestBody, requestBody.length);
67     }
68
69     InputStream inputStream = super.getInputStream();
70
71     byte[] buffer = new byte[102400];
72
73     int bytesRead;
74     while ((bytesRead = inputStream.read(buffer)) != -1) {
75       requestBody = Bytes.concat(this.requestBody, Arrays.copyOfRange(buffer, 0, bytesRead));
76     }
77
78     bufferFilled = true;
79
80     return requestBody;
81   }
82
83   @Override
84   public ServletInputStream getInputStream() throws IOException {
85     return new CustomServletInputStream(getRequestBody());
86   }
87
88   /**
89    * The Class CustomServletInputStream.
90    */
91   private static class CustomServletInputStream extends ServletInputStream {
92
93     private ByteArrayInputStream buffer;
94
95     /**
96      * Instantiates a new custom servlet input stream.
97      *
98      * @param contents the contents
99      */
100     public CustomServletInputStream(byte[] contents) {
101       this.buffer = new ByteArrayInputStream(contents);
102     }
103
104     /* (non-Javadoc)
105      * @see java.io.InputStream#read()
106      */
107     @Override
108     public int read() throws IOException {
109       return buffer.read();
110     }
111
112     @Override
113     public boolean isFinished() {
114       return buffer.available() == 0;
115     }
116
117     @Override
118     public boolean isReady() {
119       return true;
120     }
121
122     @Override
123     public void setReadListener(ReadListener arg0) {
124       throw new RuntimeException("Not implemented");
125     }
126
127   }
128
129 }