Commit 3dcdc846 authored by Matija Obreza's avatar Matija Obreza

Organized imports, formatted & cleaned up code

parent 62cd9827
......@@ -16,6 +16,39 @@
package com.hazelcast.web;
import static com.hazelcast.web.HazelcastInstanceLoader.CLIENT_CONFIG_LOCATION;
import static com.hazelcast.web.HazelcastInstanceLoader.CONFIG_LOCATION;
import static com.hazelcast.web.HazelcastInstanceLoader.INSTANCE_NAME;
import static com.hazelcast.web.HazelcastInstanceLoader.USE_CLIENT;
import java.io.IOException;
import java.io.NotSerializableException;
import java.io.Serializable;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.logging.Level;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionContext;
import com.hazelcast.config.Config;
import com.hazelcast.config.MapConfig;
import com.hazelcast.core.EntryEvent;
......@@ -26,21 +59,9 @@ import com.hazelcast.logging.ILogger;
import com.hazelcast.logging.Logger;
import com.hazelcast.nio.serialization.Data;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.NotSerializableException;
import java.io.Serializable;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.logging.Level;
import static com.hazelcast.web.HazelcastInstanceLoader.*;
/**
* Request dispatcher forward+inclue not wrapping incoming request
*
*
* @author matijaobreza
*/
public class WebFilter implements Filter {
......@@ -89,60 +110,63 @@ public class WebFilter implements Filter {
this.properties = properties;
}
@Override
public final void init(final FilterConfig config) throws ServletException {
filterConfig = config;
servletContext = config.getServletContext();
initInstance();
String debugParam = getParam("debug");
final String debugParam = getParam("debug");
if (debugParam != null) {
debug = Boolean.valueOf(debugParam);
}
String mapName = getParam("map-name");
final String mapName = getParam("map-name");
if (mapName != null) {
clusterMapName = mapName;
} else {
clusterMapName = "_web_" + servletContext.getServletContextName();
}
try {
Config hzConfig = hazelcastInstance.getConfig();
String sessionTTL = getParam("session-ttl-seconds");
final Config hzConfig = hazelcastInstance.getConfig();
final String sessionTTL = getParam("session-ttl-seconds");
if (sessionTTL != null) {
MapConfig mapConfig = new MapConfig(clusterMapName);
final MapConfig mapConfig = new MapConfig(clusterMapName);
mapConfig.setTimeToLiveSeconds(Integer.valueOf(sessionTTL));
hzConfig.addMapConfig(mapConfig);
}
} catch (UnsupportedOperationException ignored) {
} catch (final UnsupportedOperationException ignored) {
// client cannot access Config.
}
String cookieName = getParam("cookie-name");
final String cookieName = getParam("cookie-name");
if (cookieName != null) {
sessionCookieName = cookieName;
}
String cookieDomain = getParam("cookie-domain");
final String cookieDomain = getParam("cookie-domain");
if (cookieDomain != null) {
sessionCookieDomain = cookieDomain;
}
String cookieSecure = getParam("cookie-secure");
final String cookieSecure = getParam("cookie-secure");
if (cookieSecure != null) {
sessionCookieSecure = Boolean.valueOf(cookieSecure);
}
String cookieHttpOnly = getParam("cookie-http-only");
final String cookieHttpOnly = getParam("cookie-http-only");
if (cookieHttpOnly != null) {
sessionCookieHttpOnly = Boolean.valueOf(cookieHttpOnly);
}
String stickySessionParam = getParam("sticky-session");
final String stickySessionParam = getParam("sticky-session");
if (stickySessionParam != null) {
stickySession = Boolean.valueOf(stickySessionParam);
}
String shutdownOnDestroyParam = getParam("shutdown-on-destroy");
final String shutdownOnDestroyParam = getParam("shutdown-on-destroy");
if (shutdownOnDestroyParam != null) {
shutdownOnDestroy = Boolean.valueOf(shutdownOnDestroyParam);
}
if (!stickySession) {
getClusterMap().addEntryListener(new EntryListener() {
@Override
public void entryAdded(EntryEvent entryEvent) {
}
@Override
public void entryRemoved(EntryEvent entryEvent) {
if (entryEvent.getMember() == null || // client events has
// no owner member
......@@ -151,6 +175,7 @@ public class WebFilter implements Filter {
}
}
@Override
public void entryUpdated(EntryEvent entryEvent) {
if (entryEvent.getMember() == null || // client events has
// no owner member
......@@ -159,6 +184,7 @@ public class WebFilter implements Filter {
}
}
@Override
public void entryEvicted(EntryEvent entryEvent) {
entryRemoved(entryEvent);
}
......@@ -180,14 +206,14 @@ public class WebFilter implements Filter {
}
private void setProperty(String propertyName) {
String value = getParam(propertyName);
final String value = getParam(propertyName);
if (value != null) {
properties.setProperty(propertyName, value);
}
}
private void removeSessionLocally(String sessionId) {
HazelcastHttpSession hazelSession = mapSessions.remove(sessionId);
final HazelcastHttpSession hazelSession = mapSessions.remove(sessionId);
if (hazelSession != null) {
mapOriginalSessions.remove(hazelSession.originalSession.getId());
log("Destroying session locally " + hazelSession);
......@@ -196,16 +222,16 @@ public class WebFilter implements Filter {
}
private void markSessionDirty(String sessionId) {
HazelcastHttpSession hazelSession = mapSessions.get(sessionId);
final HazelcastHttpSession hazelSession = mapSessions.get(sessionId);
if (hazelSession != null) {
hazelSession.setDirty(true);
}
}
static void destroyOriginalSession(HttpSession originalSession) {
String hazelcastSessionId = mapOriginalSessions.remove(originalSession.getId());
final String hazelcastSessionId = mapOriginalSessions.remove(originalSession.getId());
if (hazelcastSessionId != null) {
HazelcastHttpSession hazelSession = mapSessions.remove(hazelcastSessionId);
final HazelcastHttpSession hazelSession = mapSessions.remove(hazelcastSessionId);
if (hazelSession != null) {
hazelSession.webFilter.destroySession(hazelSession, false);
}
......@@ -221,14 +247,14 @@ public class WebFilter implements Filter {
}
private HazelcastHttpSession createNewSession(RequestWrapper requestWrapper, String existingSessionId) {
String id = existingSessionId != null ? existingSessionId : generateSessionId();
final String id = existingSessionId != null ? existingSessionId : generateSessionId();
if (requestWrapper.getOriginalSession(false) != null) {
log("Original session exists!!!");
}
HttpSession originalSession = requestWrapper.getOriginalSession(true);
HazelcastHttpSession hazelcastSession = new HazelcastHttpSession(WebFilter.this, id, originalSession);
final HttpSession originalSession = requestWrapper.getOriginalSession(true);
final HazelcastHttpSession hazelcastSession = new HazelcastHttpSession(WebFilter.this, id, originalSession);
mapSessions.put(hazelcastSession.getId(), hazelcastSession);
String oldHazelcastSessionId = mapOriginalSessions.put(originalSession.getId(), hazelcastSession.getId());
final String oldHazelcastSessionId = mapOriginalSessions.put(originalSession.getId(), hazelcastSession.getId());
if (oldHazelcastSessionId != null) {
log("!!! Overriding an existing hazelcastSessionId " + oldHazelcastSessionId);
}
......@@ -241,7 +267,7 @@ public class WebFilter implements Filter {
/**
* Destroys a session, determining if it should be destroyed clusterwide
* automatically or via expiry.
*
*
* @param session
* The session to be destroyed
* @param removeGlobalSession
......@@ -315,9 +341,9 @@ public class WebFilter implements Filter {
hazelcastSession = null;
}
if (hazelcastSession == null) {
HttpSession originalSession = getOriginalSession(false);
final HttpSession originalSession = getOriginalSession(false);
if (originalSession != null) {
String hazelcastSessionId = mapOriginalSessions.get(originalSession.getId());
final String hazelcastSessionId = mapOriginalSessions.get(originalSession.getId());
if (hazelcastSessionId != null) {
hazelcastSession = mapSessions.get(hazelcastSessionId);
}
......@@ -329,8 +355,9 @@ public class WebFilter implements Filter {
}
}
}
if (hazelcastSession != null)
if (hazelcastSession != null) {
return hazelcastSession;
}
final String requestedSessionId = fetchHazelcastSessionId();
if (requestedSessionId != null) {
hazelcastSession = getSessionWithId(requestedSessionId);
......@@ -355,8 +382,9 @@ public class WebFilter implements Filter {
}
private void overrideSession(HazelcastHttpSession session, Map mapSession) {
if (session == null || mapSession == null)
if (session == null || mapSession == null) {
return;
}
final Enumeration<String> atts = session.getAttributeNames();
while (atts.hasMoreElements()) {
session.removeAttribute(atts.nextElement());
......@@ -407,30 +435,37 @@ public class WebFilter implements Filter {
this.originalSession = originalSession;
}
@Override
public Object getAttribute(final String name) {
return originalSession.getAttribute(name);
}
@Override
public Enumeration getAttributeNames() {
return originalSession.getAttributeNames();
}
@Override
public String getId() {
return id;
}
@Override
public ServletContext getServletContext() {
return servletContext;
}
@Override
public HttpSessionContext getSessionContext() {
return originalSession.getSessionContext();
}
@Override
public Object getValue(final String name) {
return getAttribute(name);
}
@Override
public String[] getValueNames() {
return originalSession.getValueNames();
}
......@@ -443,23 +478,28 @@ public class WebFilter implements Filter {
this.dirty = dirty;
}
@Override
public void invalidate() {
originalSession.invalidate();
destroySession(this, true);
}
@Override
public boolean isNew() {
return originalSession.isNew();
}
@Override
public void putValue(final String name, final Object value) {
setAttribute(name, value);
}
@Override
public void removeAttribute(final String name) {
originalSession.removeAttribute(name);
}
@Override
public void setAttribute(final String name, final Object value) {
if (value != null && !(value instanceof Serializable)) {
throw new IllegalArgumentException(new NotSerializableException(value.getClass().getName()));
......@@ -467,6 +507,7 @@ public class WebFilter implements Filter {
originalSession.setAttribute(name, value);
}
@Override
public void removeValue(final String name) {
removeAttribute(name);
}
......@@ -482,25 +523,30 @@ public class WebFilter implements Filter {
}
}
@Override
public long getCreationTime() {
return originalSession.getCreationTime();
}
@Override
public long getLastAccessedTime() {
return originalSession.getLastAccessedTime();
}
@Override
public int getMaxInactiveInterval() {
return originalSession.getMaxInactiveInterval();
}
@Override
public void setMaxInactiveInterval(int maxInactiveSeconds) {
originalSession.setMaxInactiveInterval(maxInactiveSeconds);
}
private Data writeObject(final Object obj) {
if (obj == null)
if (obj == null) {
return null;
}
return serializationHelper.toData(obj);
}
......@@ -521,8 +567,9 @@ public class WebFilter implements Filter {
if (c != '-') {
if (Character.isLetter(c)) {
sb.append(Character.toUpperCase(c));
} else
} else {
sb.append(c);
}
}
}
return sb.toString();
......@@ -541,7 +588,7 @@ public class WebFilter implements Filter {
}
try {
sessionCookie.setHttpOnly(sessionCookieHttpOnly);
} catch (NoSuchMethodError e) {
} catch (final NoSuchMethodError e) {
// must be servlet spec before 3.0, don't worry about it!
}
sessionCookie.setSecure(sessionCookieSecure);
......@@ -562,6 +609,7 @@ public class WebFilter implements Filter {
return null;
}
@Override
public final void doFilter(ServletRequest req, ServletResponse res, final FilterChain chain) throws IOException, ServletException {
if (!(req instanceof HttpServletRequest)) {
chain.doFilter(req, res);
......@@ -572,7 +620,7 @@ public class WebFilter implements Filter {
return;
}
HttpServletRequest httpReq = (HttpServletRequest) req;
RequestWrapper existingReq = (RequestWrapper) req.getAttribute(HAZELCAST_REQUEST);
final RequestWrapper existingReq = (RequestWrapper) req.getAttribute(HAZELCAST_REQUEST);
final ResponseWrapper resWrapper = new ResponseWrapper((HttpServletResponse) res);
final RequestWrapper reqWrapper = new RequestWrapper(httpReq, resWrapper);
resWrapper.setRequest(reqWrapper);
......@@ -583,10 +631,11 @@ public class WebFilter implements Filter {
res = null;
httpReq = null;
chain.doFilter(reqWrapper, resWrapper);
if (existingReq != null)
if (existingReq != null) {
return;
}
req = null; // for easy debugging. reqWrapper should be used
HazelcastHttpSession session = reqWrapper.getSession(false);
final HazelcastHttpSession session = reqWrapper.getSession(false);
if (session != null && session.isValid()) {
final Enumeration<String> attNames = session.getAttributeNames();
Map mapData = null;
......@@ -599,7 +648,7 @@ public class WebFilter implements Filter {
mapData.put(attName, value);
}
Data data = session.writeObject(mapData);
boolean sessionChanged = session.sessionChanged(data);
final boolean sessionChanged = session.sessionChanged(data);
if (sessionChanged) {
if (data == null) {
mapData = new HashMap<String, Object>();
......@@ -612,6 +661,7 @@ public class WebFilter implements Filter {
}
}
@Override
public final void destroy() {
mapSessions.clear();
mapOriginalSessions.clear();
......
......@@ -30,23 +30,29 @@ import java.awt.image.ColorModel;
*/
public abstract class AbstractBufferedImageOp implements BufferedImageOp, Cloneable {
@Override
public BufferedImage createCompatibleDestImage(BufferedImage src, ColorModel dstCM) {
if (dstCM == null)
if (dstCM == null) {
dstCM = src.getColorModel();
}
return new BufferedImage(dstCM, dstCM.createCompatibleWritableRaster(src.getWidth(), src.getHeight()), dstCM.isAlphaPremultiplied(), null);
}
@Override
public Rectangle2D getBounds2D(BufferedImage src) {
return new Rectangle(0, 0, src.getWidth(), src.getHeight());
}
@Override
public Point2D getPoint2D(Point2D srcPt, Point2D dstPt) {
if (dstPt == null)
if (dstPt == null) {
dstPt = new Point2D.Double();
}
dstPt.setLocation(srcPt.getX(), srcPt.getY());
return dstPt;
}
@Override
public RenderingHints getRenderingHints() {
return null;
}
......@@ -55,7 +61,7 @@ public abstract class AbstractBufferedImageOp implements BufferedImageOp, Clonea
* A convenience method for getting ARGB pixels from an image. This tries to