Add a special exception type for 'unauthorized' errors

pull/17/merge
Richard Körber 2015-12-20 15:30:23 +01:00
parent 19ce2328ea
commit 1d34b07b6d
2 changed files with 46 additions and 1 deletions

View File

@ -0,0 +1,37 @@
/*
* acme4j - Java ACME client
*
* Copyright (C) 2015 Richard "Shred" Körber
* http://acme4j.shredzone.org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
package org.shredzone.acme4j.exception;
/**
* An exception that is thrown when the client is not authorized. The details will give
* an explanation for the reasons (e.g. "client not on a whitelist").
*
* @author Richard "Shred" Körber
*/
public class AcmeUnauthorizedException extends AcmeServerException {
private static final long serialVersionUID = 9064697508262919366L;
/**
* Creates a new {@link AcmeUnauthorizedException}.
*
* @param type
* System readable error type (here {@code "urn:acme:error:unauthorized"})
* @param detail
* Human readable error message
*/
public AcmeUnauthorizedException(String type, String detail) {
super(type, detail);
}
}

View File

@ -44,6 +44,7 @@ import org.shredzone.acme4j.connector.Resource;
import org.shredzone.acme4j.connector.Session; import org.shredzone.acme4j.connector.Session;
import org.shredzone.acme4j.exception.AcmeException; import org.shredzone.acme4j.exception.AcmeException;
import org.shredzone.acme4j.exception.AcmeServerException; import org.shredzone.acme4j.exception.AcmeServerException;
import org.shredzone.acme4j.exception.AcmeUnauthorizedException;
import org.shredzone.acme4j.util.ClaimBuilder; import org.shredzone.acme4j.util.ClaimBuilder;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -284,7 +285,14 @@ public class DefaultConnection implements Connection {
Map<String, Object> map = readJsonResponse(); Map<String, Object> map = readJsonResponse();
String type = (String) map.get("type"); String type = (String) map.get("type");
String detail = (String) map.get("detail"); String detail = (String) map.get("detail");
throw new AcmeServerException(type, detail);
switch (type) {
case "urn:acme:error:unauthorized":
throw new AcmeUnauthorizedException(type, detail);
default:
throw new AcmeServerException(type, detail);
}
} else { } else {
try { try {
throw new AcmeException("HTTP " + conn.getResponseCode() + ": " throw new AcmeException("HTTP " + conn.getResponseCode() + ": "