|
30 | 30 | import java.io.InputStream; |
31 | 31 | import java.net.URLDecoder; |
32 | 32 | import java.nio.charset.StandardCharsets; |
| 33 | +import java.util.List; |
| 34 | +import java.util.stream.Collectors; |
33 | 35 |
|
34 | 36 | import com.scalar.maven.webjar.ScalarProperties; |
| 37 | +import com.scalar.maven.webjar.ScalarProperties.ScalarSource; |
35 | 38 |
|
36 | 39 | import org.springframework.http.MediaType; |
37 | 40 | import org.springframework.http.ResponseEntity; |
@@ -182,4 +185,160 @@ protected String buildJsBundleUrl(String requestUrl, String scalarPath) { |
182 | 185 | * @return the string |
183 | 186 | */ |
184 | 187 | protected abstract String buildJsBundleUrl(String requestUrl); |
| 188 | + |
| 189 | + /** |
| 190 | + * Builds the configuration JSON for the Scalar API Reference. |
| 191 | + * |
| 192 | + * @return the configuration JSON as a string |
| 193 | + */ |
| 194 | + private String buildConfigurationJson() { |
| 195 | + StringBuilder config = new StringBuilder(); |
| 196 | + config.append("{"); |
| 197 | + |
| 198 | + // Add URL |
| 199 | + config.append("\n url: \"").append(escapeJson(scalarProperties.getUrl())).append("\""); |
| 200 | + |
| 201 | + // Add sources |
| 202 | + if (scalarProperties.getSources() != null && !scalarProperties.getSources().isEmpty()) { |
| 203 | + config.append(",\n sources: ").append(buildSourcesJsonArray(scalarProperties.getSources())); |
| 204 | + } |
| 205 | + |
| 206 | + // Add showSidebar |
| 207 | + if (!scalarProperties.isShowSidebar()) { |
| 208 | + config.append(",\n showSidebar: false"); |
| 209 | + } |
| 210 | + |
| 211 | + // Add hideModels |
| 212 | + if (scalarProperties.isHideModels()) { |
| 213 | + config.append(",\n hideModels: true"); |
| 214 | + } |
| 215 | + |
| 216 | + // Add hideTestRequestButton |
| 217 | + if (scalarProperties.isHideTestRequestButton()) { |
| 218 | + config.append(",\n hideTestRequestButton: true"); |
| 219 | + } |
| 220 | + |
| 221 | + // Add darkMode |
| 222 | + if (scalarProperties.isDarkMode()) { |
| 223 | + config.append(",\n darkMode: true"); |
| 224 | + } |
| 225 | + |
| 226 | + // Add hideDarkModeToggle |
| 227 | + if (scalarProperties.isHideDarkModeToggle()) { |
| 228 | + config.append(",\n hideDarkModeToggle: true"); |
| 229 | + } |
| 230 | + |
| 231 | + // Add customCss |
| 232 | + if (scalarProperties.getCustomCss() != null && !scalarProperties.getCustomCss().trim().isEmpty()) { |
| 233 | + config.append(",\n customCss: \"").append(escapeJson(scalarProperties.getCustomCss())).append("\""); |
| 234 | + } |
| 235 | + |
| 236 | + // Add theme |
| 237 | + if (scalarProperties.getTheme() != null && !"default".equals(scalarProperties.getTheme())) { |
| 238 | + config.append(",\n theme: \"").append(escapeJson(scalarProperties.getTheme())).append("\""); |
| 239 | + } |
| 240 | + |
| 241 | + // Add layout |
| 242 | + if (scalarProperties.getLayout() != null && !"modern".equals(scalarProperties.getLayout())) { |
| 243 | + config.append(",\n layout: \"").append(escapeJson(scalarProperties.getLayout())).append("\""); |
| 244 | + } |
| 245 | + |
| 246 | + // Add hideSearch |
| 247 | + if (scalarProperties.isHideSearch()) { |
| 248 | + config.append(",\n hideSearch: true"); |
| 249 | + } |
| 250 | + |
| 251 | + // Add documentDownloadType |
| 252 | + if (scalarProperties.getDocumentDownloadType() != null && !"both".equals(scalarProperties.getDocumentDownloadType())) { |
| 253 | + config.append(",\n documentDownloadType: \"").append(escapeJson(scalarProperties.getDocumentDownloadType())).append("\""); |
| 254 | + } |
| 255 | + |
| 256 | + config.append("\n}"); |
| 257 | + return config.toString(); |
| 258 | + } |
| 259 | + |
| 260 | + /** |
| 261 | + * Escapes a string for JSON output. |
| 262 | + * |
| 263 | + * @param input the input string |
| 264 | + * @return the escaped string |
| 265 | + */ |
| 266 | + private String escapeJson(String input) { |
| 267 | + if (input == null) { |
| 268 | + return ""; |
| 269 | + } |
| 270 | + return input.replace("\\", "\\\\") |
| 271 | + .replace("\"", "\\\"") |
| 272 | + .replace("\n", "\\n") |
| 273 | + .replace("\r", "\\r") |
| 274 | + .replace("\t", "\\t"); |
| 275 | + } |
| 276 | + |
| 277 | + /** |
| 278 | + * Builds the JSON for the OpenAPI reference sources |
| 279 | + * |
| 280 | + * @param sources list of OpenAPI reference sources |
| 281 | + * @return the sources as a JSON string |
| 282 | + */ |
| 283 | + private String buildSourcesJsonArray(List<ScalarSource> sources) { |
| 284 | + final StringBuilder builder = new StringBuilder("["); |
| 285 | + |
| 286 | + // Filter out sources with invalid urls |
| 287 | + final List<ScalarProperties.ScalarSource> filteredSources = sources.stream() |
| 288 | + .filter(source -> isNotNullOrBlank(source.getUrl())) |
| 289 | + .collect(Collectors.toList()); |
| 290 | + |
| 291 | + // Append each source to json array |
| 292 | + for (int i = 0; i < filteredSources.size(); i++) { |
| 293 | + final ScalarProperties.ScalarSource source = filteredSources.get(i); |
| 294 | + |
| 295 | + final String sourceJson = buildSourceJson(source); |
| 296 | + builder.append("\n").append(sourceJson); |
| 297 | + |
| 298 | + if (i != filteredSources.size() - 1) { |
| 299 | + builder.append(","); |
| 300 | + } |
| 301 | + } |
| 302 | + |
| 303 | + builder.append("\n]"); |
| 304 | + return builder.toString(); |
| 305 | + } |
| 306 | + |
| 307 | + /** |
| 308 | + * Builds the JSON for an OpenAPI reference source |
| 309 | + * |
| 310 | + * @param source the OpenAPI reference source |
| 311 | + * @return the source as a JSON string |
| 312 | + */ |
| 313 | + private String buildSourceJson(ScalarProperties.ScalarSource source) { |
| 314 | + final StringBuilder builder = new StringBuilder("{"); |
| 315 | + |
| 316 | + builder.append("\n url: \"").append(escapeJson(source.getUrl())).append("\""); |
| 317 | + |
| 318 | + |
| 319 | + if (isNotNullOrBlank(source.getTitle())) { |
| 320 | + builder.append(",\n title: \"").append(escapeJson(source.getTitle())).append("\""); |
| 321 | + } |
| 322 | + |
| 323 | + if (isNotNullOrBlank(source.getSlug())) { |
| 324 | + builder.append(",\n slug: \"").append(escapeJson(source.getSlug())).append("\""); |
| 325 | + } |
| 326 | + |
| 327 | + if (source.isDefault() != null) { |
| 328 | + builder.append(",\n default: ").append(source.isDefault()); |
| 329 | + } |
| 330 | + |
| 331 | + builder.append("\n}"); |
| 332 | + return builder.toString(); |
| 333 | + } |
| 334 | + |
| 335 | + /** |
| 336 | + * Returns whether a String is not null or blank |
| 337 | + * |
| 338 | + * @param input the string |
| 339 | + * @return whether the string is not null or blank |
| 340 | + */ |
| 341 | + private boolean isNotNullOrBlank(String input) { |
| 342 | + return input != null && !input.isBlank(); |
| 343 | + } |
185 | 344 | } |
0 commit comments