aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-ws-generator/src/main/java/org/sonarqube/wsgenerator/Helper.java
blob: b1da0eb217f889fefc2e0d75e8f1b581d34beae3 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
 * SonarQube
 * Copyright (C) 2009-2025 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.
 */
package org.sonarqube.wsgenerator;

import com.google.common.base.CaseFormat;
import com.google.gson.JsonPrimitive;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.Arrays.asList;

public class Helper {

  static final Set<String> PATH_EXCLUSIONS = new HashSet<>(asList("api/orchestrator"));
  private static final String OUTPUT_DIR = "build/generated-sources/results";
  private final Map<String, List<String[]>> responseTypes;

  public Helper() {
    InputStream inputStream = Helper.class.getResourceAsStream("/responseClasses.config");
    responseTypes = new BufferedReader(new InputStreamReader(inputStream))
      .lines()
      .map(line -> line.split("\\s+"))
      .collect(Collectors.groupingBy(arr -> arr[0]));
  }

  public boolean isIncluded(String path) {
    return !PATH_EXCLUSIONS.contains(path);
  }

  public String packageName() {
    return "org.sonarqube.ws.client";
  }

  public String packageName(String path) {
    return packageName() + "." + rawName(path).toLowerCase();
  }

  private String rawName(String path) {
    String x = path.replaceFirst("^api\\/", "");
    if (x.contains("_")) {
      return CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, x.toLowerCase());
    }
    return capitalizeFirstLetter(x);
  }

  public String className(String path) {
    String name = rawName(path);
    return capitalizeFirstLetter(name) + "Service";
  }
  public String defaultWsClientFieldName(String path) {
    String name = rawName(path);
    return lowercaseFirstLetter(name) + "Service";
  }

  public String defaultWsClientMethodName(String path) {
    String name = rawName(path);
    return lowercaseFirstLetter(name);
  }

  public String webserviceTypeImport(String path) {
    return "import " + packageName(path) + "." + className(path) + ";";
  }

  private String capitalizeFirstLetter(String name) {
    return name.substring(0, 1).toUpperCase() + name.substring(1);
  }

  private String lowercaseFirstLetter(String name) {
    return name.substring(0, 1).toLowerCase() + name.substring(1);
  }

  public String responseType(String path, String action) {
    return responseTypeFullyQualified(path, action).replaceFirst("^.*\\.", "");
  }

  private String responseTypeFullyQualified(String path, String action) {
    String fullPath = path + "/" + action;
    List<String[]> responseTypesConfig = responseTypes.get(fullPath);
    String fullyQualified;
    if (responseTypesConfig == null) {
      fullyQualified = guessResponseType(path, action);
      responseTypes.put(fullPath, Collections.singletonList(new String[] {fullPath, fullyQualified}));
    } else {
      fullyQualified = responseTypesConfig.get(0)[1];
    }
    return fullyQualified;
  }

  private String guessResponseType(String path, String action) {
    return guessResponseOuterClassName(path).flatMap(
      potentialClassName -> guessResponseInnerClassName(action).flatMap(potentialInnerClassName -> {
        try {
          String guess = "org.sonarqube.ws." + potentialClassName + "$" + potentialInnerClassName;
          Helper.class.forName(guess);
          return Stream.of(guess.replaceFirst("\\$", "."));
        } catch (ClassNotFoundException e) {
        }
        return Stream.empty();
      })).findFirst().orElseGet(() -> "String");
  }

  private Stream<String> guessResponseInnerClassName(String action) {
    return Stream.of(
      rawName(action) + "Response",
      rawName(action) + "WsResponse",
      "Ws" + rawName(action) + "Response");
  }

  private Stream<String> guessResponseOuterClassName(String path) {
    return Stream.of(
      rawName(path),
      "Ws" + rawName(path),
      rawName(path) + "Ws");
  }

  public String responseTypeImport(String path, String action) {
    String fullyQualified = responseTypeFullyQualified(path, action);
    if ("String".equals(fullyQualified)) {
      return null;
    }
    return "import " + fullyQualified + ";";
  }

  public String methodName(String path, String action) {
    return lowercaseFirstLetter(rawName(action));
  }

  public String requestType(String path, String action) {
    return rawName(action) + "Request";
  }

  public String parameterGetter(String parameter) {
    return "get" + rawName(parameter);
  }

  public String parameterSetter(String parameter) {
    return "set" + rawName(parameter);
  }

  public String setterParameter(String parameter) {
    return lowercaseFirstLetter(rawName(parameter));
  }

  public String setterParameterType(String parameter, JsonPrimitive parameterDescription) {
    if (parameter.equals("values") || parameter.equals("fieldValues") || parameter.equals("keys")) {
      return "List<String>";
    }
    if (parameterDescription != null && parameterDescription.getAsString().matches(".*[Cc]omma.?separated.*|.*[Ll]ist of.*")) {
      return "List<String>";
    }
    return "String";
  }

  public String apiDocUrl(String path) {
    return "https://next.sonarqube.com/sonarqube/web_api/" + path;
  }

  public String apiDocUrl(String path, String action) {
    return apiDocUrl(path) + "/" + action;
  }

  public String file(String path) {
    return OUTPUT_DIR + "/org/sonarqube/ws/client/" + rawName(path).toLowerCase() + "/" + className(path) + ".java";
  }

  public String defaultWsClientFile() {
    return OUTPUT_DIR + "/org/sonarqube/ws/client/DefaultWsClient.java";
  }

  public String wsClientFile() {
    return OUTPUT_DIR + "/org/sonarqube/ws/client/WsClient.java";
  }

  public String packageInfoFile() {
    return OUTPUT_DIR + "/org/sonarqube/ws/client/package-info.java";
  }

  public String packageInfoFile(String path) {
    return OUTPUT_DIR + "/org/sonarqube/ws/client/" + rawName(path).toLowerCase() + "/package-info.java";
  }

  public String requestFile(String path, String action) {
    return OUTPUT_DIR + "/org/sonarqube/ws/client/" + rawName(path).toLowerCase() + "/" + requestType(path, action) + ".java";
  }
}