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

Java tutorial

Introduction

Here is the source code for com.outerspacecat.openid.rp.AssociationType.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 association type.
 * 
 * @author Caleb Richardson
 */
public enum AssociationType {
    HMAC_SHA1("HMAC-SHA1", "HmacSHA1"), HMAC_SHA256("HMAC-SHA256", "HmacSHA256");

    private final String value;
    private final String algorithm;

    AssociationType(final String value, final String algorithm) {
        this.value = value;
        this.algorithm = algorithm;
    }

    String getValue() {
        return value;
    }

    String getAlgorithm() {
        return algorithm;
    }

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

        return value.equalsIgnoreCase(HMAC_SHA1.getValue()) || value.equalsIgnoreCase(HMAC_SHA256.getValue());
    }

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

        if (value.equalsIgnoreCase(HMAC_SHA1.getValue())) {
            return HMAC_SHA1;
        } else if (value.equalsIgnoreCase(HMAC_SHA256.getValue())) {
            return HMAC_SHA256;
        } else {
            throw new IllegalArgumentException("invalid value: " + value);
        }
    }
}