1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package fr.cnes.doi.client;
20
21 import com.fasterxml.jackson.databind.ObjectMapper;
22 import fr.cnes.doi.exception.ClientCrossCiteException;
23 import fr.cnes.doi.utils.spec.Requirement;
24 import java.io.IOException;
25 import java.util.List;
26 import java.util.logging.Level;
27 import org.restlet.data.Reference;
28 import org.restlet.data.Status;
29 import org.restlet.representation.Representation;
30 import org.restlet.resource.ResourceException;
31
32
33
34
35
36
37
38 @Requirement(reqId = Requirement.DOI_INTER_020, reqName = Requirement.DOI_INTER_020_NAME)
39 public class ClientCrossCiteCitation extends BaseClient {
40
41
42
43
44 public static final String CROSS_CITE_URL = "http://citation.crosscite.org";
45
46
47
48
49 public static final String CROSS_CITE_MOCK_URL = "http://localhost:" + DATACITE_MOCKSERVER_PORT;
50
51
52
53
54 public static final String STYLE_URI = "styles";
55
56
57
58
59 public static final String LOCALE_URI = "locales";
60
61
62
63
64 public static final String FORMAT_URI = "format";
65
66
67
68
69 private final Context contextUse;
70
71
72
73
74
75 public ClientCrossCiteCitation() {
76 super(CROSS_CITE_URL);
77 this.contextUse = Context.PROD;
78 }
79
80
81
82
83
84
85 public ClientCrossCiteCitation(final Context context) {
86 super(context.getCrossCiteUrl());
87 this.contextUse = context;
88 }
89
90
91
92
93 protected void init() {
94 this.getClient().setReference(new Reference(this.contextUse.getCrossCiteUrl()));
95 }
96
97
98
99
100
101
102
103
104
105 private List<String> getList(final String segment) throws ClientCrossCiteException {
106 try {
107 final Reference ref = getClient().addSegment(segment);
108 this.getClient().setReference(ref);
109 final Representation rep = this.getClient().get();
110 final Status status = this.getClient().getStatus();
111 if (status.isSuccess()) {
112 final ObjectMapper mapper = new ObjectMapper();
113 return mapper.readValue(rep.getStream(), List.class);
114 } else {
115 throw new ClientCrossCiteException(status, status.getDescription());
116 }
117 } catch (IOException | ResourceException ex) {
118 throw new ClientCrossCiteException(Status.SERVER_ERROR_INTERNAL, ex.getMessage(), ex);
119 } finally {
120 this.getClient().release();
121 }
122 }
123
124
125
126
127
128
129
130
131 public List<String> getStyles() throws ClientCrossCiteException {
132 init();
133 return getList(STYLE_URI);
134 }
135
136
137
138
139
140
141
142
143 public List<String> getLanguages() throws ClientCrossCiteException {
144 init();
145 return getList(LOCALE_URI);
146 }
147
148
149
150
151
152
153
154
155
156
157
158 public String getFormat(final String doiName,
159 final String style,
160 final String language) throws ClientCrossCiteException {
161 init();
162 final String result;
163 try {
164 Reference ref = this.getClient().addSegment(FORMAT_URI);
165 ref = ref.addQueryParameter("doi", doiName);
166 ref = ref.addQueryParameter("style", style);
167 ref = ref.addQueryParameter("lang", language);
168 this.getClient().setReference(ref);
169 final Representation rep = this.getClient().get();
170 final Status status = this.getClient().getStatus();
171 if (status.isSuccess()) {
172 result = rep.getText();
173 } else {
174 throw new ClientCrossCiteException(status, status.getDescription());
175 }
176 return result;
177 } catch (IOException ex) {
178 throw new ClientCrossCiteException(Status.SERVER_ERROR_INTERNAL, ex.getMessage(), ex);
179 } catch (ResourceException ex) {
180 throw new ClientCrossCiteException(ex.getStatus(), ex.getMessage(), ex);
181 } finally {
182 this.getClient().release();
183 }
184 }
185
186
187
188
189 public enum Context {
190
191
192
193
194
195 DEV(CROSS_CITE_MOCK_URL, Level.OFF),
196
197
198
199
200 POST_DEV(CROSS_CITE_URL, Level.ALL),
201
202
203
204
205 PRE_PROD(CROSS_CITE_URL, Level.FINE),
206
207
208
209
210 PROD(CROSS_CITE_URL, Level.INFO);
211
212
213
214
215 private Level levelLog;
216
217
218
219
220 private String crossCiteUrl;
221
222
223
224
225
226
227
228 Context(final String dataciteUrl,
229 final Level levelLog) {
230 this.crossCiteUrl = dataciteUrl;
231 this.levelLog = levelLog;
232 }
233
234
235
236
237
238
239 public Level getLevelLog() {
240 return this.levelLog;
241 }
242
243
244
245
246
247
248 public String getCrossCiteUrl() {
249 return this.crossCiteUrl;
250 }
251
252
253
254
255
256
257 private void setLevelLog(final Level levelLog) {
258 this.levelLog = levelLog;
259 }
260
261
262
263
264
265
266 private void setCrossCiteUrl(final String crossCiteUrl) {
267 this.crossCiteUrl = crossCiteUrl;
268 }
269
270
271
272
273
274
275
276 public static void setLevelLog(final Context context,
277 final Level levelLog) {
278 context.setLevelLog(levelLog);
279 }
280
281
282
283
284
285
286
287 public static void setCrossCiteUrl(final Context context,
288 final String crossCiteUrl) {
289 context.setCrossCiteUrl(crossCiteUrl);
290 }
291
292 }
293 }