if (webInterface.getCore().getPreferences().isRequireFullAccess() && !toadletContext.isAllowedFullAccess()) {
return false;
}
- return (getCurrentSone(toadletContext, false) == null) || (webInterface.getCore().getLocalSones().size() == 1);
+ return (getCurrentSoneWithoutCreatingSession(toadletContext) == null) || (webInterface.getCore().getLocalSones().size() == 1);
}
}
templateContext.set("albums", albumPagination.getItems());
return;
}
- Sone sone = getCurrentSone(request.getToadletContext(), false);
+ Sone sone = getCurrentSoneWithoutCreatingSession(request.getToadletContext());
templateContext.set("soneRequested", true);
templateContext.set("sone", sone);
}
templateContext.set("sort", sortField);
templateContext.set("order", sortOrder);
templateContext.set("filter", filter);
- final Sone currentSone = getCurrentSone(request.getToadletContext(), false);
+ final Sone currentSone = getCurrentSoneWithoutCreatingSession(request.getToadletContext());
Collection<Sone> knownSones = Collections2.filter(webInterface.getCore().getSones(), Sone.EMPTY_SONE_FILTER);
if ((currentSone != null) && "followed".equals(filter)) {
knownSones = Collections2.filter(knownSones, new Predicate<Sone>() {
*/
@Override
protected String getRedirectTarget(FreenetRequest request) {
- if (getCurrentSone(request.getToadletContext(), false) != null) {
+ if (getCurrentSoneWithoutCreatingSession(request.getToadletContext()) != null) {
return "index.html";
}
return null;
if (webInterface.getCore().getPreferences().isRequireFullAccess() && !toadletContext.isAllowedFullAccess()) {
return false;
}
- return getCurrentSone(toadletContext, false) == null;
+ return getCurrentSoneWithoutCreatingSession(toadletContext) == null;
}
}
if (webInterface.getCore().getPreferences().isRequireFullAccess() && !toadletContext.isAllowedFullAccess()) {
return false;
}
- return (getCurrentSone(toadletContext, false) != null) && (webInterface.getCore().getLocalSones().size() != 1);
+ return (getCurrentSoneWithoutCreatingSession(toadletContext) != null) && (webInterface.getCore().getLocalSones().size() != 1);
}
}
@Override
protected void handleRequest(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
/* collect new elements from notifications. */
- Set<Post> posts = new HashSet<Post>(webInterface.getNewPosts(getCurrentSone(request.getToadletContext(), false)));
- for (PostReply reply : webInterface.getNewReplies(getCurrentSone(request.getToadletContext(), false))) {
+ Set<Post> posts = new HashSet<Post>(webInterface.getNewPosts(getCurrentSoneWithoutCreatingSession(request.getToadletContext())));
+ for (PostReply reply : webInterface.getNewReplies(getCurrentSoneWithoutCreatingSession(request.getToadletContext()))) {
posts.add(reply.getPost().get());
}
@Override
protected void handleRequest(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
Preferences preferences = webInterface.getCore().getPreferences();
- Sone currentSone = webInterface.getCurrentSone(request.getToadletContext(), false);
+ Sone currentSone = webInterface.getCurrentSoneWithoutCreatingSession(request.getToadletContext());
if (request.getMethod() == Method.POST) {
List<String> fieldErrors = new ArrayList<String>();
if (currentSone != null) {
*/
@Override
protected void handleRequest(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
- Sone currentSone = getCurrentSone(request.getToadletContext(), false);
+ Sone currentSone = getCurrentSoneWithoutCreatingSession(request.getToadletContext());
SoneRescuer soneRescuer = webInterface.getCore().getSoneRescuer(currentSone);
if (request.getMethod() == Method.POST) {
if ("true".equals(request.getHttpRequest().getPartAsStringFailsafe("fetch", 4))) {
* currently logged in
*/
protected Sone getCurrentSone(ToadletContext toadletContext) {
- return webInterface.getCurrentSone(toadletContext);
+ return webInterface.getCurrentSoneCreatingSession(toadletContext);
}
- /**
- * Returns the currently logged in Sone.
- *
- * @param toadletContext
- * The toadlet context
- * @param create
- * {@code true} to create a new session if no session exists,
- * {@code false} to not create a new session
- * @return The currently logged in Sone, or {@code null} if no Sone is
- * currently logged in
- */
- protected Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
- return webInterface.getCurrentSone(toadletContext, create);
+ protected Sone getCurrentSoneWithoutCreatingSession(ToadletContext toadletContext) {
+ return webInterface.getCurrentSoneWithoutCreatingSession(toadletContext);
}
/**
@Override
protected final void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
super.processTemplate(request, templateContext);
- Sone currentSone = getCurrentSone(request.getToadletContext(), false);
+ Sone currentSone = getCurrentSoneWithoutCreatingSession(request.getToadletContext());
templateContext.set("core", webInterface.getCore());
templateContext.set("currentSone", currentSone);
templateContext.set("localSones", webInterface.getCore().getLocalSones());
*/
@Override
protected String getRedirectTarget(FreenetRequest request) {
- if (requiresLogin() && (getCurrentSone(request.getToadletContext(), false) == null)) {
+ if (requiresLogin() && (getCurrentSoneWithoutCreatingSession(request.getToadletContext()) == null)) {
HTTPRequest httpRequest = request.getHttpRequest();
String originalUrl = httpRequest.getPath();
if (httpRequest.hasParameters()) {
return false;
}
if (requiresLogin()) {
- return getCurrentSone(toadletContext, false) != null;
+ return getCurrentSoneWithoutCreatingSession(toadletContext) != null;
}
return true;
}
return templateContextFactory;
}
- /**
- * Returns the current session, creating a new session if there is no
- * current session.
- *
- * @param toadletContenxt
- * The toadlet context
- * @return The current session, or {@code null} if there is no current
- * session
- */
- private Session getCurrentSession(ToadletContext toadletContenxt) {
- return getCurrentSession(toadletContenxt, true);
+ private Session getCurrentSessionWithoutCreation(ToadletContext toadletContenxt) {
+ return getSessionManager().useSession(toadletContenxt);
}
- /**
- * Returns the current session, creating a new session if there is no
- * current session and {@code create} is {@code true}.
- *
- * @param toadletContenxt
- * The toadlet context
- * @param create
- * {@code true} to create a new session if there is no current
- * session, {@code false} otherwise
- * @return The current session, or {@code null} if there is no current
- * session
- */
- private Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
- Session session = getSessionManager().useSession(toadletContenxt);
- if (create && (session == null)) {
+ private Session getOrCreateCurrentSession(ToadletContext toadletContenxt) {
+ Session session = getCurrentSessionWithoutCreation(toadletContenxt);
+ if (session == null) {
session = getSessionManager().createSession(UUID.randomUUID().toString(), toadletContenxt);
}
return session;
}
- /**
- * Returns the currently logged in Sone.
- *
- * @param toadletContext
- * The toadlet context
- * @return The currently logged in Sone, or {@code null} if no Sone is
- * currently logged in
- */
- public Sone getCurrentSone(ToadletContext toadletContext) {
- return getCurrentSone(toadletContext, true);
+ public Sone getCurrentSoneCreatingSession(ToadletContext toadletContext) {
+ Collection<Sone> localSones = getCore().getLocalSones();
+ if (localSones.size() == 1) {
+ return localSones.iterator().next();
+ }
+ return getCurrentSone(getOrCreateCurrentSession(toadletContext));
}
- /**
- * Returns the currently logged in Sone.
- *
- * @param toadletContext
- * The toadlet context
- * @param create
- * {@code true} to create a new session if no session exists,
- * {@code false} to not create a new session
- * @return The currently logged in Sone, or {@code null} if no Sone is
- * currently logged in
- */
- public Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
+ public Sone getCurrentSoneWithoutCreatingSession(ToadletContext toadletContext) {
Collection<Sone> localSones = getCore().getLocalSones();
if (localSones.size() == 1) {
return localSones.iterator().next();
}
- return getCurrentSone(getCurrentSession(toadletContext, create));
+ return getCurrentSone(getCurrentSessionWithoutCreation(toadletContext));
}
/**
* @return The currently logged in Sone, or {@code null} if no Sone is
* currently logged in
*/
- public Sone getCurrentSone(Session session) {
+ private Sone getCurrentSone(Session session) {
if (session == null) {
return null;
}
* The Sone to set as currently logged in
*/
public void setCurrentSone(ToadletContext toadletContext, Sone sone) {
- Session session = getCurrentSession(toadletContext);
+ Session session = getOrCreateCurrentSession(toadletContext);
if (sone == null) {
session.removeAttribute("Sone.CurrentSone");
} else {
*/
@Override
protected JsonReturnObject createJsonObject(FreenetRequest request) {
- Sone currentSone = getCurrentSone(request.getToadletContext(), false);
+ Sone currentSone = getCurrentSoneWithoutCreatingSession(request.getToadletContext());
if (currentSone == null) {
return createErrorJsonObject("auth-required");
}
*/
@Override
protected JsonReturnObject createJsonObject(FreenetRequest request) {
- Sone currentSone = getCurrentSone(request.getToadletContext(), false);
+ Sone currentSone = getCurrentSoneWithoutCreatingSession(request.getToadletContext());
List<Notification> notifications = new ArrayList<Notification>(webInterface.getNotifications(currentSone));
Collections.sort(notifications, Notification.CREATED_TIME_SORTER);
ArrayNode jsonNotifications = new ArrayNode(instance);
if (notification instanceof TemplateNotification) {
TemplateContext templateContext = webInterface.getTemplateContextFactory().createTemplateContext().mergeContext(((TemplateNotification) notification).getTemplateContext());
templateContext.set("core", webInterface.getCore());
- templateContext.set("currentSone", webInterface.getCurrentSone(request.getToadletContext(), false));
+ templateContext.set("currentSone", webInterface.getCurrentSoneWithoutCreatingSession(request.getToadletContext()));
templateContext.set("localSones", webInterface.getCore().getLocalSones());
templateContext.set("request", request);
templateContext.set("currentVersion", SonePlugin.getPluginVersion());
*/
@Override
protected JsonReturnObject createJsonObject(FreenetRequest request) {
- final Sone currentSone = getCurrentSone(request.getToadletContext(), false);
+ final Sone currentSone = getCurrentSoneWithoutCreatingSession(request.getToadletContext());
/* load Sones. always return the status of the current Sone. */
- Set<Sone> sones = new HashSet<Sone>(Collections.singleton(getCurrentSone(request.getToadletContext(), false)));
+ Set<Sone> sones = new HashSet<Sone>(Collections.singleton(getCurrentSoneWithoutCreatingSession(request.getToadletContext())));
String loadSoneIds = request.getHttpRequest().getParam("soneIds");
if (loadSoneIds.length() > 0) {
String[] soneIds = loadSoneIds.split(",");
List<Notification> notifications = new ArrayList<Notification>(webInterface.getNotifications(currentSone));
Collections.sort(notifications, Notification.CREATED_TIME_SORTER);
/* load new posts. */
- Collection<Post> newPosts = webInterface.getNewPosts(getCurrentSone(request.getToadletContext(), false));
+ Collection<Post> newPosts = webInterface.getNewPosts(getCurrentSoneWithoutCreatingSession(request.getToadletContext()));
ArrayNode jsonPosts = new ArrayNode(instance);
for (Post post : newPosts) {
jsonPosts.add(jsonPost);
}
/* load new replies. */
- Collection<PostReply> newReplies = webInterface.getNewReplies(getCurrentSone(request.getToadletContext(), false));
+ Collection<PostReply> newReplies = webInterface.getNewReplies(getCurrentSoneWithoutCreatingSession(request.getToadletContext()));
ArrayNode jsonReplies = new ArrayNode(instance);
for (PostReply reply : newReplies) {
* currently logged in
*/
protected Sone getCurrentSone(ToadletContext toadletContext) {
- return webInterface.getCurrentSone(toadletContext);
+ return webInterface.getCurrentSoneCreatingSession(toadletContext);
}
- /**
- * Returns the currently logged in Sone.
- *
- * @param toadletContext
- * The toadlet context
- * @param create
- * {@code true} to create a new session if no session exists,
- * {@code false} to not create a new session
- * @return The currently logged in Sone, or {@code null} if no Sone is
- * currently logged in
- */
- protected Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
- return webInterface.getCurrentSone(toadletContext, create);
+ protected Sone getCurrentSoneWithoutCreatingSession(ToadletContext toadletContext) {
+ return webInterface.getCurrentSoneWithoutCreatingSession(toadletContext);
}
//
}
}
if (requiresLogin()) {
- if (getCurrentSone(request.getToadletContext(), false) == null) {
+ if (getCurrentSoneWithoutCreatingSession(request.getToadletContext()) == null) {
return response.setStatusCode(403).setStatusText("Forbidden").setContentType("application/json").write(objectMapper.writeValueAsString(new JsonErrorReturnObject("auth-required")));
}
}
*/
@Override
protected JsonReturnObject createJsonObject(FreenetRequest request) {
- Sone currentSone = getCurrentSone(request.getToadletContext(), false);
+ Sone currentSone = getCurrentSoneWithoutCreatingSession(request.getToadletContext());
if (currentSone == null) {
return createErrorJsonObject("auth-required");
}
*/
@Override
protected JsonReturnObject createJsonObject(FreenetRequest request) {
- Sone currentSone = getCurrentSone(request.getToadletContext(), false);
+ Sone currentSone = getCurrentSoneWithoutCreatingSession(request.getToadletContext());
if (currentSone == null) {
return createErrorJsonObject("auth-required");
}
@Before
public final void setupWebInterface() {
- when(webInterface.getCurrentSone(toadletContext)).thenReturn(currentSone);
- when(webInterface.getCurrentSone(eq(toadletContext), anyBoolean())).thenReturn(currentSone);
+ when(webInterface.getCurrentSoneCreatingSession(toadletContext)).thenReturn(currentSone);
+ when(webInterface.getCurrentSoneWithoutCreatingSession(toadletContext)).thenReturn(currentSone);
when(webInterface.getNotification(anyString())).thenReturn(Optional.<Notification>absent());
when(webInterface.getNotifications(currentSone)).thenReturn(new ArrayList<Notification>());
}
}
protected void unsetCurrentSone() {
- when(webInterface.getCurrentSone(toadletContext)).thenReturn(null);
- when(webInterface.getCurrentSone(eq(toadletContext), anyBoolean())).thenReturn(null);
+ when(webInterface.getCurrentSoneCreatingSession(toadletContext)).thenReturn(null);
+ when(webInterface.getCurrentSoneWithoutCreatingSession(toadletContext)).thenReturn(null);
}
protected void request(String uri, Method method) {
}
@Test
- fun `retrieving current sone with creation is forwarded to web interface`() {
- mock<Sone>().let {
- whenever(webInterface.getCurrentSone(toadletContext, true)).thenReturn(it)
- assertThat(page.getCurrentSone(toadletContext, true), equalTo(it))
- }
- }
-
- @Test
fun `retrieving current sone without creation is forwarded to web interface`() {
mock<Sone>().let {
- whenever(webInterface.getCurrentSone(toadletContext, false)).thenReturn(it)
- assertThat(page.getCurrentSone(toadletContext, false), equalTo(it))
+ whenever(webInterface.getCurrentSoneWithoutCreatingSession(toadletContext)).thenReturn(it)
+ assertThat(page.getCurrentSoneWithoutCreatingSession(toadletContext), equalTo(it))
}
}