From 8486f7b57ea894605cd7e041ec7228a2e436d4a5 Mon Sep 17 00:00:00 2001 From: Athou Date: Wed, 17 Apr 2013 09:54:52 +0200 Subject: [PATCH] don't extend swagger classes, i'll just do the work myself. fixes issues with weld --- .../frontend/rest/ApiListingResource.java | 119 +++++------------- .../frontend/rest/resources/AbstractREST.java | 37 ++++-- 2 files changed, 61 insertions(+), 95 deletions(-) diff --git a/src/main/java/com/commafeed/frontend/rest/ApiListingResource.java b/src/main/java/com/commafeed/frontend/rest/ApiListingResource.java index 084babe2..596c76c2 100644 --- a/src/main/java/com/commafeed/frontend/rest/ApiListingResource.java +++ b/src/main/java/com/commafeed/frontend/rest/ApiListingResource.java @@ -1,120 +1,63 @@ package com.commafeed.frontend.rest; -import java.util.Enumeration; - import javax.inject.Inject; -import javax.servlet.ServletConfig; -import javax.servlet.ServletContext; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; 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.UriInfo; import com.commafeed.backend.services.ApplicationSettingsService; import com.commafeed.frontend.model.Entries; 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.jaxrs.ConfigReader; -import com.wordnik.swagger.jaxrs.JavaApiListing; +import com.wordnik.swagger.core.util.TypeUtil; @Path("/resources") @Api("/resources") @Produces({ "application/json" }) -public class ApiListingResource extends JavaApiListing { +public class ApiListingResource { + + public static final String API_VERSION = "1.0"; @Inject ApplicationSettingsService applicationSettingsService; - @Override @GET @ApiOperation(value = "Returns list of all available api endpoints", responseClass = "List[DocumentationEndPoint]") - public Response getAllApis(@Context final ServletConfig sc, - @Context Application app, @Context HttpHeaders headers, - @Context UriInfo uriInfo) { + public Response getAllApis(@Context Application app) { - return super.getAllApis(new ServletConfigProxy( - applicationSettingsService.get().getPublicUrl(), sc), app, - headers, uriInfo); + TypeUtil.addAllowablePackage(Entries.class.getPackage().getName()); + + Documentation doc = new Documentation(); + for (Class resource : app.getClasses()) { + if (ApiListingResource.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 class ServletConfigProxy implements ServletConfig { - - private ServletConfig sc; - private String publicUrl; - - public ServletConfigProxy(String publicUrl, ServletConfig sc) { - this.sc = sc; - this.publicUrl = publicUrl; - } - - @Override - public String getServletName() { - return sc.getServletName(); - } - - @Override - public ServletContext getServletContext() { - return sc.getServletContext(); - } - - @Override - public String getInitParameter(String name) { - if ("swagger.config.reader".equals(name)) { - return CustomConfigReader.class.getName(); - } - if (CustomConfigReader.class.getName().equals(name)) { - return publicUrl; - } - return sc.getInitParameter(name); - } - - @Override - public Enumeration getInitParameterNames() { - return sc.getInitParameterNames(); + public static String getBasePath(String publicUrl) { + if (publicUrl.endsWith("/")) { + publicUrl = publicUrl.substring(0, publicUrl.length() - 1); } + return publicUrl + "/rest"; } - public static class CustomConfigReader extends ConfigReader { - - private ServletConfig config; - - public CustomConfigReader(ServletConfig config) { - this.config = config; - } - - @Override - public String basePath() { - String publicUrl = config.getInitParameter(CustomConfigReader.class - .getName()); - if (publicUrl.endsWith("/")) { - publicUrl = publicUrl.substring(0, publicUrl.length() - 1); - } - return publicUrl + "/rest"; - } - - @Override - public String swaggerVersion() { - return SwaggerSpec.version(); - } - - @Override - public String apiVersion() { - return "1.0"; - } - - @Override - public String modelPackages() { - return Entries.class.getPackage().getName(); - } - - @Override - public String apiFilterClassName() { - return null; - } - } } \ No newline at end of file diff --git a/src/main/java/com/commafeed/frontend/rest/resources/AbstractREST.java b/src/main/java/com/commafeed/frontend/rest/resources/AbstractREST.java index 06fad2f3..b020eaa4 100644 --- a/src/main/java/com/commafeed/frontend/rest/resources/AbstractREST.java +++ b/src/main/java/com/commafeed/frontend/rest/resources/AbstractREST.java @@ -46,14 +46,20 @@ import com.commafeed.backend.services.UserService; import com.commafeed.frontend.CommaFeedApplication; import com.commafeed.frontend.CommaFeedSession; import com.commafeed.frontend.SecurityCheck; -import com.commafeed.frontend.rest.ApiListingResource.ServletConfigProxy; +import com.commafeed.frontend.model.Entries; +import com.commafeed.frontend.rest.ApiListingResource; +import com.wordnik.swagger.annotations.Api; import com.wordnik.swagger.annotations.ApiOperation; -import com.wordnik.swagger.jaxrs.JavaHelp; +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; @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) @SecurityCheck(Role.USER) -public abstract class AbstractREST extends JavaHelp { +public abstract class AbstractREST { @Context HttpServletRequest request; @@ -164,13 +170,30 @@ public abstract class AbstractREST extends JavaHelp { return authorized; } - @Override @GET @ApiOperation(value = "Returns information about API parameters", responseClass = "com.wordnik.swagger.core.Documentation") public Response getHelp(@Context ServletConfig sc, @Context HttpHeaders headers, @Context UriInfo uriInfo) { - return super.getHelp(new ServletConfigProxy(applicationSettingsService - .get().getPublicUrl(), sc), headers, uriInfo); - } + TypeUtil.addAllowablePackage(Entries.class.getPackage().getName()); + String apiVersion = ApiListingResource.API_VERSION; + String swaggerVersion = SwaggerSpec.version(); + String basePath = ApiListingResource + .getBasePath(applicationSettingsService.get().getPublicUrl()); + Api api = this.getClass().getAnnotation(Api.class); + if (api == null) { + return Response.status(Status.NOT_FOUND).build(); + } + String apiPath = null; + String apiListingPath = api.value(); + + Documentation doc = new HelpApi("").filterDocs(JaxrsApiReader.read( + getClass(), apiVersion, swaggerVersion, basePath, apiPath), + headers, uriInfo, apiListingPath, apiPath); + + doc.setSwaggerVersion(swaggerVersion); + doc.setBasePath(basePath); + doc.setApiVersion(apiVersion); + return Response.ok().entity(doc).build(); + } }