Skip to content

Commit

Permalink
add cxf client generator, rename cxf server generator swagger-api#2017
Browse files Browse the repository at this point in the history
  • Loading branch information
jfiala committed Oct 16, 2016
1 parent 2fb1b16 commit cf3056f
Show file tree
Hide file tree
Showing 11 changed files with 455 additions and 65 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@

package io.swagger.codegen.languages;

import java.io.File;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.swagger.codegen.CliOption;
import io.swagger.codegen.CodegenModel;
import io.swagger.codegen.CodegenOperation;
import io.swagger.codegen.CodegenProperty;
import io.swagger.codegen.CodegenType;
import io.swagger.codegen.SupportingFile;
import io.swagger.models.Operation;

public class JavaCXFClientCodegen extends AbstractJavaCodegen
{
private static final Logger LOGGER = LoggerFactory.getLogger(JavaCXFClientCodegen.class);

/**
* Name of the sub-directory in "src/main/resource" where to find the
* Mustache template for the JAX-RS Codegen.
*/
protected static final String JAXRS_TEMPLATE_DIRECTORY_NAME = "JavaJaxRS";

public static final String USE_BEANVALIDATION = "useBeanValidation";

public static final String USE_GZIP_FEATURE = "useGzipFeature";

public static final String USE_LOGGING_FEATURE = "useLoggingFeature";

public static final String USE_BEANVALIDATION_FEATURE = "useBeanValidationFeature";

protected boolean useBeanValidation = false;

protected boolean useGzipFeature = false;

protected boolean useLoggingFeature = false;

protected boolean useBeanValidationFeature = false;


public JavaCXFClientCodegen()
{
super();

supportsInheritance = true;

sourceFolder = "src/gen/java";
invokerPackage = "io.swagger.api";
artifactId = "swagger-jaxrs-client";
dateLibrary = "legacy"; //TODO: add joda support to all jax-rs

apiPackage = "io.swagger.api";
modelPackage = "io.swagger.model";

outputFolder = "generated-code/JavaJaxRS-CXF";

// clear model and api doc template as this codegen
// does not support auto-generated markdown doc at the moment
//TODO: add doc templates
modelDocTemplateFiles.remove("model_doc.mustache");
apiDocTemplateFiles.remove("api_doc.mustache");


typeMapping.put("date", "LocalDate");
typeMapping.put("DateTime", "javax.xml.datatype.XMLGregorianCalendar"); // Map DateTime fields to Java standart class 'XMLGregorianCalendar'

importMapping.put("LocalDate", "org.joda.time.LocalDate");

embeddedTemplateDir = templateDir = JAXRS_TEMPLATE_DIRECTORY_NAME + File.separator + "cxf";

cliOptions.add(CliOption.newBoolean(USE_BEANVALIDATION, "Use BeanValidation API annotations"));

cliOptions.add(CliOption.newBoolean(USE_GZIP_FEATURE, "Use Gzip Feature"));
cliOptions.add(CliOption.newBoolean(USE_BEANVALIDATION_FEATURE, "Use BeanValidation Feature"));
cliOptions.add(CliOption.newBoolean(USE_LOGGING_FEATURE, "Use Logging Feature"));


}


@Override
public void processOpts()
{
super.processOpts();

if (additionalProperties.containsKey(USE_BEANVALIDATION)) {
boolean useBeanValidationProp = convertPropertyToBooleanAndWriteBack(USE_BEANVALIDATION);
this.setUseBeanValidation(useBeanValidationProp);
}

this.setUseGzipFeature(convertPropertyToBooleanAndWriteBack(USE_GZIP_FEATURE));
this.setUseLoggingFeature(convertPropertyToBooleanAndWriteBack(USE_LOGGING_FEATURE));

boolean useBeanValidationFeature = convertPropertyToBooleanAndWriteBack(USE_BEANVALIDATION_FEATURE);
this.setUseBeanValidationFeature(useBeanValidationFeature);
if (useBeanValidationFeature) {
LOGGER.info("make sure your client supports Bean Validation 1.1");
}

supportingFiles.clear(); // Don't need extra files provided by AbstractJAX-RS & Java Codegen

writeOptional(outputFolder, new SupportingFile("pom.mustache", "", "pom.xml"));

}


private boolean convertPropertyToBooleanAndWriteBack(String propertyKey) {
boolean booleanValue = false;
if (additionalProperties.containsKey(propertyKey)) {
booleanValue = Boolean.valueOf(additionalProperties.get(propertyKey).toString());
// write back as boolean
additionalProperties.put(propertyKey, booleanValue);
}

return booleanValue;
}

@Override
public String getName()
{
return "jaxrs-cxf";
}


@Override
public CodegenType getTag()
{
return CodegenType.CLIENT;
}

@Override
public void addOperationToGroup(String tag, String resourcePath, Operation operation, CodegenOperation co, Map<String, List<CodegenOperation>> operations) {
super.addOperationToGroup(tag, resourcePath, operation, co, operations);
co.subresourceOperation = !co.path.isEmpty();
}

@Override
public void postProcessModelProperty(CodegenModel model, CodegenProperty property) {
super.postProcessModelProperty(model, property);
model.imports.remove("ApiModelProperty");
model.imports.remove("ApiModel");
model.imports.remove("JsonSerialize");
model.imports.remove("ToStringSerializer");
}

@Override
public String getHelp()
{
return "Generates a Java JAXRS Client based on Apache CXF framework.";
}

public void setUseBeanValidation(boolean useBeanValidation) {
this.useBeanValidation = useBeanValidation;
}


public void setUseGzipFeature(boolean useGzipFeature) {
this.useGzipFeature = useGzipFeature;
}


public void setUseLoggingFeature(boolean useLoggingFeature) {
this.useLoggingFeature = useLoggingFeature;
}


public void setUseBeanValidationFeature(boolean useBeanValidationFeature) {
this.useBeanValidationFeature = useBeanValidationFeature;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -130,25 +130,25 @@ public void processOpts()

supportingFiles.clear(); // Don't need extra files provided by AbstractJAX-RS & Java Codegen

writeOptional(outputFolder, new SupportingFile("pom.mustache", "", "pom.xml"));
writeOptional(outputFolder, new SupportingFile("server/pom.mustache", "", "pom.xml"));

if (this.generateSpringApplication) {
writeOptional(outputFolder, new SupportingFile("readme.md", "", "readme.md"));
writeOptional(outputFolder, new SupportingFile("server/readme.md", "", "readme.md"));

writeOptional(outputFolder, new SupportingFile("ApplicationContext.xml.mustache",
writeOptional(outputFolder, new SupportingFile("server/ApplicationContext.xml.mustache",
("src/main/resources"), "ApplicationContext.xml"));
writeOptional(outputFolder, new SupportingFile("web.mustache",
writeOptional(outputFolder, new SupportingFile("server/web.mustache",
("src/main/webapp/WEB-INF"), "web.xml"));
writeOptional(outputFolder, new SupportingFile("context.xml.mustache",
writeOptional(outputFolder, new SupportingFile("server/context.xml.mustache",
("src/main/webapp/WEB-INF"), "context.xml"));

// Jboss
writeOptional(outputFolder, new SupportingFile("jboss-web.xml.mustache",
writeOptional(outputFolder, new SupportingFile("server/jboss-web.xml.mustache",
("src/main/webapp/WEB-INF"), "jboss-web.xml"));

// Spring Boot
if (this.generateSpringBootApplication) {
writeOptional(outputFolder, new SupportingFile("SpringBootApplication.mustache",
writeOptional(outputFolder, new SupportingFile("server/SpringBootApplication.mustache",
(testFolder + '/' + apiPackage).replace(".", "/"), "SpringBootApplication.java"));

}
Expand All @@ -173,7 +173,7 @@ private boolean convertPropertyToBooleanAndWriteBack(String propertyKey) {
@Override
public String getName()
{
return "jaxrs-cxf";
return "jaxrs-cxf-server";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,19 @@ import javax.ws.rs.core.Response;
import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
import org.apache.cxf.jaxrs.client.ClientConfiguration;
import org.apache.cxf.jaxrs.client.WebClient;
{{#useGzipFeature}}
import org.apache.cxf.transport.common.gzip.GZIPInInterceptor;
import org.apache.cxf.transport.common.gzip.GZIPOutInterceptor;
{{/useGzipFeature}}

{{#useLoggingFeature}}
import org.apache.cxf.interceptor.LoggingOutInterceptor;
{{/useLoggingFeature}}
{{#useBeanValidationFeature}}
import org.apache.cxf.validation.BeanValidationProvider;
import org.apache.cxf.validation.BeanValidationOutInterceptor;
{{/useBeanValidationFeature}}

import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;

{{^fullJavaUtil}}
Expand All @@ -32,6 +43,7 @@ import org.springframework.test.context.web.WebAppConfiguration;
{{/generateSpringBootApplication}}



/**
* API tests for {{classname}}
*/
Expand Down Expand Up @@ -73,7 +85,17 @@ public class {{classname}}Test {
config.getOutInterceptors().add(gzipOutInterceptor);

config.getInInterceptors().add(new GZIPInInterceptor());
{{/useGzipFeature}}
{{/useGzipFeature}}
{{#useLoggingFeature}}
LoggingOutInterceptor loggingOutInterceptor = new LoggingOutInterceptor();
config.getOutInterceptors().add(loggingOutInterceptor);
{{/useLoggingFeature}}
{{#useBeanValidationFeature}}
BeanValidationProvider beanValProv = new BeanValidationProvider();
BeanValidationOutInterceptor beanValidationOutInterceptor = new BeanValidationOutInterceptor();
beanValidationOutInterceptor.setProvider(beanValProv);
config.getOutInterceptors().add(beanValidationOutInterceptor);
{{/useBeanValidationFeature}}
}

{{#operations}}{{#operation}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,12 @@
<modelVersion>4.0.0</modelVersion>
<groupId>{{groupId}}</groupId>
<artifactId>{{artifactId}}</artifactId>
<packaging>war</packaging>
<packaging>jar</packaging>
<name>{{artifactId}}</name>
<version>{{artifactVersion}}</version>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<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>
Expand Down Expand Up @@ -75,20 +70,12 @@
</goals>
<configuration>
<sources>
<source>gen/java</source>
<source>src/gen/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>

<!-- build WAR file -->
<plugin>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<failOnMissingWebXml>true</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
Expand Down Expand Up @@ -158,42 +145,13 @@
<version>${cxf-version}</version>
<scope>compile</scope>
</dependency>
{{#generateSpringApplication}}
<!-- Spring -->
{{#useBeanValidationFeature}}
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-version}</version>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.2.2.Final</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring-version}</version>
</dependency>
{{/generateSpringApplication}}
{{#generateSpringBootApplication}}
<!-- Spring Boot dependencies für Integrationstests -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>${spring.boot-version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring.boot-version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxrs</artifactId>
<version>${cxf-version}</version>
<scope>provided</scope>
</dependency>
{{/generateSpringBootApplication}}
{{/useBeanValidationFeature}}
</dependencies>
<repositories>
<repository>
Expand All @@ -217,12 +175,6 @@
{{#useBeanValidation}}
<beanvalidation-version>1.1.0.Final</beanvalidation-version>
{{/useBeanValidation}}
{{#generateSpringApplication}}
<spring-version>4.2.5.RELEASE</spring-version>
{{/generateSpringApplication}}
{{#generateSpringBootApplication}}
<spring.boot-version>1.3.3.RELEASE</spring.boot-version>
{{/generateSpringBootApplication}}
<cxf-version>3.1.6</cxf-version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@
<ref bean="commonValidationFeature" />
{{/useBeanValidationFeature}}
</cxf:features>
<cxf:properties>
<entry key="javax.ws.rs.ext.ExceptionMapper" value-ref="exceptionMapper"/>
</cxf:properties>
</cxf:bus>

{{#useWadlFeature}}
Expand Down Expand Up @@ -104,7 +107,12 @@
<bean id="validationOutInterceptor" class="org.apache.cxf.jaxrs.validation.JAXRSBeanValidationOutInterceptor">
<property name="provider" ref="validationProvider" />
</bean>
{{/useBeanValidationFeature}}
{{/useBeanValidationFeature}}
{{^useBeanValidationFeature}}
<bean id="exceptionMapper" class="org.apache.cxf.jaxrs.impl.WebApplicationExceptionMapper">
<property name="addMessageToResponse" value="true" />
</bean>
{{/useBeanValidationFeature}}

<!-- REST server -->
<jaxrs:server id="restServer" address="/services">
Expand Down
Loading

0 comments on commit cf3056f

Please sign in to comment.