e1e1726687153fa778bcf6914315666191e02901
[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.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;
38
39
40 import java.io.IOException;
41 import java.util.Map;
42 import java.util.concurrent.Executors;
43 import java.util.concurrent.ScheduledExecutorService;
44
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;
49
50 public class Webhooks extends PostServeAction {
51
52     private final ScheduledExecutorService scheduler;
53     private final HttpClient httpClient;
54
55     public Webhooks() {
56         scheduler = Executors.newScheduledThreadPool(10);
57         httpClient = HttpClientFactory.createClient();
58     }
59
60     @Override
61     public String getName() {
62         return "webhook";
63     }
64
65     @Override
66     public void doAction(ServeEvent serveEvent, Admin admin, Parameters parameters) {
67         final WebhookDefinition definition = parameters.as(WebhookDefinition.class);
68         final Notifier notifier = notifier();
69
70
71         scheduler.schedule(
72             new Runnable() {
73                 @Override
74                 public void run() {
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);
80
81                     try {
82                        // notifier.info("This is a request: \n" + Json.prettyPrint(serveEvent.getRequest().getBodyAsString()));
83
84                         HttpResponse response = httpClient.execute(request);
85                         notifier.info(
86                             String.format("Webhook %s request to %s returned status %s\n\n%s",
87                                 definition.getMethod(),
88                                 definition.getUrl(),
89                                 response.getStatusLine(),
90                                 EntityUtils.toString(response.getEntity())
91                             )                            
92                         );
93                         System.out.println(String.format("Webhook %s request to %s returned status %s\n\n%s",
94                                         definition.getMethod(),
95                                         definition.getUrl(),
96                                         response.getStatusLine(),
97                                         EntityUtils.toString(response.getEntity())                              
98                                         )
99                         );
100                     } catch (IOException e) {
101                         e.printStackTrace();
102                         throwUnchecked(e);
103                     }
104                 }
105             },
106             0L,
107             SECONDS
108         );
109     }
110
111     private static HttpUriRequest buildRequest(WebhookDefinition definition) {
112         HttpUriRequest request = getHttpRequestFor(
113                 definition.getMethod(),
114                 definition.getUrl().toString()
115         );
116
117         for (HttpHeader header: definition.getHeaders().all()) {
118             request.addHeader(header.key(), header.firstValue());
119         }
120
121         if (definition.getMethod().hasEntity()) {
122             HttpEntityEnclosingRequestBase entityRequest = (HttpEntityEnclosingRequestBase) request;
123             entityRequest.setEntity(new ByteArrayEntity(definition.getBinaryBody()));
124         }
125
126         return request;
127     }
128
129     public static WebhookDefinition webhook() {
130         return new WebhookDefinition();
131     }
132 }