mirror of
https://github.com/Athou/commafeed.git
synced 2026-03-21 21:37:29 +00:00
use jax rs instead of wicket for rest api
This commit is contained in:
@@ -45,5 +45,14 @@ public class FeedEntryService extends GenericDAO<FeedEntry, Long> {
|
||||
typedQuery.setParameter("user", user);
|
||||
return typedQuery.getResultList();
|
||||
}
|
||||
|
||||
public List<FeedEntry> getAllEntries(Feed feed, User user) {
|
||||
String query = "select e from FeedEntry e where e.feed=:feed";
|
||||
TypedQuery<FeedEntry> typedQuery = em.createQuery(query,
|
||||
FeedEntry.class);
|
||||
typedQuery.setParameter("feed", feed);
|
||||
typedQuery.setParameter("user", user);
|
||||
return typedQuery.getResultList();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -72,7 +72,7 @@ public class FeedParser {
|
||||
}
|
||||
|
||||
private String handleContent(String content) {
|
||||
Document doc = Jsoup.parse(content);
|
||||
Document doc = Jsoup.parse(content, "UTF-8");
|
||||
doc.select("a").attr("target", "_blank");
|
||||
return doc.outerHtml();
|
||||
}
|
||||
|
||||
@@ -29,8 +29,6 @@ import org.slf4j.LoggerFactory;
|
||||
import com.commafeed.frontend.components.auth.LoginPage;
|
||||
import com.commafeed.frontend.components.auth.LogoutPage;
|
||||
import com.commafeed.frontend.pages.home.HomePage;
|
||||
import com.commafeed.frontend.rest.FeedEntriesREST;
|
||||
import com.commafeed.frontend.rest.FeedSubscriptionsREST;
|
||||
import com.commafeed.frontend.utils.exception.DisplayExceptionPage;
|
||||
|
||||
public class CommaFeedApplication extends AuthenticatedWebApplication {
|
||||
@@ -45,11 +43,6 @@ public class CommaFeedApplication extends AuthenticatedWebApplication {
|
||||
mountPage("logout", LogoutPage.class);
|
||||
mountPage("error", DisplayExceptionPage.class);
|
||||
|
||||
mountPage("subscriptions", FeedSubscriptionsREST.class);
|
||||
mountPage(String.format("entries/${%s}/${%s}/${%s}",
|
||||
FeedEntriesREST.PARAM_TYPE, FeedEntriesREST.PARAM_ID,
|
||||
FeedEntriesREST.PARAM_READTYPE), FeedEntriesREST.class);
|
||||
|
||||
setupInjection();
|
||||
|
||||
getMarkupSettings().setStripWicketTags(true);
|
||||
|
||||
60
src/main/java/com/commafeed/frontend/rest/AbstractREST.java
Normal file
60
src/main/java/com/commafeed/frontend/rest/AbstractREST.java
Normal file
@@ -0,0 +1,60 @@
|
||||
package com.commafeed.frontend.rest;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.inject.Inject;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.core.Context;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
|
||||
import org.apache.wicket.Application;
|
||||
import org.apache.wicket.ThreadContext;
|
||||
import org.apache.wicket.protocol.http.servlet.ServletWebRequest;
|
||||
import org.apache.wicket.protocol.http.servlet.ServletWebResponse;
|
||||
import org.apache.wicket.request.cycle.RequestCycle;
|
||||
|
||||
import com.commafeed.backend.dao.FeedCategoryService;
|
||||
import com.commafeed.backend.dao.FeedEntryService;
|
||||
import com.commafeed.backend.dao.FeedEntryStatusService;
|
||||
import com.commafeed.backend.dao.FeedSubscriptionService;
|
||||
import com.commafeed.frontend.CommaFeedSession;
|
||||
import com.commafeed.model.User;
|
||||
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
public abstract class AbstractREST {
|
||||
|
||||
@Context
|
||||
HttpServletRequest request;
|
||||
|
||||
@Context
|
||||
HttpServletResponse response;
|
||||
|
||||
@Inject
|
||||
FeedSubscriptionService feedSubscriptionService;
|
||||
|
||||
@Inject
|
||||
FeedCategoryService feedCategoryService;
|
||||
|
||||
@Inject
|
||||
FeedEntryService feedEntryService;
|
||||
|
||||
@Inject
|
||||
FeedEntryStatusService feedEntryStatusService;
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
ServletWebRequest swreq = new ServletWebRequest(request, "");
|
||||
ServletWebResponse swresp = new ServletWebResponse(swreq, response);
|
||||
RequestCycle cycle = Application.get()
|
||||
.createRequestCycle(swreq, swresp);
|
||||
ThreadContext.setRequestCycle(cycle);
|
||||
Application.get().fetchCreateAndSetSession(
|
||||
Application.get().createRequestCycle(swreq, swresp));
|
||||
}
|
||||
|
||||
protected User getUser() {
|
||||
return CommaFeedSession.get().getUser();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -5,15 +5,12 @@ import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.PathParam;
|
||||
|
||||
import org.apache.commons.lang.ObjectUtils;
|
||||
import org.apache.wicket.request.mapper.parameter.PageParameters;
|
||||
|
||||
import com.commafeed.backend.dao.FeedCategoryService;
|
||||
import com.commafeed.backend.dao.FeedEntryService;
|
||||
import com.commafeed.backend.dao.FeedEntryStatusService;
|
||||
import com.commafeed.backend.dao.FeedSubscriptionService;
|
||||
import com.commafeed.frontend.rest.model.Entries;
|
||||
import com.commafeed.frontend.rest.model.Entry;
|
||||
import com.commafeed.frontend.utils.ModelFactory.MF;
|
||||
@@ -24,36 +21,13 @@ import com.commafeed.model.FeedSubscription;
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
public class FeedEntriesREST extends JSONPage {
|
||||
@Path("entries")
|
||||
public class EntriesREST extends AbstractREST {
|
||||
|
||||
public static final String PARAM_TYPE = "type";
|
||||
public static final String PARAM_ID = "id";
|
||||
public static final String PARAM_READTYPE = "readtype";
|
||||
|
||||
@Inject
|
||||
FeedEntryService feedEntryService;
|
||||
|
||||
@Inject
|
||||
FeedEntryStatusService feedEntryStatusService;
|
||||
|
||||
@Inject
|
||||
FeedSubscriptionService feedSubscriptionService;
|
||||
|
||||
@Inject
|
||||
FeedCategoryService feedCategoryService;
|
||||
|
||||
public FeedEntriesREST(PageParameters pageParameters) {
|
||||
super(pageParameters);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object getObject(PageParameters parameters) {
|
||||
|
||||
String type = parameters.get(PARAM_TYPE).toString();
|
||||
String id = parameters.get(PARAM_ID).toString();
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
String readType = parameters.get(PARAM_READTYPE).toString();
|
||||
@Path("get/{type}/{id}/{readType}")
|
||||
@GET
|
||||
public Entries getEntries(@PathParam("type") String type,
|
||||
@PathParam("id") String id, @PathParam("readType") String readType) {
|
||||
|
||||
Entries entries = new Entries();
|
||||
if ("feed".equals(type)) {
|
||||
@@ -0,0 +1,74 @@
|
||||
package com.commafeed.frontend.rest;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Type;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
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.MessageBodyWriter;
|
||||
import javax.ws.rs.ext.Provider;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.commons.lang.time.DateUtils;
|
||||
|
||||
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)
|
||||
public class JSONMessageBodyWriter implements MessageBodyWriter<Object> {
|
||||
|
||||
@Override
|
||||
public boolean isWriteable(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");
|
||||
|
||||
Gson gson = new GsonBuilder().registerTypeAdapter(Date.class,
|
||||
new DateSerializer()).create();
|
||||
IOUtils.write(gson.toJson(t), entityStream, "UTF-8");
|
||||
|
||||
}
|
||||
|
||||
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;
|
||||
return new JsonPrimitive(new SimpleDateFormat(format).format(src));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
package com.commafeed.frontend.rest;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
import org.apache.commons.lang.time.DateUtils;
|
||||
import org.apache.wicket.markup.html.WebPage;
|
||||
import org.apache.wicket.request.handler.TextRequestHandler;
|
||||
import org.apache.wicket.request.mapper.parameter.PageParameters;
|
||||
|
||||
import com.commafeed.frontend.CommaFeedSession;
|
||||
import com.commafeed.model.User;
|
||||
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;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public abstract class JSONPage extends WebPage {
|
||||
|
||||
public JSONPage(PageParameters pageParameters) {
|
||||
Gson gson = new GsonBuilder().registerTypeAdapter(Date.class,
|
||||
new DateSerializer()).create();
|
||||
getRequestCycle().scheduleRequestHandlerAfterCurrent(
|
||||
new TextRequestHandler("application/json", "UTF-8", gson
|
||||
.toJson(getObject(pageParameters))));
|
||||
}
|
||||
|
||||
protected abstract Object getObject(PageParameters parameters);
|
||||
|
||||
protected User getUser() {
|
||||
return CommaFeedSession.get().getUser();
|
||||
}
|
||||
|
||||
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;
|
||||
return new JsonPrimitive(new SimpleDateFormat(format).format(src));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.commafeed.frontend.rest;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import javax.ws.rs.ApplicationPath;
|
||||
import javax.ws.rs.core.Application;
|
||||
|
||||
import com.google.common.collect.Sets;
|
||||
|
||||
@ApplicationPath("/rest")
|
||||
public class RESTApplication extends Application {
|
||||
|
||||
@Override
|
||||
public Set<Class<?>> getClasses() {
|
||||
Set<Class<?>> set = Sets.newHashSet();
|
||||
set.add(JSONMessageBodyWriter.class);
|
||||
|
||||
set.add(SubscriptionsREST.class);
|
||||
set.add(EntriesREST.class);
|
||||
return set;
|
||||
}
|
||||
}
|
||||
@@ -2,42 +2,29 @@ package com.commafeed.frontend.rest;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.Path;
|
||||
|
||||
import org.apache.commons.lang.ObjectUtils;
|
||||
import org.apache.wicket.request.mapper.parameter.PageParameters;
|
||||
|
||||
import com.commafeed.backend.dao.FeedCategoryService;
|
||||
import com.commafeed.backend.dao.FeedEntryService;
|
||||
import com.commafeed.backend.dao.FeedSubscriptionService;
|
||||
import com.commafeed.frontend.rest.model.Category;
|
||||
import com.commafeed.frontend.rest.model.Subscription;
|
||||
import com.commafeed.model.FeedCategory;
|
||||
import com.commafeed.model.FeedSubscription;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public class FeedSubscriptionsREST extends JSONPage {
|
||||
public class SubscriptionsREST extends AbstractREST {
|
||||
|
||||
@Inject
|
||||
FeedSubscriptionService feedSubscriptionService;
|
||||
|
||||
@Inject
|
||||
FeedCategoryService feedCategoryService;
|
||||
|
||||
@Inject
|
||||
FeedEntryService feedEntryService;
|
||||
|
||||
public FeedSubscriptionsREST(PageParameters pageParameters) {
|
||||
super(pageParameters);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object getObject(PageParameters parameters) {
|
||||
List<FeedCategory> categories = feedCategoryService.findAll(getUser());
|
||||
@Path("subscriptions")
|
||||
@GET
|
||||
public Category getSubscriptions() {
|
||||
Category root = new Category();
|
||||
|
||||
List<FeedCategory> categories = feedCategoryService.findAll(getUser());
|
||||
addChildren(categories, root);
|
||||
|
||||
root.setId("all");
|
||||
root.setName("All");
|
||||
|
||||
for (FeedSubscription subscription : feedSubscriptionService
|
||||
.findWithoutCategories(getUser())) {
|
||||
Subscription sub = new Subscription();
|
||||
@@ -75,4 +62,5 @@ public class FeedSubscriptionsREST extends JSONPage {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,10 +1,12 @@
|
||||
package com.commafeed.frontend.rest.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
public class Category {
|
||||
public class Category implements Serializable {
|
||||
|
||||
private String id;
|
||||
private String name;
|
||||
private List<Category> children = Lists.newArrayList();
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
package com.commafeed.frontend.rest.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
public class Entries {
|
||||
public class Entries implements Serializable {
|
||||
private String name;
|
||||
private List<Entry> entries = Lists.newArrayList();
|
||||
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
package com.commafeed.frontend.rest.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
|
||||
public class Entry {
|
||||
public class Entry implements Serializable {
|
||||
|
||||
private String id;
|
||||
private String title;
|
||||
private String content;
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package com.commafeed.frontend.rest.model;
|
||||
|
||||
public class Subscription {
|
||||
import java.io.Serializable;
|
||||
|
||||
public class Subscription implements Serializable {
|
||||
|
||||
private Long id;
|
||||
private String name;
|
||||
private int unread;
|
||||
|
||||
Reference in New Issue
Block a user