001package com.nimbusds.jose; 002 003 004import net.jcip.annotations.Immutable; 005import net.minidev.json.JSONAware; 006import net.minidev.json.JSONObject; 007 008 009/** 010 * JOSE object type, represents the {@code typ} header parameter in plain, JSON 011 * Web Signature (JWS) and JSON Web Encryption (JWE) objects. This class is 012 * immutable. 013 * 014 * <p>Includes constants for the following standard types: 015 * 016 * <ul> 017 * <li>{@link #JWS} 018 * <li>{@link #JWE} 019 * </ul> 020 * 021 * <p>Additional types can be defined using the constructor. 022 * 023 * @author Vladimir Dzhuvinov 024 * @version $version$ (2013-01-15) 025 */ 026@Immutable 027public final class JOSEObjectType implements JSONAware { 028 029 030 /** 031 * JWS object type. 032 */ 033 public static final JOSEObjectType JWS = new JOSEObjectType("JWS"); 034 035 036 /** 037 * JWE object type. 038 */ 039 public static final JOSEObjectType JWE = new JOSEObjectType("JWE"); 040 041 042 /** 043 * The object type. 044 */ 045 private final String type; 046 047 048 /** 049 * Creates a new JOSE object type. 050 * 051 * @param type The object type. Must not be {@code null}. 052 */ 053 public JOSEObjectType(final String type) { 054 055 if (type == null) { 056 throw new IllegalArgumentException("The object type must not be null"); 057 } 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}