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