1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package fr.cnes.doi.application;
20
21 import fr.cnes.doi.db.AbstractTokenDBHelper;
22 import fr.cnes.doi.security.LoginBasedVerifier;
23 import fr.cnes.doi.security.TokenBasedVerifier;
24 import fr.cnes.doi.security.TokenSecurity;
25 import fr.cnes.doi.services.CnesStatusService;
26 import fr.cnes.doi.settings.DoiSettings;
27 import fr.cnes.doi.settings.EmailSettings;
28 import fr.cnes.doi.utils.spec.Requirement;
29 import java.util.Arrays;
30 import java.util.Collections;
31 import java.util.HashSet;
32 import java.util.Set;
33 import org.apache.logging.log4j.LogManager;
34 import org.apache.logging.log4j.Logger;
35 import org.restlet.Request;
36 import org.restlet.Response;
37 import org.restlet.data.ChallengeScheme;
38 import org.restlet.data.Method;
39 import org.restlet.data.Status;
40 import org.restlet.ext.wadl.ApplicationInfo;
41 import org.restlet.ext.wadl.WadlApplication;
42 import org.restlet.ext.wadl.WadlCnesRepresentation;
43 import org.restlet.representation.Representation;
44 import org.restlet.security.ChallengeAuthenticator;
45 import org.restlet.service.CorsService;
46
47
48
49
50
51
52
53 @Requirement(reqId = Requirement.DOI_DOC_010, reqName = Requirement.DOI_DOC_010_NAME)
54 public abstract class AbstractApplication extends WadlApplication {
55
56
57
58
59 public static final Set DEFAULT_CORS_ORIGIN = Collections.unmodifiableSet(
60 new HashSet(Arrays.asList("*"))
61 );
62
63
64
65
66 public static final boolean DEFAULT_CORS_CREDENTIALS = true;
67
68
69
70
71 private static final Logger LOG = LogManager.getLogger(AbstractApplication.class.getName());
72
73
74
75 private final AbstractTokenDBHelper tokenDB = TokenSecurity.getInstance().getTokenDB();
76
77
78
79 private final DoiSettings config = DoiSettings.getInstance();
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95 public AbstractApplication() {
96 super();
97 init();
98 }
99
100
101
102
103 private void init() {
104 getServices().add(this.createCoreService(DEFAULT_CORS_ORIGIN, DEFAULT_CORS_CREDENTIALS));
105 setStatusService(new CnesStatusService());
106 setOwner("Centre National d'Etudes Spatiales (CNES)");
107 setAuthor("Jean-Christophe Malapert (DNO/ISA/VIP)");
108 }
109
110
111
112
113
114
115
116
117 protected final CorsService createCoreService(final Set corsOrigin,
118 final boolean corsCredentials) {
119 LOG.traceEntry();
120 final CorsService corsService = new CorsService();
121 LOG.info("Allows all origins {}", corsOrigin);
122 corsService.setAllowedOrigins(corsOrigin);
123 LOG.info("Allows Credientials {}", corsCredentials);
124 corsService.setAllowedCredentials(corsCredentials);
125
126 return LOG.traceExit(corsService);
127 }
128
129
130
131
132
133
134
135 @Requirement(reqId = Requirement.DOI_AUTH_010, reqName = Requirement.DOI_AUTH_010_NAME)
136 protected ChallengeAuthenticator createAuthenticator() {
137 LOG.traceEntry();
138 final ChallengeAuthenticator guard = new ChallengeAuthenticator(
139 getContext(), ChallengeScheme.HTTP_BASIC, "realm");
140
141 guard.setVerifier(this.getContext().getDefaultVerifier());
142 guard.setEnroler(this.getContext().getDefaultEnroler());
143
144 return LOG.traceExit(guard);
145 }
146
147
148
149
150
151
152
153 @Requirement(reqId = Requirement.DOI_AUTH_010, reqName = Requirement.DOI_AUTH_010_NAME)
154 protected ChallengeAuthenticator createAuthenticatorLoginBased() {
155 LOG.traceEntry();
156 final ChallengeAuthenticator guard = new ChallengeAuthenticator(
157 getContext(), ChallengeScheme.HTTP_BASIC, "realm") {
158
159
160
161
162
163
164
165 @Override
166 public int beforeHandle(final Request request, final Response response) {
167 final int status;
168 if (request.getMethod().equals(Method.OPTIONS)) {
169 response.setStatus(Status.SUCCESS_OK);
170 status = org.restlet.routing.Filter.CONTINUE;
171 } else {
172 status = super.beforeHandle(request, response);
173 }
174 return status;
175 }
176 };
177
178 final LoginBasedVerifier verifier = new LoginBasedVerifier();
179 guard.setVerifier(verifier);
180
181 return LOG.traceExit(guard);
182 }
183
184
185
186
187
188
189
190 @Requirement(reqId = Requirement.DOI_AUTH_020, reqName = Requirement.DOI_AUTH_020_NAME)
191 protected ChallengeAuthenticator createTokenAuthenticator() {
192 LOG.traceEntry();
193 final ChallengeAuthenticator guard = new ChallengeAuthenticator(
194 getContext(), ChallengeScheme.HTTP_OAUTH_BEARER, "testRealm") {
195
196
197
198
199
200
201
202 @Override
203 public int beforeHandle(final Request request, final Response response) {
204 final int status;
205 if (request.getMethod().equals(Method.OPTIONS)) {
206 response.setStatus(Status.SUCCESS_OK);
207 status = org.restlet.routing.Filter.CONTINUE;
208 } else {
209 status = super.beforeHandle(request, response);
210 }
211 return status;
212 }
213 };
214 final TokenBasedVerifier verifier = new TokenBasedVerifier(getTokenDB());
215 guard.setVerifier(verifier);
216
217 return LOG.traceExit(guard);
218 }
219
220
221
222
223
224
225
226 @Override
227 protected Representation createHtmlRepresentation(final ApplicationInfo applicationInfo) {
228 final WadlCnesRepresentation wadl = new WadlCnesRepresentation(applicationInfo);
229 return wadl.getHtmlRepresentation();
230 }
231
232
233
234
235
236
237 public void sendAlertWhenDataCiteFailed(final Exception exception) {
238 LOG.traceEntry("Parameters : {}", exception);
239 final String subject = "Datacite problem";
240 final String message = "Dear administrator, an error has been detected"
241 + " coming from Datacite, please look to the Service status\n" + exception;
242 EmailSettings.getInstance().sendMessage(subject, message);
243 }
244
245
246
247
248
249
250 protected final DoiSettings getConfig() {
251 LOG.traceEntry();
252 return LOG.traceExit(config);
253 }
254
255
256
257
258
259
260 public AbstractTokenDBHelper getTokenDB() {
261 return this.tokenDB;
262 }
263
264
265
266
267
268
269 public Logger getLog() {
270 return LOG;
271 }
272
273 }