1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package fr.cnes.doi.resource.mds;
20
21 import fr.cnes.doi.application.DoiMdsApplication;
22 import fr.cnes.doi.application.DoiMdsApplication.API_MDS;
23 import fr.cnes.doi.client.ClientMDS;
24 import fr.cnes.doi.client.ClientMDS.DATACITE_API_RESPONSE;
25 import fr.cnes.doi.exception.ClientMdsException;
26 import fr.cnes.doi.exception.DoiServerException;
27 import static fr.cnes.doi.security.UtilsHeader.SELECTED_ROLE_PARAMETER;
28 import fr.cnes.doi.settings.Consts;
29 import fr.cnes.doi.settings.DoiSettings;
30 import fr.cnes.doi.utils.spec.Requirement;
31 import java.util.Arrays;
32 import org.apache.logging.log4j.Level;
33 import org.restlet.data.Form;
34 import org.restlet.data.MediaType;
35 import org.restlet.data.Method;
36 import org.restlet.data.Status;
37 import org.restlet.ext.wadl.DocumentationInfo;
38 import org.restlet.ext.wadl.MethodInfo;
39 import org.restlet.ext.wadl.ParameterInfo;
40 import org.restlet.ext.wadl.ParameterStyle;
41 import org.restlet.ext.wadl.RepresentationInfo;
42 import org.restlet.representation.Representation;
43 import org.restlet.representation.StringRepresentation;
44 import org.restlet.resource.Get;
45 import org.restlet.resource.Post;
46
47
48
49
50
51
52 public class MediaResource extends BaseMdsResource {
53
54
55
56
57 private volatile String mediaName;
58
59
60
61
62
63
64 @Override
65 protected void doInit() throws DoiServerException {
66 super.doInit();
67 LOG.traceEntry();
68 this.mediaName = getResourcePath().replace(DoiMdsApplication.MEDIA_URI + "/", "");
69 LOG.debug(this.mediaName);
70 LOG.traceExit();
71 }
72
73
74
75
76
77
78
79
80
81
82
83
84
85 @Requirement(reqId = Requirement.DOI_SRV_090, reqName = Requirement.DOI_SRV_090_NAME)
86 @Requirement(reqId = Requirement.DOI_MONIT_020, reqName = Requirement.DOI_MONIT_020_NAME)
87 @Get
88 public Representation getMedias() throws DoiServerException {
89 LOG.traceEntry();
90 final Representation rep;
91 final String medias;
92 try {
93 setStatus(Status.SUCCESS_OK);
94 medias = this.getDoiApp().getClient().getMedia(this.mediaName);
95 rep = new StringRepresentation(medias, MediaType.TEXT_URI_LIST);
96 } catch (ClientMdsException ex) {
97 if (ex.getStatus().getCode() == Status.CLIENT_ERROR_NOT_FOUND.getCode()) {
98 throw LOG.throwing(
99 Level.ERROR,
100 new DoiServerException(getApplication(), DATACITE_API_RESPONSE.DOI_NOT_FOUND,
101 ex)
102 );
103 } else {
104 throw LOG.throwing(
105 Level.ERROR,
106 new DoiServerException(getApplication(), API_MDS.DATACITE_PROBLEM, ex)
107 );
108 }
109 }
110
111 return LOG.traceExit(rep);
112 }
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131 @Requirement(reqId = Requirement.DOI_SRV_080, reqName = Requirement.DOI_SRV_080_NAME)
132 @Requirement(reqId = Requirement.DOI_MONIT_020, reqName = Requirement.DOI_MONIT_020_NAME)
133 @Requirement(reqId = Requirement.DOI_INTER_070, reqName = Requirement.DOI_INTER_070_NAME)
134 @Requirement(reqId = Requirement.DOI_AUTO_020, reqName = Requirement.DOI_AUTO_020_NAME)
135 @Requirement(reqId = Requirement.DOI_AUTO_030, reqName = Requirement.DOI_AUTO_030_NAME)
136 @Post
137 public Representation createMedia(final Form mediaForm) throws DoiServerException {
138 LOG.traceEntry("Parameters\n\tmediaForm : {}", mediaForm);
139 checkInputs(this.mediaName, mediaForm);
140 final String result;
141 try {
142 setStatus(Status.SUCCESS_OK);
143 final String selectedRole = extractSelectedRoleFromRequestIfExists();
144 checkPermission(this.mediaName, selectedRole);
145 result = this.getDoiApp().getClient().createMedia(this.mediaName, mediaForm);
146 } catch (ClientMdsException ex) {
147 if (ex.getStatus().getCode() == Status.CLIENT_ERROR_BAD_REQUEST.getCode()) {
148 throw LOG.throwing(
149 Level.ERROR,
150 new DoiServerException(getApplication(), DATACITE_API_RESPONSE.BAD_REQUEST,
151 ex)
152 );
153 } else {
154 throw LOG.throwing(
155 Level.ERROR,
156 new DoiServerException(getApplication(), API_MDS.DATACITE_PROBLEM, ex)
157 );
158 }
159 }
160 return LOG.traceExit(new StringRepresentation(result));
161 }
162
163
164
165
166
167
168
169
170 @Requirement(reqId = Requirement.DOI_INTER_070, reqName = Requirement.DOI_INTER_070_NAME)
171 private void checkInputs(final String doi,
172 final Form mediaForm) throws DoiServerException {
173 LOG.traceEntry("Parameters\n\tdoi={}\n\tmediaForm={}", doi, mediaForm);
174 final StringBuilder errorMsg = new StringBuilder();
175 if (doi == null || doi.isEmpty() || !doi.startsWith(DoiSettings.getInstance().getString(Consts.INIST_DOI))) {
176 errorMsg.append(DOI_PARAMETER).append(" value is not set.");
177 } else {
178 try {
179 ClientMDS.checkIfAllCharsAreValid(doi);
180 } catch (IllegalArgumentException ex) {
181 errorMsg.append(DOI_PARAMETER).append(" no valid syntax.");
182 }
183 }
184 if (errorMsg.length() == 0) {
185 LOG.debug("The form is valid");
186 } else {
187 throw LOG.throwing(
188 Level.ERROR,
189 new DoiServerException(getApplication(), API_MDS.MEDIA_VALIDATION, errorMsg.
190 toString())
191 );
192 }
193 LOG.traceExit();
194 }
195
196
197
198
199
200
201 private RepresentationInfo mediaRepresentation() {
202 final RepresentationInfo repInfo = new RepresentationInfo();
203 repInfo.setMediaType(MediaType.TEXT_PLAIN);
204 final DocumentationInfo docInfo = new DocumentationInfo();
205 docInfo.setTitle("Media representation");
206 docInfo.setTextContent("This request returns a key-value list of media "
207 + "types/urls for a given DOI name");
208 repInfo.setDocumentation(docInfo);
209 return repInfo;
210 }
211
212
213
214
215
216
217
218
219
220
221
222
223 @Requirement(reqId = Requirement.DOI_DOC_010, reqName = Requirement.DOI_DOC_010_NAME)
224 @Override
225 protected final void describeGet(final MethodInfo info) {
226 info.setName(Method.GET);
227 info.setDocumentation("Get a specific media for a given DOI");
228
229 addRequestDocToMethod(info, createQueryParamDoc(
230 DoiMdsApplication.DOI_TEMPLATE, ParameterStyle.TEMPLATE,
231 "DOI name", true, "xs:string")
232 );
233 addResponseDocToMethod(info, createResponseDoc(
234 DATACITE_API_RESPONSE.SUCCESS.getStatus(),
235 DATACITE_API_RESPONSE.SUCCESS.getShortMessage(),
236 mediaRepresentation())
237 );
238 addResponseDocToMethod(info, createResponseDoc(
239 DATACITE_API_RESPONSE.DOI_NOT_FOUND.getStatus(),
240 DATACITE_API_RESPONSE.DOI_NOT_FOUND.getShortMessage(),
241 "explainRepresentationID")
242 );
243 addResponseDocToMethod(info, createResponseDoc(
244 API_MDS.DATACITE_PROBLEM.getStatus(),
245 API_MDS.DATACITE_PROBLEM.getShortMessage(),
246 "explainRepresentationID")
247 );
248 }
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265 @Requirement(reqId = Requirement.DOI_DOC_010, reqName = Requirement.DOI_DOC_010_NAME)
266 @Override
267 protected final void describePost(final MethodInfo info) {
268 info.setName(Method.POST);
269 info.setDocumentation(
270 "POST will add/update media type/urls pairs to a DOI. Standard domain restrictions check will be performed.");
271 final ParameterInfo param = new ParameterInfo();
272 param.setName("{mediaType}");
273 param.setStyle(ParameterStyle.PLAIN);
274 param.setRequired(false);
275 param.setType("xs:string");
276 param.setFixed("{url}");
277 param.setRepeating(true);
278 param.setDocumentation("(key/value) = (mediaType/url)");
279 final RepresentationInfo rep = new RepresentationInfo(MediaType.APPLICATION_WWW_FORM);
280 rep.getParameters().add(param);
281
282 addRequestDocToMethod(info,
283 Arrays.asList(createQueryParamDoc(SELECTED_ROLE_PARAMETER, ParameterStyle.HEADER,
284 "A user can select one role when he is associated to several roles", false,
285 "xs:string")),
286 rep);
287 addResponseDocToMethod(info, createResponseDoc(
288 DATACITE_API_RESPONSE.SUCCESS.getStatus(),
289 DATACITE_API_RESPONSE.SUCCESS.getShortMessage(),
290 "explainRepresentationID")
291 );
292 addResponseDocToMethod(info, createResponseDoc(
293 API_MDS.MEDIA_VALIDATION.getStatus(),
294 API_MDS.MEDIA_VALIDATION.getShortMessage(),
295 "explainRepresentationID")
296 );
297 addResponseDocToMethod(info, createResponseDoc(
298 API_MDS.DATACITE_PROBLEM.getStatus(),
299 API_MDS.DATACITE_PROBLEM.getShortMessage(),
300 "explainRepresentationID")
301 );
302 super.describePost(info);
303 }
304 }