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 fr.cnes.httpclient.HttpClient;
22 import java.io.IOException;
23 import java.util.Collections;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.concurrent.ConcurrentHashMap;
27 import org.restlet.Client;
28 import org.restlet.data.Parameter;
29 import org.restlet.data.Status;
30 import org.restlet.representation.Representation;
31 import org.restlet.resource.ResourceException;
32 import org.restlet.util.Series;
33
34
35
36
37
38
39 public class ClientLandingPage extends BaseClient {
40
41
42
43
44 private static final String BASE_URI = "http://doi.org";
45
46
47
48
49 private final Map<String, Status> errors = new ConcurrentHashMap<>();
50
51
52
53
54
55
56 public ClientLandingPage(final List<String> dois) {
57 super(BASE_URI);
58 checkDoi(dois);
59 }
60
61
62
63
64
65
66
67 private void checkDoi(final List<String> dois) {
68 this.getLog().traceEntry("Parameters\n\tdois : {}", dois);
69 this.getClient().setMaxRedirects(5);
70 this.getClient().setLoggable(true);
71 final Client configurationClient = (Client) this.getClient().getNext();
72 final Series<Parameter> parameters = configurationClient.getContext().getParameters();
73 parameters.add(HttpClient.MAX_REDIRECTION, String.
74 valueOf(this.getClient().getMaxRedirects()));
75 this.getLog().info("{} landing pages to check.", dois.size());
76 for (final String doi : dois) {
77 this.getClient().setReference(BASE_URI);
78 this.getClient().addSegment(doi);
79 this.getLog().info("Checking landing page {}", doi);
80 try {
81 final Representation rep = this.getClient().get();
82 rep.exhaust();
83 final Status status = this.getClient().getStatus();
84 if (status.isError()) {
85 this.errors.put(doi, status);
86 this.getLog().error("Error for landing page {}", doi);
87 } else {
88 this.getLog().info("OK");
89 }
90 } catch (ResourceException ex) {
91 this.getLog().error("Checking landing pages", ex);
92 this.errors.put(doi, ex.getStatus());
93 } catch (IOException ex) {
94 this.getLog().error("Checking landing pages", ex);
95 this.errors.put(doi, new Status(Status.SERVER_ERROR_INTERNAL, ex));
96 } finally {
97 this.getClient().release();
98 }
99 }
100 this.getLog().traceExit();
101 }
102
103
104
105
106
107
108 public boolean isSuccess() {
109 this.getLog().traceEntry();
110 return this.getLog().traceExit(this.errors.isEmpty());
111 }
112
113
114
115
116
117
118 public boolean isError() {
119 return !isSuccess();
120 }
121
122
123
124
125
126
127 public Map<String, Status> getErrors() {
128 this.getLog().traceEntry();
129 return this.getLog().traceExit(Collections.unmodifiableMap(this.errors));
130 }
131
132 }