From 1d34b07b6defa90baf891d6b4ef19e8b3097a557 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20K=C3=B6rber?= Date: Sun, 20 Dec 2015 15:30:23 +0100 Subject: [PATCH] Add a special exception type for 'unauthorized' errors --- .../exception/AcmeUnauthorizedException.java | 37 +++++++++++++++++++ .../acme4j/impl/DefaultConnection.java | 10 ++++- 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 acme4j-client/src/main/java/org/shredzone/acme4j/exception/AcmeUnauthorizedException.java diff --git a/acme4j-client/src/main/java/org/shredzone/acme4j/exception/AcmeUnauthorizedException.java b/acme4j-client/src/main/java/org/shredzone/acme4j/exception/AcmeUnauthorizedException.java new file mode 100644 index 00000000..c8ca4b31 --- /dev/null +++ b/acme4j-client/src/main/java/org/shredzone/acme4j/exception/AcmeUnauthorizedException.java @@ -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); + } + +} diff --git a/acme4j-client/src/main/java/org/shredzone/acme4j/impl/DefaultConnection.java b/acme4j-client/src/main/java/org/shredzone/acme4j/impl/DefaultConnection.java index 37fef26c..2ce4e2b1 100644 --- a/acme4j-client/src/main/java/org/shredzone/acme4j/impl/DefaultConnection.java +++ b/acme4j-client/src/main/java/org/shredzone/acme4j/impl/DefaultConnection.java @@ -44,6 +44,7 @@ import org.shredzone.acme4j.connector.Resource; import org.shredzone.acme4j.connector.Session; import org.shredzone.acme4j.exception.AcmeException; import org.shredzone.acme4j.exception.AcmeServerException; +import org.shredzone.acme4j.exception.AcmeUnauthorizedException; import org.shredzone.acme4j.util.ClaimBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -284,7 +285,14 @@ public class DefaultConnection implements Connection { Map map = readJsonResponse(); String type = (String) map.get("type"); 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 { try { throw new AcmeException("HTTP " + conn.getResponseCode() + ": "