2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.onap.integration.test.mocks.sniroemulator.extension;
22 import com.fasterxml.jackson.core.JsonParser;
23 import com.fasterxml.jackson.databind.JsonNode;
24 import com.github.tomakehurst.wiremock.common.Notifier;
25 import com.github.tomakehurst.wiremock.core.Admin;
26 import com.github.tomakehurst.wiremock.extension.Parameters;
27 import com.github.tomakehurst.wiremock.extension.PostServeAction;
28 import com.github.tomakehurst.wiremock.http.HttpClientFactory;
29 import com.github.tomakehurst.wiremock.http.HttpHeader;
30 import com.github.tomakehurst.wiremock.stubbing.ServeEvent;
31 import org.apache.http.HttpResponse;
32 import org.apache.http.client.HttpClient;
33 import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
34 import org.apache.http.client.methods.HttpUriRequest;
35 import org.apache.http.entity.ByteArrayEntity;
36 import org.apache.http.util.EntityUtils;
37 import com.github.tomakehurst.wiremock.common.Json;
40 import java.io.IOException;
42 import java.util.concurrent.Executors;
43 import java.util.concurrent.ScheduledExecutorService;
45 import static com.github.tomakehurst.wiremock.common.Exceptions.throwUnchecked;
46 import static com.github.tomakehurst.wiremock.common.LocalNotifier.notifier;
47 import static com.github.tomakehurst.wiremock.http.HttpClientFactory.getHttpRequestFor;
48 import static java.util.concurrent.TimeUnit.SECONDS;
50 public class Webhooks extends PostServeAction {
52 private final ScheduledExecutorService scheduler;
53 private final HttpClient httpClient;
56 scheduler = Executors.newScheduledThreadPool(10);
57 httpClient = HttpClientFactory.createClient();
61 public String getName() {
66 public void doAction(ServeEvent serveEvent, Admin admin, Parameters parameters) {
67 final WebhookDefinition definition = parameters.as(WebhookDefinition.class);
68 final Notifier notifier = notifier();
75 JsonNode node = Json.node(serveEvent.getRequest().getBodyAsString());
76 String callBackUrl = node.get("requestInfo").get("callbackUrl").asText();
77 notifier.info("!!! Call Back Url : \n" + callBackUrl);
78 definition.withUrl(callBackUrl);
79 HttpUriRequest request = buildRequest(definition);
82 // notifier.info("This is a request: \n" + Json.prettyPrint(serveEvent.getRequest().getBodyAsString()));
84 HttpResponse response = httpClient.execute(request);
86 String.format("Webhook %s request to %s returned status %s\n\n%s",
87 definition.getMethod(),
89 response.getStatusLine(),
90 EntityUtils.toString(response.getEntity())
93 System.out.println(String.format("Webhook %s request to %s returned status %s\n\n%s",
94 definition.getMethod(),
96 response.getStatusLine(),
97 EntityUtils.toString(response.getEntity())
100 } catch (IOException e) {
111 private static HttpUriRequest buildRequest(WebhookDefinition definition) {
112 HttpUriRequest request = getHttpRequestFor(
113 definition.getMethod(),
114 definition.getUrl().toString()
117 for (HttpHeader header: definition.getHeaders().all()) {
118 request.addHeader(header.key(), header.firstValue());
121 if (definition.getMethod().hasEntity()) {
122 HttpEntityEnclosingRequestBase entityRequest = (HttpEntityEnclosingRequestBase) request;
123 entityRequest.setEntity(new ByteArrayEntity(definition.getBinaryBody()));
129 public static WebhookDefinition webhook() {
130 return new WebhookDefinition();