You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by li...@apache.org on 2022/10/31 14:02:07 UTC
[servicecomb-java-chassis] 03/04: [SCB-2713]remove swagger html render in inspector module
This is an automated email from the ASF dual-hosted git repository.
liubao pushed a commit to branch 1.3.x
in repository https://gitbox.apache.org/repos/asf/servicecomb-java-chassis.git
commit 29ff626c5177cb7f4510a4a1af2396f44899e5aa
Author: liubao <bi...@qq.com>
AuthorDate: Mon Oct 31 16:59:47 2022 +0800
[SCB-2713]remove swagger html render in inspector module
---
inspector/pom.xml | 19 ------
.../inspector/internal/InspectorConfig.java | 11 ----
.../inspector/internal/InspectorImpl.java | 69 +---------------------
.../internal/swagger/AppendStyleProcessor.java | 34 -----------
.../inspector/internal/TestInspectorImpl.java | 69 +---------------------
5 files changed, 4 insertions(+), 198 deletions(-)
diff --git a/inspector/pom.xml b/inspector/pom.xml
index e523599ee..7e3157097 100644
--- a/inspector/pom.xml
+++ b/inspector/pom.xml
@@ -30,17 +30,6 @@
<artifactId>inspector</artifactId>
<name>Java Chassis::Inspector</name>
- <repositories>
- <repository>
- <snapshots>
- <enabled>false</enabled>
- </snapshots>
- <id>jcenter-releases</id>
- <name>jcenter</name>
- <url>https://jcenter.bintray.com</url>
- </repository>
- </repositories>
-
<dependencies>
<dependency>
<groupId>org.apache.servicecomb</groupId>
@@ -50,14 +39,6 @@
<groupId>org.apache.servicecomb</groupId>
<artifactId>provider-jaxrs</artifactId>
</dependency>
- <dependency>
- <groupId>org.asciidoctor</groupId>
- <artifactId>asciidoctorj</artifactId>
- </dependency>
- <dependency>
- <groupId>io.github.swagger2markup</groupId>
- <artifactId>swagger2markup</artifactId>
- </dependency>
<dependency>
<groupId>org.apache.servicecomb</groupId>
diff --git a/inspector/src/main/java/org/apache/servicecomb/inspector/internal/InspectorConfig.java b/inspector/src/main/java/org/apache/servicecomb/inspector/internal/InspectorConfig.java
index 70683fc6a..50c8ec11f 100644
--- a/inspector/src/main/java/org/apache/servicecomb/inspector/internal/InspectorConfig.java
+++ b/inspector/src/main/java/org/apache/servicecomb/inspector/internal/InspectorConfig.java
@@ -24,9 +24,6 @@ public class InspectorConfig {
@InjectProperty(keys = "enabled", defaultValue = "true")
private boolean enabled;
- @InjectProperty(keys = "swagger.html.asciidoctorCss", defaultValue = "https://cdnjs.cloudflare.com/ajax/libs/asciidoctor.js/1.5.9/css/asciidoctor.css")
- private String asciidoctorCss;
-
public boolean isEnabled() {
return enabled;
}
@@ -34,12 +31,4 @@ public class InspectorConfig {
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
-
- public String getAsciidoctorCss() {
- return asciidoctorCss;
- }
-
- public void setAsciidoctorCss(String asciidoctorCss) {
- this.asciidoctorCss = asciidoctorCss;
- }
}
diff --git a/inspector/src/main/java/org/apache/servicecomb/inspector/internal/InspectorImpl.java b/inspector/src/main/java/org/apache/servicecomb/inspector/internal/InspectorImpl.java
index c1892f67e..32d7c35d7 100644
--- a/inspector/src/main/java/org/apache/servicecomb/inspector/internal/InspectorImpl.java
+++ b/inspector/src/main/java/org/apache/servicecomb/inspector/internal/InspectorImpl.java
@@ -57,31 +57,18 @@ import org.apache.servicecomb.core.Transport;
import org.apache.servicecomb.foundation.common.part.InputStreamPart;
import org.apache.servicecomb.inspector.internal.model.DynamicPropertyView;
import org.apache.servicecomb.inspector.internal.model.PriorityPropertyView;
-import org.apache.servicecomb.inspector.internal.swagger.AppendStyleProcessor;
import org.apache.servicecomb.inspector.internal.swagger.SchemaFormat;
import org.apache.servicecomb.serviceregistry.RegistryUtils;
import org.apache.servicecomb.swagger.SwaggerUtils;
import org.apache.servicecomb.swagger.invocation.Response;
import org.apache.servicecomb.swagger.invocation.exception.InvocationException;
-import org.asciidoctor.Asciidoctor;
-import org.asciidoctor.Asciidoctor.Factory;
-import org.asciidoctor.Attributes;
-import org.asciidoctor.AttributesBuilder;
-import org.asciidoctor.OptionsBuilder;
-import org.asciidoctor.SafeMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import com.google.common.collect.Ordering;
import com.netflix.config.DynamicProperty;
-import io.github.swagger2markup.Swagger2MarkupConfig;
-import io.github.swagger2markup.Swagger2MarkupConverter;
-import io.github.swagger2markup.Swagger2MarkupConverter.Builder;
-import io.github.swagger2markup.builder.Swagger2MarkupConfigBuilder;
import io.swagger.annotations.ApiResponse;
import io.swagger.models.Swagger;
-import io.swagger.models.parameters.Parameter;
@Path("/inspector")
public class InspectorImpl {
@@ -95,8 +82,6 @@ public class InspectorImpl {
private Map<String, String> schemas;
- private volatile Asciidoctor asciidoctor;
-
private StaticResourceHandler resourceHandler = new ClassPathStaticResourceHandler();
private PriorityPropertyManager priorityPropertyManager;
@@ -173,9 +158,6 @@ public class InspectorImpl {
zos.putNextEntry(new ZipEntry(entry.getKey() + format.getSuffix()));
String content = entry.getValue();
- if (SchemaFormat.HTML.equals(format)) {
- content = swaggerToHtml(content);
- }
zos.write(content.getBytes(StandardCharsets.UTF_8));
zos.closeEntry();
}
@@ -204,13 +186,7 @@ public class InspectorImpl {
format = SchemaFormat.SWAGGER;
}
- byte[] bytes;
- if (SchemaFormat.HTML.equals(format)) {
- String html = swaggerToHtml(swaggerContent);
- bytes = html.getBytes(StandardCharsets.UTF_8);
- } else {
- bytes = swaggerContent.getBytes(StandardCharsets.UTF_8);
- }
+ byte[] bytes = swaggerContent.getBytes(StandardCharsets.UTF_8);
Part part = new InputStreamPart(null, new ByteArrayInputStream(bytes))
.setSubmittedFileName(schemaId + format.getSuffix());
@@ -223,49 +199,6 @@ public class InspectorImpl {
return response;
}
- // swagger not support cookie parameter
- // so if swaggerContent contains cookie parameter, will cause problem.
- private String swaggerToHtml(String swaggerContent) {
- if (asciidoctor == null) {
- synchronized (this) {
- if (asciidoctor == null) {
- // very slow, need a few seconds
- LOGGER.info("create AsciiDoctor start.");
- asciidoctor = Factory.create();
- asciidoctor.javaExtensionRegistry().docinfoProcessor(AppendStyleProcessor.class);
- LOGGER.info("create AsciiDoctor end.");
- }
- }
- }
-
- // swagger to markup
- Builder markupBuilder = Swagger2MarkupConverter.from(SwaggerUtils.parseSwagger(swaggerContent));
- // default not support cookie parameter
- // so must customize config
- Swagger2MarkupConfig markupConfig = new Swagger2MarkupConfigBuilder()
- .withParameterOrdering(Ordering
- .explicit("path", "query", "header", "cookie", "formData", "body")
- .onResultOf(Parameter::getIn))
- .build();
- String markup = markupBuilder.withConfig(markupConfig).build().toString();
-
- // markup to html
- OptionsBuilder builder = OptionsBuilder.options();
- builder.docType("book")
- .backend("html5")
- .headerFooter(true)
- .safe(SafeMode.UNSAFE)
- .attributes(AttributesBuilder.attributes()
- .attribute("toclevels", 3)
- .attribute(Attributes.TOC_2, true)
- .attribute(Attributes.TOC_POSITION, "left")
- .attribute(Attributes.LINK_CSS, true)
- .attribute(Attributes.STYLESHEET_NAME, inspectorConfig.getAsciidoctorCss())
- .attribute(Attributes.SECTION_NUMBERS, true)
- .attribute(Attributes.SECT_NUM_LEVELS, 4));
- return asciidoctor.convert(markup, builder.asMap());
- }
-
@Path("/{path : .+}")
@GET
@ApiResponse(code = 200, message = "", response = File.class)
diff --git a/inspector/src/main/java/org/apache/servicecomb/inspector/internal/swagger/AppendStyleProcessor.java b/inspector/src/main/java/org/apache/servicecomb/inspector/internal/swagger/AppendStyleProcessor.java
deleted file mode 100644
index cd1aa6f23..000000000
--- a/inspector/src/main/java/org/apache/servicecomb/inspector/internal/swagger/AppendStyleProcessor.java
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.servicecomb.inspector.internal.swagger;
-
-import org.asciidoctor.ast.Document;
-import org.asciidoctor.extension.DocinfoProcessor;
-import org.asciidoctor.extension.Location;
-import org.asciidoctor.extension.LocationType;
-
-@Location(LocationType.HEADER)
-public class AppendStyleProcessor extends DocinfoProcessor {
- @Override
- public String process(Document document) {
- return "<style>\n"
- + "html,body{height:100%;overflow-y:scroll;}"
- + "#header>h1 {overflow: hidden;font-size: 1.75em}\n"
- + "#header,#content,#footnotes,#footer{max-width: 90%}\n"
- + "</style>";
- }
-}
diff --git a/inspector/src/test/java/org/apache/servicecomb/inspector/internal/TestInspectorImpl.java b/inspector/src/test/java/org/apache/servicecomb/inspector/internal/TestInspectorImpl.java
index 693a10b46..4e848388b 100644
--- a/inspector/src/test/java/org/apache/servicecomb/inspector/internal/TestInspectorImpl.java
+++ b/inspector/src/test/java/org/apache/servicecomb/inspector/internal/TestInspectorImpl.java
@@ -154,30 +154,6 @@ public class TestInspectorImpl {
}
}
- @Test
- public void downloadSchemas_html(@Mocked Microservice microservice) throws IOException {
- new Expectations(RegistryUtils.class) {
- {
- RegistryUtils.getMicroservice();
- result = microservice;
- microservice.getServiceName();
- result = "ms";
- }
- };
-
- Response response = inspector.downloadSchemas(SchemaFormat.HTML);
- Part part = response.getResult();
- Assert.assertEquals("ms.html.zip", part.getSubmittedFileName());
-
- try (InputStream is = part.getInputStream()) {
- Map<String, String> unziped = unzip(is);
-
- Assert.assertEquals(schemas.size(), unziped.size());
- Assert.assertTrue(unziped.get("schema1.html").endsWith("</html>"));
- Assert.assertTrue(unziped.get("schema2.html").endsWith("</html>"));
- }
- }
-
@Test
public void downloadSchemas_failed() {
SchemaFormat format = SchemaFormat.SWAGGER;
@@ -257,44 +233,6 @@ public class TestInspectorImpl {
}
}
- @Test
- public void getSchemaContentById_view_html() throws IOException {
- testViewHtmlById("schema1");
- testViewHtmlById("schema2");
- }
-
- private void testViewHtmlById(String schemaId) throws IOException {
- Response response = inspector.getSchemaContentById(schemaId, SchemaFormat.HTML, false);
-
- Part part = response.getResult();
- Assert.assertEquals(schemaId + ".html", part.getSubmittedFileName());
- Assert.assertEquals("inline", response.getHeader(HttpHeaders.CONTENT_DISPOSITION));
- Assert.assertEquals(MediaType.TEXT_HTML, response.getHeader(HttpHeaders.CONTENT_TYPE));
-
- try (InputStream is = part.getInputStream()) {
- Assert.assertTrue(IOUtils.toString(is, StandardCharsets.UTF_8).endsWith("</html>"));
- }
- }
-
- @Test
- public void getSchemaContentById_download_html() throws IOException {
- testDownloadHtmlById("schema1");
- testDownloadHtmlById("schema2");
- }
-
- private void testDownloadHtmlById(String schemaId) throws IOException {
- Response response = inspector.getSchemaContentById(schemaId, SchemaFormat.HTML, true);
-
- Part part = response.getResult();
- Assert.assertEquals(schemaId + ".html", part.getSubmittedFileName());
- Assert.assertNull(response.getHeader(HttpHeaders.CONTENT_DISPOSITION));
- Assert.assertEquals(MediaType.TEXT_HTML, response.getHeader(HttpHeaders.CONTENT_TYPE));
-
- try (InputStream is = part.getInputStream()) {
- Assert.assertTrue(IOUtils.toString(is, StandardCharsets.UTF_8).endsWith("</html>"));
- }
- }
-
@Test
public void getStaticResource_notExist() throws IOException {
Response response = inspector.getStaticResource("notExist");
@@ -329,14 +267,13 @@ public class TestInspectorImpl {
List<DynamicPropertyView> views = inspector.dynamicProperties();
Assert.assertThat(views.stream().map(DynamicPropertyView::getCallbackCount).collect(Collectors.toList()),
- Matchers.contains(1, 0, 0, 0));
+ Matchers.contains(1, 0, 0));
Assert.assertThat(views.stream().map(DynamicPropertyView::getKey).collect(Collectors.toList()),
- Matchers.contains("yyy", "zzz", "servicecomb.inspector.enabled",
- "servicecomb.inspector.swagger.html.asciidoctorCss"));
+ Matchers.contains("yyy", "zzz", "servicecomb.inspector.enabled"));
ConfigUtil.getAllDynamicProperties().remove("yyy");
ConfigUtil.getAllDynamicProperties().remove("zzz");
- Assert.assertEquals(2, ConfigUtil.getAllDynamicProperties().size());
+ Assert.assertEquals(1, ConfigUtil.getAllDynamicProperties().size());
}
@Test