2 * Copyright 2016 Huawei Technologies Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.restclient;
20 import java.nio.charset.Charset;
21 import java.util.Enumeration;
22 import java.util.HashMap;
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;
31 * ContentExchange implementation classe to provide access to response.
37 * @version 28-May-2016
39 public class RestHttpContentExchange {
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;
51 String _responseContentString;
54 boolean _rsponseAbort;
56 // private HttpFields setCacheFields(boolean cacheHeaders) {
57 // _responseFields = cacheHeaders ? new HttpFields() : null;
58 // return _responseFields;
62 * @return the scheme of the URL
64 // public Buffer getScheme()
73 * @param data GZipped data.
74 * @return Uncompressed data.
78 public String decompressGzipToStr(final byte[] data) throws IOException {
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));
91 return out.toString();
95 private boolean isGzip() {
99 public void setResponseContentString(String responseContentString){
100 this._responseContentString=responseContentString;
102 public void setResponseContentBytes (byte[] responseContentBytes){
103 this._responseContentByte=responseContentBytes ;
105 public void setResponseStatus ( int responseStatus ){
106 this._responseStatus = responseStatus ;
108 public void setResponseFields ( HttpFields responseFields ){
109 this._responseFields= responseFields ;
111 public synchronized String getResponseContentString() throws UnsupportedEncodingException {
112 if (_responseContentString != null)
113 return _responseContentString;
118 synchronized byte[] getResponseContentBytes() {
119 if (_responseContentByte != null)
120 return _responseContentByte;
125 synchronized int getResponseStatus() {
126 // if (_responseStatus >= 500)
127 // throw new IllegalStateException("internal server error");
128 return _responseStatus;
132 synchronized HttpFields getResponseFields() {
133 // if (_responseStatus >= STATUS_PARSING_CONTENT)
134 // throw new IllegalStateException("Headers not completely received yet");
135 return _responseFields;
139 * Get the response as RestfulResponse.
142 * @return response object.
143 * @throws IOException
146 public RestfulResponse getResponse() throws IOException {
147 final RestfulResponse rsp = new RestfulResponse();
149 rsp.setStatus(getResponseStatus());
151 final String responseString = decompressGzipToStr(getResponseContentBytes());
152 rsp.setResponseJson(responseString);
154 rsp.setResponseJson(this.getResponseContentString());
157 final HttpFields field = this.getResponseFields();
159 final Map<String, String> header = new HashMap<>();
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.getStringField(fieldName);
165 header.put(fieldName, fieldValue);
168 rsp.setRespHeaderMap(header);
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();
183 // public void onBegin(Response response) {
188 // public void onComplete(Result result) {
193 // public void onContent(Response response, ByteBuffer content) {
198 // public void onFailure(Response response, Throwable failure) {
199 // this._responseStatus = response.getStatus();
200 // this._rsponseAbort = response.abort(failure);
204 // public boolean onHeader(Response response, HttpField field) {
205 // this._responseFields = response.getHeaders();
210 // public void onHeaders(Response response) {
215 // public void onSuccess(Response response) {
216 // this._responseStatus = response.getStatus();