001package com.nimbusds.jose;
002
003
004import java.io.Serializable;
005
006import net.jcip.annotations.Immutable;
007
008import net.minidev.json.JSONAware;
009import net.minidev.json.JSONObject;
010
011
012/**
013 * JOSE object type, represents the {@code typ} header parameter in unsecured,
014 * JSON Web Signature (JWS) and JSON Web Encryption (JWE) objects. This class
015 * is immutable.
016 *
017 * <p>Includes constants for the following standard types:
018 *
019 * <ul>
020 *     <li>{@link #JOSE}
021 *     <li>{@link #JOSE_JSON JOSE+JSON}
022 *     <li>{@link #JWT}
023 * </ul>
024 *
025 * <p>Additional types can be defined using the constructor.
026 *
027 * @author Vladimir Dzhuvinov
028 * @version 2014-02-15
029 */
030@Immutable
031public final class JOSEObjectType implements JSONAware, Serializable {
032
033
034        private static final long serialVersionUID = 1L;
035
036
037        /**
038         * Compact encoded JOSE object type.
039         */
040        public static final JOSEObjectType JOSE = new JOSEObjectType("JOSE");
041
042
043        /**
044         * JSON-encoded JOSE object type..
045         */
046        public static final JOSEObjectType JOSE_JSON = new JOSEObjectType("JOSE+JSON");
047
048
049        /**
050         * JSON Web Token (JWT) object type.
051         */
052        public static final JOSEObjectType JWT = new JOSEObjectType("JWT");
053
054
055        /**
056         * The object type.
057         */
058        private final String type;
059
060
061        /**
062         * Creates a new JOSE object type.
063         *
064         * @param type The object type. Must not be {@code null}.
065         */
066        public JOSEObjectType(final String type) {
067
068                if (type == null) {
069                        throw new IllegalArgumentException("The object type must not be null");
070                }
071
072                this.type = type;
073        }
074
075
076        /**
077         * Gets the JOSE object type.
078         *
079         * @return The JOSE object type.
080         */
081        public String getType() {
082
083                return type;
084        }
085
086
087        /**
088         * Overrides {@code Object.hashCode()}.
089         *
090         * @return The object hash code.
091         */
092        @Override
093        public int hashCode() {
094
095                return type.hashCode();
096        }
097
098
099        /**
100         * Overrides {@code Object.equals()}.
101         *
102         * @param object The object to compare to.
103         *
104         * @return {@code true} if the objects have the same value, otherwise
105         *         {@code false}.
106         */
107        @Override
108        public boolean equals(final Object object) {
109
110                return object != null && 
111                                object instanceof JOSEObjectType && 
112                                this.toString().equals(object.toString());
113        }
114
115
116        /**
117         * Returns the string representation of this JOSE object type.
118         *
119         * @see #getType
120         *
121         * @return The string representation.
122         */
123        @Override
124        public String toString() {
125
126                return type;
127        }
128
129
130        /**
131         * Returns the JSON string representation of this JOSE object type.
132         * 
133         * @return The JSON string representation.
134         */
135        @Override
136        public String toJSONString() {
137
138                return "\"" + JSONObject.escape(type) + '"';
139        }
140}