com.outerspacecat.openid.rp.SessionType.java Source code

Java tutorial

Introduction

Here is the source code for com.outerspacecat.openid.rp.SessionType.java

Source

/**
 * Copyright 2011 Caleb Richardson
 * 
 * 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 com.outerspacecat.openid.rp;

import com.google.common.base.Preconditions;

/**
 * An OpenID session type.
 * 
 * @author Caleb Richardson
 */
public enum SessionType {
    NO_ENCRYPTION("no-encryption", null), DH_SHA1("DH-SHA1", DhSession.DhAlgorithm.SHA1), DH_SHA256("DH-SHA256",
            DhSession.DhAlgorithm.SHA256);

    private final String value;
    private final DhSession.DhAlgorithm algorithm;

    SessionType(final String value, final DhSession.DhAlgorithm algorithm) {
        this.value = value;
        this.algorithm = algorithm;
    }

    String getValue() {
        return value;
    }

    DhSession.DhAlgorithm getAlgorithm() {
        return algorithm;
    }

    /**
     * Returns whether or not {@code value} represents a session type.
     * 
     * @param value the value to test. Must be non {@code null}.
     * @return whether or not {@code value} represents a session type
     */
    static boolean isValidValue(final String value) {
        Preconditions.checkNotNull(value, "value required");

        return value.equalsIgnoreCase(NO_ENCRYPTION.getValue()) || value.equalsIgnoreCase(DH_SHA1.getValue())
                || value.equalsIgnoreCase(DH_SHA256.getValue());
    }

    /**
     * Returns the session type represented by {@code value}.
     * 
     * @param value the value of the session type. Must be non {@code null}.
     * @return the session type represented by {@code value}
     */
    static SessionType fromValue(final String value) {
        Preconditions.checkNotNull(value, "value required");

        if (value.equalsIgnoreCase(NO_ENCRYPTION.getValue())) {
            return NO_ENCRYPTION;
        } else if (value.equalsIgnoreCase(DH_SHA1.getValue())) {
            return DH_SHA1;
        } else if (value.equalsIgnoreCase(DH_SHA256.getValue())) {
            return DH_SHA256;
        } else {
            throw new IllegalArgumentException("invalid value: " + value);
        }
    }
}