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$ (2012-09-17)
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 instanceof JOSEObjectType && this.toString().equals(object.toString());
098            }
099            
100            
101            /**
102             * Returns the string representation of this JOSE object type.
103             *
104             * @see #getType
105             *
106             * @return The string representation.
107             */
108            @Override
109            public String toString() {
110            
111                    return type;
112            }
113            
114            
115            /**
116             * Returns the JSON string representation of this JOSE object type.
117             * 
118             * @return The JSON string representation.
119             */
120            @Override
121            public String toJSONString() {
122            
123                    StringBuilder sb = new StringBuilder();
124                    sb.append('"');
125                    sb.append(JSONObject.escape(type));
126                    sb.append('"');
127                    return sb.toString();
128            }
129    }