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 java.util.Arrays;
22
23 import javax.xml.bind.JAXBException;
24 import javax.xml.bind.ValidationException;
25
26 import org.apache.logging.log4j.Level;
27 import org.datacite.schema.kernel_4.Resource;
28 import org.restlet.data.MediaType;
29 import org.restlet.data.Method;
30 import org.restlet.data.Status;
31 import org.restlet.ext.wadl.MethodInfo;
32 import org.restlet.ext.wadl.ParameterStyle;
33 import org.restlet.representation.Representation;
34 import org.restlet.resource.Post;
35 import org.xml.sax.SAXException;
36
37 import fr.cnes.doi.application.DoiMdsApplication.API_MDS;
38 import fr.cnes.doi.exception.ClientMdsException;
39 import fr.cnes.doi.exception.DoiServerException;
40 import fr.cnes.doi.utils.spec.Requirement;
41
42
43
44
45
46
47 public class MetadatasResource extends BaseMdsResource {
48
49
50
51
52 public static final String CREATE_METADATA = "Create Metadata";
53
54
55
56
57
58
59 @Override
60 protected void doInit() throws DoiServerException {
61 super.doInit();
62 LOG.traceEntry();
63 setDescription("This resource can create metadata");
64 LOG.traceExit();
65 }
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86 @Requirement(reqId = Requirement.DOI_SRV_010, reqName = Requirement.DOI_SRV_010_NAME)
87 @Requirement(reqId = Requirement.DOI_SRV_040, reqName = Requirement.DOI_SRV_040_NAME)
88 @Requirement(reqId = Requirement.DOI_MONIT_020, reqName = Requirement.DOI_MONIT_020_NAME)
89 @Requirement(reqId = Requirement.DOI_INTER_070, reqName = Requirement.DOI_INTER_070_NAME)
90 @Requirement(reqId = Requirement.DOI_AUTO_020, reqName = Requirement.DOI_AUTO_020_NAME)
91 @Requirement(reqId = Requirement.DOI_AUTO_030, reqName = Requirement.DOI_AUTO_030_NAME)
92 @Post
93 public String createMetadata(final Representation entity) throws DoiServerException {
94 LOG.traceEntry("Parameter\n\tentity:{}", entity);
95 checkInputs(entity);
96 final String result;
97 try {
98 setStatus(Status.SUCCESS_CREATED);
99 final Resource resource = createDataCiteResourceObject(entity);
100 final String selectedRole = extractSelectedRoleFromRequestIfExists();
101 checkPermission(resource.getIdentifier().getValue(), selectedRole);
102 resource.setPublisher("CNES");
103 result = this.getDoiApp().getClient().createMetadata(resource);
104 } catch (ClientMdsException ex) {
105 throw LOG.throwing(Level.ERROR,
106 new DoiServerException(getApplication(), API_MDS.DATACITE_PROBLEM, ex.
107 getMessage(), ex)
108 );
109 } catch (ValidationException ex) {
110 throw LOG.throwing(Level.ERROR,
111 new DoiServerException(getApplication(), API_MDS.METADATA_VALIDATION,
112 "invalid XML", ex)
113 );
114 } catch (JAXBException ex) {
115 throw LOG.throwing(Level.ERROR,
116 new DoiServerException(getApplication(), API_MDS.METADATA_VALIDATION,
117 "invalid XML", ex)
118 );
119 } catch (SAXException ex) {
120 throw LOG.throwing(Level.ERROR,
121 new DoiServerException(getApplication(), API_MDS.NETWORK_PROBLEM,
122 "DataCite schema not available", ex)
123 );
124 }
125 return LOG.traceExit(result);
126 }
127
128
129
130
131
132
133
134
135
136 @Requirement(reqId = Requirement.DOI_INTER_070, reqName = Requirement.DOI_INTER_070_NAME)
137 private void checkInputs(final Object obj) throws DoiServerException {
138 LOG.traceEntry("Parameter : " + obj);
139 if (isObjectNotExist(obj)) {
140 throw LOG.throwing(Level.ERROR,
141 new DoiServerException(getApplication(), API_MDS.METADATA_VALIDATION,
142 "Input is not set")
143 );
144 }
145 LOG.traceExit();
146 }
147
148
149
150
151
152
153
154
155
156
157
158 @Requirement(reqId = Requirement.DOI_INTER_060, reqName = Requirement.DOI_INTER_060_NAME)
159 @Requirement(reqId = Requirement.DOI_INTER_070, reqName = Requirement.DOI_INTER_070_NAME)
160 private Resource createDataCiteResourceObject(final Representation entity)
161 throws JAXBException, SAXException, ValidationException {
162 LOG.traceEntry("Parameter : " + entity);
163 return LOG.traceExit(this.getDoiApp().getClient().parseMetadata(entity));
164 }
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185 @Requirement(reqId = Requirement.DOI_DOC_010, reqName = Requirement.DOI_DOC_010_NAME)
186 @Override
187 protected final void describePost(final MethodInfo info) {
188 info.setName(Method.POST);
189 info.setDocumentation("This request stores new version of metadata. "
190 + "The request body must contain a valid XML.");
191
192 addRequestDocToMethod(info,
193 Arrays.asList(
194 createQueryParamDoc("selectedRole",
195 ParameterStyle.HEADER,
196 "A user can select one role when he is associated "
197 + "to several roles", false, "xs:string"
198 )
199 ),
200 createQueryRepresentationDoc("metadataRepresentation",
201 MediaType.APPLICATION_XML,
202 "DataCite metadata",
203 "default:Resource"
204 )
205 );
206
207 addResponseDocToMethod(info, createResponseDoc(API_MDS.CREATE_METADATA.getStatus(),
208 API_MDS.CREATE_METADATA.getShortMessage(),
209 "explainRepresentationID")
210 );
211 addResponseDocToMethod(info, createResponseDoc(API_MDS.METADATA_VALIDATION.getStatus(),
212 API_MDS.METADATA_VALIDATION.getShortMessage(),
213 "explainRepresentationID")
214 );
215 addResponseDocToMethod(info, createResponseDoc(API_MDS.DATACITE_PROBLEM.getStatus(),
216 API_MDS.DATACITE_PROBLEM.getShortMessage(),
217 "explainRepresentationID")
218 );
219 addResponseDocToMethod(info, createResponseDoc(API_MDS.NETWORK_PROBLEM.getStatus(),
220 API_MDS.NETWORK_PROBLEM.getShortMessage(),
221 "explainRepresentationID")
222 );
223 super.describePost(info);
224
225 }
226
227 }