handle session expiration

This commit is contained in:
Athou
2013-03-23 19:46:09 +01:00
parent 1871fa1169
commit f0ac0d1722
9 changed files with 128 additions and 17 deletions

View File

@@ -21,7 +21,7 @@ public class FeedEntryStatusService extends GenericDAO<FeedEntryStatus, Long> {
FeedEntryStatus status = null;
try {
criteria.getSingleResult();
status = criteria.getSingleResult();
} catch (NoResultException e) {
status = null;
}

View File

@@ -10,13 +10,15 @@ import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import com.commafeed.backend.model.AbstractModel;
import com.commafeed.frontend.utils.ModelFactory.MF;
import com.google.common.reflect.TypeToken;
import com.uaihebert.factory.EasyCriteriaFactory;
import com.uaihebert.model.EasyCriteria;
@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()) {
};
@@ -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) {
object = em.merge(object);
em.remove(object);

View File

@@ -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;
}
}

View File

@@ -4,6 +4,7 @@ import javax.enterprise.inject.spi.BeanManager;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.apache.wicket.Application;
import org.apache.wicket.Page;
import org.apache.wicket.Session;
import org.apache.wicket.ajax.AjaxRequestTarget;
@@ -105,4 +106,9 @@ public class CommaFeedApplication extends AuthenticatedWebApplication {
return CommaFeedSession.class;
}
public static CommaFeedApplication get() {
return (CommaFeedApplication) Application.get();
}
}

View 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;
}
}

View File

@@ -5,8 +5,8 @@ import java.util.Set;
import javax.ws.rs.ApplicationPath;
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.SettingsREST;
import com.commafeed.frontend.rest.resources.SubscriptionsREST;
import com.google.common.collect.Sets;
@@ -17,10 +17,11 @@ public class RESTApplication extends Application {
public Set<Class<?>> getClasses() {
Set<Class<?>> set = Sets.newHashSet();
set.add(JSONMessageBodyWriter.class);
set.add(AbstractREST.class);
set.add(SubscriptionsREST.class);
set.add(EntriesREST.class);
set.add(SettingsREST.class);
return set;
}
}

View File

@@ -10,8 +10,8 @@ import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.apache.wicket.Application;
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.ServletWebResponse;
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.FeedEntryStatusService;
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.frontend.CommaFeedApplication;
import com.commafeed.frontend.CommaFeedSession;
@Produces(MediaType.APPLICATION_JSON)
@@ -44,19 +47,33 @@ public abstract class AbstractREST {
@Inject
FeedEntryStatusService feedEntryStatusService;
@Inject
UserService userService;
@Inject
UserSettingsService userSettingsService;
@PostConstruct
public void init() {
CommaFeedApplication app = CommaFeedApplication.get();
ServletWebRequest swreq = new ServletWebRequest(request, "");
ServletWebResponse swresp = new ServletWebResponse(swreq, response);
RequestCycle cycle = Application.get()
.createRequestCycle(swreq, swresp);
RequestCycle cycle = app.createRequestCycle(swreq, swresp);
ThreadContext.setRequestCycle(cycle);
Application.get().fetchCreateAndSetSession(
Application.get().createRequestCycle(swreq, swresp));
CommaFeedSession session = (CommaFeedSession) app
.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) {
throw new WebApplicationException(Response.Status.UNAUTHORIZED);
}
}
protected User getUser() {

View File

@@ -70,8 +70,8 @@ public class EntriesREST extends AbstractREST {
feedEntries = feedEntryService.getUnreadEntries(
subscription.getFeed(), getUser());
} else {
feedEntries = feedEntryService.getAllEntries(
subscription.getFeed());
feedEntries = feedEntryService
.getAllEntries(subscription.getFeed());
}
List<Entry> entries = Lists.newArrayList();
@@ -118,11 +118,7 @@ public class EntriesREST extends AbstractREST {
status.setEntry(entry);
}
status.setRead(read);
if (status.getId() == null) {
feedEntryStatusService.save(status);
} else {
feedEntryStatusService.update(status);
}
feedEntryStatusService.saveOrUpdate(status);
}
}

View File

@@ -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);
}
}