forked from Archives/Athou_commafeed
use swagger instead of enunciate
This commit is contained in:
@@ -15,9 +15,10 @@ import com.commafeed.backend.model.UserRole.Role;
|
||||
import com.commafeed.backend.services.UserService;
|
||||
import com.google.common.collect.Sets;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public class CommaFeedSession extends AuthenticatedWebSession {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Inject
|
||||
UserService userService;
|
||||
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
package com.commafeed.frontend.rest;
|
||||
|
||||
import java.util.Enumeration;
|
||||
|
||||
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.frontend.model.Entries;
|
||||
import com.wordnik.swagger.annotations.Api;
|
||||
import com.wordnik.swagger.annotations.ApiOperation;
|
||||
import com.wordnik.swagger.core.SwaggerSpec;
|
||||
import com.wordnik.swagger.jaxrs.ConfigReader;
|
||||
import com.wordnik.swagger.jaxrs.JavaApiListing;
|
||||
|
||||
@Path("/resources")
|
||||
@Api("/resources")
|
||||
@Produces({ "application/json" })
|
||||
public class ApiListingResource extends JavaApiListing {
|
||||
|
||||
@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) {
|
||||
|
||||
return super.getAllApis(new ServletConfigProxy(sc), app, headers,
|
||||
uriInfo);
|
||||
}
|
||||
|
||||
public static class ServletConfigProxy implements ServletConfig {
|
||||
|
||||
private ServletConfig sc;
|
||||
|
||||
public ServletConfigProxy(ServletConfig sc) {
|
||||
this.sc = sc;
|
||||
}
|
||||
|
||||
@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();
|
||||
}
|
||||
return sc.getInitParameter(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Enumeration<String> getInitParameterNames() {
|
||||
return sc.getInitParameterNames();
|
||||
}
|
||||
}
|
||||
|
||||
public static class CustomConfigReader extends ConfigReader {
|
||||
|
||||
public CustomConfigReader(ServletConfig config) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String basePath() {
|
||||
return "http://localhost:8082/commafeed/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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,124 +0,0 @@
|
||||
package com.commafeed.frontend.rest;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Type;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import javax.ws.rs.Consumes;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.WebApplicationException;
|
||||
import javax.ws.rs.core.HttpHeaders;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.MultivaluedMap;
|
||||
import javax.ws.rs.ext.MessageBodyReader;
|
||||
import javax.ws.rs.ext.MessageBodyWriter;
|
||||
import javax.ws.rs.ext.Provider;
|
||||
|
||||
import org.apache.commons.lang.time.DateUtils;
|
||||
|
||||
import com.commafeed.frontend.CommaFeedSession;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
import com.google.gson.JsonSerializationContext;
|
||||
import com.google.gson.JsonSerializer;
|
||||
|
||||
@Provider
|
||||
@Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN })
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
public class JSONMessageBodyReaderWriter implements MessageBodyWriter<Object>,
|
||||
MessageBodyReader<Object> {
|
||||
|
||||
private static final String UTF_8 = "UTF-8";
|
||||
|
||||
private Gson gson;
|
||||
|
||||
@Override
|
||||
public boolean isWriteable(Class<?> type, Type genericType,
|
||||
Annotation[] annotations, MediaType mediaType) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isReadable(Class<?> type, Type genericType,
|
||||
Annotation[] annotations, MediaType mediaType) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getSize(Object t, Class<?> type, Type genericType,
|
||||
Annotation[] annotations, MediaType mediaType) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeTo(Object t, Class<?> type, Type genericType,
|
||||
Annotation[] annotations, MediaType mediaType,
|
||||
MultivaluedMap<String, Object> httpHeaders,
|
||||
OutputStream entityStream) throws IOException,
|
||||
WebApplicationException {
|
||||
httpHeaders.putSingle(HttpHeaders.CONTENT_TYPE, mediaType.toString()
|
||||
+ ";charset=" + UTF_8);
|
||||
httpHeaders.putSingle(HttpHeaders.CACHE_CONTROL, "no-cache");
|
||||
httpHeaders.putSingle("Pragma", "no-cache");
|
||||
if (type == String.class) {
|
||||
entityStream.write(t.toString().getBytes(UTF_8));
|
||||
} else {
|
||||
OutputStreamWriter writer = new OutputStreamWriter(entityStream,
|
||||
UTF_8);
|
||||
getGson().toJson(t, type, writer);
|
||||
writer.flush();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object readFrom(Class<Object> type, Type genericType,
|
||||
Annotation[] annotations, MediaType mediaType,
|
||||
MultivaluedMap<String, String> httpHeaders, InputStream entityStream)
|
||||
throws IOException, WebApplicationException {
|
||||
InputStreamReader reader = new InputStreamReader(
|
||||
new BufferedInputStream(entityStream), UTF_8);
|
||||
return getGson().fromJson(reader, type);
|
||||
}
|
||||
|
||||
private Gson getGson() {
|
||||
if (gson == null) {
|
||||
gson = new GsonBuilder().registerTypeAdapter(Date.class,
|
||||
new DateSerializer()).create();
|
||||
}
|
||||
return gson;
|
||||
}
|
||||
|
||||
private static class DateSerializer implements JsonSerializer<Date> {
|
||||
|
||||
private static final String DAY_FORMAT = "yyyy-MM-dd";
|
||||
private static final String TIME_FORMAT = "HH:mm";
|
||||
|
||||
public JsonElement serialize(Date src, Type typeOfSrc,
|
||||
JsonSerializationContext context) {
|
||||
Date now = Calendar.getInstance().getTime();
|
||||
String format = DateUtils.isSameDay(now, src) ? TIME_FORMAT
|
||||
: DAY_FORMAT;
|
||||
|
||||
Locale locale = CommaFeedSession.get().getLocale();
|
||||
Calendar calendar = Calendar.getInstance(locale);
|
||||
TimeZone timeZone = calendar.getTimeZone();
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat(format);
|
||||
dateFormat.setTimeZone(timeZone);
|
||||
|
||||
return new JsonPrimitive(dateFormat.format(src));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -11,15 +11,22 @@ import com.commafeed.frontend.rest.resources.EntriesREST;
|
||||
import com.commafeed.frontend.rest.resources.SessionREST;
|
||||
import com.commafeed.frontend.rest.resources.SettingsREST;
|
||||
import com.commafeed.frontend.rest.resources.SubscriptionsREST;
|
||||
import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
|
||||
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();
|
||||
set.add(JSONMessageBodyReaderWriter.class);
|
||||
set.add(ApiListingResource.class);
|
||||
set.add(JacksonJsonProvider.class);
|
||||
|
||||
set.add(SubscriptionsREST.class);
|
||||
set.add(EntriesREST.class);
|
||||
|
||||
@@ -6,15 +6,19 @@ import javax.annotation.PostConstruct;
|
||||
import javax.inject.Inject;
|
||||
import javax.interceptor.AroundInvoke;
|
||||
import javax.interceptor.InvocationContext;
|
||||
import javax.servlet.ServletConfig;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.ws.rs.Consumes;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.WebApplicationException;
|
||||
import javax.ws.rs.core.Context;
|
||||
import javax.ws.rs.core.HttpHeaders;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.core.Response.Status;
|
||||
import javax.ws.rs.core.UriInfo;
|
||||
|
||||
import org.apache.wicket.ThreadContext;
|
||||
import org.apache.wicket.authentication.IAuthenticationStrategy;
|
||||
@@ -41,11 +45,14 @@ 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.wordnik.swagger.annotations.ApiOperation;
|
||||
import com.wordnik.swagger.jaxrs.JavaHelp;
|
||||
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
@SecurityCheck(Role.USER)
|
||||
public abstract class AbstractREST {
|
||||
public abstract class AbstractREST extends JavaHelp {
|
||||
|
||||
@Context
|
||||
HttpServletRequest request;
|
||||
@@ -126,7 +133,7 @@ public abstract class AbstractREST {
|
||||
.entity("You need to be authenticated to do this.").build());
|
||||
}
|
||||
|
||||
boolean allowed = false;
|
||||
boolean allowed = true;
|
||||
Method method = context.getMethod();
|
||||
|
||||
if (method.isAnnotationPresent(SecurityCheck.class)) {
|
||||
@@ -153,4 +160,12 @@ public abstract class AbstractREST {
|
||||
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(sc), headers, uriInfo);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -5,8 +5,6 @@ import javax.ws.rs.GET;
|
||||
import javax.ws.rs.POST;
|
||||
import javax.ws.rs.Path;
|
||||
|
||||
import org.codehaus.enunciate.doc.DocumentationGroup;
|
||||
|
||||
import com.commafeed.backend.model.ApplicationSettings;
|
||||
import com.commafeed.backend.model.UserRole.Role;
|
||||
import com.commafeed.backend.services.ApplicationSettingsService;
|
||||
@@ -14,7 +12,6 @@ import com.commafeed.frontend.SecurityCheck;
|
||||
|
||||
@SecurityCheck(Role.ADMIN)
|
||||
@Path("admin/settings")
|
||||
@DocumentationGroup("Application Settings")
|
||||
public class AdminSettingsREST {
|
||||
|
||||
@Inject
|
||||
|
||||
@@ -12,7 +12,6 @@ import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.core.Response.Status;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.codehaus.enunciate.doc.DocumentationGroup;
|
||||
|
||||
import com.commafeed.backend.StartupBean;
|
||||
import com.commafeed.backend.model.User;
|
||||
@@ -27,7 +26,6 @@ import com.google.common.collect.Sets;
|
||||
|
||||
@SecurityCheck(Role.ADMIN)
|
||||
@Path("admin/users")
|
||||
@DocumentationGroup("User Management")
|
||||
public class AdminUsersREST extends AbstractREST {
|
||||
|
||||
@Path("save")
|
||||
|
||||
@@ -13,7 +13,6 @@ import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.core.Response.Status;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.codehaus.enunciate.doc.DocumentationGroup;
|
||||
|
||||
import com.commafeed.backend.model.FeedCategory;
|
||||
import com.commafeed.backend.model.FeedEntry;
|
||||
@@ -24,9 +23,12 @@ import com.commafeed.frontend.model.Entries;
|
||||
import com.commafeed.frontend.model.Entry;
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.wordnik.swagger.annotations.Api;
|
||||
import com.wordnik.swagger.annotations.ApiOperation;
|
||||
import com.wordnik.swagger.annotations.ApiParam;
|
||||
|
||||
@Path("entries")
|
||||
@DocumentationGroup("Entries")
|
||||
@Path("/entries/")
|
||||
@Api(value = "/entries", description = "Operations about entries")
|
||||
public class EntriesREST extends AbstractREST {
|
||||
|
||||
public static final String ALL = "all";
|
||||
@@ -39,9 +41,11 @@ public class EntriesREST extends AbstractREST {
|
||||
all, unread;
|
||||
}
|
||||
|
||||
@Path("get")
|
||||
@Path("/get")
|
||||
@GET
|
||||
public Entries getEntries(@QueryParam("type") Type type,
|
||||
@ApiOperation(value = "Find entry by ID", notes = "Add extra notes here", responseClass = "com.commafeed.frontend.model.Entries")
|
||||
public Entries getEntries(
|
||||
@ApiParam(value = "ID of entry that needs to be fetched", allowableValues = "range[1,5]", required = true) @QueryParam("type") Type type,
|
||||
@QueryParam("id") String id,
|
||||
@QueryParam("readType") ReadType readType,
|
||||
@DefaultValue("0") @QueryParam("offset") int offset,
|
||||
|
||||
@@ -6,7 +6,6 @@ import javax.ws.rs.Path;
|
||||
import javax.ws.rs.core.Response;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.codehaus.enunciate.doc.DocumentationGroup;
|
||||
|
||||
import com.commafeed.backend.model.User;
|
||||
import com.commafeed.backend.model.UserRole;
|
||||
@@ -15,7 +14,6 @@ import com.commafeed.frontend.model.ProfileModificationRequest;
|
||||
import com.commafeed.frontend.model.UserModel;
|
||||
|
||||
@Path("session")
|
||||
@DocumentationGroup("User settings")
|
||||
public class SessionREST extends AbstractREST {
|
||||
|
||||
@Path("get")
|
||||
|
||||
@@ -6,8 +6,6 @@ import javax.ws.rs.Path;
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.core.Response.Status;
|
||||
|
||||
import org.codehaus.enunciate.doc.DocumentationGroup;
|
||||
|
||||
import com.commafeed.backend.model.UserSettings;
|
||||
import com.commafeed.backend.model.UserSettings.ReadingMode;
|
||||
import com.commafeed.backend.model.UserSettings.ReadingOrder;
|
||||
@@ -15,7 +13,6 @@ import com.commafeed.frontend.model.Settings;
|
||||
import com.google.common.base.Preconditions;
|
||||
|
||||
@Path("settings")
|
||||
@DocumentationGroup("User settings")
|
||||
public class SettingsREST extends AbstractREST {
|
||||
|
||||
@Path("get")
|
||||
|
||||
@@ -21,7 +21,6 @@ import org.apache.commons.fileupload.disk.DiskFileItemFactory;
|
||||
import org.apache.commons.fileupload.servlet.ServletFileUpload;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.commons.lang.ObjectUtils;
|
||||
import org.codehaus.enunciate.doc.DocumentationGroup;
|
||||
|
||||
import com.commafeed.backend.model.Feed;
|
||||
import com.commafeed.backend.model.FeedCategory;
|
||||
@@ -33,7 +32,6 @@ import com.commafeed.frontend.rest.resources.EntriesREST.Type;
|
||||
import com.google.common.base.Preconditions;
|
||||
|
||||
@Path("subscriptions")
|
||||
@DocumentationGroup("Subscriptions")
|
||||
public class SubscriptionsREST extends AbstractREST {
|
||||
|
||||
@GET
|
||||
|
||||
Reference in New Issue
Block a user