google-nomulus/java/google/registry/flows/SessionMetadata.java
cgoldfeder 2a3a3fbc30 Break SessionSource out of SessionMetadata and rename it EppRequestSource.
The "SessionSource" has nothing to do with sessions (and it's often
used in sessionless contexts). What it does indicate is the endpoint
used to make the request.
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=125295224
2016-06-27 16:19:21 -04:00

108 lines
3.8 KiB
Java

// Copyright 2016 The Domain Registry Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
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);
/**
* 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();
/** Subclasses can override this to verify that this is a valid session. */
protected void checkValid() {}
/** Check that the session is valid and set a property. */
private void setPropertyChecked(String key, Object value) {
checkValid();
setProperty(key, value);
}
/**
* 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));
}
public String getClientId() {
return getProperty(String.class, CLIENT_ID_KEY);
}
@SuppressWarnings("unchecked")
public Set<String> getServiceExtensionUris() {
return getProperty(Set.class, SERVICE_EXTENSIONS_KEY);
}
public void setClientId(String clientId) {
setPropertyChecked(CLIENT_ID_KEY, clientId);
}
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();
}
}