1 /*
2 * Copyright (C) 2017-2019 Centre National d'Etudes Spatiales (CNES).
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 3.0 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17 * MA 02110-1301 USA
18 */
19 package fr.cnes.doi.exception;
20
21 import org.restlet.data.Status;
22
23 /**
24 * Exception for Client Cross Cite.
25 *
26 * @author Jean-Christophe Malapert (jean-christophe.malapert@cnes.fr)
27 */
28 public class ClientCrossCiteException extends Exception {
29
30 /**
31 * serialVersionUID.
32 */
33 private static final long serialVersionUID = -246999030222838204L;
34
35 /**
36 * Detail message.
37 */
38 private final String detailMessage;
39
40 /**
41 * HTTP status.
42 */
43 private final Status status;
44
45 /**
46 * Constructs a new exception with HTTP status as its detail message.
47 *
48 * @param status HTTP status
49 */
50 public ClientCrossCiteException(final Status status) {
51 super();
52 this.detailMessage = computeDetailMessage(status);
53 this.status = status;
54 }
55
56 /**
57 * Constructs a new exception with the specified HTTP status and detail
58 * message.
59 *
60 * @param status HTTP message
61 * @param message message
62 */
63 public ClientCrossCiteException(final Status status,
64 final String message) {
65 super(message);
66 this.detailMessage = computeDetailMessage(status);
67 this.status = status;
68 }
69
70 /**
71 * Constructs a new exception with the specified detail cause.
72 *
73 * @param status HTTP status
74 * @param cause cause
75 */
76 public ClientCrossCiteException(final Status status,
77 final Throwable cause) {
78 super(cause);
79 this.detailMessage = computeDetailMessage(status);
80 this.status = status;
81 }
82
83 /**
84 * Constructs a new exception with the specified detail HTTP status, message
85 * and cause.
86 *
87 * @param status HTTP status
88 * @param message message
89 * @param cause cause
90 */
91 public ClientCrossCiteException(
92 final Status status,
93 final String message,
94 final Throwable cause) {
95 super(message, cause);
96 this.detailMessage = computeDetailMessage(status);
97 this.status = status;
98 }
99
100 /**
101 * Returns the detail message according to the status.
102 *
103 * @param status HTTP status
104 * @return the detail message
105 */
106 private String computeDetailMessage(final Status status) {
107 final String result;
108 switch (status.getCode()) {
109 case 200:
110 result = "Operation successful";
111 break;
112 case 404:
113 result = "DOI not found";
114 break;
115 case 400:
116 result = "Wrong input parameters";
117 break;
118 default:
119 result = "Internal error";
120 break;
121 }
122 return result;
123 }
124
125 /**
126 * Returns the status.
127 *
128 * @return the status
129 */
130 public Status getStatus() {
131 return this.status;
132 }
133
134 /**
135 * Returns detail message;
136 *
137 * @return the detail message
138 */
139 public String getDetailMessage() {
140 return this.detailMessage;
141 }
142
143 }