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.github.tomakehurst.wiremock.common.Notifier;
23 import com.github.tomakehurst.wiremock.core.Admin;
24 import com.github.tomakehurst.wiremock.extension.Parameters;
25 import com.github.tomakehurst.wiremock.extension.PostServeAction;
26 import com.github.tomakehurst.wiremock.http.HttpClientFactory;
27 import com.github.tomakehurst.wiremock.http.HttpHeader;
28 import com.github.tomakehurst.wiremock.stubbing.ServeEvent;
29 import org.apache.http.HttpResponse;
30 import org.apache.http.client.HttpClient;
31 import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
32 import org.apache.http.client.methods.HttpUriRequest;
33 import org.apache.http.entity.ByteArrayEntity;
34 import org.apache.http.util.EntityUtils;
36 import java.io.IOException;
37 import java.util.concurrent.Executors;
38 import java.util.concurrent.ScheduledExecutorService;
40 import static com.github.tomakehurst.wiremock.common.Exceptions.throwUnchecked;
41 import static com.github.tomakehurst.wiremock.common.LocalNotifier.notifier;
42 import static com.github.tomakehurst.wiremock.http.HttpClientFactory.getHttpRequestFor;
43 import static java.util.concurrent.TimeUnit.SECONDS;
45 public class Webhooks extends PostServeAction {
47 private final ScheduledExecutorService scheduler;
48 private final HttpClient httpClient;
51 scheduler = Executors.newScheduledThreadPool(10);
52 httpClient = HttpClientFactory.createClient();
56 public String getName() {
61 public void doAction(ServeEvent serveEvent, Admin admin, Parameters parameters) {
62 final WebhookDefinition definition = parameters.as(WebhookDefinition.class);
63 final Notifier notifier = notifier();
69 HttpUriRequest request = buildRequest(definition);
72 HttpResponse response = httpClient.execute(request);
74 String.format("Webhook %s request to %s returned status %s\n\n%s",
75 definition.getMethod(),
77 response.getStatusLine(),
78 EntityUtils.toString(response.getEntity())
81 System.out.println(String.format("Webhook %s request to %s returned status %s\n\n%s",
82 definition.getMethod(),
84 response.getStatusLine(),
85 EntityUtils.toString(response.getEntity())
88 } catch (IOException e) {
98 private static HttpUriRequest buildRequest(WebhookDefinition definition) {
99 HttpUriRequest request = getHttpRequestFor(
100 definition.getMethod(),
101 definition.getUrl().toString()
104 for (HttpHeader header: definition.getHeaders().all()) {
105 request.addHeader(header.key(), header.firstValue());
108 if (definition.getMethod().hasEntity()) {
109 HttpEntityEnclosingRequestBase entityRequest = (HttpEntityEnclosingRequestBase) request;
110 entityRequest.setEntity(new ByteArrayEntity(definition.getBinaryBody()));
116 public static WebhookDefinition webhook() {
117 return new WebhookDefinition();