|
| 1 | +/** |
| 2 | + * Jooby https://jooby.io |
| 3 | + * Apache License Version 2.0 https://jooby.io/LICENSE.txt |
| 4 | + * Copyright 2014 Edgar Espina |
| 5 | + */ |
| 6 | +package io.jooby; |
| 7 | + |
| 8 | +import io.jooby.SneakyThrows.Consumer2; |
| 9 | +import org.apache.commons.io.IOUtils; |
| 10 | + |
| 11 | +import javax.annotation.Nonnull; |
| 12 | +import java.io.IOException; |
| 13 | +import java.util.Arrays; |
| 14 | +import java.util.EnumSet; |
| 15 | +import java.util.HashMap; |
| 16 | +import java.util.Map; |
| 17 | +import java.util.Optional; |
| 18 | +import java.util.function.BiConsumer; |
| 19 | + |
| 20 | +public class OpenAPIModule implements Extension { |
| 21 | + |
| 22 | + public enum Format { |
| 23 | + JSON, |
| 24 | + YAML |
| 25 | + } |
| 26 | + |
| 27 | + private static final String REDOC = "<!DOCTYPE html>\n" |
| 28 | + + "<html>\n" |
| 29 | + + " <head>\n" |
| 30 | + + " <title>ReDoc</title>\n" |
| 31 | + + " <!-- needed for adaptive design -->\n" |
| 32 | + + " <meta charset=\"utf-8\"/>\n" |
| 33 | + + " <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n" |
| 34 | + + " <link href=\"https://fonts.googleapis.com/css?family=Montserrat:300,400,700|Roboto:300,400,700\" rel=\"stylesheet\">\n" |
| 35 | + + "\n" |
| 36 | + + " <!--\n" |
| 37 | + + " ReDoc doesn't change outer page styles\n" |
| 38 | + + " -->\n" |
| 39 | + + " <style>\n" |
| 40 | + + " body {\n" |
| 41 | + + " margin: 0;\n" |
| 42 | + + " padding: 0;\n" |
| 43 | + + " }\n" |
| 44 | + + " </style>\n" |
| 45 | + + " </head>\n" |
| 46 | + + " <body>\n" |
| 47 | + + " <redoc spec-url='${openAPIPath}'></redoc>\n" |
| 48 | + + " <script src=\"${redocPath}/bundles/redoc.standalone.js\"> </script>\n" |
| 49 | + + " </body>\n" |
| 50 | + + "</html>"; |
| 51 | + |
| 52 | + private static final String OPENAPI_PATH = "https://petstore.swagger.io/v2/swagger.json"; |
| 53 | + |
| 54 | + private final String openAPIPath; |
| 55 | + private String swaggerUIPath = "/swagger"; |
| 56 | + private String redocPath = "/redoc"; |
| 57 | + private EnumSet<Format> format = EnumSet.of(Format.JSON, Format.YAML); |
| 58 | + |
| 59 | + public OpenAPIModule(String path) { |
| 60 | + this.openAPIPath = Router.normalizePath(path); |
| 61 | + } |
| 62 | + |
| 63 | + public OpenAPIModule() { |
| 64 | + this("/"); |
| 65 | + } |
| 66 | + |
| 67 | + public OpenAPIModule swaggerUI(String path) { |
| 68 | + this.swaggerUIPath = Router.normalizePath(path); |
| 69 | + return this; |
| 70 | + } |
| 71 | + |
| 72 | + public OpenAPIModule redoc(String path) { |
| 73 | + this.redocPath = Router.normalizePath(path); |
| 74 | + return this; |
| 75 | + } |
| 76 | + |
| 77 | + public OpenAPIModule format(Format... format) { |
| 78 | + this.format = EnumSet.copyOf(Arrays.asList(format)); |
| 79 | + return this; |
| 80 | + } |
| 81 | + |
| 82 | + @Override public void install(@Nonnull Jooby application) throws Exception { |
| 83 | + String dir = Optional.ofNullable(application.getClass().getPackage()) |
| 84 | + .map(Package::getName) |
| 85 | + .orElse("/") |
| 86 | + .replace(".", "/"); |
| 87 | + |
| 88 | + for (Format ext : format) { |
| 89 | + String filename = "/openapi." + ext.name().toLowerCase(); |
| 90 | + String openAPIFileLocation = Router.normalizePath(dir) + filename; |
| 91 | + application.assets(fullPath(openAPIPath, filename), openAPIFileLocation); |
| 92 | + } |
| 93 | + |
| 94 | + /** Configure UI: */ |
| 95 | + configureUI(application); |
| 96 | + } |
| 97 | + |
| 98 | + private void configureUI(@Nonnull Jooby application) { |
| 99 | + Map<String, Consumer2<Jooby, AssetSource>> ui = new HashMap<>(); |
| 100 | + ui.put("swagger-ui", this::swaggerUI); |
| 101 | + ui.put("redoc", this::redoc); |
| 102 | + for (Map.Entry<String, Consumer2<Jooby, AssetSource>> e : ui.entrySet()) { |
| 103 | + String name = e.getKey(); |
| 104 | + Optional<AssetSource> source = assetSource(application.getClassLoader(), name); |
| 105 | + if (source.isPresent()) { |
| 106 | + if (format.contains(Format.JSON)) { |
| 107 | + Consumer2<Jooby, AssetSource> consumer = e.getValue(); |
| 108 | + consumer.accept(application, source.get()); |
| 109 | + } else { |
| 110 | + application.getLog().debug("{} is disabled when json format is not supported", name); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + private Optional<AssetSource> assetSource(ClassLoader loader, String name) { |
| 117 | + try { |
| 118 | + return Optional.of(AssetSource.webjars(loader, name)); |
| 119 | + } catch (Exception x) { |
| 120 | + return Optional.empty(); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + private void redoc(Jooby application, AssetSource source) throws IOException { |
| 125 | + application.assets(redocPath + "/*", source); |
| 126 | + String openAPIJSON = fullPath( |
| 127 | + fullPath(application.getContextPath(), openAPIPath), "/openapi.json"); |
| 128 | + String template = REDOC |
| 129 | + .replace("${openAPIPath}", openAPIJSON) |
| 130 | + .replace("${redocPath}", fullPath(application.getContextPath(), redocPath)); |
| 131 | + application |
| 132 | + .get(redocPath, ctx -> ctx.setResponseType(MediaType.html).send(template)); |
| 133 | + } |
| 134 | + |
| 135 | + private void swaggerUI(Jooby application, AssetSource source) throws IOException { |
| 136 | + String template = swaggerTemplate(source, |
| 137 | + fullPath(application.getContextPath(), swaggerUIPath), |
| 138 | + fullPath(application.getContextPath(), openAPIPath)); |
| 139 | + |
| 140 | + application.assets(swaggerUIPath + "/*", source); |
| 141 | + application.get(swaggerUIPath, ctx -> ctx.setResponseType(MediaType.html).send(template)); |
| 142 | + } |
| 143 | + |
| 144 | + static String swaggerTemplate(AssetSource source, String contextPath, String openAPIPath) |
| 145 | + throws IOException { |
| 146 | + String template = IOUtils |
| 147 | + .toString(source.resolve("index.html").stream(), "UTF-8") |
| 148 | + .replace("./", contextPath + "/"); |
| 149 | + if (template.contains(OPENAPI_PATH)) { |
| 150 | + return template.replace(OPENAPI_PATH, fullPath(openAPIPath, "/openapi.json")); |
| 151 | + } else { |
| 152 | + throw new IllegalStateException("Unable to find openAPI path from template"); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + private static String fullPath(String contextPath, String path) { |
| 157 | + return Router.noTrailingSlash(Router.normalizePath(contextPath + path)); |
| 158 | + } |
| 159 | +} |
0 commit comments