changed signature for usage in generator

This commit is contained in:
Tony Tam 2015-02-07 14:59:35 -08:00
parent cbc9fdbe34
commit ffe5fc7fd2
24 changed files with 1719 additions and 16 deletions

View File

@ -12,26 +12,33 @@ import java.io.File;
import java.util.*;
public class Codegen extends DefaultGenerator {
static Map<String, CodegenConfig> configs = new HashMap<String, CodegenConfig>();
static String configString;
static {
List<CodegenConfig> extensions = getExtensions();
StringBuilder sb = new StringBuilder();
for(CodegenConfig config : extensions) {
if(sb.toString().length() != 0)
sb.append(", ");
sb.append(config.getName());
configs.put(config.getName(), config);
configString = sb.toString();
}
}
static String debugInfoOptions = "\nThe following additional debug options are available for all codegen targets:" +
"\n -DdebugSwagger prints the swagger specification as interpreted by the codegen" +
"\n -DdebugModels prints models passed to the template engine" +
"\n -DdebugOperations prints operations passed to the template engine" +
"\n -DdebugSupportingFiles prints additional data passed to the template engine";
public static void main(String[] args) {
List<CodegenConfig> extensions = getExtensions();
Map<String, CodegenConfig> configs = new HashMap<String, CodegenConfig>();
StringBuilder sb = new StringBuilder();
for(CodegenConfig config : extensions) {
if(sb.toString().length() != 0)
sb.append(", ");
sb.append(config.getName());
configs.put(config.getName(), config);
}
Options options = new Options();
options.addOption("h", "help", false, "shows this message");
options.addOption("l", "lang", true, "client language to generate.\nAvailable languages include:\n\t[" + sb.toString() + "]");
options.addOption("l", "lang", true, "client language to generate.\nAvailable languages include:\n\t[" + configString + "]");
options.addOption("o", "output", true, "where to write the generated files");
options.addOption("i", "input-spec", true, "location of the swagger spec, as URL or file");
options.addOption("t", "template-dir", true, "folder containing the template files");
@ -53,12 +60,12 @@ public class Codegen extends DefaultGenerator {
return;
}
if (cmd.hasOption("l"))
clientOptInput.setConfig(getConfig(cmd.getOptionValue("l"), configs));
clientOptInput.setConfig(getConfig(cmd.getOptionValue("l")));
if (cmd.hasOption("o"))
clientOptInput.getConfig().setOutputDir(cmd.getOptionValue("o"));
if (cmd.hasOption("h")) {
if(cmd.hasOption("l")) {
config = getConfig(String.valueOf(cmd.getOptionValue("l")), configs);
config = getConfig(String.valueOf(cmd.getOptionValue("l")));
if(config != null) {
options.addOption("h", "help", true, config.getHelp());
usage(options);
@ -103,7 +110,7 @@ public class Codegen extends DefaultGenerator {
formatter.printHelp( "Codegen", options );
}
static CodegenConfig getConfig(String name, Map<String, CodegenConfig> configs) {
public static CodegenConfig getConfig(String name) {
if(configs.containsKey(name)) {
return configs.get(name);
}

View File

@ -26,13 +26,14 @@ public class DefaultGenerator implements Generator {
return this;
}
public void generate() {
public List<File> generate() {
if(swagger == null || config == null) {
throw new RuntimeException("missing swagger input or config!");
}
if(System.getProperty("debugSwagger") != null) {
Json.prettyPrint(swagger);
}
List<File> files = new ArrayList<File>();
try {
config.processOpts();
if(swagger.getInfo() != null) {
@ -94,6 +95,7 @@ public class DefaultGenerator implements Generator {
.defaultValue("")
.compile(template);
writeToFile(filename, tmpl.execute(models));
files.add(new File(filename));
}
}
}
@ -131,6 +133,7 @@ public class DefaultGenerator implements Generator {
.compile(template);
writeToFile(filename, tmpl.execute(operation));
files.add(new File(filename));
}
}
if(System.getProperty("debugOperations") != null) {
@ -187,11 +190,13 @@ public class DefaultGenerator implements Generator {
.compile(template);
writeToFile(outputFilename, tmpl.execute(bundle));
files.add(new File(outputFilename));
}
else {
String template = readTemplate(config.templateDir() + File.separator + support.templateFile);
FileUtils.writeStringToFile(new File(outputFilename), template);
System.out.println("copying file to " + outputFilename);
files.add(new File(outputFilename));
}
}
@ -200,6 +205,7 @@ public class DefaultGenerator implements Generator {
catch (Exception e) {
e.printStackTrace();
}
return files;
}
public Map<String, List<CodegenOperation>> processPaths(Map<String, Path> paths) {

View File

@ -2,7 +2,10 @@ package com.wordnik.swagger.codegen;
import com.wordnik.swagger.models.Swagger;
import java.io.File;
import java.util.List;
public interface Generator {
Generator opts(ClientOptInput opts);
void generate();
List<File> generate();
}

View File

@ -0,0 +1,241 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.wordnik</groupId>
<artifactId>swagger-codegen-project</artifactId>
<version>2.1.0-SNAPSHOT</version>
<relativePath>../..</relativePath>
</parent>
<groupId>com.wordnik</groupId>
<artifactId>swagger-generator</artifactId>
<packaging>war</packaging>
<name>swagger-generator</name>
<version>1.0.0</version>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<executions>
<execution>
<id>scala-compile-first</id>
<phase>process-resources</phase>
<goals>
<goal>add-source</goal>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>scala-test-compile</id>
<phase>process-test-resources</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<configuration>
<configuration>
<recompileMode>incremental</recompileMode>
</configuration>
<jvmArgs>
<jvmArg>-Xmx384m</jvmArg>
</jvmArgs>
<args>
<arg>-target:jvm-1.6</arg>
<arg>-deprecation</arg>
</args>
<launchers>
<launcher>
<id>run-scalatest</id>
<mainClass>org.scalatest.tools.Runner</mainClass>
<args>
<arg>-p</arg>
<arg>${project.build.testOutputDirectory}</arg>
</args>
<jvmArgs>
<jvmArg>-Xmx512m</jvmArg>
</jvmArgs>
</launcher>
</launchers>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty-version}</version>
<configuration>
<webApp>
<contextPath>/</contextPath>
</webApp>
<webAppSourceDirectory>target/${project.artifactId}-${project.version}</webAppSourceDirectory>
<stopPort>8079</stopPort>
<stopKey>stopit</stopKey>
<httpConnector>
<port>8002</port>
<idleTimeout>60000</idleTimeout>
</httpConnector>
</configuration>
<executions>
<execution>
<id>start-jetty</id>
<phase>pre-integration-test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<scanIntervalSeconds>0</scanIntervalSeconds>
<daemon>true</daemon>
</configuration>
</execution>
<execution>
<id>stop-jetty</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.wordnik</groupId>
<artifactId>swagger-jersey2-jaxrs</artifactId>
<version>${swagger-core-version}</version>
</dependency>
<dependency>
<groupId>com.wordnik</groupId>
<artifactId>swagger-codegen</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback-version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${logback-version}</version>
</dependency>
<dependency>
<groupId>org.scalatest</groupId>
<artifactId>scalatest_2.10</artifactId>
<version>${scala-test-version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit-version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>${servlet-api-version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>${jersey2-version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>${jersey2-version}</version>
</dependency>
<dependency>
<groupId>net.lingala.zip4j</groupId>
<artifactId>zip4j</artifactId>
<version>${zip-version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>${jetty-version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-start</artifactId>
<version>${jetty-version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-xml</artifactId>
<version>${jetty-version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>${jetty-version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-deploy</artifactId>
<version>${jetty-version}</version>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>${scala-version}</version>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-compiler</artifactId>
<version>${scala-version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<felix-version>2.3.4</felix-version>
<servlet-api-version>2.5</servlet-api-version>
<logback-version>1.0.1</logback-version>
<junit-version>4.8.1</junit-version>
<maven-plugin-version>1.0.0</maven-plugin-version>
<commons-lang-version>2.4</commons-lang-version>
<slf4j-version>1.6.3</slf4j-version>
<servlet-api-version>2.5</servlet-api-version>
<zip-version>1.3.2</zip-version>
<jetty-version>9.0.7.v20131107</jetty-version>
<jersey2-version>2.4.1</jersey2-version>
<swagger-core-version>1.5.0-SNAPSHOT</swagger-core-version>
<scala-maven-plugin-version>3.1.5</scala-maven-plugin-version>
<scala-version>2.10.0</scala-version>
<scala-test-version>1.9</scala-test-version>
</properties>
</project>

View File

@ -0,0 +1,479 @@
{
"opts": {},
"spec": {
"swagger": "2.0",
"title": "Petstore Sample API",
"info": {
"description": "A sample API that uses a petstore as an example to demonstrate features in the swagger-2.0 specification",
"title": "Petstore Sample API",
"contact": {
"name": "Wordnik API Team"
},
"license": {
"name": "MIT",
"url": "http://github.com/gruntjs/grunt/blob/master/LICENSE-MIT"
}
},
"host": "petstore.swagger.wordnik.com",
"basePath": "/api",
"paths": {
"/pet": {
"put": {
"tags": [
"pet"
],
"parameters": [
{
"name": "body",
"in": "body",
"description": "Pet object that needs to be added to the store",
"required": false,
"schema": {
"$ref": "#/definitions/Pet"
}
}
],
"responses": {
"405": {
"description": "Validation exception"
},
"404": {
"description": "Pet not found"
},
"400": {
"description": "Invalid ID supplied"
}
}
},
"post": {
"tags": [
"pet"
],
"parameters": [
{
"name": "body",
"in": "body",
"description": "Pet object that needs to be added to the store",
"required": false,
"schema": {
"$ref": "#/definitions/Tag"
}
}
],
"responses": {
"405": {
"description": "Invalid input"
}
}
}
},
"/user/createWithList": {
"post": {
"tags": [
"user"
],
"parameters": [
{
"name": "body",
"in": "body",
"description": "List of user object",
"required": false
}
]
}
},
"/store/order/{orderId}": {
"get": {
"tags": [
"store"
],
"parameters": [
{
"name": "orderId",
"in": "path",
"description": "ID of pet that needs to be fetched",
"required": true,
"type": "string"
}
],
"responses": {
"404": {
"description": "Order not found"
},
"400": {
"description": "Invalid ID supplied"
}
}
},
"delete": {
"tags": [
"store"
],
"parameters": [
{
"name": "orderId",
"in": "path",
"description": "ID of the order that needs to be deleted",
"required": true,
"type": "string"
}
],
"responses": {
"404": {
"description": "Order not found"
},
"400": {
"description": "Invalid ID supplied"
}
}
}
},
"/user/createWithArray": {
"post": {
"tags": [
"user"
],
"parameters": [
{
"name": "body",
"in": "body",
"description": "List of user object",
"required": false,
"schema": {
"$ref": "#/definitions/User"
}
}
]
}
},
"/store/order": {
"post": {
"tags": [
"store"
],
"parameters": [
{
"name": "body",
"in": "body",
"description": "order placed for purchasing the pet",
"required": false,
"schema": {
"$ref": "#/definitions/Order"
}
}
],
"responses": {
"400": {
"description": "Invalid Order"
}
}
}
},
"/pet/findByStatus": {
"get": {
"tags": [
"pet"
],
"parameters": [
{
"name": "status",
"in": "query",
"description": "Status values that need to be considered for filter",
"required": false,
"type": "string"
}
],
"responses": {
"400": {
"description": "Invalid status value"
}
}
}
},
"/user/{username}": {
"get": {
"tags": [
"user"
],
"parameters": [
{
"name": "username",
"in": "path",
"description": "The name that needs to be fetched. Use user1 for testing. ",
"required": true,
"type": "string"
}
],
"responses": {
"404": {
"description": "User not found"
},
"400": {
"description": "Invalid username supplied"
}
}
},
"put": {
"tags": [
"user"
],
"parameters": [
{
"name": "username",
"in": "path",
"description": "name that need to be deleted",
"required": true,
"type": "string"
},
{
"name": "body",
"in": "body",
"description": "Updated user object",
"required": false,
"schema": {
"$ref": "#/definitions/User"
}
}
],
"responses": {
"404": {
"description": "User not found"
},
"400": {
"description": "Invalid user supplied"
}
}
},
"delete": {
"tags": [
"user"
],
"parameters": [
{
"name": "username",
"in": "path",
"description": "The name that needs to be deleted",
"required": true,
"type": "string"
}
],
"responses": {
"404": {
"description": "User not found"
},
"400": {
"description": "Invalid username supplied"
}
}
}
},
"/pet/findByTags": {
"get": {
"tags": [
"pet"
],
"parameters": [
{
"name": "tags",
"in": "query",
"description": "Tags to filter by",
"required": false,
"type": "string"
}
],
"responses": {
"400": {
"description": "Invalid tag value"
}
}
}
},
"/user": {
"post": {
"tags": [
"user"
],
"parameters": [
{
"name": "body",
"in": "body",
"description": "Created user object",
"required": false,
"schema": {
"$ref": "#/definitions/User"
}
}
]
}
},
"/pet/{petId}": {
"get": {
"tags": [
"pet"
],
"parameters": [
{
"name": "petId",
"in": "path",
"description": "ID of pet that needs to be fetched",
"required": true,
"type": "string"
}
],
"responses": {
"404": {
"description": "Pet not found"
},
"400": {
"description": "Invalid ID supplied"
}
}
}
},
"/user/logout": {
"get": {
"tags": [
"user"
]
}
},
"/user/login": {
"get": {
"tags": [
"user"
],
"parameters": [
{
"name": "username",
"in": "query",
"description": "The user name for login",
"required": false,
"type": "string"
},
{
"name": "password",
"in": "query",
"description": "The password for login in clear text",
"required": false,
"type": "string"
}
],
"responses": {
"400": {
"description": "Invalid username/password supplied"
}
}
}
}
},
"definitions": {
"User": {
"properties": {
"id": {
"type": "integer",
"format": "int32"
},
"lastName": {
"type": "string"
},
"username": {
"type": "string"
},
"phone": {
"type": "string"
},
"email": {
"type": "string"
},
"userStatus": {
"type": "integer",
"format": "int32"
},
"firstName": {
"type": "string"
},
"password": {
"type": "string"
}
}
},
"Category": {
"properties": {
"id": {
"type": "integer",
"format": "int32"
},
"name": {
"type": "string"
}
}
},
"Pet": {
"properties": {
"id": {
"type": "integer",
"format": "int32"
},
"tags": {
"type": "array",
"items": {
"$ref": "#/definitions/Tag"
}
},
"category": {
"$ref": "#/definitions/Category"
},
"status": {
"type": "string"
},
"name": {
"type": "string"
},
"photoUrls": {
"type": "array",
"items": {
"type": "string"
}
}
}
},
"Tag": {
"properties": {
"id": {
"type": "integer",
"format": "int32"
},
"name": {
"type": "string"
}
}
},
"Order": {
"properties": {
"id": {
"type": "integer",
"format": "int32"
},
"petId": {
"type": "integer",
"format": "int32"
},
"status": {
"type": "string"
},
"complete": {
"type": "boolean"
},
"quantity": {
"type": "integer",
"format": "int32"
},
"shipDate": {
"type": "string",
"format": "date-time"
}
}
}
}
}
}

View File

@ -0,0 +1,44 @@
/**
* Copyright 2014 Reverb, Inc.
*
* Licensed 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 com.wordnik.swagger.generator;
import com.wordnik.swagger.models.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
public class Bootstrap extends HttpServlet {
public void init(ServletConfig config) throws ServletException {
Info info = new Info()
.title("Swagger Generator")
.description("This is an online swagger codegen server. You can find out more " +
"at <a href=\"https://github.com/wordnik/swagger-generator\">https://github.com/wordnik/swagger-generator</a> or on irc.freenode.net, #swagger." +
"http://helloreverb.com/terms/")
.termsOfService("http://helloreverb.com/terms/")
.contact(new Contact()
.email("apiteam@swagger.io"))
.license(new License()
.name("Apache 2.0")
.url("http://www.apache.org/licenses/LICENSE-2.0.html"));
ServletContext context = config.getServletContext();
Swagger swagger = new Swagger().info(info);
context.setAttribute("swagger", swagger);
}
}

View File

@ -0,0 +1,25 @@
/**
* Copyright 2014 Reverb, Inc.
*
* Licensed 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 com.wordnik.swagger.generator.exception;
public class ApiException extends Exception{
private int code;
public ApiException (int code, String msg) {
super(msg);
this.code = code;
}
}

View File

@ -0,0 +1,25 @@
/**
* Copyright 2014 Reverb, Inc.
*
* Licensed 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 com.wordnik.swagger.generator.exception;
public class BadRequestException extends ApiException {
private int code;
public BadRequestException (int code, String msg) {
super(code, msg);
this.code = code;
}
}

View File

@ -0,0 +1,25 @@
/**
* Copyright 2014 Reverb, Inc.
*
* Licensed 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 com.wordnik.swagger.generator.exception;
public class NotFoundException extends ApiException {
private int code;
public NotFoundException (int code, String msg) {
super(code, msg);
this.code = code;
}
}

View File

@ -0,0 +1,84 @@
/**
* Copyright 2014 Reverb, Inc.
*
* Licensed 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 com.wordnik.swagger.generator.model;
import javax.xml.bind.annotation.XmlTransient;
@javax.xml.bind.annotation.XmlRootElement
public class ApiResponse {
public static final int ERROR = 1;
public static final int WARNING = 2;
public static final int INFO = 3;
public static final int OK = 4;
public static final int TOO_BUSY = 5;
int code;
String type;
String message;
public ApiResponse(){}
public ApiResponse(int code, String message){
this.code = code;
switch(code){
case ERROR:
setType("error");
break;
case WARNING:
setType("warning");
break;
case INFO:
setType("info");
break;
case OK:
setType("ok");
break;
case TOO_BUSY:
setType("too busy");
break;
default:
setType("unknown");
break;
}
this.message = message;
}
@XmlTransient
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}

View File

@ -0,0 +1,36 @@
/**
* Copyright 2014 Reverb, Inc.
*
* Licensed 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 com.wordnik.swagger.generator.model;
public class Generated {
private String filename;
private String friendlyName;
public String getFilename() {
return filename;
}
public void setFilename(String filename) {
this.filename = filename;
}
public String getFriendlyName() {
return friendlyName;
}
public void setFriendlyName(String friendlyName) {
this.friendlyName = friendlyName;
}
}

View File

@ -0,0 +1,44 @@
package com.wordnik.swagger.generator.model;
import com.wordnik.swagger.annotations.ApiModelProperty;
import com.wordnik.swagger.models.auth.SecuritySchemeDefinition;
import com.fasterxml.jackson.databind.*;
import java.util.*;
public class GeneratorInput {
private JsonNode spec;
private Map<String, String> options;
private String swaggerUrl;
private SecuritySchemeDefinition auth;
@ApiModelProperty(dataType="Object")
public JsonNode getSpec() {
return spec;
}
public void setSpec(JsonNode spec) {
this.spec = spec;
}
public Map<String, String> getOptions() {
return options;
}
public void setOptions(Map<String, String> options) {
this.options = options;
}
public String getSwaggerUrl() {
return swaggerUrl;
}
public void setSwaggerUrl(String url) {
this.swaggerUrl = url;
}
public SecuritySchemeDefinition getSecurityDefinition() {
return auth;
}
public void setSecurityDefinition(SecuritySchemeDefinition auth) {
this.auth = auth;
}
}

View File

@ -0,0 +1,65 @@
/**
* Copyright 2014 Reverb, Inc.
*
* Licensed 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 com.wordnik.swagger.generator.model;
public class InputOption {
private String name;
private String description;
private Boolean required;
private String defaultValue;
public InputOption() {}
public InputOption(String name, String description, String defaultValue, Boolean required) {
this.name = name;
this.description = description;
this.defaultValue = defaultValue;
this.required = required;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setDescription(String description) {
this.description = description;
}
public String getDescription() {
return description;
}
public void setRequired(Boolean required) {
this.required = required;
}
public Boolean getRequired() {
return required;
}
public void setDefaultValue(String defaultValue) {
this.defaultValue = defaultValue;
}
public String getDefaultValue() {
return defaultValue;
}
}

View File

@ -0,0 +1,19 @@
package com.wordnik.swagger.generator.model;
public class ResponseCode {
private String code;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public ResponseCode() {}
public ResponseCode(String code) {
setCode(code);
}
}

View File

@ -0,0 +1,95 @@
package com.wordnik.swagger.online;
import io.swagger.parser.SwaggerParser;
import com.wordnik.swagger.generator.exception.*;
import com.wordnik.swagger.codegen.*;
import com.wordnik.swagger.models.Swagger;
import com.wordnik.swagger.generator.model.*;
import com.wordnik.swagger.util.Json;
import com.wordnik.swagger.generator.util.ZipUtil;
import com.fasterxml.jackson.databind.JsonNode;
import java.io.File;
import java.util.List;
import java.util.ArrayList;
public class Generator {
public static String generateClient(String language, GeneratorInput opts) throws ApiException {
if(opts == null) {
throw new BadRequestException(400, "No options were supplied");
}
JsonNode node = opts.getSpec();
if(node == null) {
throw new BadRequestException(400, "No swagger specification was supplied");
}
Swagger swagger = new SwaggerParser().read(node);
if(swagger == null) {
throw new BadRequestException(400, "The swagger specification supplied was not valid");
}
ClientOptInput clientOptInput = new ClientOptInput();
ClientOpts clientOpts = new ClientOpts();
String outputFolder = getTmpFolder().getAbsolutePath() + File.separator + language + "-client";
String outputFilename = outputFolder + "-bundle.zip";
clientOptInput
.opts(clientOpts)
.swagger(swagger);
CodegenConfig codegenConfig = Codegen.getConfig(language);
if(codegenConfig == null) {
throw new BadRequestException(400, "Unsupported target " + language + " supplied");
}
codegenConfig.setOutputDir(outputFolder);
Json.prettyPrint(clientOpts);
clientOptInput.setConfig(codegenConfig);
try{
List<File> files = new Codegen().opts(clientOptInput).generate();
if(files.size() > 0) {
List<File> filesToAdd = new ArrayList<File>();
filesToAdd.add(new File(outputFolder));
ZipUtil zip = new ZipUtil();
zip.compressFiles(filesToAdd, outputFilename);
}
else {
throw new BadRequestException(400, "A target generation was attempted, but no files were created!");
}
}
catch (Exception e) {
throw new BadRequestException(500, "Unable to build target: " + e.getMessage());
}
return outputFilename;
}
public static String generateServer(String language, GeneratorInput opts) {
return "";
}
public static InputOption clientOptions(String language) {
return null;
}
public static InputOption serverOptions(String language) {
return null;
}
protected static File getTmpFolder() {
try {
File outputFolder = File.createTempFile("codegen-", "-tmp");
outputFolder.delete();
outputFolder.mkdir();
outputFolder.deleteOnExit();
return outputFolder;
}
catch (Exception e) {
e.printStackTrace();
return null;
}
}
}

View File

@ -0,0 +1,50 @@
package com.wordnik.swagger.generator.resource;
import com.wordnik.swagger.generator.util.ValidationException;
import com.wordnik.swagger.generator.exception.ApiException;
import com.wordnik.swagger.generator.exception.BadRequestException;
import com.wordnik.swagger.generator.exception.NotFoundException;
import com.wordnik.swagger.generator.model.ApiResponse;
import javax.ws.rs.ext.*;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
@Provider
public class ExceptionWriter implements ExceptionMapper<Exception> {
public Response toResponse(Exception exception) {
if (exception instanceof javax.ws.rs.WebApplicationException) {
javax.ws.rs.WebApplicationException e = (javax.ws.rs.WebApplicationException) exception;
return Response
.status(e.getResponse().getStatus())
.entity(new ApiResponse(e.getResponse().getStatus(),
exception.getMessage())).build();
} else if (exception instanceof com.fasterxml.jackson.core.JsonParseException) {
return Response.status(400)
.entity(new ApiResponse(400, "bad input")).build();
} else if (exception instanceof ValidationException) {
ValidationException e = (ValidationException) exception;
return Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build();
} else if (exception instanceof NotFoundException) {
return Response
.status(Status.NOT_FOUND)
.entity(new ApiResponse(ApiResponse.ERROR, exception
.getMessage())).build();
} else if (exception instanceof BadRequestException) {
return Response
.status(Status.BAD_REQUEST)
.entity(new ApiResponse(ApiResponse.ERROR, exception
.getMessage())).build();
} else if (exception instanceof ApiException) {
return Response
.status(Status.BAD_REQUEST)
.entity(new ApiResponse(ApiResponse.ERROR, exception
.getMessage())).build();
} else {
return Response.status(500)
.entity(new ApiResponse(500, "something bad happened"))
.build();
}
}
}

View File

@ -0,0 +1,155 @@
/**
* Copyright 2014 Reverb, Inc.
*
* Licensed 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 com.wordnik.swagger.generator.resource;
import com.wordnik.swagger.codegen.*;
import com.wordnik.swagger.generator.util.*;
import com.wordnik.swagger.annotations.*;
import com.wordnik.swagger.generator.model.*;
import com.wordnik.swagger.generator.exception.BadRequestException;
import com.wordnik.swagger.online.Generator;
import java.io.File;
import java.util.*;
import javax.ws.rs.*;
import javax.ws.rs.core.*;
@Path("/gen")
@Api(value = "/gen", description = "Resource for generating swagger components")
public class SwaggerResource {
private static Map<String, Generated> fileMap = new HashMap<String, Generated>();
@GET
@Path("/download/{fileId}")
@Produces({"application/zip", "application/json"})
public Response downloadFile(@PathParam("fileId") String fileId) throws Exception {
Generated g = fileMap.get(fileId);
System.out.println("looking for fileId " + fileId);
System.out.println("got filename " + g.getFilename());
if(g.getFilename() != null) {
byte[] bytes = org.apache.commons.io.FileUtils.readFileToByteArray(new java.io.File(g.getFilename()));
return Response.ok(bytes, "application/zip")
.header("Content-Disposition","attachment; filename=\"" + g.getFriendlyName() + "-generated.zip\"")
.header("Accept-Range", "bytes")
.header("Content-Length", bytes.length)
.build();
}
else {
return Response.status(404).build();
}
}
@POST
@Path("/clients/{language}")
@Produces({"application/zip", "application/json"})
@ApiOperation(value = "Generates a client library based on the config",
notes = "The model representing this is not accurate, it needs to contain a consolidated JSON structure")
public Response generateClient(
@ApiParam(value = "The target language for the client library", allowableValues = "android,java,php,objc,docs", required = true) @PathParam("language") String language,
@ApiParam(value = "Configuration for building the client library", required = true) GeneratorInput opts) throws Exception {
String filename = Generator.generateClient(language, opts);
if(filename != null) {
String code = String.valueOf(System.currentTimeMillis());
Generated g = new Generated();
g.setFilename(filename);
g.setFriendlyName(language + "-client");
fileMap.put(code, g);
System.out.println(code + ", " + filename);
return Response.ok().entity(new ResponseCode(code)).build();
}
else {
return Response.status(500).build();
}
}
@GET
@Path("/clients")
@ApiOperation(value = "Gets languages supported by the client generator",
response = String.class,
responseContainer = "List")
public Response clientOptions() {
String[] languages = {"android", "java", "php", "objc", "docs"};
return Response.ok().entity(languages).build();
}
@GET
@Path("/clients/{language}")
@ApiOperation(value = "Gets options for a client generation",
notes = "Values which are not required will use the provided default values",
response = InputOption.class,
responseContainer = "List")
@ApiResponses(value = {
@com.wordnik.swagger.annotations.ApiResponse(code = 400, message = "Invalid model supplied", response = ValidationMessage.class),
})
public Response clientLibraryOptions(
@ApiParam(value = "The target language for the client library", allowableValues = "android,java,php,objc,docs", required = true) @PathParam("language") String language) {
return Response.ok().entity(Generator.clientOptions(language)).build();
}
@GET
@Path("/servers")
@ApiOperation(value = "Gets languages supported by the server generator",
response = String.class,
responseContainer = "List")
public Response serverOptions() {
String[] languages = {"jaxrs","nodejs"};
return Response.ok().entity(languages).build();
}
@GET
@Path("/servers/{language}")
@ApiOperation(value = "Gets options for a server generation",
notes = "Values which are not required will use the provided default values",
response = InputOption.class,
responseContainer = "List")
public Response serverFrameworkOptions(
@ApiParam(value = "The target framework for the client library", allowableValues = "jaxrs,nodejs", required = true) @PathParam("language") String framework) {
return Response.ok().entity(Generator.serverOptions(framework)).build();
}
@POST
@Path("/servers/{framework}")
@ApiOperation(value = "Generates a server library for the supplied server framework",
notes = "The model representing this is not accurate, it needs to contain a consolidated JSON structure")
public Response generateServerForLanguage(
@ApiParam(value = "framework", allowableValues = "jaxrs,nodejs", required = true) @PathParam("framework") String framework,
@ApiParam(value = "parameters", required = true) GeneratorInput opts)
throws Exception {
if(framework == null)
throw new BadRequestException(400, "Framework is required");
String filename = Generator.generateServer(framework, opts);
System.out.println("generated name: " + filename);
if(filename != null) {
String code = String.valueOf(System.currentTimeMillis());
Generated g = new Generated();
g.setFilename(filename);
g.setFriendlyName(framework + "-server");
fileMap.put(code, g);
System.out.println(code + ", " + filename);
return Response.ok().entity(new ResponseCode(code)).build();
}
else {
return Response.status(500).build();
}
}
}

View File

@ -0,0 +1,42 @@
/**
* Copyright 2014 Reverb, Inc.
*
* Licensed 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 com.wordnik.swagger.generator.util;
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
public class ApiOriginFilter implements javax.servlet.Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletResponse res = (HttpServletResponse) response;
res.addHeader("Access-Control-Allow-Origin", "*");
res.addHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT");
res.addHeader("Access-Control-Allow-Headers", "Content-Type");
chain.doFilter(request, response);
}
@Override
public void destroy() {
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
}

View File

@ -0,0 +1,27 @@
package com.wordnik.swagger.generator.util;
import java.util.List;
public class ValidationException extends Exception {
private int code;
private String msg;
private List<ValidationMessage> errors;
public ValidationException(String msg) {
super(msg);
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMessage() {
return msg;
}
public void setMessage(String msg) {
this.msg = msg;
}
}

View File

@ -0,0 +1,26 @@
package com.wordnik.swagger.generator.util;
public class ValidationMessage {
private String path, message, severity;
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getSeverity() {
return severity;
}
public void setSeverity(String severity) {
this.severity = severity;
}
}

View File

@ -0,0 +1,124 @@
/**
* Copyright 2014 Reverb, Inc.
*
* Licensed 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 com.wordnik.swagger.generator.util;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* This utility compresses a list of files to standard ZIP format file.
* It is able to compresses all sub files and sub directories, recursively.
* @author Ha Minh Nam
*
*/
public class ZipUtil {
/**
* A constants for buffer size used to read/write data
*/
private static final int BUFFER_SIZE = 4096;
/**
* Compresses a collection of files to a destination zip file
* @param listFiles A collection of files and directories
* @param destZipFile The path of the destination zip file
* @throws FileNotFoundException
* @throws IOException
*/
public void compressFiles(List<File> listFiles, String destZipFile) throws FileNotFoundException, IOException {
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(destZipFile));
for (File file : listFiles) {
if (file.isDirectory()) {
addFolderToZip(file, file.getName(), zos);
} else {
addFileToZip(file, zos);
}
}
zos.flush();
zos.close();
}
/**
* Adds a directory to the current zip output stream
* @param folder the directory to be added
* @param parentFolder the path of parent directory
* @param zos the current zip output stream
* @throws FileNotFoundException
* @throws IOException
*/
private void addFolderToZip(File folder, String parentFolder,
ZipOutputStream zos) throws FileNotFoundException, IOException {
for (File file : folder.listFiles()) {
if (file.isDirectory()) {
addFolderToZip(file, parentFolder + "/" + file.getName(), zos);
continue;
}
zos.putNextEntry(new ZipEntry(parentFolder + "/" + file.getName()));
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(file));
long bytesRead = 0;
byte[] bytesIn = new byte[BUFFER_SIZE];
int read = 0;
while ((read = bis.read(bytesIn)) != -1) {
zos.write(bytesIn, 0, read);
bytesRead += read;
}
zos.closeEntry();
}
}
/**
* Adds a file to the current zip output stream
* @param file the file to be added
* @param zos the current zip output stream
* @throws FileNotFoundException
* @throws IOException
*/
private void addFileToZip(File file, ZipOutputStream zos)
throws FileNotFoundException, IOException {
zos.putNextEntry(new ZipEntry(file.getName()));
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
file));
long bytesRead = 0;
byte[] bytesIn = new byte[BUFFER_SIZE];
int read = 0;
while ((read = bis.read(bytesIn)) != -1) {
zos.write(bytesIn, 0, read);
bytesRead += read;
}
zos.closeEntry();
}
}

View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern>
</layout>
</appender>
<logger name="com.wordnik.swagger.converter" level="error"/>
<root level="error">
<appender-ref ref="STDOUT" />
</root>
</configuration>

View File

@ -0,0 +1,68 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
<servlet-name>jersey</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>
com.wordnik.swagger.jaxrs.json,
com.wordnik.swagger.generator.resource
</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>
com.wordnik.swagger.online.ExceptionWriter,
com.wordnik.swagger.jersey.listing.ApiListingResourceJSON,
com.wordnik.swagger.jersey.listing.JerseyApiDeclarationProvider,
com.wordnik.swagger.jersey.listing.JerseyResourceListingProvider
</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.wadl.disableWadl</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Jersey2Config</servlet-name>
<servlet-class>com.wordnik.swagger.jersey.config.JerseyJaxrsConfig</servlet-class>
<init-param>
<param-name>api.version</param-name>
<param-value>1.0.0</param-value>
</init-param>
<init-param>
<param-name>swagger.api.basepath</param-name>
<param-value>http://generator.wordnik.com/online/api</param-value>
</init-param>
<init-param>
<param-name>swagger.filter</param-name>
<param-value>com.wordnik.swagger.generator.util.ApiAuthorizationFilterImpl</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<filter>
<filter-name>ApiOriginFilter</filter-name>
<filter-class>com.wordnik.swagger.generator.util.ApiOriginFilter</filter-class>
</filter>
<servlet>
<servlet-name>Bootstrap</servlet-name>
<servlet-class>com.wordnik.swagger.generator.Bootstrap</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<filter-mapping>
<filter-name>ApiOriginFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

View File

@ -243,6 +243,7 @@
<modules>
<module>modules/swagger-codegen</module>
<module>modules/swagger-codegen-distribution</module>
<module>modules/swagger-generator</module>
</modules>
<reporting>
<outputDirectory>target/site</outputDirectory>
@ -302,7 +303,7 @@
<swagger-parser-version>1.0.0-SNAPSHOT</swagger-parser-version>
<scala-version>2.11.1</scala-version>
<felix-version>2.3.4</felix-version>
<swagger-core-version>1.5.0-M1</swagger-core-version>
<swagger-core-version>1.5.0-SNAPSHOT</swagger-core-version>
<scala-test-version>2.1.4</scala-test-version>
<commons-io-version>2.3</commons-io-version>
<commons-cli-version>1.2</commons-cli-version>
@ -314,4 +315,4 @@
<scala-version>2.10.4</scala-version>
<jmustache-version>1.9</jmustache-version>
</properties>
</project>
</project>