X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=huawei%2Fvnfmadapter%2FVnfmadapterService%2Fservice%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fvfc%2Fnfvo%2Fvnfm%2Fsvnfm%2Fvnfmadapter%2Fcommon%2Frestclient%2FRestHttpContentExchange.java;h=d96690b3b2cd39ac451d158c93c1207cdc5d3714;hb=3fdb2cd4e76feb67afa1d7ac1ee1981a09d5932e;hp=64017c790d29d0c5a1c8226d26aadd3353429fa8;hpb=b30fcd4ea8301f5cf0b176601eac9724b3347e25;p=vfc%2Fnfvo%2Fdriver%2Fvnfm%2Fsvnfm.git diff --git a/huawei/vnfmadapter/VnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/common/restclient/RestHttpContentExchange.java b/huawei/vnfmadapter/VnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/common/restclient/RestHttpContentExchange.java index 64017c79..d96690b3 100644 --- a/huawei/vnfmadapter/VnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/common/restclient/RestHttpContentExchange.java +++ b/huawei/vnfmadapter/VnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/common/restclient/RestHttpContentExchange.java @@ -16,22 +16,14 @@ package org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.restclient; -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.InputStreamReader; +import java.io.*; import java.nio.charset.Charset; import java.util.Enumeration; import java.util.HashMap; import java.util.Map; import java.util.zip.GZIPInputStream; - -import org.apache.commons.lang.StringUtils; -import org.eclipse.jetty.client.ContentExchange; -import org.eclipse.jetty.client.HttpDestination; +import org.eclipse.jetty.client.api.Request; import org.eclipse.jetty.http.HttpFields; -import org.eclipse.jetty.http.HttpHeaders; -import org.eclipse.jetty.io.Buffer; -import org.eclipse.jetty.util.StringUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -40,45 +32,51 @@ import org.slf4j.LoggerFactory; *
*

*

- * + * * @author * @version 28-May-2016 */ -public class RestHttpContentExchange extends ContentExchange { +public class RestHttpContentExchange { + private static final int STATUS_PARSING_HEADERS = 500; + private static final int STATUS_PARSING_CONTENT = 500; + private String _encoding = "utf-8"; + private byte[] _responseContent; private static final Logger LOGGER = LoggerFactory.getLogger(RestHttpContentExchange.class); - private static final String PATH = "path:"; - private boolean gzip = false; + private HttpFields _responseFields; + private byte[] _responseContentByte; - private RestfulAsyncCallback callback = null; + String _responseContentString; + int _responseStatus; + Request request; + boolean _rsponseAbort; + +// private HttpFields setCacheFields(boolean cacheHeaders) { +// _responseFields = cacheHeaders ? new HttpFields() : null; +// return _responseFields; +// } /** - * Constructor
- *

- *

- * - * @since - * @param cacheFields whether to cache response header. - * @param asyncCallback callback method. + * @return the scheme of the URL */ - RestHttpContentExchange(final boolean cacheFields, final RestfulAsyncCallback asyncCallback) { - super(cacheFields); - this.callback = asyncCallback; - } +// public Buffer getScheme() +// { +// return _scheme; +// } /** * Extract message. *
- * + * * @param data GZipped data. * @return Uncompressed data. * @throws IOException * @since */ public String decompressGzipToStr(final byte[] data) throws IOException { - if(data == null) { + if (data == null) { return ""; } final StringBuilder out = new StringBuilder(); @@ -86,7 +84,7 @@ public class RestHttpContentExchange extends ContentExchange { GZIPInputStream gzis = new GZIPInputStream(input); InputStreamReader reader = new InputStreamReader(gzis, Charset.forName(RestfulClientConst.ENCODING));) { final char[] buff = new char[1024]; - for(int n; (n = reader.read(buff)) != -1;) { + for (int n; (n = reader.read(buff)) != -1; ) { out.append(new String(buff, 0, n)); } } @@ -94,110 +92,76 @@ public class RestHttpContentExchange extends ContentExchange { } - /** - * View response headers Content-Encoding values if you need to extract data.
- * - * @param name buffer - * @param value value - * @throws IOException - * @since - */ - @Override - protected synchronized void onResponseHeader(final Buffer name, final Buffer value) throws IOException { - super.onResponseHeader(name, value); - final int header = HttpHeaders.CACHE.getOrdinal(name); - if(header == HttpHeaders.CONTENT_ENCODING_ORDINAL) { - final String encoding = StringUtil.asciiToLowerCase(value.toString()); - gzip = encoding != null && StringUtils.contains(encoding, "gzip"); - } - + private boolean isGzip() { + return gzip; } - @Override - protected void onResponseComplete() throws IOException { - if(LOGGER.isInfoEnabled()) { - LOGGER.info("Response has Complete:" + PATH + this.getRequestURI().replace("\n", "0x0A")); - } - super.onResponseComplete(); - if(callback != null) { - final RestfulResponse rsp = getResponse(); - callback.callback(rsp); - } + public void setResponseContentString(String responseContentString){ + this._responseContentString=responseContentString; } - - @Override - protected void onRequestCommitted() throws IOException { - if(LOGGER.isInfoEnabled()) { - LOGGER.info("Request Header has been send:" + PATH + this.getRequestURI().replace("\n", "0x0A")); - } - super.onRequestCommitted(); + public void setResponseContentBytes (byte[] responseContentBytes){ + this._responseContentByte=responseContentBytes ; } - - @Override - protected void onRequestComplete() throws IOException { - if(LOGGER.isInfoEnabled()) { - LOGGER.info("Request has bend send complete:" + PATH + this.getRequestURI().replace("\n", "0x0A")); - } - super.onRequestComplete(); + public void setResponseStatus ( int responseStatus ){ + this._responseStatus = responseStatus ; } - - @Override - protected void onException(final Throwable x) { - LOGGER.warn("onException:", x); - super.onException(x); - if(callback != null) { - callback.handleExcepion(x); - } + public void setResponseFields ( HttpFields responseFields ){ + this._responseFields= responseFields ; + } + public synchronized String getResponseContentString() throws UnsupportedEncodingException { + if (_responseContentString != null) + return _responseContentString; + return null; } - @Override - protected void onConnectionFailed(final Throwable x) { - LOGGER.warn("onConnectionFailed:", x); - super.onConnectionFailed(x); - if(callback != null) { - callback.handleExcepion(x); - } + synchronized byte[] getResponseContentBytes() { + if (_responseContentByte != null) + return _responseContentByte; + return null; } - @Override - protected void expire(final HttpDestination destination) { - super.expire(destination); - if(callback != null) { - callback.handleExcepion(new ServiceException("request is expired, status:" + toState(getStatus()))); - } + + synchronized int getResponseStatus() { +// if (_responseStatus >= 500) +// throw new IllegalStateException("internal server error"); + return _responseStatus; } - public boolean isGzip() { - return gzip; + + synchronized HttpFields getResponseFields() { +// if (_responseStatus >= STATUS_PARSING_CONTENT) +// throw new IllegalStateException("Headers not completely received yet"); + return _responseFields; } /** * Get the response as RestfulResponse. *
- * + * * @return response object. * @throws IOException * @since */ public RestfulResponse getResponse() throws IOException { final RestfulResponse rsp = new RestfulResponse(); - rsp.setStatus(this.getResponseStatus()); - if(isGzip()) { + + rsp.setStatus(getResponseStatus()); + if (isGzip()) { final String responseString = decompressGzipToStr(getResponseContentBytes()); rsp.setResponseJson(responseString); } else { - rsp.setResponseJson(this.getResponseContent()); + rsp.setResponseJson(this.getResponseContentString()); } final HttpFields field = this.getResponseFields(); - if(field != null) { + if (field != null) { final Map header = new HashMap<>(); final Enumeration names = field.getFieldNames(); - for(final Enumeration e = names; e.hasMoreElements();) { + for (final Enumeration e = names; e.hasMoreElements(); ) { final String fieldName = e.nextElement(); - final String fieldValue = field.getStringField(fieldName); + final String fieldValue = field.getField(fieldName).getValue(); header.put(fieldName, fieldValue); } @@ -206,4 +170,50 @@ public class RestHttpContentExchange extends ContentExchange { return rsp; } + +// @Override +// public void onContent(Response response, ByteBuffer content, Callback callback) { +// System.out.println("ContentExchange inside " + response.getStatus()); +// super.onContent(response, content, callback); +// this._responseContentString = StandardCharsets.UTF_8.decode(content).toString(); +// this._responseContent = content.array(); +// } +// +// @Override +// public void onBegin(Response response) { +// +// } +// +// @Override +// public void onComplete(Result result) { +// +// } +// +// @Override +// public void onContent(Response response, ByteBuffer content) { +// +// } +// +// @Override +// public void onFailure(Response response, Throwable failure) { +// this._responseStatus = response.getStatus(); +// this._rsponseAbort = response.abort(failure); +// } +// +// @Override +// public boolean onHeader(Response response, HttpField field) { +// this._responseFields = response.getHeaders(); +// return false; +// } +// +// @Override +// public void onHeaders(Response response) { +// +// } +// +// @Override +// public void onSuccess(Response response) { +// this._responseStatus = response.getStatus(); +// } + }