001 package com.nimbusds.jose; 002 003 004 import java.text.ParseException; 005 006 import net.jcip.annotations.ThreadSafe; 007 008 import com.nimbusds.jose.util.Base64URL; 009 010 011 /** 012 * Plaintext (unsecured) JOSE object. This class is thread-safe. 013 * 014 * @author Vladimir Dzhuvinov 015 * @version $version$ (2012-10-23) 016 */ 017 @ThreadSafe 018 public class PlainObject extends JOSEObject { 019 020 021 /** 022 * The header. 023 */ 024 private final PlainHeader header; 025 026 027 /** 028 * Creates a new plaintext JOSE object with a default {@link PlainHeader} 029 * and the specified payload. 030 * 031 * @param payload The payload. Must not be {@code null}. 032 */ 033 public PlainObject(final Payload payload) { 034 035 if (payload == null) { 036 throw new IllegalArgumentException("The payload must not be null"); 037 } 038 039 setPayload(payload); 040 041 header = new PlainHeader(); 042 } 043 044 045 /** 046 * Creates a new plaintext JOSE object with the specified header and 047 * payload. 048 * 049 * @param header The plaintext header. Must not be {@code null}. 050 * @param payload The payload. Must not be {@code null}. 051 */ 052 public PlainObject(final PlainHeader header, final Payload payload) { 053 054 if (header == null) { 055 throw new IllegalArgumentException("The plain header must not be null"); 056 } 057 058 this.header = header; 059 060 if (payload == null) { 061 throw new IllegalArgumentException("The payload must not be null"); 062 } 063 064 setPayload(payload); 065 } 066 067 068 /** 069 * Creates a new plaintext JOSE object with the specified Base64URL-encoded 070 * parts. 071 * 072 * @param firstPart The first part, corresponding to the plaintext header. 073 * Must not be {@code null}. 074 * @param secondPart The second part, corresponding to the payload. Must 075 * not be {@code null}. 076 * 077 * @throws ParseException If parsing of the serialised parts failed. 078 */ 079 public PlainObject(final Base64URL firstPart, final Base64URL secondPart) 080 throws ParseException { 081 082 if (firstPart == null) { 083 throw new IllegalArgumentException("The first part must not be null"); 084 } 085 086 try { 087 header = PlainHeader.parse(firstPart); 088 089 } catch (ParseException e) { 090 091 throw new ParseException("Invalid plain header: " + e.getMessage(), 0); 092 } 093 094 if (secondPart == null) { 095 throw new IllegalArgumentException("The second part must not be null"); 096 } 097 098 setPayload(new Payload(secondPart)); 099 100 setParsedParts(firstPart, secondPart, null); 101 } 102 103 104 @Override 105 public ReadOnlyPlainHeader getHeader() { 106 107 return header; 108 } 109 110 111 /** 112 * Serialises this plaintext JOSE object to its compact format consisting 113 * of Base64URL-encoded parts delimited by period ('.') characters. 114 * 115 * <pre> 116 * [header-base64url].[payload-base64url].[] 117 * </pre> 118 * 119 * @return The serialised plaintext JOSE object. 120 */ 121 @Override 122 public String serialize() { 123 124 StringBuilder sb = new StringBuilder(header.toBase64URL().toString()); 125 sb.append('.'); 126 sb.append(getPayload().toBase64URL().toString()); 127 sb.append('.'); 128 return sb.toString(); 129 } 130 131 132 /** 133 * Parses a plaintext JOSE object from the specified string in compact 134 * format. 135 * 136 * @param s The string to parse. Must not be {@code null}. 137 * 138 * @return The plain JOSE object. 139 * 140 * @throws ParseException If the string couldn't be parsed to a valid 141 * plaintext JOSE object. 142 */ 143 public static PlainObject parse(final String s) 144 throws ParseException { 145 146 Base64URL[] parts = JOSEObject.split(s); 147 148 if (! parts[2].toString().isEmpty()) { 149 throw new ParseException("Unexpected third Base64URL part", 0); 150 } 151 152 return new PlainObject(parts[0], parts[1]); 153 } 154 }