Skip to content

Commit

Permalink
fixing multiple get session case
Browse files Browse the repository at this point in the history
edited getsession method and fixed minor bug

checkstyle fixed

clusteredSessionId refactored , checkstyle fixed
  • Loading branch information
bilalyasar committed Jun 23, 2015
1 parent 68a8d42 commit 19913ab
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 19 deletions.
Expand Up @@ -94,6 +94,7 @@ public void setAttribute(final String name, final Object value) {
entry.setValue(value);
entry.setDirty(true);
entry.setRemoved(false);
entry.setReload(false);
if (!deferredWrite && !transientEntry) {
try {
webFilter.getClusteredSessionService().setAttribute(id, name, value);
Expand Down Expand Up @@ -191,6 +192,7 @@ public void removeAttribute(final String name) {
entry.setValue(null);
entry.setRemoved(true);
entry.setDirty(true);
entry.setReload(false);
}
if (!deferredWrite) {
try {
Expand Down
44 changes: 25 additions & 19 deletions hazelcast-wm/src/main/java/com/hazelcast/web/WebFilter.java
Expand Up @@ -348,7 +348,7 @@ public final void doFilter(ServletRequest req, ServletResponse res, final Filter
final ResponseWrapper resWrapper = new ResponseWrapper((HttpServletResponse) res);
final RequestWrapper reqWrapper = new RequestWrapper(httpReq, resWrapper);
if (existingReq != null) {
reqWrapper.setHazelcastSession(existingReq.hazelcastSession, existingReq.requestedSessionId);
reqWrapper.setHazelcastSession(existingReq.hazelcastSession, existingReq.clusteredSessionId);
}
chain.doFilter(reqWrapper, resWrapper);
if (existingReq != null) {
Expand Down Expand Up @@ -392,7 +392,7 @@ public ResponseWrapper(final HttpServletResponse original) {
protected class RequestWrapper extends HttpServletRequestWrapper {
final ResponseWrapper res;
HazelcastHttpSession hazelcastSession;
String requestedSessionId;
String clusteredSessionId;

public RequestWrapper(final HttpServletRequest req,
final ResponseWrapper res) {
Expand All @@ -403,7 +403,7 @@ public RequestWrapper(final HttpServletRequest req,

public void setHazelcastSession(HazelcastHttpSession hazelcastSession, String requestedSessionId) {
this.hazelcastSession = hazelcastSession;
this.requestedSessionId = requestedSessionId;
this.clusteredSessionId = requestedSessionId;
}

HttpSession getOriginalSession(boolean create) {
Expand Down Expand Up @@ -436,17 +436,9 @@ public void include(ServletRequest servletRequest, ServletResponse servletRespon
}

public HazelcastHttpSession getOrCreateHazelcastSession() {
if (requestedSessionId == null) {
requestedSessionId = getSessionCookie(this);
if (requestedSessionId == null) {
requestedSessionId = getParameter(HAZELCAST_SESSION_COOKIE_NAME);
}
}
if (requestedSessionId != null) {
hazelcastSession = getSessionWithId(requestedSessionId);
}
if (hazelcastSession == null && !res.isCommitted()) {
hazelcastSession = createNewSession(RequestWrapper.this, requestedSessionId);

if (hazelcastSession == null && !res.isCommitted()) {
hazelcastSession = createNewSession(RequestWrapper.this, clusteredSessionId);
}
return hazelcastSession;
}
Expand All @@ -472,9 +464,6 @@ private HazelcastHttpSession readSessionFromLocal() {
destroySession(hazelcastSession, true);
hazelcastSession = null;
} else if (hazelcastSession != null) {
if (!hazelcastSession.isStickySession()) {
hazelcastSession.updateReloadFlag();
}
return hazelcastSession;
}
HttpSession originalSession = getOriginalSession(false);
Expand All @@ -491,8 +480,25 @@ private HazelcastHttpSession readSessionFromLocal() {
originalSessions.remove(originalSession.getId());
originalSession.invalidate();
}
if (requestedSessionId != null) {
hazelcastSession = sessions.get(requestedSessionId);
if (clusteredSessionId != null) {
hazelcastSession = sessions.get(clusteredSessionId);
}
return readFromCookie();
}

private HazelcastHttpSession readFromCookie() {
if (clusteredSessionId == null) {
clusteredSessionId = getSessionCookie(this);
if (clusteredSessionId == null) {
clusteredSessionId = getParameter(HAZELCAST_SESSION_COOKIE_NAME);
}
}
if (clusteredSessionId != null) {
hazelcastSession = getSessionWithId(clusteredSessionId);
if (hazelcastSession != null && !hazelcastSession.isStickySession()) {
hazelcastSession.updateReloadFlag();
return hazelcastSession;
}
}
return null;
}
Expand Down
Expand Up @@ -24,6 +24,7 @@
import com.hazelcast.web.SessionState;
import org.apache.http.client.CookieStore;
import org.apache.http.impl.client.BasicCookieStore;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.categories.Category;
Expand Down Expand Up @@ -126,9 +127,25 @@ public void test_setAttribute() throws Exception {
assertEquals("value-updated", executeRequest("read", serverPort2, cookieStore));
assertEquals("value-updated", executeRequest("read", serverPort1, cookieStore));
}
@Test
public void testMultipleGetSession() throws Exception {
CookieStore cookieStore = new BasicCookieStore();
assertEquals("value", executeRequest("multiplesession", serverPort1, cookieStore));
}

@Test(timeout = 20000)
public void test_setThenGetAttribute() throws Exception {
CookieStore cookieStore = new BasicCookieStore();
assertEquals("value", executeRequest("setGet", serverPort1, cookieStore));
}

@Override
protected ServletContainer getServletContainer(int port, String sourceDir, String serverXml) throws Exception {
return new JettyServer(port, sourceDir, serverXml);
}

@After
public void shutdown() throws Exception {
teardownClass();
}
}
Expand Up @@ -60,6 +60,11 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se
} else if (req.getRequestURI().endsWith("update")) {
session.setAttribute("key", "value-updated");
resp.getWriter().write("true");
} else if (req.getRequestURI().endsWith("multiplesession")) {
session.setAttribute("key", "value");
session = req.getSession();
Object value = session.getAttribute("key");
resp.getWriter().write(value == null ? "null" : value.toString());
} else if (req.getRequestURI().endsWith("update-and-read-same-request")) {
session.setAttribute("key", "value-updated");
Object value = session.getAttribute("key");
Expand All @@ -69,6 +74,10 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se
String nameList = names.toString();
// Return comma-separated list of attribute names
resp.getWriter().write(nameList.substring(1, nameList.length() - 1).replace(", ", ","));
} else if (req.getRequestURI().endsWith("setGet")) {
session.setAttribute("key", "value");
Object value = session.getAttribute("key");
resp.getWriter().write(value == null ? "null" : value.toString());
} else if (req.getRequestURI().endsWith("reload")) {
session.invalidate();
session = req.getSession();
Expand Down

0 comments on commit 19913ab

Please sign in to comment.