- webResourceBuilder.header("X-ECOMP-RequestID",org.slf4j.MDC.get("X-ECOMP-RequestID"));
-
- ClientResponse response = webResourceBuilder.method(p.httpMethod.toString(), ClientResponse.class, request);
-
- r.code = response.getStatus();
- r.headers = response.getHeaders();
- EntityTag etag = response.getEntityTag();
- if (etag != null)
- r.message = etag.getValue();
- if (response.hasEntity() && r.code != 204)
- r.body = response.getEntity(String.class);
- }
-
- long t2 = System.currentTimeMillis();
- log.info("Response received. Time: " + (t2 - t1));
- log.info("HTTP response code: " + r.code);
- log.info("HTTP response message: " + r.message);
- logHeaders(r.headers);
- log.info("HTTP response: " + r.body);
-
- return r;
- }
-
- protected SSLContext createSSLContext(Parameters p) {
- try {
- System.setProperty("jsse.enableSNIExtension", "false");
- System.setProperty("javax.net.ssl.trustStore", p.trustStoreFileName);
- System.setProperty("javax.net.ssl.trustStorePassword", p.trustStorePassword);
-
- HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
-
- @Override
- public boolean verify(String string, SSLSession ssls) {
- return true;
- }
- });
-
- KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
- FileInputStream in = new FileInputStream(p.keyStoreFileName);
- KeyStore ks = KeyStore.getInstance("PKCS12");
- char[] pwd = p.keyStorePassword.toCharArray();
- ks.load(in, pwd);
- kmf.init(ks, pwd);
-
- SSLContext ctx = SSLContext.getInstance("TLS");
- ctx.init(kmf.getKeyManagers(), null, null);
- return ctx;
- } catch (Exception e) {
- log.error("Error creating SSLContext: " + e.getMessage(), e);
- }
- return null;
- }
-
- protected void setFailureResponseStatus(SvcLogicContext ctx, String prefix, String errorMessage, HttpResponse r) {
- r = new HttpResponse();
- r.code = 500;
- r.message = errorMessage;
- String pp = prefix != null ? prefix + '.' : "";
- ctx.setAttribute(pp + "response-code", String.valueOf(r.code));
- ctx.setAttribute(pp + "response-message", r.message);
- }
-
- protected void setResponseStatus(SvcLogicContext ctx, String prefix, HttpResponse r) {
- String pp = prefix != null ? prefix + '.' : "";
- ctx.setAttribute(pp + "response-code", String.valueOf(r.code));
- ctx.setAttribute(pp + "response-message", r.message);
- }
-
- public void sendFile(Map<String, String> paramMap, SvcLogicContext ctx) throws SvcLogicException {
- HttpResponse r = null;
- try {
- FileParam p = getFileParameters(paramMap);
- byte[] data = Files.readAllBytes(Paths.get(p.fileName));
-
- r = sendHttpData(data, p);
- setResponseStatus(ctx, p.responsePrefix, r);
-
- } catch (Exception e) {
- log.error("Error sending the request: " + e.getMessage(), e);
-
- r = new HttpResponse();
- r.code = 500;
- r.message = e.getMessage();
- String prefix = parseParam(paramMap, "responsePrefix", false, null);
- setResponseStatus(ctx, prefix, r);
- }
-
- if (r != null && r.code >= 300)
- throw new SvcLogicException(String.valueOf(r.code) + ": " + r.message);
- }
-
- private static class FileParam {
-
- public String fileName;
- public String url;
- public String user;
- public String password;
- public HttpMethod httpMethod;
- public String responsePrefix;
- public boolean skipSending;
- }
-
- private FileParam getFileParameters(Map<String, String> paramMap) throws SvcLogicException {
- FileParam p = new FileParam();
- p.fileName = parseParam(paramMap, "fileName", true, null);
- p.url = parseParam(paramMap, "url", true, null);
- p.user = parseParam(paramMap, "user", false, null);
- p.password = parseParam(paramMap, "password", false, null);
- p.httpMethod = HttpMethod.fromString(parseParam(paramMap, "httpMethod", false, "post"));
- p.responsePrefix = parseParam(paramMap, "responsePrefix", false, null);
- String skipSendingStr = paramMap.get("skipSending");
- p.skipSending = skipSendingStr != null && skipSendingStr.equalsIgnoreCase("true");
- return p;
- }
-
- protected HttpResponse sendHttpData(byte[] data, FileParam p) {
- Client client = Client.create();
- client.setConnectTimeout(5000);
- client.setFollowRedirects(true);
- if (p.user != null)
- client.addFilter(new HTTPBasicAuthFilter(p.user, p.password));
- WebResource webResource = client.resource(p.url);
-
- log.info("Sending file");
- long t1 = System.currentTimeMillis();
-
- HttpResponse r = new HttpResponse();
- r.code = 200;
-
- if (!p.skipSending) {
- String tt = "application/octet-stream";
-
- ClientResponse response = null;
- if (p.httpMethod == HttpMethod.POST)
- response = webResource.accept(tt).type(tt).post(ClientResponse.class, data);
- else if (p.httpMethod == HttpMethod.PUT)
- response = webResource.accept(tt).type(tt).put(ClientResponse.class, data);
-
- r.code = response.getStatus();
- r.headers = response.getHeaders();
- EntityTag etag = response.getEntityTag();
- if (etag != null)
- r.message = etag.getValue();
- if (response.hasEntity() && r.code != 204)
- r.body = response.getEntity(String.class);
-
- if (r.code == 301) {
- String newUrl = response.getHeaders().getFirst("Location");
-
- log.info("Got response code 301. Sending same request to URL: " + newUrl);
-
- webResource = client.resource(newUrl);
-
- if (p.httpMethod == HttpMethod.POST)
- response = webResource.accept(tt).type(tt).post(ClientResponse.class, data);
- else if (p.httpMethod == HttpMethod.PUT)
- response = webResource.accept(tt).type(tt).put(ClientResponse.class, data);
-
- r.code = response.getStatus();
- etag = response.getEntityTag();
- if (etag != null)
- r.message = etag.getValue();
- if (response.hasEntity() && r.code != 204)
- r.body = response.getEntity(String.class);
- }
- }
-
- long t2 = System.currentTimeMillis();
- log.info("Response received. Time: " + (t2 - t1));
- log.info("HTTP response code: " + r.code);
- log.info("HTTP response message: " + r.message);
- logHeaders(r.headers);
- log.info("HTTP response: " + r.body);
-
- return r;
- }
-
- public void postMessageOnUeb(Map<String, String> paramMap, SvcLogicContext ctx) throws SvcLogicException {
- HttpResponse r = null;
- try {
- UebParam p = getUebParameters(paramMap);
-
- String pp = p.responsePrefix != null ? p.responsePrefix + '.' : "";
-
- String req = null;
-
- if (p.templateFileName == null) {
- log.info("No template file name specified. Using default UEB template: " + defaultUebTemplateFileName);
- p.templateFileName = defaultUebTemplateFileName;
- }
-
- String reqTemplate = readFile(p.templateFileName);
- reqTemplate = reqTemplate.replaceAll("rootVarName", p.rootVarName);
- req = buildXmlJsonRequest(ctx, reqTemplate, Format.JSON);
-
- r = postOnUeb(req, p);
- setResponseStatus(ctx, p.responsePrefix, r);
- if (r.body != null)
- ctx.setAttribute(pp + "httpResponse", r.body);
-
- } catch (Exception e) {
- log.error("Error sending the request: " + e.getMessage(), e);
-
- r = new HttpResponse();
- r.code = 500;
- r.message = e.getMessage();
- String prefix = parseParam(paramMap, "responsePrefix", false, null);
- setResponseStatus(ctx, prefix, r);
- }
-
- if (r != null && r.code >= 300)
- throw new SvcLogicException(String.valueOf(r.code) + ": " + r.message);
- }
-
- private static class UebParam {
-
- public String topic;
- public String templateFileName;
- public String rootVarName;
- public String responsePrefix;
- public boolean skipSending;
- }
-
- private UebParam getUebParameters(Map<String, String> paramMap) throws SvcLogicException {
- UebParam p = new UebParam();
- p.topic = parseParam(paramMap, "topic", true, null);
- p.templateFileName = parseParam(paramMap, "templateFileName", false, null);
- p.rootVarName = parseParam(paramMap, "rootVarName", false, null);
- p.responsePrefix = parseParam(paramMap, "responsePrefix", false, null);
- String skipSendingStr = paramMap.get("skipSending");
- p.skipSending = skipSendingStr != null && skipSendingStr.equalsIgnoreCase("true");
- return p;
- }
-
- protected HttpResponse postOnUeb(String request, UebParam p) throws Exception {
- String[] urls = uebServers.split(" ");
- for (int i = 0; i < urls.length; i++) {
- if (!urls[i].endsWith("/"))
- urls[i] += "/";
- urls[i] += "events/" + p.topic;
- }
-
- Client client = Client.create();
- client.setConnectTimeout(5000);
- WebResource webResource = client.resource(urls[0]);
-
- log.info("UEB URL: " + urls[0]);
- log.info("Sending request:");
- log.info(request);
- long t1 = System.currentTimeMillis();
-
- HttpResponse r = new HttpResponse();
- r.code = 200;
-
- if (!p.skipSending) {
- String tt = "application/json";
- String tt1 = tt + ";charset=UTF-8";
-
- ClientResponse response = webResource.accept(tt).type(tt1).post(ClientResponse.class, request);
-
- r.code = response.getStatus();
- r.headers = response.getHeaders();
- if (response.hasEntity())
- r.body = response.getEntity(String.class);
- }
-
- long t2 = System.currentTimeMillis();
- log.info("Response received. Time: " + (t2 - t1));
- log.info("HTTP response code: " + r.code);
- logHeaders(r.headers);
- log.info("HTTP response:\n" + r.body);
-
- return r;
- }
-
- protected void logProperties(Map<String, Object> mm) {
- List<String> ll = new ArrayList<>();
- for (Object o : mm.keySet())
- ll.add((String) o);
- Collections.sort(ll);
-
- log.info("Properties:");
- for (String name : ll)
- log.info("--- " + name + ": " + String.valueOf(mm.get(name)));
- }
-
- protected void logHeaders(MultivaluedMap<String, String> mm) {
- log.info("HTTP response headers:");
-
- if (mm == null)
- return;
-
- List<String> ll = new ArrayList<>();
- for (Object o : mm.keySet())
- ll.add((String) o);
- Collections.sort(ll);
-
- for (String name : ll)
- log.info("--- " + name + ": " + String.valueOf(mm.get(name)));
- }
-
- public void setUebServers(String uebServers) {
- this.uebServers = uebServers;
- }
-
- public void setDefaultUebTemplateFileName(String defaultUebTemplateFileName) {
- this.defaultUebTemplateFileName = defaultUebTemplateFileName;
- }
+ invocationBuilder.header("X-ECOMP-RequestID", org.slf4j.MDC.get("X-ECOMP-RequestID"));
+
+ Response response;
+
+ try {
+ response = invocationBuilder.method(p.httpMethod.toString(), entity(multiPart, multiPart.getMediaType()));
+ } catch (ProcessingException | IllegalStateException e) {
+ throw new SvcLogicException(requestPostingException +
+ e.getLocalizedMessage(), e);
+ }
+
+ r.code = response.getStatus();
+ r.headers = response.getStringHeaders();
+ EntityTag etag = response.getEntityTag();
+ if (etag != null) {
+ r.message = etag.getValue();
+ }
+ if (response.hasEntity() && r.code != 204) {
+ r.body = response.readEntity(String.class);
+ }
+
+ }
+
+ long t2 = System.currentTimeMillis();
+ log.info(responseReceivedMessage, (t2 - t1));
+ log.info(responseHttpCodeMessage, r.code);
+ log.info("HTTP response message: {}", r.message);
+ logHeaders(r.headers);
+ log.info("HTTP response: {}", r.body);
+
+ return r;
+ }
+
+ protected SSLContext createSSLContext(Parameters p) {
+ try (FileInputStream in = new FileInputStream(p.keyStoreFileName)) {
+ System.setProperty("jsse.enableSNIExtension", "false");
+ System.setProperty("javax.net.ssl.trustStore", p.trustStoreFileName);
+ System.setProperty("javax.net.ssl.trustStorePassword", p.trustStorePassword);
+
+ HttpsURLConnection.setDefaultHostnameVerifier((string, ssls) -> true);
+
+ KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
+ KeyStore ks = KeyStore.getInstance("PKCS12");
+ char[] pwd = p.keyStorePassword.toCharArray();
+ ks.load(in, pwd);
+ kmf.init(ks, pwd);
+
+ SSLContext ctx = SSLContext.getInstance("TLS");
+ ctx.init(kmf.getKeyManagers(), null, null);
+ return ctx;
+ } catch (Exception e) {
+ log.error("Error creating SSLContext: {}", e.getMessage(), e);
+ }
+ return null;
+ }
+
+ protected void setFailureResponseStatus(SvcLogicContext ctx, String prefix, String errorMessage,
+ HttpResponse resp) {
+ resp.code = 500;
+ resp.message = errorMessage;
+ String pp = prefix != null ? prefix + '.' : "";
+ ctx.setAttribute(pp + "response-code", String.valueOf(resp.code));
+ ctx.setAttribute(pp + "response-message", resp.message);
+ }
+
+ protected void setResponseStatus(SvcLogicContext ctx, String prefix, HttpResponse r) {
+ String pp = prefix != null ? prefix + '.' : "";
+ ctx.setAttribute(pp + "response-code", String.valueOf(r.code));
+ ctx.setAttribute(pp + "response-message", r.message);
+ }
+
+ public void sendFile(Map<String, String> paramMap, SvcLogicContext ctx) throws SvcLogicException {
+ HttpResponse r = null;
+ try {
+ FileParam p = getFileParameters(paramMap);
+ byte[] data = Files.readAllBytes(Paths.get(p.fileName));
+
+ r = sendHttpData(data, p);
+ setResponseStatus(ctx, p.responsePrefix, r);
+
+ } catch (SvcLogicException | IOException e) {
+ log.error("Error sending the request: {}", e.getMessage(), e);
+
+ r = new HttpResponse();
+ r.code = 500;
+ r.message = e.getMessage();
+ String prefix = parseParam(paramMap, responsePrefix, false, null);
+ setResponseStatus(ctx, prefix, r);
+ }
+
+ if (r != null && r.code >= 300) {
+ throw new SvcLogicException(String.valueOf(r.code) + ": " + r.message);
+ }
+ }
+
+ private FileParam getFileParameters(Map<String, String> paramMap) throws SvcLogicException {
+ FileParam p = new FileParam();
+ p.fileName = parseParam(paramMap, "fileName", true, null);
+ p.url = parseParam(paramMap, "url", true, null);
+ p.user = parseParam(paramMap, "user", false, null);
+ p.password = parseParam(paramMap, "password", false, null);
+ p.httpMethod = HttpMethod.fromString(parseParam(paramMap, "httpMethod", false, "post"));
+ p.responsePrefix = parseParam(paramMap, responsePrefix, false, null);
+ String skipSendingStr = paramMap.get(skipSendingMessage);
+ p.skipSending = "true".equalsIgnoreCase(skipSendingStr);
+ p.oAuthConsumerKey = parseParam(paramMap, "oAuthConsumerKey", false, null);
+ p.oAuthVersion = parseParam(paramMap, "oAuthVersion", false, null);
+ p.oAuthConsumerSecret = parseParam(paramMap, "oAuthConsumerSecret", false, null);
+ p.oAuthSignatureMethod = parseParam(paramMap, "oAuthSignatureMethod", false, null);
+ p.authtype = fromString(parseParam(paramMap, "authType", false, "unspecified"));
+ return p;
+ }
+
+ public void postMessageOnUeb(Map<String, String> paramMap, SvcLogicContext ctx) throws SvcLogicException {
+ HttpResponse r;
+ try {
+ UebParam p = getUebParameters(paramMap);
+
+ String pp = p.responsePrefix != null ? p.responsePrefix + '.' : "";
+
+ String req;
+
+ if (p.templateFileName == null) {
+ log.info("No template file name specified. Using default UEB template: {}", defaultUebTemplateFileName);
+ p.templateFileName = defaultUebTemplateFileName;
+ }
+
+ String reqTemplate = readFile(p.templateFileName);
+ reqTemplate = reqTemplate.replaceAll("rootVarName", p.rootVarName);
+ req = buildXmlJsonRequest(ctx, reqTemplate, Format.JSON);
+
+ r = postOnUeb(req, p);
+ setResponseStatus(ctx, p.responsePrefix, r);
+ if (r.body != null) {
+ ctx.setAttribute(pp + "httpResponse", r.body);
+ }
+
+ } catch (SvcLogicException e) {
+ log.error("Error sending the request: {}", e.getMessage(), e);
+
+ r = new HttpResponse();
+ r.code = 500;
+ r.message = e.getMessage();
+ String prefix = parseParam(paramMap, responsePrefix, false, null);
+ setResponseStatus(ctx, prefix, r);
+ }
+
+ if (r.code >= 300) {
+ throw new SvcLogicException(String.valueOf(r.code) + ": " + r.message);
+ }
+ }
+
+ protected HttpResponse sendHttpData(byte[] data, FileParam p) throws SvcLogicException {
+
+ Client client = ClientBuilder.newBuilder().build();
+ client.property(ClientProperties.CONNECT_TIMEOUT, 5000);
+ client.property(ClientProperties.FOLLOW_REDIRECTS, true);
+ WebTarget webTarget = addAuthType(client, p).target(p.url);
+
+ log.info("Sending file");
+ long t1 = System.currentTimeMillis();
+
+ HttpResponse r = new HttpResponse();
+ r.code = 200;
+
+ if (!p.skipSending) {
+ String tt = "application/octet-stream";
+ Invocation.Builder invocationBuilder = webTarget.request(tt).accept(tt);
+
+ Response response;
+
+ try {
+ if (p.httpMethod == HttpMethod.POST) {
+ response = invocationBuilder.post(Entity.entity(data, tt));
+ } else if (p.httpMethod == HttpMethod.PUT) {
+ response = invocationBuilder.put(Entity.entity(data, tt));
+ } else {
+ throw new SvcLogicException("Http operation" + p.httpMethod + "not supported");
+ }
+ } catch (ProcessingException e) {
+ throw new SvcLogicException(requestPostingException +
+ e.getLocalizedMessage(), e);
+ }
+
+ r.code = response.getStatus();
+ r.headers = response.getStringHeaders();
+ EntityTag etag = response.getEntityTag();
+ if (etag != null) {
+ r.message = etag.getValue();
+ }
+ if (response.hasEntity() && r.code != 204) {
+ r.body = response.readEntity(String.class);
+ }
+
+ if (r.code == 301) {
+ String newUrl = response.getStringHeaders().getFirst("Location");
+
+ log.info("Got response code 301. Sending same request to URL: {}", newUrl);
+
+ webTarget = client.target(newUrl);
+ invocationBuilder = webTarget.request(tt).accept(tt);
+
+ try {
+ if (p.httpMethod == HttpMethod.POST) {
+ response = invocationBuilder.post(Entity.entity(data, tt));
+ } else if (p.httpMethod == HttpMethod.PUT) {
+ response = invocationBuilder.put(Entity.entity(data, tt));
+ } else {
+ throw new SvcLogicException("Http operation" + p.httpMethod + "not supported");
+ }
+ } catch (ProcessingException e) {
+ throw new SvcLogicException(requestPostingException +
+ e.getLocalizedMessage(), e);
+ }
+
+ r.code = response.getStatus();
+ etag = response.getEntityTag();
+ if (etag != null) {
+ r.message = etag.getValue();
+ }
+ if (response.hasEntity() && r.code != 204) {
+ r.body = response.readEntity(String.class);
+ }
+ }
+ }
+
+ long t2 = System.currentTimeMillis();
+ log.info(responseReceivedMessage, (t2 - t1));
+ log.info(responseHttpCodeMessage, r.code);
+ log.info("HTTP response message: {}", r.message);
+ logHeaders(r.headers);
+ log.info("HTTP response: {}", r.body);
+
+ return r;
+ }
+
+ private UebParam getUebParameters(Map<String, String> paramMap) throws SvcLogicException {
+ UebParam p = new UebParam();
+ p.topic = parseParam(paramMap, "topic", true, null);
+ p.templateFileName = parseParam(paramMap, "templateFileName", false, null);
+ p.rootVarName = parseParam(paramMap, "rootVarName", false, null);
+ p.responsePrefix = parseParam(paramMap, responsePrefix, false, null);
+ String skipSendingStr = paramMap.get(skipSendingMessage);
+ p.skipSending = "true".equalsIgnoreCase(skipSendingStr);
+ return p;
+ }
+
+ protected void logProperties(Map<String, Object> mm) {
+ List<String> ll = new ArrayList<>();
+ for (Object o : mm.keySet()) {
+ ll.add((String) o);
+ }
+ Collections.sort(ll);
+
+ log.info("Properties:");
+ for (String name : ll) {
+ log.info("--- {}:{}", name, String.valueOf(mm.get(name)));
+ }
+ }
+
+ protected void logHeaders(MultivaluedMap<String, String> mm) {
+ log.info("HTTP response headers:");
+
+ if (mm == null) {
+ return;
+ }
+
+ List<String> ll = new ArrayList<>();
+ for (Object o : mm.keySet()) {
+ ll.add((String) o);
+ }
+ Collections.sort(ll);
+
+ for (String name : ll) {
+ log.info("--- {}:{}", name, String.valueOf(mm.get(name)));
+ }
+ }
+
+ protected HttpResponse postOnUeb(String request, UebParam p) throws SvcLogicException {
+ String[] urls = uebServers.split(" ");
+ for (int i = 0; i < urls.length; i++) {
+ if (!urls[i].endsWith("/")) {
+ urls[i] += "/";
+ }
+ urls[i] += "events/" + p.topic;
+ }
+
+ Client client = ClientBuilder.newBuilder().build();
+ client.property(ClientProperties.CONNECT_TIMEOUT, 5000);
+ WebTarget webTarget = client.target(urls[0]);
+
+ log.info("UEB URL: {}", urls[0]);
+ log.info("Sending request:");
+ log.info(request);
+ long t1 = System.currentTimeMillis();
+
+ HttpResponse r = new HttpResponse();
+ r.code = 200;
+
+ if (!p.skipSending) {
+ String tt = "application/json";
+ String tt1 = tt + ";charset=UTF-8";
+
+ Response response;
+ Invocation.Builder invocationBuilder = webTarget.request(tt1).accept(tt);
+
+ try {
+ response = invocationBuilder.post(Entity.entity(request, tt1));
+ } catch (ProcessingException e) {
+ throw new SvcLogicException(requestPostingException +
+ e.getLocalizedMessage(), e);
+ }
+ r.code = response.getStatus();
+ r.headers = response.getStringHeaders();
+ if (response.hasEntity()) {
+ r.body = response.readEntity(String.class);
+ }
+ }
+
+ long t2 = System.currentTimeMillis();
+ log.info(responseReceivedMessage, (t2 - t1));
+ log.info(responseHttpCodeMessage, r.code);
+ logHeaders(r.headers);
+ log.info("HTTP response:\n {}", r.body);
+
+ return r;
+ }
+
+ public void setUebServers(String uebServers) {
+ this.uebServers = uebServers;
+ }
+
+ public void setDefaultUebTemplateFileName(String defaultUebTemplateFileName) {
+ this.defaultUebTemplateFileName = defaultUebTemplateFileName;
+ }
+
+ private static class FileParam {
+
+ public String fileName;
+ public String url;
+ public String user;
+ public String password;
+ public HttpMethod httpMethod;
+ public String responsePrefix;
+ public boolean skipSending;
+ public String oAuthConsumerKey;
+ public String oAuthConsumerSecret;
+ public String oAuthSignatureMethod;
+ public String oAuthVersion;
+ public AuthType authtype;
+ }
+
+ private static class UebParam {
+
+ public String topic;
+ public String templateFileName;
+ public String rootVarName;
+ public String responsePrefix;
+ public boolean skipSending;
+ }