1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package fr.cnes.doi.resource;
20
21 import fr.cnes.doi.application.AbstractApplication;
22 import fr.cnes.doi.utils.spec.Requirement;
23 import java.util.List;
24 import org.apache.logging.log4j.Logger;
25 import org.restlet.data.Form;
26 import org.restlet.data.MediaType;
27 import org.restlet.data.Status;
28 import org.restlet.ext.wadl.DocumentationInfo;
29 import org.restlet.ext.wadl.MethodInfo;
30 import org.restlet.ext.wadl.ParameterInfo;
31 import org.restlet.ext.wadl.ParameterStyle;
32 import org.restlet.ext.wadl.RepresentationInfo;
33 import org.restlet.ext.wadl.RequestInfo;
34 import org.restlet.ext.wadl.ResponseInfo;
35 import org.restlet.ext.wadl.WadlServerResource;
36 import org.restlet.resource.ResourceException;
37
38
39
40
41
42
43
44
45
46
47 @Requirement(reqId = Requirement.DOI_DOC_010, reqName = Requirement.DOI_DOC_010_NAME)
48 public abstract class AbstractResource extends WadlServerResource {
49
50
51
52
53 private volatile Logger LOG;
54
55
56
57
58
59
60 @Override
61 protected void doInit() throws ResourceException {
62 super.doInit();
63 this.LOG = ((AbstractApplication) getApplication()).getLog();
64 }
65
66
67
68
69
70
71
72
73
74 public boolean isValueExist(final Form form,
75 final String parameterName) {
76 LOG.traceEntry("Parameters : {} and {}", form, parameterName);
77 final boolean result;
78 if (isObjectNotExist(form)) {
79 result = false;
80 } else {
81 result = form.getFirstValue(parameterName) != null;
82 }
83 return LOG.traceExit(result);
84 }
85
86
87
88
89
90
91
92
93
94 public boolean isValueNotExist(final Form form,
95 final String parameterName) {
96 LOG.traceEntry("Parameters : {} and {}", form, parameterName);
97 return LOG.traceExit(!isValueExist(form, parameterName));
98 }
99
100
101
102
103
104
105
106 public boolean isObjectExist(final Object obj) {
107 LOG.traceEntry("Parameter : {}", obj);
108 return LOG.traceExit(obj != null);
109 }
110
111
112
113
114
115
116
117 public boolean isObjectNotExist(final Object obj) {
118 LOG.traceEntry("Parameter : {}", obj);
119 return LOG.traceExit(!isObjectExist(obj));
120 }
121
122
123
124
125
126
127
128 protected void addRequestDocToMethod(final MethodInfo info,
129 final ParameterInfo param) {
130 final RequestInfo request = new RequestInfo();
131 request.getParameters().add(param);
132 info.setRequest(request);
133 }
134
135
136
137
138
139
140
141 protected void addRequestDocToMethod(final MethodInfo info,
142 final List<ParameterInfo> params) {
143 final RequestInfo request = new RequestInfo();
144 for (final ParameterInfo param : params) {
145 request.getParameters().add(param);
146 }
147 info.setRequest(request);
148 }
149
150
151
152
153
154
155
156
157 protected void addRequestDocToMethod(
158 final MethodInfo info,
159 final List<ParameterInfo> params,
160 final RepresentationInfo rep) {
161 addRequestDocToMethod(info, params);
162 info.getRequest().getRepresentations().add(rep);
163 }
164
165
166
167
168
169
170
171 protected void addResponseDocToMethod(final MethodInfo info,
172 final ResponseInfo response) {
173 info.getResponses().add(response);
174 }
175
176
177
178
179
180
181
182
183 protected ResponseInfo createResponseDoc(final Status status,
184 final String doc) {
185 final ResponseInfo responseInfo = new ResponseInfo();
186 responseInfo.getStatuses().add(status);
187 responseInfo.setDocumentation(doc);
188 return responseInfo;
189 }
190
191
192
193
194
195
196
197
198
199 protected ResponseInfo createResponseDoc(
200 final Status status,
201 final String doc,
202 final String refRepresentation) {
203 final ResponseInfo response = createResponseDoc(status, doc);
204 final RepresentationInfo rep = new RepresentationInfo();
205 rep.setReference(refRepresentation);
206 response.getRepresentations().add(rep);
207 return response;
208 }
209
210
211
212
213
214
215
216
217
218 protected ResponseInfo createResponseDoc(
219 final Status status,
220 final String doc,
221 final RepresentationInfo representation) {
222 final ResponseInfo response = createResponseDoc(status, doc);
223 response.getRepresentations().add(representation);
224 return response;
225 }
226
227
228
229
230
231
232
233
234
235
236
237 protected ParameterInfo createQueryParamDoc(
238 final String name,
239 final ParameterStyle style,
240 final String doc,
241 final boolean isRequired,
242 final String datatype) {
243 final ParameterInfo param = new ParameterInfo();
244 param.setName(name);
245 param.setStyle(style);
246 param.setDocumentation(doc);
247 param.setRequired(isRequired);
248 param.setType(datatype);
249 return param;
250 }
251
252
253
254
255
256
257
258
259
260
261 protected RepresentationInfo createQueryRepresentationDoc(
262 final String identifier,
263 final MediaType mediaType,
264 final String doc,
265 final String xmlElement) {
266 final RepresentationInfo rep = new RepresentationInfo();
267 rep.setIdentifier(identifier);
268 rep.setMediaType(mediaType);
269 rep.setDocumentation(doc);
270 rep.setXmlElement(xmlElement);
271 return rep;
272 }
273
274
275
276
277
278
279 protected RepresentationInfo stringRepresentation() {
280 final RepresentationInfo repInfo = new RepresentationInfo();
281 repInfo.setMediaType(MediaType.TEXT_PLAIN);
282 final DocumentationInfo docInfo = new DocumentationInfo();
283 docInfo.setTitle("String Representation");
284 docInfo.setTextContent("The representation contains a simple string.");
285 repInfo.setDocumentation(docInfo);
286 return repInfo;
287 }
288
289
290
291
292
293
294 protected RepresentationInfo htmlRepresentation() {
295 final RepresentationInfo repInfo = new RepresentationInfo();
296 repInfo.setMediaType(MediaType.TEXT_HTML);
297 final DocumentationInfo docInfo = new DocumentationInfo();
298 docInfo.setTitle("HTML Representation");
299 docInfo.setTextContent("The representation contains the HTML representation.");
300 repInfo.setDocumentation(docInfo);
301 return repInfo;
302 }
303
304 }