use an annotation processor instead of a groovy script because of classloading issues

This commit is contained in:
Athou
2013-08-01 17:11:57 +02:00
parent e90747fd08
commit bfbe39993f
10 changed files with 135 additions and 40 deletions

36
pom.xml
View File

@@ -138,6 +138,19 @@
<outputDirectory>target/generated-sources/metamodel</outputDirectory>
</configuration>
</execution>
<execution>
<id>doc</id>
<phase>process-classes</phase>
<goals>
<goal>process</goal>
</goals>
<configuration>
<processors>
<processor>com.commafeed.frontend.APIGenerator</processor>
</processors>
<outputDirectory>target/generated-sources/api-docs</outputDirectory>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
@@ -340,6 +353,12 @@
<artifactId>swagger-annotations_2.9.1</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>com.wordnik</groupId>
<artifactId>swagger-jaxrs_2.9.1</artifactId>
<version>1.2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
@@ -471,15 +490,10 @@
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>com.wordnik</groupId>
<artifactId>swagger-jaxrs_2.9.1</artifactId>
<version>1.2.5</version>
</dependency>
</dependencies>
<executions>
<execution>
<phase>generate-sources</phase>
<phase>process-classes</phase>
<goals>
<goal>execute</goal>
</goals>
@@ -489,7 +503,6 @@
<prefix>templates/</prefix>
<destination>${basedir}/target/generated-sources/angularjs/all-templates.html</destination>
<i18nPath>${basedir}/src/main/resources/i18n/</i18nPath>
<doc.path>${basedir}/target/generated-sources/swagger-doc</doc.path>
</properties>
<scriptpath>
<element>${basedir}/src/main/script</element>
@@ -505,11 +518,6 @@
new
HTMLConcat().concat(source,
prefix, dest, i18n);
def docPath =
project.properties['doc.path'];
new
SwaggerStaticGenerator().generate(docPath);
</source>
</configuration>
</execution>
@@ -545,8 +553,8 @@
</includes>
</resource>
<resource>
<directory>target/generated-sources/swagger-doc/</directory>
<targetPath>api/swagger-doc</targetPath>
<directory>target/generated-sources/api-docs/</directory>
<targetPath>api/api-docs</targetPath>
<includes>
<include>**/*</include>
</includes>

View File

@@ -103,6 +103,7 @@ public class FeedDAO extends GenericDAO<Feed> {
}
@XmlRootElement
public static enum DuplicateMode {
NORMALIZED_URL(Feed_.normalizedUrlHash), LAST_CONTENT(Feed_.lastContentHash), PUSH_TOPIC(Feed_.pushTopicHash);
private SingularAttribute<Feed, String> path;

View File

@@ -10,6 +10,7 @@ import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
@@ -21,14 +22,17 @@ import org.hibernate.annotations.CacheConcurrencyStrategy;
@Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL)
public class UserSettings extends AbstractModel {
@XmlRootElement
public enum ReadingMode {
all, unread
}
@XmlRootElement
public enum ReadingOrder {
asc, desc
}
@XmlRootElement
public enum ViewMode {
title, expanded
}

View File

@@ -0,0 +1,96 @@
package com.commafeed.frontend;
import java.io.File;
import java.util.Set;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedOptions;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic.Kind;
import javax.tools.FileObject;
import javax.tools.StandardLocation;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import com.commafeed.frontend.model.Entries;
import com.commafeed.frontend.model.request.MarkRequest;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.core.Documentation;
import com.wordnik.swagger.core.DocumentationEndPoint;
import com.wordnik.swagger.core.SwaggerSpec;
import com.wordnik.swagger.core.util.TypeUtil;
import com.wordnik.swagger.jaxrs.HelpApi;
import com.wordnik.swagger.jaxrs.JaxrsApiReader;
@SupportedAnnotationTypes("com.wordnik.swagger.annotations.Api")
@SupportedOptions("outputDirectory")
public class APIGenerator extends AbstractProcessor {
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
try {
return processInternal(annotations, roundEnv);
} catch (Exception e) {
e.printStackTrace();
processingEnv.getMessager().printMessage(Kind.ERROR, e.getMessage());
}
return false;
}
private boolean processInternal(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) throws Exception {
JaxrsApiReader.setFormatString("");
TypeUtil.addAllowablePackage(Entries.class.getPackage().getName());
TypeUtil.addAllowablePackage(MarkRequest.class.getPackage().getName());
String apiVersion = "1.0";
String swaggerVersion = SwaggerSpec.version();
String basePath = "../rest";
Documentation doc = new Documentation();
for (Element element : roundEnv.getElementsAnnotatedWith(Api.class)) {
TypeElement type = (TypeElement) element;
String fqn = type.getQualifiedName().toString();
Class<?> resource = Class.forName(fqn);
Api api = resource.getAnnotation(Api.class);
String apiPath = api.value();
Documentation apiDoc = JaxrsApiReader.read(resource, apiVersion, swaggerVersion, basePath, apiPath);
apiDoc = new HelpApi(null).filterDocs(apiDoc, null, null, null, null);
apiDoc.setSwaggerVersion(swaggerVersion);
apiDoc.setApiVersion(apiVersion);
write(apiDoc, element);
doc.addApi(new DocumentationEndPoint(api.value(), api.description()));
}
doc.setSwaggerVersion(swaggerVersion);
doc.setApiVersion(apiVersion);
write(doc, null);
return true;
}
private void write(Documentation doc, Element element) throws Exception {
String fileName = doc.getResourcePath() == null ? "resources" : doc.getResourcePath();
fileName = StringUtils.removeStart(fileName, "/");
FileObject resource = null;
try {
resource = processingEnv.getFiler().createResource(StandardLocation.SOURCE_OUTPUT, "", fileName, element);
} catch (Exception e) {
// already processed
}
if (resource != null) {
FileUtils.writeStringToFile(new File(resource.toUri()), new ObjectMapper().writeValueAsString(doc));
}
}
}

View File

@@ -1,13 +0,0 @@
package com.commafeed.frontend.rest;
public class Enums {
public enum Type {
category, feed, entry;
}
public enum ReadType {
all, unread;
}
}

View File

@@ -32,6 +32,7 @@ import com.commafeed.backend.model.FeedEntryStatus;
import com.commafeed.backend.model.FeedSubscription;
import com.commafeed.backend.model.User;
import com.commafeed.backend.model.UserRole.Role;
import com.commafeed.backend.model.UserSettings.ReadingMode;
import com.commafeed.backend.model.UserSettings.ReadingOrder;
import com.commafeed.backend.services.ApplicationSettingsService;
import com.commafeed.backend.services.FeedEntryService;
@@ -47,7 +48,6 @@ import com.commafeed.frontend.model.request.CategoryModificationRequest;
import com.commafeed.frontend.model.request.CollapseRequest;
import com.commafeed.frontend.model.request.IDRequest;
import com.commafeed.frontend.model.request.MarkRequest;
import com.commafeed.frontend.rest.Enums.ReadType;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.sun.syndication.feed.synd.SyndEntry;
@@ -98,7 +98,7 @@ public class CategoryREST extends AbstractREST {
@ApiParam(value = "id of the category, 'all' or 'starred'", required = true) @QueryParam("id") String id, @ApiParam(
value = "all entries or only unread ones",
allowableValues = "all,unread",
required = true) @DefaultValue("unread") @QueryParam("readType") ReadType readType, @ApiParam(
required = true) @DefaultValue("unread") @QueryParam("readType") ReadingMode readType, @ApiParam(
value = "only entries newer than this") @QueryParam("newerThan") Long newerThan,
@ApiParam(value = "offset for paging") @DefaultValue("0") @QueryParam("offset") int offset, @ApiParam(
value = "limit for paging, default 20, maximum 50") @DefaultValue("20") @QueryParam("limit") int limit, @ApiParam(
@@ -112,7 +112,7 @@ public class CategoryREST extends AbstractREST {
Entries entries = new Entries();
entries.setOffset(offset);
entries.setLimit(limit);
boolean unreadOnly = readType == ReadType.unread;
boolean unreadOnly = readType == ReadingMode.unread;
if (StringUtils.isBlank(id)) {
id = ALL;
}
@@ -175,7 +175,7 @@ public class CategoryREST extends AbstractREST {
Preconditions.checkNotNull(id);
ReadType readType = ReadType.all;
ReadingMode readType = ReadingMode.all;
ReadingOrder order = ReadingOrder.desc;
int offset = 0;
int limit = 20;

View File

@@ -54,6 +54,7 @@ import com.commafeed.backend.model.FeedCategory;
import com.commafeed.backend.model.FeedEntryStatus;
import com.commafeed.backend.model.FeedSubscription;
import com.commafeed.backend.model.UserRole.Role;
import com.commafeed.backend.model.UserSettings.ReadingMode;
import com.commafeed.backend.model.UserSettings.ReadingOrder;
import com.commafeed.backend.services.ApplicationSettingsService;
import com.commafeed.backend.services.FeedEntryService;
@@ -68,7 +69,6 @@ import com.commafeed.frontend.model.request.FeedModificationRequest;
import com.commafeed.frontend.model.request.IDRequest;
import com.commafeed.frontend.model.request.MarkRequest;
import com.commafeed.frontend.model.request.SubscribeRequest;
import com.commafeed.frontend.rest.Enums.ReadType;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.sun.syndication.feed.opml.Opml;
@@ -135,7 +135,7 @@ public class FeedREST extends AbstractREST {
public Response getFeedEntries(@ApiParam(value = "id of the feed", required = true) @QueryParam("id") String id, @ApiParam(
value = "all entries or only unread ones",
allowableValues = "all,unread",
required = true) @DefaultValue("unread") @QueryParam("readType") ReadType readType, @ApiParam(
required = true) @DefaultValue("unread") @QueryParam("readType") ReadingMode readType, @ApiParam(
value = "only entries newer than this") @QueryParam("newerThan") Long newerThan,
@ApiParam(value = "offset for paging") @DefaultValue("0") @QueryParam("offset") int offset, @ApiParam(
value = "limit for paging, default 20, maximum 50") @DefaultValue("20") @QueryParam("limit") int limit, @ApiParam(
@@ -152,7 +152,7 @@ public class FeedREST extends AbstractREST {
entries.setOffset(offset);
entries.setLimit(limit);
boolean unreadOnly = readType == ReadType.unread;
boolean unreadOnly = readType == ReadingMode.unread;
Date newerThanDate = newerThan == null ? null : new Date(Long.valueOf(newerThan));
@@ -192,7 +192,7 @@ public class FeedREST extends AbstractREST {
Preconditions.checkNotNull(id);
ReadType readType = ReadType.all;
ReadingMode readType = ReadingMode.all;
ReadingOrder order = ReadingOrder.desc;
int offset = 0;
int limit = 20;

View File

@@ -32,10 +32,9 @@ public class SwaggerStaticGenerator {
Api api = resource.getAnnotation(Api.class);
if (api != null) {
String apiPath = api.value();
String apiListingPath = api.value();
Documentation apiDoc = new HelpApi(null).filterDocs(
JaxrsApiReader.read(resource, apiVersion, swaggerVersion, basePath, apiPath), null, null, apiListingPath, apiPath);
Documentation apiDoc = JaxrsApiReader.read(resource, apiVersion, swaggerVersion, basePath, apiPath);
apiDoc = new HelpApi(null).filterDocs(apiDoc, null, null, null, null);
apiDoc.setSwaggerVersion(swaggerVersion);
apiDoc.setApiVersion(apiVersion);

View File

@@ -29,7 +29,7 @@
link.attr('href', url + '/rest');
link.html(link.attr('href'));
url = url + '/api/swagger-doc/resources';
url = url + '/api/api-docs/resources';
window.swaggerUi = new SwaggerUi({
discoveryUrl:url,
apiKey:"",

View File

@@ -721,7 +721,7 @@ module.controller('FeedListCtrl', [
}
var callback = function(data) {
if (data.offset == 0) {
if (data.offset === 0) {
$scope.entries = [];
}
for ( var i = 0; i < data.entries.length; i++) {