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