e3fc286cb6f0b93fc401f3aec806f9ead6513b40
[integration.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.integration
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.onap.integration.test.mocks.sniroemulator.extension;
21
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;
35
36 import java.io.IOException;
37 import java.util.concurrent.Executors;
38 import java.util.concurrent.ScheduledExecutorService;
39
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;
44
45 public class Webhooks extends PostServeAction {
46
47     private final ScheduledExecutorService scheduler;
48     private final HttpClient httpClient;
49
50     public Webhooks() {
51         scheduler = Executors.newScheduledThreadPool(10);
52         httpClient = HttpClientFactory.createClient();
53     }
54
55     @Override
56     public String getName() {
57         return "webhook";
58     }
59
60     @Override
61     public void doAction(ServeEvent serveEvent, Admin admin, Parameters parameters) {
62         final WebhookDefinition definition = parameters.as(WebhookDefinition.class);
63         final Notifier notifier = notifier();
64
65         scheduler.schedule(
66             new Runnable() {
67                 @Override
68                 public void run() {
69                     HttpUriRequest request = buildRequest(definition);
70
71                     try {
72                         HttpResponse response = httpClient.execute(request);
73                         notifier.info(
74                             String.format("Webhook %s request to %s returned status %s\n\n%s",
75                                 definition.getMethod(),
76                                 definition.getUrl(),
77                                 response.getStatusLine(),
78                                 EntityUtils.toString(response.getEntity())
79                             )                            
80                         );
81                         System.out.println(String.format("Webhook %s request to %s returned status %s\n\n%s",
82                                         definition.getMethod(),
83                                         definition.getUrl(),
84                                         response.getStatusLine(),
85                                         EntityUtils.toString(response.getEntity())                              
86                                         )
87                         );
88                     } catch (IOException e) {
89                         throwUnchecked(e);
90                     }
91                 }
92             },
93             0L,
94             SECONDS
95         );
96     }
97
98     private static HttpUriRequest buildRequest(WebhookDefinition definition) {
99         HttpUriRequest request = getHttpRequestFor(
100                 definition.getMethod(),
101                 definition.getUrl().toString()
102         );
103
104         for (HttpHeader header: definition.getHeaders().all()) {
105             request.addHeader(header.key(), header.firstValue());
106         }
107
108         if (definition.getMethod().hasEntity()) {
109             HttpEntityEnclosingRequestBase entityRequest = (HttpEntityEnclosingRequestBase) request;
110             entityRequest.setEntity(new ByteArrayEntity(definition.getBinaryBody()));
111         }
112
113         return request;
114     }
115
116     public static WebhookDefinition webhook() {
117         return new WebhookDefinition();
118     }
119 }