~ubuntu-branches/ubuntu/saucy/restlet/saucy

« back to all changes in this revision

Viewing changes to org.restlet.ext.wadl/src/org/restlet/ext/wadl/ResourceInfo.java

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2012-06-11 16:25:45 UTC
  • Revision ID: package-import@ubuntu.com-20120611162545-5w2o0resi5y3pybc
Tags: upstream-2.0.14
ImportĀ upstreamĀ versionĀ 2.0.14

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * Copyright 2005-2012 Restlet S.A.S.
 
3
 * 
 
4
 * The contents of this file are subject to the terms of one of the following
 
5
 * open source licenses: Apache 2.0 or LGPL 3.0 or LGPL 2.1 or CDDL 1.0 or EPL
 
6
 * 1.0 (the "Licenses"). You can select the license that you prefer but you may
 
7
 * not use this file except in compliance with one of these Licenses.
 
8
 * 
 
9
 * You can obtain a copy of the Apache 2.0 license at
 
10
 * http://www.opensource.org/licenses/apache-2.0
 
11
 * 
 
12
 * You can obtain a copy of the LGPL 3.0 license at
 
13
 * http://www.opensource.org/licenses/lgpl-3.0
 
14
 * 
 
15
 * You can obtain a copy of the LGPL 2.1 license at
 
16
 * http://www.opensource.org/licenses/lgpl-2.1
 
17
 * 
 
18
 * You can obtain a copy of the CDDL 1.0 license at
 
19
 * http://www.opensource.org/licenses/cddl1
 
20
 * 
 
21
 * You can obtain a copy of the EPL 1.0 license at
 
22
 * http://www.opensource.org/licenses/eclipse-1.0
 
23
 * 
 
24
 * See the Licenses for the specific language governing permissions and
 
25
 * limitations under the Licenses.
 
26
 * 
 
27
 * Alternatively, you can obtain a royalty free commercial license with less
 
28
 * limitations, transferable or non-transferable, directly at
 
29
 * http://www.restlet.com/products/restlet-framework
 
30
 * 
 
31
 * Restlet is a registered trademark of Restlet S.A.S.
 
32
 */
 
33
 
 
34
package org.restlet.ext.wadl;
 
35
 
 
36
import static org.restlet.ext.wadl.WadlRepresentation.APP_NAMESPACE;
 
37
 
 
38
import java.util.ArrayList;
 
39
import java.util.Iterator;
 
40
import java.util.List;
 
41
import java.util.Map;
 
42
 
 
43
import org.restlet.data.MediaType;
 
44
import org.restlet.data.Method;
 
45
import org.restlet.data.Reference;
 
46
import org.restlet.ext.xml.XmlWriter;
 
47
import org.restlet.representation.Variant;
 
48
import org.restlet.resource.Directory;
 
49
import org.restlet.resource.ServerResource;
 
50
import org.xml.sax.SAXException;
 
51
import org.xml.sax.helpers.AttributesImpl;
 
52
 
 
53
/**
 
54
 * Describes a class of closely related resources.
 
55
 * 
 
56
 * @author Jerome Louvel
 
57
 */
 
58
public class ResourceInfo extends DocumentedInfo {
 
59
 
 
60
    /**
 
61
     * Returns a WADL description of the current resource.
 
62
     * 
 
63
     * @param applicationInfo
 
64
     *            The parent application.
 
65
     * @param resource
 
66
     *            The resource to describe.
 
67
     * @param path
 
68
     *            Path of the current resource.
 
69
     * @param info
 
70
     *            WADL description of the current resource to update.
 
71
     */
 
72
    @SuppressWarnings("deprecation")
 
73
    public static void describe(ApplicationInfo applicationInfo,
 
74
            ResourceInfo info, Object resource, String path) {
 
75
        if ((path != null) && path.startsWith("/")) {
 
76
            path = path.substring(1);
 
77
        }
 
78
 
 
79
        info.setPath(path);
 
80
 
 
81
        // Introspect the current resource to detect the allowed methods
 
82
        List<Method> methodsList = new ArrayList<Method>();
 
83
 
 
84
        if (resource instanceof ServerResource) {
 
85
            ((ServerResource) resource).updateAllowedMethods();
 
86
            methodsList.addAll(((ServerResource) resource).getAllowedMethods());
 
87
 
 
88
            if (resource instanceof WadlServerResource) {
 
89
                info.setParameters(((WadlServerResource) resource)
 
90
                        .describeParameters());
 
91
 
 
92
                if (applicationInfo != null) {
 
93
                    ((WadlServerResource) resource).describe(applicationInfo);
 
94
                }
 
95
            }
 
96
        } else if (resource instanceof org.restlet.resource.Resource) {
 
97
            methodsList.addAll(((org.restlet.resource.Resource) resource)
 
98
                    .getAllowedMethods());
 
99
 
 
100
            if (resource instanceof WadlResource) {
 
101
                info.setParameters(((WadlResource) resource)
 
102
                        .getParametersInfo());
 
103
            }
 
104
        } else if (resource instanceof Directory) {
 
105
            Directory directory = (Directory) resource;
 
106
            methodsList.add(Method.GET);
 
107
 
 
108
            if (directory.isModifiable()) {
 
109
                methodsList.add(Method.DELETE);
 
110
                methodsList.add(Method.PUT);
 
111
            }
 
112
        }
 
113
 
 
114
        Method.sort(methodsList);
 
115
 
 
116
        // Update the resource info with the description of the allowed methods
 
117
        List<MethodInfo> methods = info.getMethods();
 
118
        MethodInfo methodInfo;
 
119
 
 
120
        for (Method method : methodsList) {
 
121
            methodInfo = new MethodInfo();
 
122
            methods.add(methodInfo);
 
123
            methodInfo.setName(method);
 
124
 
 
125
            if (resource instanceof ServerResource) {
 
126
                if (resource instanceof WadlServerResource) {
 
127
                    WadlServerResource wsResource = (WadlServerResource) resource;
 
128
 
 
129
                    if (wsResource.canDescribe(method)) {
 
130
                        wsResource.describeMethod(method, methodInfo);
 
131
                    }
 
132
                } else {
 
133
                    MethodInfo.describeAnnotations(methodInfo,
 
134
                            (ServerResource) resource);
 
135
                }
 
136
            } else if (resource instanceof org.restlet.resource.Resource) {
 
137
                if (resource instanceof WadlResource) {
 
138
                    WadlResource wsResource = (WadlResource) resource;
 
139
 
 
140
                    if (wsResource.isDescribable(method)) {
 
141
                        wsResource.describeMethod(method, methodInfo);
 
142
                    }
 
143
                } else {
 
144
                    // Can document the list of supported variants.
 
145
                    if (Method.GET.equals(method)) {
 
146
                        ResponseInfo responseInfo = null;
 
147
 
 
148
                        for (Variant variant : ((org.restlet.resource.Resource) resource)
 
149
                                .getVariants()) {
 
150
                            RepresentationInfo representationInfo = new RepresentationInfo();
 
151
                            representationInfo.setMediaType(variant
 
152
                                    .getMediaType());
 
153
 
 
154
                            if (responseInfo == null) {
 
155
                                responseInfo = new ResponseInfo();
 
156
                                methodInfo.getResponses().add(responseInfo);
 
157
                            }
 
158
 
 
159
                            responseInfo.getRepresentations().add(
 
160
                                    representationInfo);
 
161
                        }
 
162
                    }
 
163
                }
 
164
            }
 
165
        }
 
166
 
 
167
        // Document the resource
 
168
        String title = null;
 
169
        String textContent = null;
 
170
 
 
171
        if (resource instanceof WadlServerResource) {
 
172
            title = ((WadlServerResource) resource).getName();
 
173
            textContent = ((WadlServerResource) resource).getDescription();
 
174
        } else if (resource instanceof WadlResource) {
 
175
            title = ((WadlResource) resource).getTitle();
 
176
        }
 
177
 
 
178
        if ((title != null) && !"".equals(title)) {
 
179
            DocumentationInfo doc = null;
 
180
 
 
181
            if (info.getDocumentations().isEmpty()) {
 
182
                doc = new DocumentationInfo();
 
183
                info.getDocumentations().add(doc);
 
184
            } else {
 
185
                info.getDocumentations().get(0);
 
186
            }
 
187
 
 
188
            doc.setTitle(title);
 
189
            doc.setTextContent(textContent);
 
190
        }
 
191
    }
 
192
 
 
193
    /** List of child resources. */
 
194
    private List<ResourceInfo> childResources;
 
195
 
 
196
    /** Identifier for that element. */
 
197
    private String identifier;
 
198
 
 
199
    /** List of supported methods. */
 
200
    private List<MethodInfo> methods;
 
201
 
 
202
    /** List of parameters. */
 
203
    private List<ParameterInfo> parameters;
 
204
 
 
205
    /** URI template for the identifier of the resource. */
 
206
    private String path;
 
207
 
 
208
    /** Media type for the query component of the resource URI. */
 
209
    private MediaType queryType;
 
210
 
 
211
    /** List of references to resource type elements. */
 
212
    private List<Reference> type;
 
213
 
 
214
    /**
 
215
     * Constructor.
 
216
     */
 
217
    public ResourceInfo() {
 
218
        super();
 
219
    }
 
220
 
 
221
    /**
 
222
     * Constructor with a single documentation element.
 
223
     * 
 
224
     * @param documentation
 
225
     *            A single documentation element.
 
226
     */
 
227
    public ResourceInfo(DocumentationInfo documentation) {
 
228
        super(documentation);
 
229
    }
 
230
 
 
231
    /**
 
232
     * Constructor with a list of documentation elements.
 
233
     * 
 
234
     * @param documentations
 
235
     *            The list of documentation elements.
 
236
     */
 
237
    public ResourceInfo(List<DocumentationInfo> documentations) {
 
238
        super(documentations);
 
239
    }
 
240
 
 
241
    /**
 
242
     * Constructor with a single documentation element.
 
243
     * 
 
244
     * @param documentation
 
245
     *            A single documentation element.
 
246
     */
 
247
    public ResourceInfo(String documentation) {
 
248
        super(documentation);
 
249
    }
 
250
 
 
251
    /**
 
252
     * Creates an application descriptor that wraps this resource descriptor.
 
253
     * The title of the resource, that is to say the title of its first
 
254
     * documentation tag is transfered to the title of the first documentation
 
255
     * tag of the main application tag.
 
256
     * 
 
257
     * @return The new application descriptor.
 
258
     */
 
259
    public ApplicationInfo createApplication() {
 
260
        ApplicationInfo result = new ApplicationInfo();
 
261
 
 
262
        if (!getDocumentations().isEmpty()) {
 
263
            String titleResource = getDocumentations().get(0).getTitle();
 
264
            if (titleResource != null && !"".equals(titleResource)) {
 
265
                DocumentationInfo doc = null;
 
266
 
 
267
                if (result.getDocumentations().isEmpty()) {
 
268
                    doc = new DocumentationInfo();
 
269
                    result.getDocumentations().add(doc);
 
270
                } else {
 
271
                    doc = result.getDocumentations().get(0);
 
272
                }
 
273
 
 
274
                doc.setTitle(titleResource);
 
275
            }
 
276
        }
 
277
 
 
278
        ResourcesInfo resources = new ResourcesInfo();
 
279
        result.setResources(resources);
 
280
        resources.getResources().add(this);
 
281
        return result;
 
282
    }
 
283
 
 
284
    /**
 
285
     * Returns the list of child resources.
 
286
     * 
 
287
     * @return The list of child resources.
 
288
     */
 
289
    public List<ResourceInfo> getChildResources() {
 
290
        // Lazy initialization with double-check.
 
291
        List<ResourceInfo> r = this.childResources;
 
292
        if (r == null) {
 
293
            synchronized (this) {
 
294
                r = this.childResources;
 
295
                if (r == null) {
 
296
                    this.childResources = r = new ArrayList<ResourceInfo>();
 
297
                }
 
298
            }
 
299
        }
 
300
        return r;
 
301
    }
 
302
 
 
303
    /**
 
304
     * Returns the identifier for that element.
 
305
     * 
 
306
     * @return The identifier for that element.
 
307
     */
 
308
    public String getIdentifier() {
 
309
        return this.identifier;
 
310
    }
 
311
 
 
312
    /**
 
313
     * Returns the list of supported methods.
 
314
     * 
 
315
     * @return The list of supported methods.
 
316
     */
 
317
    public List<MethodInfo> getMethods() {
 
318
        // Lazy initialization with double-check.
 
319
        List<MethodInfo> m = this.methods;
 
320
        if (m == null) {
 
321
            synchronized (this) {
 
322
                m = this.methods;
 
323
 
 
324
                if (m == null) {
 
325
                    this.methods = m = new ArrayList<MethodInfo>();
 
326
                }
 
327
            }
 
328
        }
 
329
        return m;
 
330
    }
 
331
 
 
332
    /**
 
333
     * Returns the list of parameters.
 
334
     * 
 
335
     * @return The list of parameters.
 
336
     */
 
337
    public List<ParameterInfo> getParameters() {
 
338
        // Lazy initialization with double-check.
 
339
        List<ParameterInfo> p = this.parameters;
 
340
        if (p == null) {
 
341
            synchronized (this) {
 
342
                p = this.parameters;
 
343
                if (p == null) {
 
344
                    this.parameters = p = new ArrayList<ParameterInfo>();
 
345
                }
 
346
            }
 
347
        }
 
348
        return p;
 
349
    }
 
350
 
 
351
    /**
 
352
     * Returns the URI template for the identifier of the resource.
 
353
     * 
 
354
     * @return The URI template for the identifier of the resource.
 
355
     */
 
356
    public String getPath() {
 
357
        return this.path;
 
358
    }
 
359
 
 
360
    /**
 
361
     * Returns the media type for the query component of the resource URI.
 
362
     * 
 
363
     * @return The media type for the query component of the resource URI.
 
364
     */
 
365
    public MediaType getQueryType() {
 
366
        return this.queryType;
 
367
    }
 
368
 
 
369
    /**
 
370
     * Returns the list of references to resource type elements.
 
371
     * 
 
372
     * @return The list of references to resource type elements.
 
373
     */
 
374
    public List<Reference> getType() {
 
375
        // Lazy initialization with double-check.
 
376
        List<Reference> t = this.type;
 
377
        if (t == null) {
 
378
            synchronized (this) {
 
379
                t = this.type;
 
380
                if (t == null) {
 
381
                    this.type = t = new ArrayList<Reference>();
 
382
                }
 
383
            }
 
384
        }
 
385
        return t;
 
386
    }
 
387
 
 
388
    /**
 
389
     * Sets the list of child resources.
 
390
     * 
 
391
     * @param resources
 
392
     *            The list of child resources.
 
393
     */
 
394
    public void setChildResources(List<ResourceInfo> resources) {
 
395
        this.childResources = resources;
 
396
    }
 
397
 
 
398
    /**
 
399
     * Sets the identifier for that element.
 
400
     * 
 
401
     * @param identifier
 
402
     *            The identifier for that element.
 
403
     */
 
404
    public void setIdentifier(String identifier) {
 
405
        this.identifier = identifier;
 
406
    }
 
407
 
 
408
    /**
 
409
     * Sets the list of supported methods.
 
410
     * 
 
411
     * @param methods
 
412
     *            The list of supported methods.
 
413
     */
 
414
    public void setMethods(List<MethodInfo> methods) {
 
415
        this.methods = methods;
 
416
    }
 
417
 
 
418
    /**
 
419
     * Sets the list of parameters.
 
420
     * 
 
421
     * @param parameters
 
422
     *            The list of parameters.
 
423
     */
 
424
    public void setParameters(List<ParameterInfo> parameters) {
 
425
        this.parameters = parameters;
 
426
    }
 
427
 
 
428
    /**
 
429
     * Sets the URI template for the identifier of the resource.
 
430
     * 
 
431
     * @param path
 
432
     *            The URI template for the identifier of the resource.
 
433
     */
 
434
    public void setPath(String path) {
 
435
        this.path = path;
 
436
    }
 
437
 
 
438
    /**
 
439
     * Sets the media type for the query component of the resource URI.
 
440
     * 
 
441
     * @param queryType
 
442
     *            The media type for the query component of the resource URI.
 
443
     */
 
444
    public void setQueryType(MediaType queryType) {
 
445
        this.queryType = queryType;
 
446
    }
 
447
 
 
448
    /**
 
449
     * Sets the list of references to resource type elements.
 
450
     * 
 
451
     * @param type
 
452
     *            The list of references to resource type elements.
 
453
     */
 
454
    public void setType(List<Reference> type) {
 
455
        this.type = type;
 
456
    }
 
457
 
 
458
    @Override
 
459
    public void updateNamespaces(Map<String, String> namespaces) {
 
460
        namespaces.putAll(resolveNamespaces());
 
461
 
 
462
        for (final ParameterInfo parameterInfo : getParameters()) {
 
463
            parameterInfo.updateNamespaces(namespaces);
 
464
        }
 
465
        for (final ResourceInfo resourceInfo : getChildResources()) {
 
466
            resourceInfo.updateNamespaces(namespaces);
 
467
        }
 
468
        for (final MethodInfo methodInfo : getMethods()) {
 
469
            methodInfo.updateNamespaces(namespaces);
 
470
        }
 
471
    }
 
472
 
 
473
    /**
 
474
     * Writes the current object as an XML element using the given SAX writer.
 
475
     * 
 
476
     * @param writer
 
477
     *            The SAX writer.
 
478
     * @throws SAXException
 
479
     */
 
480
    public void writeElement(XmlWriter writer) throws SAXException {
 
481
        final AttributesImpl attributes = new AttributesImpl();
 
482
        if ((getIdentifier() != null) && !getIdentifier().equals("")) {
 
483
            attributes.addAttribute("", "id", null, "xs:ID", getIdentifier());
 
484
        }
 
485
 
 
486
        if ((getPath() != null) && !getPath().equals("")) {
 
487
            attributes.addAttribute("", "path", null, "xs:string", getPath());
 
488
        }
 
489
 
 
490
        if (getQueryType() != null) {
 
491
            attributes.addAttribute("", "queryType", null, "xs:string",
 
492
                    getQueryType().getMainType());
 
493
        }
 
494
        if ((getType() != null) && !getType().isEmpty()) {
 
495
            final StringBuilder builder = new StringBuilder();
 
496
            for (final Iterator<Reference> iterator = getType().iterator(); iterator
 
497
                    .hasNext();) {
 
498
                final Reference reference = iterator.next();
 
499
                builder.append(reference.toString());
 
500
                if (iterator.hasNext()) {
 
501
                    builder.append(" ");
 
502
                }
 
503
            }
 
504
            attributes.addAttribute("", "type", null, "xs:string", builder
 
505
                    .toString());
 
506
        }
 
507
 
 
508
        if (getChildResources().isEmpty() && getDocumentations().isEmpty()
 
509
                && getMethods().isEmpty() && getParameters().isEmpty()) {
 
510
            writer.emptyElement(APP_NAMESPACE, "resource", null, attributes);
 
511
        } else {
 
512
            writer.startElement(APP_NAMESPACE, "resource", null, attributes);
 
513
 
 
514
            for (final ResourceInfo resourceInfo : getChildResources()) {
 
515
                resourceInfo.writeElement(writer);
 
516
            }
 
517
 
 
518
            for (final DocumentationInfo documentationInfo : getDocumentations()) {
 
519
                documentationInfo.writeElement(writer);
 
520
            }
 
521
 
 
522
            for (final ParameterInfo parameterInfo : getParameters()) {
 
523
                parameterInfo.writeElement(writer);
 
524
            }
 
525
 
 
526
            for (final MethodInfo methodInfo : getMethods()) {
 
527
                methodInfo.writeElement(writer);
 
528
            }
 
529
 
 
530
            writer.endElement(APP_NAMESPACE, "resource");
 
531
        }
 
532
    }
 
533
 
 
534
}