001 package com.nimbusds.jose;
002
003
004 import net.minidev.json.JSONAware;
005 import net.minidev.json.JSONObject;
006
007 import net.jcip.annotations.Immutable;
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
028 public 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 this.type = type;
060 }
061
062
063 /**
064 * Gets the JOSE object type.
065 *
066 * @return The JOSE object type.
067 */
068 public String getType() {
069
070 return type;
071 }
072
073
074 /**
075 * Overrides {@code Object.hashCode()}.
076 *
077 * @return The object hash code.
078 */
079 @Override
080 public int hashCode() {
081
082 return type.hashCode();
083 }
084
085
086 /**
087 * Overrides {@code Object.equals()}.
088 *
089 * @param object The object to compare to.
090 *
091 * @return {@code true} if the objects have the same value, otherwise
092 * {@code false}.
093 */
094 @Override
095 public boolean equals(final Object object) {
096
097 return object != null &&
098 object instanceof JOSEObjectType &&
099 this.toString().equals(object.toString());
100 }
101
102
103 /**
104 * Returns the string representation of this JOSE object type.
105 *
106 * @see #getType
107 *
108 * @return The string representation.
109 */
110 @Override
111 public String toString() {
112
113 return type;
114 }
115
116
117 /**
118 * Returns the JSON string representation of this JOSE object type.
119 *
120 * @return The JSON string representation.
121 */
122 @Override
123 public String toJSONString() {
124
125 StringBuilder sb = new StringBuilder();
126 sb.append('"');
127 sb.append(JSONObject.escape(type));
128 sb.append('"');
129 return sb.toString();
130 }
131 }