1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package fr.cnes.doi.exception;
20
21 import fr.cnes.doi.client.ClientMDS.DATACITE_API_RESPONSE;
22 import java.io.IOException;
23 import org.restlet.data.Status;
24 import org.restlet.representation.Representation;
25
26
27
28
29
30
31 public class ClientMdsException extends Exception {
32
33
34
35
36 public static final int SUCCESS_CREATED = 201;
37
38
39
40
41 public static final int SUCCESS_OK = 200;
42
43
44
45
46 public static final int SUCCESS_NO_CONTENT = 204;
47
48
49
50
51
52 public static final int CLIENT_BAD_REQUEST = 400;
53
54
55
56
57 public static final int CLIENT_ERROR_UNAUTHORIZED = 401;
58
59
60
61
62 public static final int CONNECTOR_ERROR_COMMUNICATION = 1001;
63
64
65
66
67 public static final int CLIENT_ERROR_FORBIDDEN = 403;
68
69
70
71
72 public static final int CLIENT_ERROR_NOT_FOUND = 404;
73
74
75
76
77 public static final int CLIENT_ERROR_CONFLICT = 409;
78
79
80
81
82 public static final int CLIENT_ERROR_GONE = 410;
83
84
85
86
87 public static final int CLIENT_ERROR_PRECONDITION_FAILED = 412;
88
89
90
91
92 private static final long serialVersionUID = -5061913391706889102L;
93
94
95
96
97 private final String detailMessage;
98
99
100
101
102 private final Status status;
103
104
105
106
107
108
109 public ClientMdsException(final Status status) {
110 super();
111 this.detailMessage = computeDetailMessage(status);
112 this.status = computeStatus(status);
113 }
114
115
116
117
118
119
120
121
122 public ClientMdsException(final Status status,
123 final String message) {
124 super(message);
125 this.detailMessage = computeDetailMessage(status);
126 this.status = computeStatus(status);
127 }
128
129
130
131
132
133
134
135 public ClientMdsException(final Status status,
136 final Throwable cause) {
137 super(cause);
138 this.detailMessage = computeDetailMessage(status);
139 this.status = computeStatus(status);
140 }
141
142
143
144
145
146
147
148
149
150 public ClientMdsException(final Status status,
151 final String message,
152 final Throwable cause) {
153 super(message, cause);
154 this.detailMessage = computeDetailMessage(status);
155 this.status = computeStatus(status);
156 }
157
158
159
160
161
162
163
164
165
166
167 public ClientMdsException(final Status status,
168 final String message,
169 final Representation responseEntity,
170 final Throwable cause) {
171 super(message, cause);
172 this.status = computeStatus(status);
173 String txt;
174 try {
175 txt = responseEntity.getText();
176 } catch (IOException ex) {
177 txt = computeDetailMessage(this.status);
178 }
179 this.detailMessage = txt;
180 }
181
182
183
184
185
186
187
188 private Status computeStatus(final Status status) {
189 return (status.getCode() == CONNECTOR_ERROR_COMMUNICATION) ? new Status(
190 CLIENT_ERROR_UNAUTHORIZED) : status;
191 }
192
193
194
195
196
197
198
199 private String computeDetailMessage(final Status status) {
200 return DATACITE_API_RESPONSE.getMessageFromStatus(status);
201 }
202
203
204
205
206
207
208 public Status getStatus() {
209 return this.status;
210 }
211
212
213
214
215
216
217 public String getDetailMessage() {
218 return this.detailMessage;
219 }
220 }