forked from swagger-api/swagger-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refs swagger-api#2312 - bootstrap draft
- Loading branch information
Showing
31 changed files
with
1,660 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" | ||
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>io.swagger</groupId> | ||
<artifactId>swagger-project</artifactId> | ||
<version>2.0.0-SNAPSHOT</version> | ||
<relativePath>../..</relativePath> | ||
</parent> | ||
<groupId>io.swagger</groupId> | ||
<artifactId>swagger-integration</artifactId> | ||
<version>2.0.0-SNAPSHOT</version> | ||
<packaging>bundle</packaging> | ||
<name>swagger-integration</name> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.reflections</groupId> | ||
<artifactId>reflections</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>javax.ws.rs</groupId> | ||
<artifactId>javax.ws.rs-api</artifactId> | ||
<version>2.0.1</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.testng</groupId> | ||
<artifactId>testng</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.swagger</groupId> | ||
<artifactId>swagger-core</artifactId> | ||
<version>${models.version}</version> | ||
</dependency> | ||
</dependencies> | ||
<build> | ||
<sourceDirectory>src/main/java</sourceDirectory> | ||
<defaultGoal>install</defaultGoal> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.felix</groupId> | ||
<artifactId>maven-bundle-plugin</artifactId> | ||
<version>${felix-version}</version> | ||
<extensions>true</extensions> | ||
<executions> | ||
<execution> | ||
<id>set_failok</id> | ||
<goals> | ||
<goal>manifest</goal> | ||
</goals> | ||
<configuration> | ||
<instructions> | ||
<_failok>true</_failok> | ||
</instructions> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
<configuration> | ||
<manifestLocation>src/main/resources/META-INF</manifestLocation> | ||
<rebuildBundle>true</rebuildBundle> | ||
<instructions> | ||
<Export-Package>io.swagger.oas.integration</Export-Package> | ||
</instructions> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
<properties> | ||
<models.version>2.0.0-SNAPSHOT</models.version> | ||
</properties> | ||
</project> |
270 changes: 270 additions & 0 deletions
270
...s/swagger-integration/src/main/java/io/swagger/oas/integration/GenericOpenApiContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,270 @@ | ||
package io.swagger.oas.integration; | ||
|
||
import io.swagger.oas.models.OpenAPI; | ||
import io.swagger.oas.models.info.Info; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import java.io.File; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
public class GenericOpenApiContext<T extends GenericOpenApiContext> implements OpenApiContext { | ||
|
||
private OpenApiConfiguration openApiConfiguration; | ||
|
||
protected String resourcePackage; | ||
|
||
private String basePath = "/"; | ||
|
||
private Map<String, OpenApiProcessor> openApiProcessors = new HashMap<String, OpenApiProcessor>(); | ||
|
||
protected String id = OPENAPI_CONTEXT_ID_DEFAULT; | ||
protected OpenApiContext parent; | ||
|
||
|
||
public String getResourcePackage() { | ||
return resourcePackage; | ||
} | ||
|
||
public T withResourcePackage(String resourcePackage) { | ||
this.resourcePackage = resourcePackage; | ||
return (T) this; | ||
} | ||
|
||
public String getBasePath() { | ||
return basePath; | ||
} | ||
|
||
public void setBasePath(String basePath) { | ||
this.basePath = basePath; | ||
} | ||
|
||
public final T withBasePath(String basePath) { | ||
this.basePath = basePath; | ||
return (T) this; | ||
} | ||
|
||
public T withOpenApiConfiguration(OpenApiConfiguration openApiConfiguration) { | ||
this.openApiConfiguration = openApiConfiguration; | ||
return (T) this; | ||
} | ||
|
||
public void setOpenApiConfiguration(OpenApiConfiguration openApiConfiguration) { | ||
this.openApiConfiguration = openApiConfiguration; | ||
} | ||
|
||
public String getConfigLocation() { | ||
return configLocation; | ||
} | ||
|
||
public void setConfigLocation(String configLocation) { | ||
this.configLocation = configLocation; | ||
} | ||
|
||
protected String configLocation; | ||
|
||
public final T withConfigLocation(String configLocation) { | ||
this.configLocation = configLocation; | ||
return (T) this; | ||
} | ||
|
||
public void setOpenApiProcessors(Map<String, OpenApiProcessor> openApiProcessors) { | ||
this.openApiProcessors = openApiProcessors; | ||
} | ||
|
||
public void setId(String id) { | ||
this.id = id; | ||
} | ||
|
||
@Override | ||
public String getId() { | ||
return this.id; | ||
} | ||
|
||
public final T withId(String id) { | ||
this.id = id; | ||
return (T) this; | ||
} | ||
|
||
@Override | ||
public Map<String, OpenApiProcessor> getOpenApiProcessors() { | ||
return openApiProcessors; | ||
} | ||
|
||
@Override | ||
public OpenApiProcessor getDefaultProcessor() { | ||
return openApiProcessors.get(id); | ||
} | ||
|
||
|
||
public void setParent(OpenApiContext parent) { | ||
this.parent = parent; | ||
} | ||
|
||
@Override | ||
public OpenApiContext getParent() { | ||
return this.parent; | ||
} | ||
|
||
public final T withParent(OpenApiContext parent) { | ||
this.parent = parent; | ||
return (T) this; | ||
} | ||
|
||
|
||
public GenericOpenApiContext addOpenApiProcessor(OpenApiProcessor openApiProcessor) { | ||
if (StringUtils.isEmpty(openApiProcessor.getId())) { | ||
openApiProcessor.getOpenApiConfiguration().getOpenApi().setInfo( | ||
(openApiProcessor.getOpenApiConfiguration().getOpenApi().getInfo() == null ? | ||
new Info() : | ||
openApiProcessor.getOpenApiConfiguration().getOpenApi().getInfo()).title(id) | ||
); | ||
} | ||
openApiProcessors.put(openApiProcessor.getId(), openApiProcessor); | ||
return this; | ||
} | ||
|
||
protected OpenApiProcessor buildProcessor(String id, final OpenApiConfiguration openApiConfiguration) throws Exception { | ||
OpenApiProcessor processor; | ||
if (StringUtils.isNotBlank(openApiConfiguration.getProcessorClass())) { | ||
Class cls = getClass().getClassLoader().loadClass(openApiConfiguration.getProcessorClass()); | ||
processor = (OpenApiProcessor) cls.newInstance(); | ||
} else { | ||
processor = new GenericOpenApiProcessor().withId(id).withOpenApiConfiguration(openApiConfiguration); | ||
} | ||
processor.setOpenApiScanner(buildScanner(openApiConfiguration)); | ||
processor.setOpenApiReader(buildReader(openApiConfiguration)); | ||
return processor; | ||
} | ||
|
||
protected OpenApiReader buildReader(final OpenApiConfiguration openApiConfiguration) throws Exception { | ||
OpenApiReader reader; | ||
if (StringUtils.isNotBlank(openApiConfiguration.getReaderClass())) { | ||
Class cls = getClass().getClassLoader().loadClass(openApiConfiguration.getReaderClass()); | ||
// TODO instantiate with configuration | ||
reader = (OpenApiReader) cls.newInstance(); | ||
} else { | ||
reader = new OpenApiReader() { | ||
@Override | ||
public OpenAPI read(Set<Class<?>> classes, Map<String, Object> resources) { | ||
OpenAPI openApi = openApiConfiguration.getOpenApi(); | ||
return openApi; | ||
|
||
} | ||
}; | ||
} | ||
return reader; | ||
} | ||
|
||
protected OpenApiScanner buildScanner(final OpenApiConfiguration openApiConfiguration) throws Exception { | ||
OpenApiScanner scanner; | ||
if (StringUtils.isNotBlank(openApiConfiguration.getScannerClass())) { | ||
Class cls = getClass().getClassLoader().loadClass(openApiConfiguration.getScannerClass()); | ||
// TODO instantiate with configuration | ||
scanner = (OpenApiScanner) cls.newInstance(); | ||
} else { | ||
scanner = new GenericOpenApiScanner(openApiConfiguration); | ||
} | ||
return scanner; | ||
} | ||
|
||
// TODO implement in subclass, also handle classpath | ||
protected URL buildConfigLocationURL(String configLocation) { | ||
|
||
|
||
// TODO CLASSPATH, file path.. | ||
//ServletContext.getResource() | ||
String sanitize = (configLocation.startsWith("/") ? configLocation : "/" + configLocation); | ||
if (true) return this.getClass().getResource(configLocation); | ||
|
||
|
||
|
||
configLocation = "file://" + configLocation; | ||
try { | ||
return new File(configLocation).toURL(); | ||
//return new URL(configLocation); | ||
} catch (MalformedURLException e) { | ||
e.printStackTrace(); | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
protected URL locateConfig() { | ||
if (StringUtils.isNotEmpty(configLocation)) { | ||
return buildConfigLocationURL(configLocation); | ||
} | ||
// TODO check known location in classpath, or same dir or whatever.. | ||
return null; | ||
} | ||
|
||
@Override | ||
public OpenApiContext init() { | ||
|
||
URL configUrl = locateConfig(); | ||
if (configUrl != null) { | ||
// TODO handle urls and stuff, also use loadConfiguration protected now in WebXmlContext.. | ||
Map<String, OpenApiConfiguration> configurations = OpenApiConfiguration.fromUrl(locateConfig(), id); | ||
for (String id : configurations.keySet()) { | ||
try { | ||
openApiProcessors.put(id, buildProcessor(id, configurations.get(id))); | ||
} catch (Exception e) { | ||
// TODO | ||
e.printStackTrace(); | ||
} | ||
|
||
} | ||
} | ||
|
||
// TODO here try with openApiController? and replace OpenApiConfiguration.fromUrl | ||
|
||
if (openApiProcessors.isEmpty()) { | ||
try { | ||
|
||
if (openApiConfiguration == null) { | ||
openApiConfiguration = new OpenApiConfiguration().withResourcePackage(resourcePackage); | ||
openApiConfiguration.setId(id); | ||
} | ||
|
||
openApiProcessors.put(id, buildProcessor(id, openApiConfiguration)); | ||
} catch (Exception e) { | ||
// TODO | ||
e.printStackTrace(); | ||
} | ||
} | ||
for (OpenApiProcessor p : openApiProcessors.values()) { | ||
p.init(); | ||
} | ||
register(); | ||
return this; | ||
} | ||
|
||
protected void register() { | ||
OpenApiContextLocator.getInstance().putOpenApiContext(id, this); | ||
} | ||
|
||
@Override | ||
public OpenAPI read() { | ||
if (openApiProcessors.isEmpty()) { | ||
return null; | ||
} | ||
return getDefaultProcessor().read(); | ||
} | ||
|
||
@Override | ||
public OpenApiConfiguration getOpenApiConfiguration() { | ||
if (openApiConfiguration != null) { | ||
return openApiConfiguration; | ||
} | ||
if (!openApiProcessors.isEmpty()) { | ||
if (openApiProcessors.get(id) != null) { | ||
return openApiProcessors.get(id).getOpenApiConfiguration(); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.