@@ -86,13 +86,14 @@ public AbstractMessage(Map<String, Object> claims) {
8686 }
8787
8888 /**
89- * Constructs message from urlEncoded String representation of a message .
89+ * Constructs a message from the URL string .
9090 *
91- * @param input
92- * the urlEncoded String representation of a message
91+ * @param input The urlEncoded String representation of a message.
92+ * @throws MalformedURLException Thrown if the message cannot be parsed from the input.
93+ * @throws InvalidClaimException Thrown if the message content is invalid.
9394 */
9495 public void fromUrlEncoded (String input )
95- throws MalformedURLException , IOException , InvalidClaimException {
96+ throws MalformedURLException , DeserializationException {
9697 if (Strings .isNullOrEmpty (input )) {
9798 return ;
9899 }
@@ -101,8 +102,15 @@ public void fromUrlEncoded(String input)
101102 while (paramTokenizer .hasMoreTokens ()) {
102103 String pair = paramTokenizer .nextToken ();
103104 StringTokenizer pairTokenizer = new StringTokenizer (pair , "=" );
104- String key = URLDecoder .decode (pairTokenizer .nextToken (), "UTF-8" );
105- String value = URLDecoder .decode (pairTokenizer .nextToken (), "UTF-8" );
105+
106+ String key ;
107+ String value ;
108+ try {
109+ key = URLDecoder .decode (pairTokenizer .nextToken (), "UTF-8" );
110+ value = URLDecoder .decode (pairTokenizer .nextToken (), "UTF-8" );
111+ } catch (UnsupportedEncodingException e ) {
112+ throw new MalformedURLException ("The parameters cannot be decoded using UTF-8" );
113+ }
106114 jsonBuilder .append ("\" " + key + "\" : " );
107115 jsonBuilder .append (
108116 value .startsWith ("{" ) || value .startsWith ("[" ) ? "\" " + value .replace ("\" " , "\\ \" " ) + "\" "
@@ -119,11 +127,10 @@ public void fromUrlEncoded(String input)
119127 * urlEncoded string.
120128 *
121129 * @return an urlEncoded string
122- * @throws InvalidClaimException
123- * if the message is invalid
130+ * @throws SerializationException Thrown if message cannot be serialized.
124131 */
125132 public String toUrlEncoded ()
126- throws SerializationException , JsonProcessingException , InvalidClaimException {
133+ throws SerializationException {
127134 if (claims .size () == 0 ) {
128135 return "" ;
129136 }
@@ -154,18 +161,18 @@ public String toUrlEncoded()
154161 }
155162
156163 /**
157- * Constructs message from JSON string values .
164+ * Constructs a message from the JSON string.
158165 *
159- * @param input
160- * The JSON String representation of a message
166+ * @param input The JSON String representation of a message
167+ * @throws InvalidClaimException Thrown if the message content is invalid.
161168 */
162- public void fromJson (String input ) throws InvalidClaimException {
169+ public void fromJson (String input ) throws DeserializationException {
163170 Map <String , Object > newClaims ;
164171 try {
165172 newClaims = mapper .readValue (input , new TypeReference <Map <String , Object >>() {
166173 });
167174 } catch (IOException e ) {
168- throw new InvalidClaimException (String .format ("Unable to parse message from '%s'" , input ));
175+ throw new DeserializationException (String .format ("Unable to parse message from '%s'" , input ));
169176 }
170177 this .claims = newClaims ;
171178 verified = false ;
@@ -176,15 +183,17 @@ public void fromJson(String input) throws InvalidClaimException {
176183 * json string.
177184 *
178185 * @return a JSON String representation in the form of a hashMap mapping string -> string
179- * @throws InvalidClaimException
180- * thrown if message parameters do not match the message requirements.
186+ * @throws SerializationException Thrown if message cannot be serialized.
181187 */
182- public String toJson () throws JsonProcessingException , InvalidClaimException {
188+ public String toJson () throws SerializationException {
183189 SimpleModule module = new SimpleModule ();
184190 module .addSerializer (AbstractMessage .class , new MessageSerializer ());
185191 mapper .registerModule (module );
186- String jsonMsg = mapper .writeValueAsString (this );
187- return jsonMsg ;
192+ try {
193+ return mapper .writeValueAsString (this );
194+ } catch (JsonProcessingException e ) {
195+ throw new SerializationException ("Could not serialize to JSON" , e );
196+ }
188197 }
189198
190199 /**
@@ -200,7 +209,7 @@ public String toJson() throws JsonProcessingException, InvalidClaimException {
200209 * @throws IOException
201210 * thrown if message parameters do not match the message requirements.
202211 */
203- public void fromJwt (String jwt , KeyJar keyJar , String keyOwner ) throws IOException {
212+ public void fromJwt (String jwt , KeyJar keyJar , String keyOwner ) throws DeserializationException {
204213 fromJwt (jwt , keyJar , keyOwner , null , true , true );
205214 }
206215
@@ -220,13 +229,13 @@ public void fromJwt(String jwt, KeyJar keyJar, String keyOwner) throws IOExcepti
220229 * If jwt is missing kid, try any of the owners keys to verify jwt.
221230 * @param trustJKU
222231 * Whether extending keyjar by JKU is allowed or not.
223- * @throws JWTDecodeException
224- * thrown if message parameters do not match the message requirements .
232+ * @throws DeserializationException Thrown if the message content is invalid.
233+ * @throws JWTDecodeException Thrown if the JWT cannot be decoded .
225234 */
226235 @ SuppressWarnings ("unchecked" )
227236 public void fromJwt (String jwt , KeyJar keyJar , String keyOwner ,
228237 Map <String , List <String >> noKidIssuers , boolean allowMissingKid , boolean trustJKU )
229- throws IOException {
238+ throws DeserializationException , JWTDecodeException {
230239 String [] parts = MessageUtil .splitToken (jwt );
231240 String headerJson ;
232241 String payloadJson ;
@@ -237,8 +246,12 @@ public void fromJwt(String jwt, KeyJar keyJar, String keyOwner,
237246 throw new JWTDecodeException ("The UTF-8 Charset isn't initialized." , e );
238247 }
239248
240- this .header = mapper .readValue (headerJson , Map .class );
241- this .claims = mapper .readValue (payloadJson , Map .class );
249+ try {
250+ this .header = mapper .readValue (headerJson , Map .class );
251+ this .claims = mapper .readValue (payloadJson , Map .class );
252+ } catch (IOException e ) {
253+ throw new DeserializationException ("Could not read the JWT contents" , e );
254+ }
242255 verified = false ;
243256
244257 if (keyJar == null ) {
@@ -259,9 +272,7 @@ public void fromJwt(String jwt, KeyJar keyJar, String keyOwner,
259272 List <java .security .Key > keys ;
260273 try {
261274 keys = keyJar .getJWTVerifyKeys (jwt , keyOwner , noKidIssuers , allowMissingKid , trustJKU );
262- } catch (JWKException | ValueError e ) {
263- // TODO: Replace JWTDecodeException with better exception. Generic note to this class and this
264- // method.
275+ } catch (JWKException | ValueError | IOException e ) {
265276 throw new JWTDecodeException (
266277 String .format ("Not able to locate keys to verify JWT, '%s'" , e .getMessage ()));
267278 }
@@ -322,7 +333,7 @@ public void fromJwt(String jwt, KeyJar keyJar, String keyOwner,
322333 * signing algorithm
323334 * @return message as jwt string.
324335 */
325- public String toJwt (Key key , String alg ) {
336+ public String toJwt (Key key , String alg ) throws SerializationException {
326337
327338 header = new HashMap <String , Object >();
328339 header .put ("alg" , alg );
@@ -360,13 +371,11 @@ public String toJwt(Key key, String alg) {
360371 // TODO: HMAC algorithms, are getting client secret also from key jar?
361372 }
362373 } catch (IllegalArgumentException | ValueError e ) {
363- // TODO: This is not Decoding exception, replace it.
364- throw new JWTDecodeException (String
374+ throw new SerializationException (String
365375 .format ("Not able to initialize algorithm '%s' to sign JWT, '%s'" , alg , e .getMessage ()));
366376 }
367377 if (algorithm == null ) {
368- // TODO: This is not Decoding exception, replace it.
369- throw new JWTDecodeException (
378+ throw new SerializationException (
370379 String .format ("Not able to initialize algorithm '%s' to sign JWT" , alg ));
371380 }
372381 JWTCreator .Builder newBuilder = JWT .create ().withHeader (this .header );
0 commit comments