From a1b6e65e76682a322cfa32e885faff03bb6dcdf8 Mon Sep 17 00:00:00 2001 From: "prakash.e" Date: Sat, 7 Mar 2020 15:45:42 +0530 Subject: Fix SVNFM jetty-all vulnerability fix Upgraded jetty-all version to vulnerability issue Change-Id: I11d0c3ad92920f8c26bd92009e67820a2e541316 Issue-ID: VFC-1598 Signed-off-by: Prakash.E --- .../vnfmadapter/common/ResultRequestUtil.java | 3 - .../vnfm/svnfm/vnfmadapter/common/VnfmUtil.java | 4 +- .../common/restclient/HttpBaseRest.java | 235 +++++++++++++-------- .../vnfmadapter/common/restclient/HttpRest.java | 137 ++++++------ .../common/restclient/RestHttpContentExchange.java | 208 +++++++++--------- 5 files changed, 322 insertions(+), 265 deletions(-) (limited to 'huawei/vnfmadapter/VnfmadapterService/service/src/main/java') diff --git a/huawei/vnfmadapter/VnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/common/ResultRequestUtil.java b/huawei/vnfmadapter/VnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/common/ResultRequestUtil.java index 4c3b301c..fe4b888c 100644 --- a/huawei/vnfmadapter/VnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/common/ResultRequestUtil.java +++ b/huawei/vnfmadapter/VnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/common/ResultRequestUtil.java @@ -20,9 +20,6 @@ import java.io.IOException; import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandles; import java.lang.invoke.MethodType; - -import org.apache.http.client.methods.HttpPost; - import org.apache.commons.httpclient.HttpMethod; import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.Constant; import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.ParamConstants; diff --git a/huawei/vnfmadapter/VnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/common/VnfmUtil.java b/huawei/vnfmadapter/VnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/common/VnfmUtil.java index 5fb7322d..aa446084 100644 --- a/huawei/vnfmadapter/VnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/common/VnfmUtil.java +++ b/huawei/vnfmadapter/VnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/common/VnfmUtil.java @@ -74,7 +74,9 @@ public final class VnfmUtil { LOGGER.error("funtion=getVnfmById, status={}", rsp.getStatus()); return null; }*/ - String rsp = "{\"esr-system-info\": [{\"esr-system-info-id\": \"1234\",\"system-name\": \"hwvnfm\",\"type\": \"vnfm\",\"vendor\": \"huawei\",\"version\": \"v1.0\",\"service-url\": \"https://192.188.15.64:30001\",\"user-name\":\"vfctest\",\"password\": \"Huawei12#$\",\"system-type\": \"VNFM\", \"resource-version\": \"1508828777218\"}]}"; + //String rsp = "{\"esr-system-info\": [{\"esr-system-info-id\": \"1234\",\"system-name\": \"hwvnfm\",\"type\": \"vnfm\",\"vendor\": \"huawei\",\"version\": \"v1.0\",\"service-url\": \"https://192.188.15.64:30001\",\"user-name\":\"vfctest\",\"password\": \"Huawei12#$\",\"system-type\": \"VNFM\", \"resource-version\": \"1508828777218\"}]}"; + String rsp = "{\"esr-system-info\": [{\"esr-system-info-id\": \"1234\",\"system-name\": \"hwvnfm\",\"type\": \"vnfm\",\"vendor\": \"huawei\",\"version\": \"v1.0\",\"service-url\": \"http://localhost:8981\",\"user-name\":\"vfctest\",\"password\": \"Huawei12#$\",\"system-type\": \"VNFM\", \"resource-version\": \"1508828777218\"}]}"; + JSONObject esrVnfm = JSONObject.fromObject(rsp); LOGGER.info("esrVnfm: {}", esrVnfm); JSONObject vnfmJson = parseEsrVnfm(vnfmId, esrVnfm); diff --git a/huawei/vnfmadapter/VnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/common/restclient/HttpBaseRest.java b/huawei/vnfmadapter/VnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/common/restclient/HttpBaseRest.java index cc80b5ed..af182f1f 100644 --- a/huawei/vnfmadapter/VnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/common/restclient/HttpBaseRest.java +++ b/huawei/vnfmadapter/VnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/common/restclient/HttpBaseRest.java @@ -16,34 +16,37 @@ package org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.restclient; -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.UnsupportedEncodingException; +import java.io.*; import java.net.URLEncoder; import java.text.SimpleDateFormat; -import java.util.Calendar; -import java.util.Date; -import java.util.Map; +import java.util.*; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicInteger; -import org.eclipse.jetty.client.Address; -import org.eclipse.jetty.client.HttpClient; -import org.eclipse.jetty.client.HttpExchange; -import org.eclipse.jetty.http.HttpMethods; +import org.eclipse.jetty.client.*; +import org.eclipse.jetty.client.api.*; +import org.eclipse.jetty.http.HttpHeader; +import org.eclipse.jetty.http.HttpMethod; +import org.eclipse.jetty.http.HttpVersion; import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.Constant; import org.slf4j.Logger; import org.slf4j.LoggerFactory; + /** *
*

*

- * + * * @author * @version Aug 9, 2016 */ public abstract class HttpBaseRest implements Restful { - + private Response responseGlobal; + private ContentResponse contentResponse; + private Request request; private static final Logger LOG = LoggerFactory.getLogger(HttpRest.class); final AtomicInteger requestId = new AtomicInteger(0); @@ -53,32 +56,28 @@ public abstract class HttpBaseRest implements Restful { static final String HTTP_PATCH = "PATCH"; String defaultIP = Constant.LOCAL_HOST; - int defaultPort = -10000; - int defaultTimeout = 30000; - final String procenameRouteID = "RouteID-" + System.currentTimeMillis() + "-"; + /** * Constructor
*

*

- * + * * @since */ public HttpBaseRest() { super(); } - protected void createHttpClient() { - client = new HttpClient(); + public HttpBaseRest(final Response response) { + this.responseGlobal = response; } - protected RestHttpContentExchange createRestHttpContentExchange(final RestfulAsyncCallback callback) { - final RestHttpContentExchange exchange = new RestHttpContentExchange(true, callback); - exchange.setScheme("http"); - return exchange; + protected void createHttpClient() { + client = new HttpClient(); } private String encodeParams(final RestfulParametes restParametes) throws ServiceException { @@ -87,13 +86,13 @@ public abstract class HttpBaseRest implements Restful { boolean bHasParma = false; final StringBuilder builder = new StringBuilder(); try { - for(final String key : parm.keySet()) { + for (final String key : parm.keySet()) { value = parm.get(key); - if(value == null) { + if (value == null) { value = ""; } String str; - if(bHasParma) { + if (bHasParma) { str = String.format("&%s=%s", URLEncoder.encode(key, RestfulClientConst.ENCODING), URLEncoder.encode(value, RestfulClientConst.ENCODING)); } else { @@ -103,39 +102,49 @@ public abstract class HttpBaseRest implements Restful { } builder.append(str); } - } catch(final UnsupportedEncodingException ex) { + } catch (final UnsupportedEncodingException ex) { LOG.error("unsupported encoding: ", ex); throw new ServiceException("Broken VM does not support UTF-8"); } return builder.toString(); } - private void processHeader(final RestHttpContentExchange contentExchange, final Map headerMap) { - for(final String key : headerMap.keySet()) { + private void processHeader(final Request request, final Map headerMap) { + for (final String key : headerMap.keySet()) { final String value = headerMap.get(key); - contentExchange.addRequestHeader(key, value); + HttpHeader headers[] = HttpHeader.values(); + if (Arrays.asList(headers).contains('"' + key + '"')) ; + { + request.header(key, value); + + } + } } - private void setContentExchangeParams(final RestHttpContentExchange contentExchange) { - final String contentType = contentExchange.getRequestFields().getStringField("Content-Type"); - if(null == contentType || contentType.isEmpty()) { + private void setRequestParams(final Request request, String httpMethod) { + final String contentType = request.getHeaders().get("Content-Type"); + if (null == contentType || contentType.isEmpty()) { // application/json;charset=utf-8 - contentExchange.setRequestContentType(RestfulClientConst.APPLICATION_FORM_URLENCODED); + request.header(HttpHeader.CONTENT_TYPE, RestfulClientConst.APPLICATION_FORM_URLENCODED); } - final String encoding = contentExchange.getRequestFields().getStringField("Accept-Encoding"); - if(null == encoding || encoding.isEmpty()) { + //final String encoding = contentExchange.getRequestFields().getStringField("Accept-Encoding"); + final String encoding = request.getHeaders().get("Accept-Encoding"); + if (null == encoding || encoding.isEmpty()) { // compress,gzip - contentExchange.setRequestHeader("Accept-Encoding", "*/*"); + request.header(HttpHeader.ACCEPT_ENCODING, "*/*"); } - contentExchange.setVersion(11); + request.version(HttpVersion.HTTP_1_1); + request.scheme("http"); + request.method(httpMethod); } + /** *
- * - * @param method + * + * @param httpMethod * @param servicePath * @param restParametes * @param options @@ -144,70 +153,120 @@ public abstract class HttpBaseRest implements Restful { * @throws ServiceException * @since */ - protected RestfulResponse sendHttpRequest(final String method, final String servicePath, - final RestfulParametes restParametes, final RestfulOptions options, final RestfulAsyncCallback callback) + + protected RestfulResponse sendHttpRequest(final String httpMethod, final String servicePath, + final RestfulParametes restParametes, final RestfulOptions options, RestfulAsyncCallback callback) throws ServiceException { - final RestHttpContentExchange contentExchange = createRestHttpContentExchange(callback); - if(null == restParametes) { + + + if (null == restParametes) { return new RestfulResponse(); } - final String requestTrace = this.getReuqestIdString(); + final String requestTrace = this.getRequestIdString(); restParametes.putHttpContextHeader(RestfulClientConst.REQUEST_ID, requestTrace); RestfulResponse rsp = null; try { - contentExchange.setMethod(method); final String str = encodeParams(restParametes); final StringBuilder builder = new StringBuilder(); builder.append(servicePath); - if(str.length() > 0 && (method.equals(HttpMethods.GET) || method.equals(HttpMethods.DELETE) - || method.equals(HttpMethods.HEAD))) { + if (str.length() > 0 && (httpMethod.equals(HttpMethod.GET.asString()) || httpMethod.equals(HttpMethod.DELETE.asString()) + || httpMethod.equals(HttpMethod.HEAD.asString()))) { builder.append('?'); builder.append(str); } - setDefaultUrl(contentExchange, options, builder); - processHeader(contentExchange, restParametes.getHeaderMap()); - setContentExchangeParams(contentExchange); + String url = setDefaultUrl(options, builder); + System.out.println(url); + request = client.newRequest(url); + setRequestParams(request, httpMethod); + processHeader(request, restParametes.getHeaderMap()); + setPostPutParam(httpMethod, restParametes, request, str); + setTimeout(options, request); + ContentResponse contentResponse = getResponse(); +// HttpRequestListeners httpRequestListeners = new HttpRequestListeners(); +// RestHttpContentExchange contentExchange = new RestHttpContentExchange(); +// Response.CompleteListener responseListener = contentExchange; +// // Response.CompleteListener responseListener =f; +// request.method(httpMethod) +// .onRequestSuccess(httpRequestListeners) +// .onRequestBegin(httpRequestListeners) +// .scheme("http") +// .send(responseListener); +// Thread.sleep(2000); +// System.out.println("content:- " + contentExchange._responseContentString); +// System.out.println("code :-" + contentExchange._responseStatus); - setPostPutParam(method, restParametes, contentExchange, str); - setTimeout(options, contentExchange); - client.send(contentExchange); +// Origin origin=new Origin("http","localhost",8980); +// HttpDestination httpDestination=new HttpDestination(client,origin) { +// @Override +// protected SendFailure send(Connection connection, HttpExchange exchange) { +// return null; +// } +// }; +// List listenersList=new ArrayList<>(); +// listenersList.add(responseListener); +// +// HttpExchange httpExchange=new HttpExchange(httpDestination,(HttpRequest)request,listenersList); +// System.out.println("httpExchange : "+httpExchange.getResponse().getStatus()); + //System.out.println("request :- " + httpRequestListeners.method); + RestHttpContentExchange contentExchange = new RestHttpContentExchange(); + contentExchange.setResponseStatus(contentResponse.getStatus()); + contentExchange.setResponseContentBytes(contentResponse.getContent()); + contentExchange.setResponseFields(contentResponse.getHeaders()); + contentExchange.setResponseContentString(contentResponse.getContentAsString()); rsp = callbackExecute(callback, contentExchange); - } catch(final Exception e) { - LOG.error("request reply message have exception:status is " - + RestHttpContentExchange.toState(contentExchange.getStatus())); +// List list = contentResponse.getListeners(Response.Listener.class); +// for (Response.Listener listener : list) { +// System.out.println(listener.getClass()); +// } + System.out.println("Testing :: " + contentResponse.getContentAsString()); + System.out.println("rsp::::::: " + rsp); + } catch (final Exception e) { + System.out.println("ex : " + e.getMessage()); + if(request!=null){ + LOG.error("request reply message have exception:status is " + + request.getAbortCause()); + } + throw new ServiceException(e); } return rsp; } - private void setDefaultUrl(final RestHttpContentExchange contentExchange, final RestfulOptions options, - final StringBuilder url) { + private ContentResponse getResponse() throws InterruptedException, ExecutionException, TimeoutException { + return request.send(); + } + + + private String setDefaultUrl(final RestfulOptions options, final StringBuilder url) { // server - if(url.toString().startsWith("http")) { - contentExchange.setURL(url.toString()); + + if (url.toString().startsWith("http")) { + return url.toString(); } else { - String host = defaultIP; + String host = Constant.LOCAL_HOST; int iPort = defaultPort; - if(options != null) { + if (options != null) { host = options.getHost(); - if(host.isEmpty()) { + if (host.isEmpty()) { host = defaultIP; } iPort = options.getPort(); - if(iPort == 0) { + if (iPort == 0) { iPort = defaultPort; } } // Integer.getInteger(".http.client.maxThread",30) - contentExchange.setAddress(new Address(host, iPort)); - contentExchange.setRequestURI(url.toString()); + return "http://" + host + ":" + iPort + "/" + url; + //return "http://reqres.in/api/users/4"; + //return "https://jsonplaceholder.typicode.com/users"; //for 404 bad request + } } - private String getReuqestIdString() { - if(this.requestId.get() == 0x7FFFFFFF) { + private String getRequestIdString() { + if (this.requestId.get() == 0x7FFFFFFF) { this.requestId.set(1); } final int reqId = this.requestId.getAndIncrement(); @@ -223,49 +282,45 @@ public abstract class HttpBaseRest implements Restful { } private void setPostPutParam(final String method, final RestfulParametes restParametes, - final RestHttpContentExchange contentExchange, final String str) throws UnsupportedEncodingException { - if(HttpMethods.POST.equals(method) || HttpMethods.PUT.equals(method) || HTTP_PATCH.equals(method)) { + final Request request, final String str) throws UnsupportedEncodingException { + if (HttpMethod.POST.equals(method) || HttpMethod.PUT.equals(method) || HTTP_PATCH.equals(method)) { ByteArrayInputStream buff; final String tmpRaw = restParametes.getRawData(); - if(tmpRaw == null) { + if (tmpRaw == null) { buff = new ByteArrayInputStream(str.getBytes(RestfulClientConst.ENCODING)); } else { buff = new ByteArrayInputStream(tmpRaw.getBytes(RestfulClientConst.ENCODING)); } final int len = buff.available(); - contentExchange.setRequestContentSource(buff); - contentExchange.setRequestHeader("content-length", String.valueOf(len)); + //contentExchange.setRequestContentSource(buff); + request.header("content-length", String.valueOf(len)); } } - private void setTimeout(final RestfulOptions options, final RestHttpContentExchange contentExchange) { - if(options != null) { + private void setTimeout(final RestfulOptions options, final Request request) { + if (options != null) { final long timeout = options.getRestTimeout(); - if(timeout != 0) { - contentExchange.setTimeout(timeout); + if (timeout != 0) { + request.idleTimeout(timeout, TimeUnit.MILLISECONDS); } else { - contentExchange.setTimeout(defaultTimeout); + request.idleTimeout(defaultTimeout, TimeUnit.MILLISECONDS); } } else { - contentExchange.setTimeout(defaultTimeout); + request.idleTimeout(defaultTimeout, TimeUnit.MILLISECONDS); } } private RestfulResponse callbackExecute(final RestfulAsyncCallback callback, - final RestHttpContentExchange contentExchange) throws InterruptedException, IOException, ServiceException { - if(callback == null) { - final int exchangeState = contentExchange.waitForDone(); - if(exchangeState == HttpExchange.STATUS_COMPLETED) { + final RestHttpContentExchange contentExchange) throws ServiceException, IOException { + if (callback == null) { + int exchangeState = contentExchange.getResponse().getStatus(); + if (exchangeState == 200) { + System.out.println("Restful Response " + contentExchange.getResponse().getResponseContent()); return contentExchange.getResponse(); - } else if(exchangeState == HttpExchange.STATUS_EXCEPTED) { - throw new ServiceException( - "request is exception: " + RestHttpContentExchange.toState(HttpExchange.STATUS_EXCEPTED)); - } else if(exchangeState == HttpExchange.STATUS_EXPIRED) { - throw new ServiceException( - "request is expierd: " + RestHttpContentExchange.toState(HttpExchange.STATUS_EXPIRED)); + } else { + throw new ServiceException("status code : "+exchangeState); } } return null; } - } diff --git a/huawei/vnfmadapter/VnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/common/restclient/HttpRest.java b/huawei/vnfmadapter/VnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/common/restclient/HttpRest.java index 7215424d..1b1cf9c7 100644 --- a/huawei/vnfmadapter/VnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/common/restclient/HttpRest.java +++ b/huawei/vnfmadapter/VnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/common/restclient/HttpRest.java @@ -16,17 +16,17 @@ package org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.restclient; -import org.eclipse.jetty.client.HttpClient; -import org.eclipse.jetty.http.HttpMethods; +import org.eclipse.jetty.http.HttpMethod; import org.eclipse.jetty.util.thread.QueuedThreadPool; import org.slf4j.Logger; import org.slf4j.LoggerFactory; + /** *
*

*

- * + * * @author * @version Aug 9, 2016 */ @@ -34,15 +34,15 @@ public class HttpRest extends HttpBaseRest { private static final Logger LOG = LoggerFactory.getLogger(HttpRest.class); + /** * Initializing Rest options.
- * - * @param options: rest options. + * * @throws ServiceException * @since */ public void initHttpRest(final RestfulOptions option) throws ServiceException { - if(option == null) { + if (option == null) { client = null; throw new ServiceException("option is null."); } @@ -50,30 +50,23 @@ public class HttpRest extends HttpBaseRest { try { int iValue; iValue = option.getIntOption(RestfulClientConst.MAX_CONN_PER_ADDR_KEY_NAME); - // max 200 concurrent,connections to every address - client.setMaxConnectionsPerAddress(iValue); - +// // max 200 concurrent,connections to every address + client.setMaxConnectionsPerDestination(iValue); iValue = option.getIntOption(RestfulClientConst.THREAD_KEY_NAME); - // max threads - client.setThreadPool(new QueuedThreadPool(iValue)); +// // max threads + client.setExecutor(new QueuedThreadPool(iValue)); iValue = option.getIntOption(RestfulClientConst.CONN_TIMEOUT_KEY_NAME); client.setConnectTimeout(iValue); + iValue = option.getRestTimeout(); defaultTimeout = iValue; - client.setTimeout(iValue); - + client.setIdleTimeout(iValue); iValue = option.getIntOption(RestfulClientConst.IDLE_TIMEOUT_KEY_NAME); client.setIdleTimeout(iValue); - iValue = option.getIntOption(RestfulClientConst.MAX_RESPONSE_HEADER_SIZE); - client.setResponseHeaderSize(iValue); - iValue = option.getIntOption(RestfulClientConst.MAX_REQUEST_HEADER_SIZE); - client.setRequestHeaderSize(iValue); - // HttpClient.CONNECTOR_SOCKET - client.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL); client.start(); defaultIP = option.getStringOption(RestfulClientConst.HOST_KEY_NAME); defaultPort = option.getIntOption(RestfulClientConst.PORT_KEY_NAME); - } catch(final Exception e) { + } catch (final Exception e) { LOG.error("start httpclient error", e); client = null; throw new ServiceException("http client init failed."); @@ -82,139 +75,139 @@ public class HttpRest extends HttpBaseRest { @Override public RestfulResponse get(final String servicePath, final RestfulParametes restParametes) throws ServiceException { - return this.sendHttpRequest(HttpMethods.GET, servicePath, restParametes, null, null); + return this.sendHttpRequest(HttpMethod.GET.asString(), servicePath, restParametes, null, null); } @Override public RestfulResponse get(final String servicePath, final RestfulParametes restParametes, - final RestfulOptions option) throws ServiceException { - return this.sendHttpRequest(HttpMethods.GET, servicePath, restParametes, option, null); + final RestfulOptions option) throws ServiceException { + return this.sendHttpRequest(HttpMethod.GET.asString(), servicePath, restParametes, option, null); } @Override public RestfulResponse head(final String servicePath, final RestfulParametes restParametes) throws ServiceException { - return this.sendHttpRequest(HttpMethods.HEAD, servicePath, restParametes, null, null); + return this.sendHttpRequest(HttpMethod.HEAD.asString(), servicePath, restParametes, null, null); } @Override public RestfulResponse head(final String servicePath, final RestfulParametes restParametes, - final RestfulOptions option) throws ServiceException { - return this.sendHttpRequest(HttpMethods.HEAD, servicePath, restParametes, option, null); + final RestfulOptions option) throws ServiceException { + return this.sendHttpRequest(HttpMethod.HEAD.asString(), servicePath, restParametes, option, null); } @Override public void asyncGet(final String servicePath, final RestfulParametes restParametes, - final RestfulAsyncCallback callback) throws ServiceException { - if(callback == null) { - this.sendHttpRequest(HttpMethods.GET, servicePath, restParametes, null, new DefaultAsyncCallback()); + final RestfulAsyncCallback callback) throws ServiceException { + if (callback == null) { + this.sendHttpRequest(HttpMethod.GET.asString(), servicePath, restParametes, null, new DefaultAsyncCallback()); } else { - this.sendHttpRequest(HttpMethods.GET, servicePath, restParametes, null, callback); + this.sendHttpRequest(HttpMethod.GET.asString(), servicePath, restParametes, null, callback); } } @Override public void asyncGet(final String servicePath, final RestfulParametes restParametes, final RestfulOptions option, - final RestfulAsyncCallback callback) throws ServiceException { - if(callback == null) { - this.sendHttpRequest(HttpMethods.GET, servicePath, restParametes, option, new DefaultAsyncCallback()); + final RestfulAsyncCallback callback) throws ServiceException { + if (callback == null) { + this.sendHttpRequest(HttpMethod.GET.asString(), servicePath, restParametes, option, new DefaultAsyncCallback()); } else { - this.sendHttpRequest(HttpMethods.GET, servicePath, restParametes, option, callback); + this.sendHttpRequest(HttpMethod.GET.asString(), servicePath, restParametes, option, callback); } } @Override public RestfulResponse put(final String servicePath, final RestfulParametes restParametes) throws ServiceException { - return this.sendHttpRequest(HttpMethods.PUT, servicePath, restParametes, null, null); + return this.sendHttpRequest(HttpMethod.PUT.asString(), servicePath, restParametes, null, null); } @Override public RestfulResponse put(final String servicePath, final RestfulParametes restParametes, - final RestfulOptions option) throws ServiceException { - return this.sendHttpRequest(HttpMethods.PUT, servicePath, restParametes, option, null); + final RestfulOptions option) throws ServiceException { + return this.sendHttpRequest(HttpMethod.PUT.asString(), servicePath, restParametes, option, null); } @Override public void asyncPut(final String servicePath, final RestfulParametes restParametes, - final RestfulAsyncCallback callback) throws ServiceException { - if(callback == null) { - this.sendHttpRequest(HttpMethods.PUT, servicePath, restParametes, null, new DefaultAsyncCallback()); + final RestfulAsyncCallback callback) throws ServiceException { + if (callback == null) { + this.sendHttpRequest(HttpMethod.PUT.asString(), servicePath, restParametes, null, new DefaultAsyncCallback()); } else { - this.sendHttpRequest(HttpMethods.PUT, servicePath, restParametes, null, callback); + this.sendHttpRequest(HttpMethod.PUT.asString(), servicePath, restParametes, null, callback); } } @Override public void asyncPut(final String servicePath, final RestfulParametes restParametes, final RestfulOptions option, - final RestfulAsyncCallback callback) throws ServiceException { - if(callback == null) { - this.sendHttpRequest(HttpMethods.PUT, servicePath, restParametes, option, new DefaultAsyncCallback()); + final RestfulAsyncCallback callback) throws ServiceException { + if (callback == null) { + this.sendHttpRequest(HttpMethod.PUT.asString(), servicePath, restParametes, option, new DefaultAsyncCallback()); } else { - this.sendHttpRequest(HttpMethods.PUT, servicePath, restParametes, option, callback); + this.sendHttpRequest(HttpMethod.PUT.asString(), servicePath, restParametes, option, callback); } } @Override public RestfulResponse post(final String servicePath, final RestfulParametes restParametes) throws ServiceException { - return this.sendHttpRequest(HttpMethods.POST, servicePath, restParametes, null, null); + return this.sendHttpRequest(HttpMethod.POST.asString(), servicePath, restParametes, null, null); } @Override public RestfulResponse post(final String servicePath, final RestfulParametes restParametes, - final RestfulOptions option) throws ServiceException { - return this.sendHttpRequest(HttpMethods.POST, servicePath, restParametes, option, null); + final RestfulOptions option) throws ServiceException { + return this.sendHttpRequest(HttpMethod.POST.asString(), servicePath, restParametes, option, null); } @Override public void asyncPost(final String servicePath, final RestfulParametes restParametes, - final RestfulAsyncCallback callback) throws ServiceException { - if(callback == null) { - this.sendHttpRequest(HttpMethods.POST, servicePath, restParametes, null, new DefaultAsyncCallback()); + final RestfulAsyncCallback callback) throws ServiceException { + if (callback == null) { + this.sendHttpRequest(HttpMethod.POST.asString(), servicePath, restParametes, null, new DefaultAsyncCallback()); } else { - this.sendHttpRequest(HttpMethods.POST, servicePath, restParametes, null, callback); + this.sendHttpRequest(HttpMethod.POST.asString(), servicePath, restParametes, null, callback); } } @Override public void asyncPost(final String servicePath, final RestfulParametes restParametes, final RestfulOptions option, - final RestfulAsyncCallback callback) throws ServiceException { - if(callback == null) { - this.sendHttpRequest(HttpMethods.POST, servicePath, restParametes, option, new DefaultAsyncCallback()); + final RestfulAsyncCallback callback) throws ServiceException { + if (callback == null) { + this.sendHttpRequest(HttpMethod.POST.asString(), servicePath, restParametes, option, new DefaultAsyncCallback()); } else { - this.sendHttpRequest(HttpMethods.POST, servicePath, restParametes, option, callback); + this.sendHttpRequest(HttpMethod.POST.asString(), servicePath, restParametes, option, callback); } } @Override public RestfulResponse delete(final String servicePath, final RestfulParametes restParametes) throws ServiceException { - return this.sendHttpRequest(HttpMethods.DELETE, servicePath, restParametes, null, null); + return this.sendHttpRequest(HttpMethod.DELETE.asString(), servicePath, restParametes, null, null); } @Override public RestfulResponse delete(final String servicePath, final RestfulParametes restParametes, - final RestfulOptions option) throws ServiceException { - return this.sendHttpRequest(HttpMethods.DELETE, servicePath, restParametes, option, null); + final RestfulOptions option) throws ServiceException { + return this.sendHttpRequest(HttpMethod.DELETE.asString(), servicePath, restParametes, option, null); } @Override public void asyncDelete(final String servicePath, final RestfulParametes restParametes, - final RestfulAsyncCallback callback) throws ServiceException { - if(callback == null) { - this.sendHttpRequest(HttpMethods.DELETE, servicePath, restParametes, null, new DefaultAsyncCallback()); + final RestfulAsyncCallback callback) throws ServiceException { + if (callback == null) { + this.sendHttpRequest(HttpMethod.DELETE.asString(), servicePath, restParametes, null, new DefaultAsyncCallback()); } else { - this.sendHttpRequest(HttpMethods.DELETE, servicePath, restParametes, null, callback); + this.sendHttpRequest(HttpMethod.DELETE.asString(), servicePath, restParametes, null, callback); } } @Override public void asyncDelete(final String servicePath, final RestfulParametes restParametes, final RestfulOptions option, - final RestfulAsyncCallback callback) throws ServiceException { - if(callback == null) { - this.sendHttpRequest(HttpMethods.DELETE, servicePath, restParametes, option, new DefaultAsyncCallback()); + final RestfulAsyncCallback callback) throws ServiceException { + if (callback == null) { + this.sendHttpRequest(HttpMethod.DELETE.asString(), servicePath, restParametes, option, new DefaultAsyncCallback()); } else { - this.sendHttpRequest(HttpMethods.DELETE, servicePath, restParametes, option, callback); + this.sendHttpRequest(HttpMethod.DELETE.asString(), servicePath, restParametes, option, callback); } } @@ -226,14 +219,14 @@ public class HttpRest extends HttpBaseRest { @Override public RestfulResponse patch(final String servicePath, final RestfulParametes restParametes, - final RestfulOptions option) throws ServiceException { + final RestfulOptions option) throws ServiceException { return this.sendHttpRequest(HTTP_PATCH, servicePath, restParametes, option, null); } @Override public void asyncPatch(final String servicePath, final RestfulParametes restParametes, - final RestfulAsyncCallback callback) throws ServiceException { - if(callback == null) { + final RestfulAsyncCallback callback) throws ServiceException { + if (callback == null) { this.sendHttpRequest(HTTP_PATCH, servicePath, restParametes, null, new DefaultAsyncCallback()); } else { this.sendHttpRequest(HTTP_PATCH, servicePath, restParametes, null, callback); @@ -242,8 +235,8 @@ public class HttpRest extends HttpBaseRest { @Override public void asyncPatch(final String servicePath, final RestfulParametes restParametes, final RestfulOptions option, - final RestfulAsyncCallback callback) throws ServiceException { - if(callback == null) { + final RestfulAsyncCallback callback) throws ServiceException { + if (callback == null) { this.sendHttpRequest(HTTP_PATCH, servicePath, restParametes, option, new DefaultAsyncCallback()); } else { this.sendHttpRequest(HTTP_PATCH, servicePath, restParametes, option, callback); 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..2f116f15 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,108 +92,74 @@ 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); 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(); +// } + } -- cgit 1.2.3-korg