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

« back to all changes in this revision

Viewing changes to org.restlet.ext.atom/src/org/restlet/ext/atom/Service.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.atom;
 
35
 
 
36
import static org.restlet.ext.atom.Feed.ATOM_NAMESPACE;
 
37
 
 
38
import java.io.IOException;
 
39
import java.util.ArrayList;
 
40
import java.util.List;
 
41
 
 
42
import org.restlet.Client;
 
43
import org.restlet.Context;
 
44
import org.restlet.Request;
 
45
import org.restlet.data.MediaType;
 
46
import org.restlet.data.Method;
 
47
import org.restlet.data.Reference;
 
48
import org.restlet.data.Status;
 
49
import org.restlet.ext.atom.internal.ServiceContentReader;
 
50
import org.restlet.ext.xml.SaxRepresentation;
 
51
import org.restlet.ext.xml.XmlWriter;
 
52
import org.restlet.representation.Representation;
 
53
import org.xml.sax.SAXException;
 
54
 
 
55
/**
 
56
 * Represents an Atom introspection document.
 
57
 * 
 
58
 * @author Jerome Louvel
 
59
 */
 
60
public class Service extends SaxRepresentation {
 
61
    /** Atom Publishing Protocol namespace. */
 
62
    public static final String APP_NAMESPACE = "http://www.w3.org/2007/app";
 
63
 
 
64
    /**
 
65
     * The base reference used to resolve relative references found within the
 
66
     * scope of the xml:base attribute.
 
67
     */
 
68
    private volatile Reference baseReference;
 
69
 
 
70
    /**
 
71
     * The client HTTP dispatcher.
 
72
     */
 
73
    private volatile Client clientDispatcher;
 
74
 
 
75
    /**
 
76
     * The reference.
 
77
     */
 
78
    private volatile Reference reference;
 
79
 
 
80
    /**
 
81
     * The list of workspaces.
 
82
     */
 
83
    private volatile List<Workspace> workspaces;
 
84
 
 
85
    /**
 
86
     * Constructor.
 
87
     * 
 
88
     * @param clientDispatcher
 
89
     *            The client HTTP dispatcher.
 
90
     */
 
91
    public Service(Client clientDispatcher) {
 
92
        super(new MediaType("***"));
 
93
        setNamespaceAware(true);
 
94
        this.clientDispatcher = clientDispatcher;
 
95
    }
 
96
 
 
97
    /**
 
98
     * Constructor.
 
99
     * 
 
100
     * @param clientDispatcher
 
101
     *            The client HTTP dispatcher.
 
102
     * @param serviceUri
 
103
     *            The service URI.
 
104
     * @throws IOException
 
105
     */
 
106
    public Service(Client clientDispatcher, String serviceUri)
 
107
            throws IOException {
 
108
        this(clientDispatcher, serviceUri, clientDispatcher.handle(
 
109
                new Request(Method.GET, serviceUri)).getEntity());
 
110
    }
 
111
 
 
112
    /**
 
113
     * Constructor.
 
114
     * 
 
115
     * @param clientDispatcher
 
116
     *            The client HTTP dispatcher.
 
117
     * @param serviceUri
 
118
     *            The service URI.
 
119
     * @param xmlService
 
120
     *            The XML introspection document.
 
121
     * @throws IOException
 
122
     */
 
123
    public Service(Client clientDispatcher, String serviceUri,
 
124
            Representation xmlService) throws IOException {
 
125
        super(xmlService);
 
126
        setNamespaceAware(true);
 
127
        this.clientDispatcher = clientDispatcher;
 
128
        this.reference = (serviceUri == null) ? null
 
129
                : new Reference(serviceUri);
 
130
        parse(new ServiceContentReader(this));
 
131
    }
 
132
 
 
133
    /**
 
134
     * Constructor.
 
135
     * 
 
136
     * @param context
 
137
     *            The context from which the client dispatcher will be
 
138
     *            retrieved.
 
139
     * @param serviceUri
 
140
     *            The service URI.
 
141
     * @throws IOException
 
142
     */
 
143
    public Service(Context context, String serviceUri) throws IOException {
 
144
        this(context.getClientDispatcher(), serviceUri, context
 
145
                .getClientDispatcher().handle(
 
146
                        new Request(Method.GET, serviceUri)).getEntity());
 
147
    }
 
148
 
 
149
    /**
 
150
     * Constructor.
 
151
     * 
 
152
     * @param xmlService
 
153
     *            The XML introspection document.
 
154
     * @throws IOException
 
155
     */
 
156
    public Service(Representation xmlService) throws IOException {
 
157
        this(null, null, xmlService);
 
158
    }
 
159
 
 
160
    /**
 
161
     * Constructor.
 
162
     * 
 
163
     * @param serviceUri
 
164
     *            The service URI.
 
165
     * @throws IOException
 
166
     */
 
167
    public Service(String serviceUri) throws IOException {
 
168
        this(new Client(new Reference(serviceUri).getSchemeProtocol()),
 
169
                serviceUri);
 
170
    }
 
171
 
 
172
    /**
 
173
     * Constructor.
 
174
     * 
 
175
     * @param serviceUri
 
176
     *            The service URI.
 
177
     * @param xmlService
 
178
     *            The XML introspection document.
 
179
     * @throws IOException
 
180
     */
 
181
    public Service(String serviceUri, Representation xmlService)
 
182
            throws IOException {
 
183
        this(new Client(new Reference(serviceUri).getSchemeProtocol()),
 
184
                serviceUri, xmlService);
 
185
    }
 
186
 
 
187
    /**
 
188
     * Deletes a resource.
 
189
     * 
 
190
     * @param uri
 
191
     *            The resource URI.
 
192
     * @return The result status.
 
193
     */
 
194
    public Status deleteResource(String uri) {
 
195
        return getClientDispatcher().handle(new Request(Method.DELETE, uri))
 
196
                .getStatus();
 
197
    }
 
198
 
 
199
    /**
 
200
     * Returns the base reference used to resolve relative references found
 
201
     * within the scope of the xml:base attribute.
 
202
     * 
 
203
     * @return The base reference used to resolve relative references found
 
204
     *         within the scope of the xml:base attribute.
 
205
     */
 
206
    public Reference getBaseReference() {
 
207
        return baseReference;
 
208
    }
 
209
 
 
210
    /**
 
211
     * Returns the client HTTP dispatcher.
 
212
     * 
 
213
     * @return The client HTTP dispatcher.
 
214
     */
 
215
    public Client getClientDispatcher() {
 
216
        return this.clientDispatcher;
 
217
    }
 
218
 
 
219
    /**
 
220
     * Returns the hypertext reference.
 
221
     * 
 
222
     * @return The hypertext reference.
 
223
     */
 
224
    public Reference getReference() {
 
225
        return this.reference;
 
226
    }
 
227
 
 
228
    /**
 
229
     * Retrieves a resource representation.
 
230
     * 
 
231
     * @param uri
 
232
     *            The resource URI.
 
233
     * @return The resource representation.
 
234
     */
 
235
    public Representation getResource(String uri) {
 
236
        return getClientDispatcher().handle(new Request(Method.GET, uri))
 
237
                .getEntity();
 
238
    }
 
239
 
 
240
    /**
 
241
     * Returns the list of workspaces.
 
242
     * 
 
243
     * @return The list of workspaces.
 
244
     */
 
245
    public List<Workspace> getWorkspaces() {
 
246
        if (this.workspaces == null) {
 
247
            this.workspaces = new ArrayList<Workspace>();
 
248
        }
 
249
 
 
250
        return this.workspaces;
 
251
    }
 
252
 
 
253
    /**
 
254
     * Sets the base reference used to resolve relative references found within
 
255
     * the scope of the xml:base attribute.
 
256
     * 
 
257
     * @param baseReference
 
258
     *            The base reference used to resolve relative references found
 
259
     *            within the scope of the xml:base attribute.
 
260
     */
 
261
    public void setBaseReference(Reference baseReference) {
 
262
        this.baseReference = baseReference;
 
263
    }
 
264
 
 
265
    /**
 
266
     * Sets the client HTTP dispatcher.
 
267
     * 
 
268
     * @param clientDispatcher
 
269
     *            The client HTTP dispatcher.
 
270
     */
 
271
    public void setClientDispatcher(Client clientDispatcher) {
 
272
        this.clientDispatcher = clientDispatcher;
 
273
    }
 
274
 
 
275
    /**
 
276
     * Sets the hypertext reference.
 
277
     * 
 
278
     * @param ref
 
279
     *            The hypertext reference.
 
280
     */
 
281
    public void setReference(Reference ref) {
 
282
        this.reference = ref;
 
283
    }
 
284
 
 
285
    /**
 
286
     * Updates a resource representation.
 
287
     * 
 
288
     * @param uri
 
289
     *            The resource URI.
 
290
     * @return The resource representation.
 
291
     */
 
292
    public Status updateResource(String uri,
 
293
            Representation updatedRepresentation) {
 
294
        return getClientDispatcher().handle(
 
295
                new Request(Method.PUT, uri, updatedRepresentation))
 
296
                .getStatus();
 
297
    }
 
298
 
 
299
    /**
 
300
     * Writes the representation to a XML writer.
 
301
     * 
 
302
     * @param writer
 
303
     *            The XML writer to write to.
 
304
     * @throws IOException
 
305
     */
 
306
    @Override
 
307
    public void write(XmlWriter writer) throws IOException {
 
308
        try {
 
309
            writer.forceNSDecl(APP_NAMESPACE, "");
 
310
            writer.forceNSDecl(ATOM_NAMESPACE, "atom");
 
311
            writer.setDataFormat(true);
 
312
            writer.setIndentStep(3);
 
313
            writer.startDocument();
 
314
            writer.startElement(APP_NAMESPACE, "service");
 
315
 
 
316
            for (final Workspace workspace : getWorkspaces()) {
 
317
                workspace.writeElement(writer);
 
318
            }
 
319
 
 
320
            writer.endElement(APP_NAMESPACE, "service");
 
321
            writer.endDocument();
 
322
        } catch (SAXException se) {
 
323
            throw new IOException("Couldn't write the service representation: "
 
324
                    + se.getMessage());
 
325
        }
 
326
    }
 
327
 
 
328
}