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 #JOSE} 019 * <li>{@link #JOSE_JSON JOSE+JSON} 020 * <li>{@link #JWT} 021 * </ul> 022 * 023 * <p>Additional types can be defined using the constructor. 024 * 025 * @author Vladimir Dzhuvinov 026 * @version $version$ (2014-02-15) 027 */ 028@Immutable 029public final class JOSEObjectType implements JSONAware { 030 031 032 /** 033 * Compact encoded JOSE object type. 034 */ 035 public static final JOSEObjectType JOSE = new JOSEObjectType("JOSE"); 036 037 038 /** 039 * JSON-encoded JOSE object type.. 040 */ 041 public static final JOSEObjectType JOSE_JSON = new JOSEObjectType("JOSE+JSON"); 042 043 044 /** 045 * JSON Web Token (JWT) object type. 046 */ 047 public static final JOSEObjectType JWT = new JOSEObjectType("JWT"); 048 049 050 /** 051 * The object type. 052 */ 053 private final String type; 054 055 056 /** 057 * Creates a new JOSE object type. 058 * 059 * @param type The object type. Must not be {@code null}. 060 */ 061 public JOSEObjectType(final String type) { 062 063 if (type == null) { 064 throw new IllegalArgumentException("The object type must not be null"); 065 } 066 067 this.type = type; 068 } 069 070 071 /** 072 * Gets the JOSE object type. 073 * 074 * @return The JOSE object type. 075 */ 076 public String getType() { 077 078 return type; 079 } 080 081 082 /** 083 * Overrides {@code Object.hashCode()}. 084 * 085 * @return The object hash code. 086 */ 087 @Override 088 public int hashCode() { 089 090 return type.hashCode(); 091 } 092 093 094 /** 095 * Overrides {@code Object.equals()}. 096 * 097 * @param object The object to compare to. 098 * 099 * @return {@code true} if the objects have the same value, otherwise 100 * {@code false}. 101 */ 102 @Override 103 public boolean equals(final Object object) { 104 105 return object != null && 106 object instanceof JOSEObjectType && 107 this.toString().equals(object.toString()); 108 } 109 110 111 /** 112 * Returns the string representation of this JOSE object type. 113 * 114 * @see #getType 115 * 116 * @return The string representation. 117 */ 118 @Override 119 public String toString() { 120 121 return type; 122 } 123 124 125 /** 126 * Returns the JSON string representation of this JOSE object type. 127 * 128 * @return The JSON string representation. 129 */ 130 @Override 131 public String toJSONString() { 132 133 StringBuilder sb = new StringBuilder(); 134 sb.append('"'); 135 sb.append(JSONObject.escape(type)); 136 sb.append('"'); 137 return sb.toString(); 138 } 139}