mirror of
https://github.com/Athou/commafeed.git
synced 2026-03-21 21:37:29 +00:00
don't extend swagger classes, i'll just do the work myself. fixes issues with weld
This commit is contained in:
@@ -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<String> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user