documentation is now generated during build time and swagger is not needed for runtime anymore

This commit is contained in:
Athou
2013-08-01 13:35:04 +02:00
parent 0ff1d58dfb
commit 8926f9784d
29 changed files with 128 additions and 430 deletions

View File

@@ -6,7 +6,6 @@ import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import com.commafeed.frontend.rest.resources.AdminREST;
import com.commafeed.frontend.rest.resources.ApiDocumentationREST;
import com.commafeed.frontend.rest.resources.CategoryREST;
import com.commafeed.frontend.rest.resources.EntryREST;
import com.commafeed.frontend.rest.resources.FeedREST;
@@ -14,15 +13,10 @@ import com.commafeed.frontend.rest.resources.PubSubHubbubCallbackREST;
import com.commafeed.frontend.rest.resources.ServerREST;
import com.commafeed.frontend.rest.resources.UserREST;
import com.google.common.collect.Sets;
import com.wordnik.swagger.jaxrs.JaxrsApiReader;
@ApplicationPath("/rest")
public class RESTApplication extends Application {
static {
JaxrsApiReader.setFormatString("");
}
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> set = Sets.newHashSet();
@@ -35,7 +29,6 @@ public class RESTApplication extends Application {
set.add(ServerREST.class);
set.add(AdminREST.class);
set.add(ApiDocumentationREST.class);
set.add(PubSubHubbubCallbackREST.class);
return set;

View File

@@ -33,6 +33,7 @@ import com.commafeed.frontend.SecurityCheck;
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@SecurityCheck(Role.USER)
public abstract class AbstractREST {
@Context

View File

@@ -1,77 +0,0 @@
package com.commafeed.frontend.rest.resources;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.UriInfo;
import org.apache.commons.lang.StringUtils;
import com.commafeed.backend.model.UserRole.Role;
import com.commafeed.backend.services.ApplicationSettingsService;
import com.commafeed.frontend.SecurityCheck;
import com.commafeed.frontend.model.Entries;
import com.commafeed.frontend.model.request.MarkRequest;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.core.Documentation;
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;
@SecurityCheck(Role.USER)
public abstract class AbstractResourceREST extends AbstractREST {
@Inject
ApplicationSettingsService applicationSettingsService;
@GET
@SecurityCheck(value = Role.NONE)
@ApiOperation(value = "Returns information about API parameters", responseClass = "com.wordnik.swagger.core.Documentation")
public Response getHelp(@Context Application app, @Context HttpHeaders headers, @Context UriInfo uriInfo) {
TypeUtil.addAllowablePackage(Entries.class.getPackage().getName());
TypeUtil.addAllowablePackage(MarkRequest.class.getPackage().getName());
String apiVersion = ApiDocumentationREST.API_VERSION;
String swaggerVersion = SwaggerSpec.version();
String basePath = ApiDocumentationREST.getBasePath(applicationSettingsService.get().getPublicUrl());
Class<?> resource = null;
String path = prependSlash(uriInfo.getPath());
for (Class<?> klass : app.getClasses()) {
Api api = klass.getAnnotation(Api.class);
if (api != null && api.value() != null && StringUtils.equals(prependSlash(api.value()), path)) {
resource = klass;
break;
}
}
if (resource == null) {
return Response.status(Status.NOT_FOUND).entity("Api annotation not found on class " + getClass().getName()).build();
}
Api api = resource.getAnnotation(Api.class);
String apiPath = api.value();
String apiListingPath = api.value();
Documentation doc = new HelpApi(null).filterDocs(JaxrsApiReader.read(resource, apiVersion, swaggerVersion, basePath, apiPath),
headers, uriInfo, apiListingPath, apiPath);
doc.setSwaggerVersion(swaggerVersion);
doc.setBasePath(basePath);
doc.setApiVersion(apiVersion);
return Response.ok().entity(doc).build();
}
private String prependSlash(String path) {
if (!path.startsWith("/")) {
path = "/" + path;
}
return path;
}
}

View File

@@ -33,6 +33,7 @@ import com.commafeed.backend.model.Feed;
import com.commafeed.backend.model.User;
import com.commafeed.backend.model.UserRole;
import com.commafeed.backend.model.UserRole.Role;
import com.commafeed.backend.services.ApplicationSettingsService;
import com.commafeed.backend.services.FeedService;
import com.commafeed.backend.services.PasswordEncryptionService;
import com.commafeed.backend.services.UserService;
@@ -51,7 +52,7 @@ import com.wordnik.swagger.annotations.ApiParam;
@SecurityCheck(Role.ADMIN)
@Path("/admin")
@Api(value = "/admin", description = "Operations about application administration")
public class AdminREST extends AbstractResourceREST {
public class AdminREST extends AbstractREST {
@Inject
FeedService feedService;
@@ -86,6 +87,9 @@ public class AdminREST extends AbstractResourceREST {
@Inject
PasswordEncryptionService encryptionService;
@Inject
ApplicationSettingsService applicationSettingsService;
@Path("/user/save")
@POST
@ApiOperation(value = "Save or update a user", notes = "Save or update a user. If the id is not specified, a new user will be created")

View File

@@ -1,63 +0,0 @@
package com.commafeed.frontend.rest.resources;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import com.commafeed.backend.services.ApplicationSettingsService;
import com.commafeed.frontend.model.Entries;
import com.commafeed.frontend.model.request.MarkRequest;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
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;
@Path("/resources")
@Api("/resources")
public class ApiDocumentationREST extends AbstractREST {
public static final String API_VERSION = "1.0";
@Inject
ApplicationSettingsService applicationSettingsService;
@GET
@ApiOperation(value = "Returns list of all available api endpoints", responseClass = "List[DocumentationEndPoint]")
public Response getAllApis(@Context Application app) {
TypeUtil.addAllowablePackage(Entries.class.getPackage().getName());
TypeUtil.addAllowablePackage(MarkRequest.class.getPackage().getName());
Documentation doc = new Documentation();
for (Class<?> resource : app.getClasses()) {
if (ApiDocumentationREST.class.equals(resource)) {
continue;
}
Api api = resource.getAnnotation(Api.class);
if (api != null) {
doc.addApi(new DocumentationEndPoint(api.value(), api.description()));
}
}
doc.setSwaggerVersion(SwaggerSpec.version());
doc.setBasePath(getBasePath(applicationSettingsService.get().getPublicUrl()));
doc.setApiVersion(API_VERSION);
return Response.ok().entity(doc).build();
}
public static String getBasePath(String publicUrl) {
if (!publicUrl.endsWith("/")) {
publicUrl = publicUrl + "/";
}
publicUrl += "rest";
return publicUrl;
}
}

View File

@@ -33,6 +33,7 @@ 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.ReadingOrder;
import com.commafeed.backend.services.ApplicationSettingsService;
import com.commafeed.backend.services.FeedEntryService;
import com.commafeed.backend.services.FeedSubscriptionService;
import com.commafeed.frontend.SecurityCheck;
@@ -59,7 +60,7 @@ import com.wordnik.swagger.annotations.ApiParam;
@Path("/category")
@Api(value = "/category", description = "Operations about user categories")
public class CategoryREST extends AbstractResourceREST {
public class CategoryREST extends AbstractREST {
private static Logger log = LoggerFactory.getLogger(CategoryREST.class);
@@ -83,6 +84,9 @@ public class CategoryREST extends AbstractResourceREST {
@Inject
CacheService cache;
@Inject
ApplicationSettingsService applicationSettingsService;
@Path("/entries")
@GET

View File

@@ -20,6 +20,7 @@ import com.commafeed.backend.dao.FeedSubscriptionDAO;
import com.commafeed.backend.model.FeedEntryStatus;
import com.commafeed.backend.model.FeedSubscription;
import com.commafeed.backend.model.UserSettings.ReadingOrder;
import com.commafeed.backend.services.ApplicationSettingsService;
import com.commafeed.backend.services.FeedEntryService;
import com.commafeed.frontend.model.Entries;
import com.commafeed.frontend.model.Entry;
@@ -33,7 +34,7 @@ import com.wordnik.swagger.annotations.ApiParam;
@Path("/entry")
@Api(value = "/entry", description = "Operations about feed entries")
public class EntryREST extends AbstractResourceREST {
public class EntryREST extends AbstractREST {
@Inject
FeedEntryService feedEntryService;
@@ -43,6 +44,9 @@ public class EntryREST extends AbstractResourceREST {
@Inject
FeedSubscriptionDAO feedSubscriptionDAO;
@Inject
ApplicationSettingsService applicationSettingsService;
@Path("/mark")
@POST

View File

@@ -55,6 +55,7 @@ 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.ReadingOrder;
import com.commafeed.backend.services.ApplicationSettingsService;
import com.commafeed.backend.services.FeedEntryService;
import com.commafeed.backend.services.FeedSubscriptionService;
import com.commafeed.frontend.SecurityCheck;
@@ -82,7 +83,7 @@ import com.wordnik.swagger.annotations.ApiParam;
@Path("/feed")
@Api(value = "/feed", description = "Operations about feeds")
public class FeedREST extends AbstractResourceREST {
public class FeedREST extends AbstractREST {
private static Logger log = LoggerFactory.getLogger(FeedREST.class);
@@ -124,6 +125,9 @@ public class FeedREST extends AbstractResourceREST {
@Context
private HttpServletRequest request;
@Inject
ApplicationSettingsService applicationSettingsService;
@Path("/entries")
@GET

View File

@@ -28,16 +28,18 @@ import com.commafeed.backend.feeds.FeedParser;
import com.commafeed.backend.feeds.FeedRefreshTaskGiver;
import com.commafeed.backend.feeds.FetchedFeed;
import com.commafeed.backend.model.Feed;
import com.commafeed.backend.model.UserRole.Role;
import com.commafeed.backend.services.ApplicationSettingsService;
import com.google.api.client.repackaged.com.google.common.base.Preconditions;
import com.commafeed.frontend.SecurityCheck;
import com.google.common.base.Preconditions;
@Path("/push")
public class PubSubHubbubCallbackREST {
public class PubSubHubbubCallbackREST extends AbstractREST {
private static Logger log = LoggerFactory.getLogger(PubSubHubbubCallbackREST.class);
@Context
HttpServletRequest request;
private HttpServletRequest request;
@Inject
FeedDAO feedDAO;
@@ -57,6 +59,7 @@ public class PubSubHubbubCallbackREST {
@Path("/callback")
@GET
@Produces(MediaType.TEXT_PLAIN)
@SecurityCheck(Role.NONE)
public Response verify(@QueryParam("hub.mode") String mode, @QueryParam("hub.topic") String topic,
@QueryParam("hub.challenge") String challenge, @QueryParam("hub.lease_seconds") String leaseSeconds,
@QueryParam("hub.verify_token") String verifyToken) {
@@ -87,6 +90,7 @@ public class PubSubHubbubCallbackREST {
@Path("/callback")
@POST
@Consumes({ MediaType.APPLICATION_ATOM_XML, "application/rss+xml" })
@SecurityCheck(Role.NONE)
public Response callback() {
if (!applicationSettingsService.get().isPubsubhubbub()) {

View File

@@ -13,19 +13,23 @@ import com.commafeed.backend.HttpGetter.HttpResult;
import com.commafeed.backend.StartupBean;
import com.commafeed.backend.feeds.FeedUtils;
import com.commafeed.backend.services.ApplicationPropertiesService;
import com.commafeed.backend.services.ApplicationSettingsService;
import com.commafeed.frontend.model.ServerInfo;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
@Path("/server")
@Api(value = "/server", description = "Operations about server infos")
public class ServerREST extends AbstractResourceREST {
public class ServerREST extends AbstractREST {
@Inject
StartupBean startupBean;
@Inject
HttpGetter httpGetter;
@Inject
ApplicationSettingsService applicationSettingsService;
@Path("/get")
@GET

View File

@@ -37,7 +37,7 @@ import com.wordnik.swagger.annotations.ApiParam;
@Path("/user")
@Api(value = "/user", description = "Operations about the user")
public class UserREST extends AbstractResourceREST {
public class UserREST extends AbstractREST {
@Inject
UserDAO userDAO;