Search in sources :

Example 1 with SwaggerFeature

use of io.fabric8.cxf.endpoint.SwaggerFeature in project fabric8 by jboss-fuse.

the class Java2SwaggerJsonMojo method execute.

public void execute() throws MojoExecutionException {
    List<Class<?>> resourceClasses = loadResourceClasses();
    List<Object> resourceObjects = new ArrayList<Object>();
    for (Class<?> resourceClass : resourceClasses) {
        try {
            resourceObjects.add(resourceClass.newInstance());
        } catch (InstantiationException e) {
            throw new MojoExecutionException(e.getMessage(), e);
        } catch (IllegalAccessException e) {
            throw new MojoExecutionException(e.getMessage(), e);
        }
    }
    Thread.currentThread().setContextClassLoader(getClassLoader());
    List<Feature> features = new ArrayList<Feature>();
    features.add(new SwaggerFeature());
    JAXRSServerFactoryBean serverFacBean = new JAXRSServerFactoryBean();
    serverFacBean.setAddress(address);
    serverFacBean.setServiceBeans(resourceObjects);
    serverFacBean.setFeatures(features);
    Server server = serverFacBean.create();
    InputStream in = null;
    try {
        String res = "";
        for (Class<?> resourceClass : resourceClasses) {
            com.wordnik.swagger.annotations.Api api = resourceClass.getAnnotation(com.wordnik.swagger.annotations.Api.class);
            if (api != null) {
                String apiPath = api.value();
                String serverAddress = server.getEndpoint().getEndpointInfo().getAddress();
                String apiDocs = serverAddress + "/api-docs";
                URL url = new URL(apiDocs + apiPath);
                in = url.openStream();
                res = res + getStringFromInputStream(in);
            }
        }
        generateJson(resourceClasses, res);
    } catch (Exception e) {
        throw new MojoExecutionException(e.getMessage(), e);
    } finally {
        server.stop();
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Server(org.apache.cxf.endpoint.Server) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) JAXRSServerFactoryBean(org.apache.cxf.jaxrs.JAXRSServerFactoryBean) SwaggerFeature(io.fabric8.cxf.endpoint.SwaggerFeature) Feature(org.apache.cxf.feature.Feature) URL(java.net.URL) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) SwaggerFeature(io.fabric8.cxf.endpoint.SwaggerFeature)

Example 2 with SwaggerFeature

use of io.fabric8.cxf.endpoint.SwaggerFeature in project fabric8 by fabric8io.

the class Java2SwaggerJsonMojo method execute.

public void execute() throws MojoExecutionException {
    List<Class<?>> resourceClasses = loadResourceClasses();
    List<Object> resourceObjects = new ArrayList<Object>();
    for (Class<?> resourceClass : resourceClasses) {
        try {
            resourceObjects.add(resourceClass.newInstance());
        } catch (InstantiationException e) {
            throw new MojoExecutionException(e.getMessage(), e);
        } catch (IllegalAccessException e) {
            throw new MojoExecutionException(e.getMessage(), e);
        }
    }
    Thread.currentThread().setContextClassLoader(getClassLoader());
    List<Feature> features = new ArrayList<Feature>();
    features.add(new SwaggerFeature());
    JAXRSServerFactoryBean serverFacBean = new JAXRSServerFactoryBean();
    serverFacBean.setAddress(address);
    serverFacBean.setServiceBeans(resourceObjects);
    serverFacBean.setFeatures(features);
    Server server = serverFacBean.create();
    InputStream in = null;
    try {
        String serverAddress = server.getEndpoint().getEndpointInfo().getAddress();
        String apiDocs = serverAddress + "/swagger.json";
        URL url = new URL(apiDocs);
        in = url.openStream();
        String res = getStringFromInputStream(in);
        generateJson(resourceClasses, res);
    } catch (Exception e) {
        throw new MojoExecutionException(e.getMessage(), e);
    } finally {
        server.stop();
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Server(org.apache.cxf.endpoint.Server) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) JAXRSServerFactoryBean(org.apache.cxf.jaxrs.JAXRSServerFactoryBean) SwaggerFeature(io.fabric8.cxf.endpoint.SwaggerFeature) Feature(org.apache.cxf.feature.Feature) URL(java.net.URL) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) SwaggerFeature(io.fabric8.cxf.endpoint.SwaggerFeature)

Aggregations

SwaggerFeature (io.fabric8.cxf.endpoint.SwaggerFeature)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 URL (java.net.URL)2 ArrayList (java.util.ArrayList)2 Server (org.apache.cxf.endpoint.Server)2 Feature (org.apache.cxf.feature.Feature)2 JAXRSServerFactoryBean (org.apache.cxf.jaxrs.JAXRSServerFactoryBean)2 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)2