3 * Copyright (C) 2009-2017 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.computation.task.projectanalysis.webhook;
22 import java.io.IOException;
23 import okhttp3.HttpUrl;
24 import okhttp3.MediaType;
25 import okhttp3.OkHttpClient;
26 import okhttp3.Request;
27 import okhttp3.RequestBody;
28 import okhttp3.Response;
29 import org.sonar.api.ce.ComputeEngineSide;
30 import org.sonar.api.utils.System2;
32 import static java.lang.String.format;
33 import static java.net.HttpURLConnection.HTTP_MOVED_PERM;
34 import static java.net.HttpURLConnection.HTTP_MOVED_TEMP;
35 import static okhttp3.internal.http.StatusLine.HTTP_PERM_REDIRECT;
36 import static okhttp3.internal.http.StatusLine.HTTP_TEMP_REDIRECT;
39 public class WebhookCallerImpl implements WebhookCaller {
41 private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
42 private static final String PROJECT_KEY_HEADER = "X-SonarQube-Project";
44 private final System2 system;
45 private final OkHttpClient okHttpClient;
47 public WebhookCallerImpl(System2 system, OkHttpClient okHttpClient) {
49 this.okHttpClient = newClientWithoutRedirect(okHttpClient);
53 public WebhookDelivery call(Webhook webhook, WebhookPayload payload) {
54 WebhookDelivery.Builder builder = new WebhookDelivery.Builder();
55 long startedAt = system.now();
62 Request request = buildHttpRequest(webhook, payload);
63 try (Response response = execute(request)) {
64 builder.setHttpStatus(response.code());
65 builder.setDurationInMs((int) (system.now() - startedAt));
67 } catch (Exception e) {
70 return builder.build();
73 private static Request buildHttpRequest(Webhook webhook, WebhookPayload payload) {
74 Request.Builder request = new Request.Builder();
75 request.url(webhook.getUrl());
76 request.header(PROJECT_KEY_HEADER, payload.getProjectKey());
77 RequestBody body = RequestBody.create(JSON, payload.getJson());
79 return request.build();
82 private Response execute(Request request) throws IOException {
83 Response response = okHttpClient.newCall(request).execute();
84 switch (response.code()) {
87 case HTTP_TEMP_REDIRECT:
88 case HTTP_PERM_REDIRECT:
89 // OkHttpClient does not follow the redirect with the same HTTP method. A POST is
90 // redirected to a GET. Because of that the redirect must be manually
93 // https://github.com/square/okhttp/blob/07309c1c7d9e296014268ebd155ebf7ef8679f6c/okhttp/src/main/java/okhttp3/internal/http/RetryAndFollowUpInterceptor.java#L316
94 // https://github.com/square/okhttp/issues/936#issuecomment-266430151
95 return followPostRedirect(response);
102 * Inspired by https://github.com/square/okhttp/blob/parent-3.6.0/okhttp/src/main/java/okhttp3/internal/http/RetryAndFollowUpInterceptor.java#L286
104 private Response followPostRedirect(Response response) throws IOException {
105 String location = response.header("Location");
106 if (location == null) {
107 throw new IllegalStateException(format("Missing HTTP header 'Location' in redirect of %s", response.request().url()));
109 HttpUrl url = response.request().url().resolve(location);
111 // Don't follow redirects to unsupported protocols.
113 throw new IllegalStateException(format("Unsupported protocol in redirect of %s to %s", response.request().url(), location));
116 Request.Builder redirectRequest = response.request().newBuilder();
117 redirectRequest.post(response.request().body());
118 response.body().close();
119 return okHttpClient.newCall(redirectRequest.url(url).build()).execute();
122 private static OkHttpClient newClientWithoutRedirect(OkHttpClient client) {
123 return client.newBuilder()
124 .followRedirects(false)
125 .followSslRedirects(false)