Clean up the tattered shreds of SessionMetadata

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=125334811
This commit is contained in:
cgoldfeder 2016-06-20 07:26:00 -07:00 committed by Ben McIlwain
parent 2a3a3fbc30
commit bb82f5bc05
11 changed files with 169 additions and 185 deletions

View file

@ -14,42 +14,77 @@
package google.registry.flows;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.base.MoreObjects.toStringHelper;
import static google.registry.util.CollectionUtils.nullToEmpty;
import com.google.common.base.Joiner;
import com.google.common.base.Optional;
import java.util.Set;
import javax.servlet.http.HttpSession;
/** A metadata class that is a wrapper around {@link HttpSession}. */
public class HttpSessionMetadata extends SessionMetadata {
public class HttpSessionMetadata implements SessionMetadata {
private static final String CLIENT_ID = "CLIENT_ID";
private static final String SERVICE_EXTENSIONS = "SERVICE_EXTENSIONS";
private static final String FAILED_LOGIN_ATTEMPTS = "FAILED_LOGIN_ATTEMPTS";
private final HttpSession session;
private boolean isValid = true;
public HttpSessionMetadata(HttpSession session) {
this.session = session;
}
@Override
protected void checkValid() {
checkState(isValid, "This session has been invalidated.");
}
@Override
public void invalidate() {
session.invalidate();
isValid = false;
}
@Override
protected void setProperty(String key, Object value) {
if (value == null) {
session.removeAttribute(key);
} else {
session.setAttribute(key, value);
}
public String getClientId() {
return (String) session.getAttribute(CLIENT_ID);
}
@Override
protected Object getProperty(String key) {
return session.getAttribute(key);
@SuppressWarnings("unchecked")
public Set<String> getServiceExtensionUris() {
return (Set<String>) session.getAttribute(SERVICE_EXTENSIONS);
}
@Override
public void setClientId(String clientId) {
session.setAttribute(CLIENT_ID, clientId);
}
@Override
public void setServiceExtensionUris(Set<String> serviceExtensionUris) {
session.setAttribute(SERVICE_EXTENSIONS, serviceExtensionUris);
}
@Override
public int getFailedLoginAttempts() {
return Optional.fromNullable((Integer) session.getAttribute(FAILED_LOGIN_ATTEMPTS)).or(0);
}
@Override
public void incrementFailedLoginAttempts() {
session.setAttribute(FAILED_LOGIN_ATTEMPTS, getFailedLoginAttempts() + 1);
}
@Override
public void resetFailedLoginAttempts() {
session.removeAttribute(FAILED_LOGIN_ATTEMPTS);
}
@Override
public String toString() {
return toStringHelper(getClass())
.add("system hash code", System.identityHashCode(this))
.add("clientId", getClientId())
.add("failedLoginAttempts", getFailedLoginAttempts())
.add("serviceExtensionUris", Joiner.on('.').join(nullToEmpty(getServiceExtensionUris())))
.toString();
}
}

View file

@ -14,95 +14,29 @@
package google.registry.flows;
import static com.google.common.base.MoreObjects.toStringHelper;
import static com.google.common.base.Preconditions.checkNotNull;
import static google.registry.util.CollectionUtils.nullToEmpty;
import com.google.common.base.Joiner;
import com.google.common.base.Optional;
import java.util.Set;
/** Class to allow setting and retrieving session information in flows. */
public abstract class SessionMetadata {
/** The key used for looking up the current client id on the session object. */
protected static final String CLIENT_ID_KEY = "CLIENT_ID";
/** The key used for looking up the service extensions on the session object. */
protected static final String SERVICE_EXTENSIONS_KEY = "SERVICE_EXTENSIONS";
/** The key used for looking up the number of failed login attempts. */
protected static final String FAILED_LOGIN_ATTEMPTS_KEY = "FAILED_LOGIN_ATTEMPTS";
protected abstract void setProperty(String key, Object value);
protected abstract Object getProperty(String key);
/** Object to allow setting and retrieving session information in flows. */
public interface SessionMetadata {
/**
* Invalidates the session. A new instance must be created after this for future sessions.
* Attempts to invoke methods of this class after this method has been called will throw
* {@code IllegalStateException}.
*/
public abstract void invalidate();
void invalidate();
/** Subclasses can override this to verify that this is a valid session. */
protected void checkValid() {}
String getClientId();
/** Check that the session is valid and set a property. */
private void setPropertyChecked(String key, Object value) {
checkValid();
setProperty(key, value);
}
Set<String> getServiceExtensionUris();
/**
* Check that the session is valid and get a property as a given type.
*
* @param clazz type to return, specified as a param to enforce typesafe generics
* @see "http://errorprone.info/bugpattern/TypeParameterUnusedInFormals"
*/
private <T> T getProperty(Class<T> clazz, String key) {
checkValid();
return clazz.cast(getProperty(key));
}
void setClientId(String clientId);
public String getClientId() {
return getProperty(String.class, CLIENT_ID_KEY);
}
void setServiceExtensionUris(Set<String> serviceExtensionUris);
@SuppressWarnings("unchecked")
public Set<String> getServiceExtensionUris() {
return getProperty(Set.class, SERVICE_EXTENSIONS_KEY);
}
int getFailedLoginAttempts();
public void setClientId(String clientId) {
setPropertyChecked(CLIENT_ID_KEY, clientId);
}
void incrementFailedLoginAttempts();
public void setServiceExtensionUris(Set<String> serviceExtensionUris) {
setPropertyChecked(SERVICE_EXTENSIONS_KEY, checkNotNull(serviceExtensionUris));
}
public int getFailedLoginAttempts() {
return Optional.fromNullable(getProperty(Integer.class, FAILED_LOGIN_ATTEMPTS_KEY))
.or(0);
}
public void incrementFailedLoginAttempts() {
setPropertyChecked(FAILED_LOGIN_ATTEMPTS_KEY, getFailedLoginAttempts() + 1);
}
public void resetFailedLoginAttempts() {
setPropertyChecked(FAILED_LOGIN_ATTEMPTS_KEY, null);
}
@Override
public String toString() {
return toStringHelper(getClass())
.add("system hash code", System.identityHashCode(this))
.add("clientId", getClientId())
.add("failedLoginAttempts", getFailedLoginAttempts())
.add("serviceExtensionUris", Joiner.on('.').join(nullToEmpty(getServiceExtensionUris())))
.toString();
}
void resetFailedLoginAttempts();
}

View file

@ -14,19 +14,25 @@
package google.registry.flows;
import static com.google.common.base.MoreObjects.toStringHelper;
import static dagger.internal.Preconditions.checkNotNull;
import static google.registry.util.CollectionUtils.nullToEmpty;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableSet;
import java.util.Set;
/** A read-only {@link SessionMetadata} that doesn't support login/logout. */
public class StatelessRequestSessionMetadata extends SessionMetadata {
public class StatelessRequestSessionMetadata implements SessionMetadata {
private final String clientId;
private final Set<String> serviceExtensionUris;
private final ImmutableSet<String> serviceExtensionUris;
public StatelessRequestSessionMetadata(
String clientId,
Set<String> serviceExtensionUris) {
this.clientId = clientId;
this.serviceExtensionUris = serviceExtensionUris;
String clientId, ImmutableSet<String> serviceExtensionUris) {
this.clientId = checkNotNull(clientId);
this.serviceExtensionUris = checkNotNull(serviceExtensionUris);
}
@Override
@ -45,14 +51,38 @@ public class StatelessRequestSessionMetadata extends SessionMetadata {
}
@Override
protected void setProperty(String key, Object value) {
public void setClientId(String clientId) {
throw new UnsupportedOperationException();
}
@Override
protected Object getProperty(String key) {
// We've overridden the getters of all of the properties that we care about. Return null for
// everything else so that toString() continues to work.
return null;
public void setServiceExtensionUris(Set<String> serviceExtensionUris) {
throw new UnsupportedOperationException();
}
@Override
public int getFailedLoginAttempts() {
throw new UnsupportedOperationException();
}
@Override
public void incrementFailedLoginAttempts() {
throw new UnsupportedOperationException();
}
@Override
public void resetFailedLoginAttempts() {
throw new UnsupportedOperationException();
}
@Override
public String toString() {
return toStringHelper(getClass())
.add("system hash code", System.identityHashCode(this))
.add("clientId", getClientId())
.add("serviceExtensionUris", Joiner.on('.').join(nullToEmpty(getServiceExtensionUris())))
.toString();
}
}