forked from Archives/Athou_commafeed
handle session expiration
This commit is contained in:
@@ -21,7 +21,7 @@ public class FeedEntryStatusService extends GenericDAO<FeedEntryStatus, Long> {
|
|||||||
|
|
||||||
FeedEntryStatus status = null;
|
FeedEntryStatus status = null;
|
||||||
try {
|
try {
|
||||||
criteria.getSingleResult();
|
status = criteria.getSingleResult();
|
||||||
} catch (NoResultException e) {
|
} catch (NoResultException e) {
|
||||||
status = null;
|
status = null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,13 +10,15 @@ import javax.persistence.criteria.CriteriaBuilder;
|
|||||||
import javax.persistence.criteria.CriteriaQuery;
|
import javax.persistence.criteria.CriteriaQuery;
|
||||||
import javax.persistence.criteria.Root;
|
import javax.persistence.criteria.Root;
|
||||||
|
|
||||||
|
import com.commafeed.backend.model.AbstractModel;
|
||||||
import com.commafeed.frontend.utils.ModelFactory.MF;
|
import com.commafeed.frontend.utils.ModelFactory.MF;
|
||||||
import com.google.common.reflect.TypeToken;
|
import com.google.common.reflect.TypeToken;
|
||||||
import com.uaihebert.factory.EasyCriteriaFactory;
|
import com.uaihebert.factory.EasyCriteriaFactory;
|
||||||
import com.uaihebert.model.EasyCriteria;
|
import com.uaihebert.model.EasyCriteria;
|
||||||
|
|
||||||
@SuppressWarnings("serial")
|
@SuppressWarnings("serial")
|
||||||
public abstract class GenericDAO<T, K> implements Serializable {
|
public abstract class GenericDAO<T, K> implements
|
||||||
|
Serializable {
|
||||||
|
|
||||||
private TypeToken<T> type = new TypeToken<T>(getClass()) {
|
private TypeToken<T> type = new TypeToken<T>(getClass()) {
|
||||||
};
|
};
|
||||||
@@ -41,6 +43,14 @@ public abstract class GenericDAO<T, K> implements Serializable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void saveOrUpdate(AbstractModel m) {
|
||||||
|
if (m.getId() == null) {
|
||||||
|
em.persist(m);
|
||||||
|
} else {
|
||||||
|
em.merge(m);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void delete(T object) {
|
public void delete(T object) {
|
||||||
object = em.merge(object);
|
object = em.merge(object);
|
||||||
em.remove(object);
|
em.remove(object);
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package com.commafeed.backend.dao;
|
||||||
|
|
||||||
|
import javax.ejb.Stateless;
|
||||||
|
import javax.persistence.NoResultException;
|
||||||
|
|
||||||
|
import com.commafeed.backend.model.User;
|
||||||
|
import com.commafeed.backend.model.UserSettings;
|
||||||
|
import com.commafeed.frontend.utils.ModelFactory.MF;
|
||||||
|
import com.uaihebert.factory.EasyCriteriaFactory;
|
||||||
|
import com.uaihebert.model.EasyCriteria;
|
||||||
|
|
||||||
|
@Stateless
|
||||||
|
public class UserSettingsService extends GenericDAO<UserSettings, Long> {
|
||||||
|
|
||||||
|
public UserSettings findByUser(User user) {
|
||||||
|
|
||||||
|
EasyCriteria<UserSettings> criteria = EasyCriteriaFactory
|
||||||
|
.createQueryCriteria(em, getType());
|
||||||
|
criteria.andEquals(MF.i(proxy().getUser()), user);
|
||||||
|
|
||||||
|
UserSettings settings = null;
|
||||||
|
try {
|
||||||
|
settings = criteria.getSingleResult();
|
||||||
|
} catch (NoResultException e) {
|
||||||
|
settings = null;
|
||||||
|
}
|
||||||
|
return settings;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,6 +4,7 @@ import javax.enterprise.inject.spi.BeanManager;
|
|||||||
import javax.naming.InitialContext;
|
import javax.naming.InitialContext;
|
||||||
import javax.naming.NamingException;
|
import javax.naming.NamingException;
|
||||||
|
|
||||||
|
import org.apache.wicket.Application;
|
||||||
import org.apache.wicket.Page;
|
import org.apache.wicket.Page;
|
||||||
import org.apache.wicket.Session;
|
import org.apache.wicket.Session;
|
||||||
import org.apache.wicket.ajax.AjaxRequestTarget;
|
import org.apache.wicket.ajax.AjaxRequestTarget;
|
||||||
@@ -105,4 +106,9 @@ public class CommaFeedApplication extends AuthenticatedWebApplication {
|
|||||||
return CommaFeedSession.class;
|
return CommaFeedSession.class;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static CommaFeedApplication get() {
|
||||||
|
|
||||||
|
return (CommaFeedApplication) Application.get();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
17
src/main/java/com/commafeed/frontend/model/Settings.java
Normal file
17
src/main/java/com/commafeed/frontend/model/Settings.java
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package com.commafeed.frontend.model;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
public class Settings implements Serializable {
|
||||||
|
|
||||||
|
private String readingMode;
|
||||||
|
|
||||||
|
public String getReadingMode() {
|
||||||
|
return readingMode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setReadingMode(String readingMode) {
|
||||||
|
this.readingMode = readingMode;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -5,8 +5,8 @@ import java.util.Set;
|
|||||||
import javax.ws.rs.ApplicationPath;
|
import javax.ws.rs.ApplicationPath;
|
||||||
import javax.ws.rs.core.Application;
|
import javax.ws.rs.core.Application;
|
||||||
|
|
||||||
import com.commafeed.frontend.rest.resources.AbstractREST;
|
|
||||||
import com.commafeed.frontend.rest.resources.EntriesREST;
|
import com.commafeed.frontend.rest.resources.EntriesREST;
|
||||||
|
import com.commafeed.frontend.rest.resources.SettingsREST;
|
||||||
import com.commafeed.frontend.rest.resources.SubscriptionsREST;
|
import com.commafeed.frontend.rest.resources.SubscriptionsREST;
|
||||||
import com.google.common.collect.Sets;
|
import com.google.common.collect.Sets;
|
||||||
|
|
||||||
@@ -17,10 +17,11 @@ public class RESTApplication extends Application {
|
|||||||
public Set<Class<?>> getClasses() {
|
public Set<Class<?>> getClasses() {
|
||||||
Set<Class<?>> set = Sets.newHashSet();
|
Set<Class<?>> set = Sets.newHashSet();
|
||||||
set.add(JSONMessageBodyWriter.class);
|
set.add(JSONMessageBodyWriter.class);
|
||||||
|
|
||||||
set.add(AbstractREST.class);
|
|
||||||
set.add(SubscriptionsREST.class);
|
set.add(SubscriptionsREST.class);
|
||||||
set.add(EntriesREST.class);
|
set.add(EntriesREST.class);
|
||||||
|
set.add(SettingsREST.class);
|
||||||
|
|
||||||
return set;
|
return set;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,8 +10,8 @@ import javax.ws.rs.core.Context;
|
|||||||
import javax.ws.rs.core.MediaType;
|
import javax.ws.rs.core.MediaType;
|
||||||
import javax.ws.rs.core.Response;
|
import javax.ws.rs.core.Response;
|
||||||
|
|
||||||
import org.apache.wicket.Application;
|
|
||||||
import org.apache.wicket.ThreadContext;
|
import org.apache.wicket.ThreadContext;
|
||||||
|
import org.apache.wicket.authentication.IAuthenticationStrategy;
|
||||||
import org.apache.wicket.protocol.http.servlet.ServletWebRequest;
|
import org.apache.wicket.protocol.http.servlet.ServletWebRequest;
|
||||||
import org.apache.wicket.protocol.http.servlet.ServletWebResponse;
|
import org.apache.wicket.protocol.http.servlet.ServletWebResponse;
|
||||||
import org.apache.wicket.request.cycle.RequestCycle;
|
import org.apache.wicket.request.cycle.RequestCycle;
|
||||||
@@ -20,7 +20,10 @@ import com.commafeed.backend.dao.FeedCategoryService;
|
|||||||
import com.commafeed.backend.dao.FeedEntryService;
|
import com.commafeed.backend.dao.FeedEntryService;
|
||||||
import com.commafeed.backend.dao.FeedEntryStatusService;
|
import com.commafeed.backend.dao.FeedEntryStatusService;
|
||||||
import com.commafeed.backend.dao.FeedSubscriptionService;
|
import com.commafeed.backend.dao.FeedSubscriptionService;
|
||||||
|
import com.commafeed.backend.dao.UserService;
|
||||||
|
import com.commafeed.backend.dao.UserSettingsService;
|
||||||
import com.commafeed.backend.model.User;
|
import com.commafeed.backend.model.User;
|
||||||
|
import com.commafeed.frontend.CommaFeedApplication;
|
||||||
import com.commafeed.frontend.CommaFeedSession;
|
import com.commafeed.frontend.CommaFeedSession;
|
||||||
|
|
||||||
@Produces(MediaType.APPLICATION_JSON)
|
@Produces(MediaType.APPLICATION_JSON)
|
||||||
@@ -44,19 +47,33 @@ public abstract class AbstractREST {
|
|||||||
@Inject
|
@Inject
|
||||||
FeedEntryStatusService feedEntryStatusService;
|
FeedEntryStatusService feedEntryStatusService;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
UserService userService;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
UserSettingsService userSettingsService;
|
||||||
|
|
||||||
@PostConstruct
|
@PostConstruct
|
||||||
public void init() {
|
public void init() {
|
||||||
|
CommaFeedApplication app = CommaFeedApplication.get();
|
||||||
ServletWebRequest swreq = new ServletWebRequest(request, "");
|
ServletWebRequest swreq = new ServletWebRequest(request, "");
|
||||||
ServletWebResponse swresp = new ServletWebResponse(swreq, response);
|
ServletWebResponse swresp = new ServletWebResponse(swreq, response);
|
||||||
RequestCycle cycle = Application.get()
|
RequestCycle cycle = app.createRequestCycle(swreq, swresp);
|
||||||
.createRequestCycle(swreq, swresp);
|
|
||||||
ThreadContext.setRequestCycle(cycle);
|
ThreadContext.setRequestCycle(cycle);
|
||||||
Application.get().fetchCreateAndSetSession(
|
CommaFeedSession session = (CommaFeedSession) app
|
||||||
Application.get().createRequestCycle(swreq, swresp));
|
.fetchCreateAndSetSession(cycle);
|
||||||
|
|
||||||
|
IAuthenticationStrategy authenticationStrategy = app
|
||||||
|
.getSecuritySettings().getAuthenticationStrategy();
|
||||||
|
String[] data = authenticationStrategy.load();
|
||||||
|
if (data != null && data.length > 1) {
|
||||||
|
session.signIn(data[0], data[1]);
|
||||||
|
}
|
||||||
|
|
||||||
if (getUser() == null) {
|
if (getUser() == null) {
|
||||||
throw new WebApplicationException(Response.Status.UNAUTHORIZED);
|
throw new WebApplicationException(Response.Status.UNAUTHORIZED);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected User getUser() {
|
protected User getUser() {
|
||||||
|
|||||||
@@ -70,8 +70,8 @@ public class EntriesREST extends AbstractREST {
|
|||||||
feedEntries = feedEntryService.getUnreadEntries(
|
feedEntries = feedEntryService.getUnreadEntries(
|
||||||
subscription.getFeed(), getUser());
|
subscription.getFeed(), getUser());
|
||||||
} else {
|
} else {
|
||||||
feedEntries = feedEntryService.getAllEntries(
|
feedEntries = feedEntryService
|
||||||
subscription.getFeed());
|
.getAllEntries(subscription.getFeed());
|
||||||
}
|
}
|
||||||
|
|
||||||
List<Entry> entries = Lists.newArrayList();
|
List<Entry> entries = Lists.newArrayList();
|
||||||
@@ -118,11 +118,7 @@ public class EntriesREST extends AbstractREST {
|
|||||||
status.setEntry(entry);
|
status.setEntry(entry);
|
||||||
}
|
}
|
||||||
status.setRead(read);
|
status.setRead(read);
|
||||||
if (status.getId() == null) {
|
feedEntryStatusService.saveOrUpdate(status);
|
||||||
feedEntryStatusService.save(status);
|
|
||||||
} else {
|
|
||||||
feedEntryStatusService.update(status);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,35 @@
|
|||||||
|
package com.commafeed.frontend.rest.resources;
|
||||||
|
|
||||||
|
import javax.ws.rs.GET;
|
||||||
|
import javax.ws.rs.POST;
|
||||||
|
import javax.ws.rs.Path;
|
||||||
|
|
||||||
|
import com.commafeed.backend.model.UserSettings;
|
||||||
|
import com.commafeed.backend.model.UserSettings.ReadingMode;
|
||||||
|
import com.commafeed.frontend.model.Settings;
|
||||||
|
|
||||||
|
@Path("settings")
|
||||||
|
public class SettingsREST extends AbstractREST {
|
||||||
|
|
||||||
|
@Path("get")
|
||||||
|
@GET
|
||||||
|
public Settings get() {
|
||||||
|
UserSettings settings = userSettingsService.findByUser(getUser());
|
||||||
|
Settings s = new Settings();
|
||||||
|
s.setReadingMode(settings.getReadingMode().name().toLowerCase());
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Path("save")
|
||||||
|
@POST
|
||||||
|
public void save(Settings settings) {
|
||||||
|
UserSettings s = userSettingsService.findByUser(getUser());
|
||||||
|
if (s == null) {
|
||||||
|
s = new UserSettings();
|
||||||
|
s.setUser(getUser());
|
||||||
|
}
|
||||||
|
s.setReadingMode(ReadingMode.valueOf(settings.getReadingMode()));
|
||||||
|
userSettingsService.saveOrUpdate(s);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user