update the pom version for the security issue fix
[vfc/nfvo/driver/vnfm/svnfm.git] / huawei / vnfmadapter / VnfmadapterService / service / src / main / java / org / onap / vfc / nfvo / vnfm / svnfm / vnfmadapter / common / restclient / RestHttpContentExchange.java
1 /*
2  * Copyright 2016 Huawei Technologies Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.restclient;
18
19 import java.io.*;
20 import java.nio.charset.Charset;
21 import java.util.Enumeration;
22 import java.util.HashMap;
23 import java.util.Map;
24 import java.util.zip.GZIPInputStream;
25 import org.eclipse.jetty.client.api.Request;
26 import org.eclipse.jetty.http.HttpFields;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * ContentExchange implementation classe to provide access to response.
32  * <br/>
33  * <p>
34  * </p>
35  *
36  * @author
37  * @version 28-May-2016
38  */
39 public class RestHttpContentExchange  {
40
41     private static final int STATUS_PARSING_HEADERS = 500;
42     private static final int STATUS_PARSING_CONTENT = 500;
43     private String _encoding = "utf-8";
44     private byte[] _responseContent;
45     private static final Logger LOGGER = LoggerFactory.getLogger(RestHttpContentExchange.class);
46     private static final String PATH = "path:";
47     private boolean gzip = false;
48     private HttpFields _responseFields;
49     private byte[] _responseContentByte;
50
51     String _responseContentString;
52     int _responseStatus;
53     Request request;
54     boolean _rsponseAbort;
55
56 //    private HttpFields setCacheFields(boolean cacheHeaders) {
57 //        _responseFields = cacheHeaders ? new HttpFields() : null;
58 //        return _responseFields;
59 //    }
60
61     /**
62      * @return the scheme of the URL
63      */
64 //    public Buffer getScheme()
65 //    {
66 //        return _scheme;
67 //    }
68
69     /**
70      * Extract message.
71      * <br/>
72      *
73      * @param data GZipped data.
74      * @return Uncompressed data.
75      * @throws IOException
76      * @since
77      */
78     public String decompressGzipToStr(final byte[] data) throws IOException {
79         if (data == null) {
80             return "";
81         }
82         final StringBuilder out = new StringBuilder();
83         try (ByteArrayInputStream input = new ByteArrayInputStream(data);
84              GZIPInputStream gzis = new GZIPInputStream(input);
85              InputStreamReader reader = new InputStreamReader(gzis, Charset.forName(RestfulClientConst.ENCODING));) {
86             final char[] buff = new char[1024];
87             for (int n; (n = reader.read(buff)) != -1; ) {
88                 out.append(new String(buff, 0, n));
89             }
90         }
91         return out.toString();
92
93     }
94
95     private boolean isGzip() {
96         return gzip;
97     }
98
99     public void setResponseContentString(String responseContentString){
100         this._responseContentString=responseContentString;
101     }
102     public void setResponseContentBytes (byte[] responseContentBytes){
103         this._responseContentByte=responseContentBytes ;
104     }
105      public void setResponseStatus  ( int responseStatus ){
106         this._responseStatus = responseStatus ;
107     }
108      public void setResponseFields  ( HttpFields responseFields  ){
109         this._responseFields= responseFields ;
110     }
111     public synchronized String getResponseContentString() throws UnsupportedEncodingException {
112         if (_responseContentString != null)
113             return _responseContentString;
114         return null;
115     }
116
117
118       synchronized byte[] getResponseContentBytes() {
119         if (_responseContentByte != null)
120             return _responseContentByte;
121         return null;
122     }
123
124
125       synchronized int getResponseStatus() {
126 //        if (_responseStatus >= 500)
127 //            throw new IllegalStateException("internal server error");
128         return _responseStatus;
129     }
130
131
132       synchronized HttpFields getResponseFields() {
133 //        if (_responseStatus >= STATUS_PARSING_CONTENT)
134 //            throw new IllegalStateException("Headers not completely received yet");
135         return _responseFields;
136     }
137
138     /**
139      * Get the response as RestfulResponse.
140      * <br/>
141      *
142      * @return response object.
143      * @throws IOException
144      * @since
145      */
146     public RestfulResponse getResponse() throws IOException {
147         final RestfulResponse rsp = new RestfulResponse();
148
149         rsp.setStatus(getResponseStatus());
150         if (isGzip()) {
151             final String responseString = decompressGzipToStr(getResponseContentBytes());
152             rsp.setResponseJson(responseString);
153         } else {
154             rsp.setResponseJson(this.getResponseContentString());
155         }
156
157         final HttpFields field = this.getResponseFields();
158         if (field != null) {
159             final Map<String, String> header = new HashMap<>();
160
161             final Enumeration<String> names = field.getFieldNames();
162             for (final Enumeration<String> e = names; e.hasMoreElements(); ) {
163                 final String fieldName = e.nextElement();
164                 final String fieldValue = field.getField(fieldName).getValue();
165                 header.put(fieldName, fieldValue);
166             }
167
168             rsp.setRespHeaderMap(header);
169         }
170         return rsp;
171     }
172
173
174 //    @Override
175 //    public void onContent(Response response, ByteBuffer content, Callback callback) {
176 //        System.out.println("ContentExchange inside " + response.getStatus());
177 //        super.onContent(response, content, callback);
178 //        this._responseContentString = StandardCharsets.UTF_8.decode(content).toString();
179 //        this._responseContent = content.array();
180 //    }
181 //
182 //    @Override
183 //    public void onBegin(Response response) {
184 //
185 //    }
186 //
187 //    @Override
188 //    public void onComplete(Result result) {
189 //
190 //    }
191 //
192 //    @Override
193 //    public void onContent(Response response, ByteBuffer content) {
194 //
195 //    }
196 //
197 //    @Override
198 //    public void onFailure(Response response, Throwable failure) {
199 //        this._responseStatus = response.getStatus();
200 //        this._rsponseAbort = response.abort(failure);
201 //    }
202 //
203 //    @Override
204 //    public boolean onHeader(Response response, HttpField field) {
205 //        this._responseFields = response.getHeaders();
206 //        return false;
207 //    }
208 //
209 //    @Override
210 //    public void onHeaders(Response response) {
211 //
212 //    }
213 //
214 //    @Override
215 //    public void onSuccess(Response response) {
216 //        this._responseStatus = response.getStatus();
217 //    }
218
219 }