aboutsummaryrefslogtreecommitdiffstats
path: root/it/it-plugins/ws-plugin/src/main/java/LocalCallWebService.java
blob: 7de752bba251c8d63e836b43198daff7274327e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
 * SonarQube
 * Copyright (C) 2009-2017 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.RequestHandler;
import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.WebService;
import org.sonarqube.ws.MediaTypes;
import org.sonarqube.ws.WsCe;
import org.sonarqube.ws.client.GetRequest;
import org.sonarqube.ws.client.LocalWsClientFactory;
import org.sonarqube.ws.client.WsClient;
import org.sonarqube.ws.client.WsResponse;

public final class LocalCallWebService implements WebService {

  private final LocalWsClientFactory wsClientFactory;

  public LocalCallWebService(LocalWsClientFactory wsClientFactory) {
    this.wsClientFactory = wsClientFactory;
  }

  @Override
  public void define(Context context) {
    NewController controller = context.createController("local_ws_call");
    controller.createAction("protobuf_data").setHandler(new ProtobufHandler());
    controller.createAction("json_data").setHandler(new JsonHandler());
    controller.createAction("require_permission").setHandler(new RequirePermissionHandler());
    controller.done();
  }

  private class ProtobufHandler implements RequestHandler {
    @Override
    public void handle(Request request, Response response) throws Exception {
      WsClient client = wsClientFactory.newClient(request.localConnector());

      WsCe.TaskTypesWsResponse ceTaskTypes = client.ce().taskTypes();
      response.stream().setStatus(ceTaskTypes.getTaskTypesCount() > 0 ? 200 : 500);
    }
  }

  private class JsonHandler implements RequestHandler {
    @Override
    public void handle(Request request, Response response) throws Exception {
      WsClient client = wsClientFactory.newClient(request.localConnector());

      WsResponse jsonResponse = client.wsConnector().call(new GetRequest("api/issues/search"));
      boolean ok = jsonResponse.contentType().equals(MediaTypes.JSON)
        && jsonResponse.isSuccessful()
        && jsonResponse.content().contains("\"issues\":");
      response.stream().setStatus(ok ? 200 : 500);
    }
  }

  private class RequirePermissionHandler implements RequestHandler {
    @Override
    public void handle(Request request, Response response) throws Exception {
      WsClient client = wsClientFactory.newClient(request.localConnector());

      WsResponse jsonResponse = client.wsConnector().call(new GetRequest("api/system/info"));

      boolean ok = MediaTypes.JSON.equals(jsonResponse.contentType())
        && jsonResponse.isSuccessful()
        && jsonResponse.content().startsWith("{");
      response.stream().setStatus(ok ? 200 : 500);
    }
  }
}