2
* Copyright 2005-2012 Restlet S.A.S.
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.
9
* You can obtain a copy of the Apache 2.0 license at
10
* http://www.opensource.org/licenses/apache-2.0
12
* You can obtain a copy of the LGPL 3.0 license at
13
* http://www.opensource.org/licenses/lgpl-3.0
15
* You can obtain a copy of the LGPL 2.1 license at
16
* http://www.opensource.org/licenses/lgpl-2.1
18
* You can obtain a copy of the CDDL 1.0 license at
19
* http://www.opensource.org/licenses/cddl1
21
* You can obtain a copy of the EPL 1.0 license at
22
* http://www.opensource.org/licenses/eclipse-1.0
24
* See the Licenses for the specific language governing permissions and
25
* limitations under the Licenses.
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
31
* Restlet is a registered trademark of Restlet S.A.S.
34
package org.restlet.ext.atom;
36
import static org.restlet.ext.atom.Feed.ATOM_NAMESPACE;
38
import java.io.IOException;
39
import java.util.ArrayList;
40
import java.util.List;
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;
56
* Represents an Atom introspection document.
58
* @author Jerome Louvel
60
public class Service extends SaxRepresentation {
61
/** Atom Publishing Protocol namespace. */
62
public static final String APP_NAMESPACE = "http://www.w3.org/2007/app";
65
* The base reference used to resolve relative references found within the
66
* scope of the xml:base attribute.
68
private volatile Reference baseReference;
71
* The client HTTP dispatcher.
73
private volatile Client clientDispatcher;
78
private volatile Reference reference;
81
* The list of workspaces.
83
private volatile List<Workspace> workspaces;
88
* @param clientDispatcher
89
* The client HTTP dispatcher.
91
public Service(Client clientDispatcher) {
92
super(new MediaType("***"));
93
setNamespaceAware(true);
94
this.clientDispatcher = clientDispatcher;
100
* @param clientDispatcher
101
* The client HTTP dispatcher.
104
* @throws IOException
106
public Service(Client clientDispatcher, String serviceUri)
108
this(clientDispatcher, serviceUri, clientDispatcher.handle(
109
new Request(Method.GET, serviceUri)).getEntity());
115
* @param clientDispatcher
116
* The client HTTP dispatcher.
120
* The XML introspection document.
121
* @throws IOException
123
public Service(Client clientDispatcher, String serviceUri,
124
Representation xmlService) throws IOException {
126
setNamespaceAware(true);
127
this.clientDispatcher = clientDispatcher;
128
this.reference = (serviceUri == null) ? null
129
: new Reference(serviceUri);
130
parse(new ServiceContentReader(this));
137
* The context from which the client dispatcher will be
141
* @throws IOException
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());
153
* The XML introspection document.
154
* @throws IOException
156
public Service(Representation xmlService) throws IOException {
157
this(null, null, xmlService);
165
* @throws IOException
167
public Service(String serviceUri) throws IOException {
168
this(new Client(new Reference(serviceUri).getSchemeProtocol()),
178
* The XML introspection document.
179
* @throws IOException
181
public Service(String serviceUri, Representation xmlService)
183
this(new Client(new Reference(serviceUri).getSchemeProtocol()),
184
serviceUri, xmlService);
188
* Deletes a resource.
192
* @return The result status.
194
public Status deleteResource(String uri) {
195
return getClientDispatcher().handle(new Request(Method.DELETE, uri))
200
* Returns the base reference used to resolve relative references found
201
* within the scope of the xml:base attribute.
203
* @return The base reference used to resolve relative references found
204
* within the scope of the xml:base attribute.
206
public Reference getBaseReference() {
207
return baseReference;
211
* Returns the client HTTP dispatcher.
213
* @return The client HTTP dispatcher.
215
public Client getClientDispatcher() {
216
return this.clientDispatcher;
220
* Returns the hypertext reference.
222
* @return The hypertext reference.
224
public Reference getReference() {
225
return this.reference;
229
* Retrieves a resource representation.
233
* @return The resource representation.
235
public Representation getResource(String uri) {
236
return getClientDispatcher().handle(new Request(Method.GET, uri))
241
* Returns the list of workspaces.
243
* @return The list of workspaces.
245
public List<Workspace> getWorkspaces() {
246
if (this.workspaces == null) {
247
this.workspaces = new ArrayList<Workspace>();
250
return this.workspaces;
254
* Sets the base reference used to resolve relative references found within
255
* the scope of the xml:base attribute.
257
* @param baseReference
258
* The base reference used to resolve relative references found
259
* within the scope of the xml:base attribute.
261
public void setBaseReference(Reference baseReference) {
262
this.baseReference = baseReference;
266
* Sets the client HTTP dispatcher.
268
* @param clientDispatcher
269
* The client HTTP dispatcher.
271
public void setClientDispatcher(Client clientDispatcher) {
272
this.clientDispatcher = clientDispatcher;
276
* Sets the hypertext reference.
279
* The hypertext reference.
281
public void setReference(Reference ref) {
282
this.reference = ref;
286
* Updates a resource representation.
290
* @return The resource representation.
292
public Status updateResource(String uri,
293
Representation updatedRepresentation) {
294
return getClientDispatcher().handle(
295
new Request(Method.PUT, uri, updatedRepresentation))
300
* Writes the representation to a XML writer.
303
* The XML writer to write to.
304
* @throws IOException
307
public void write(XmlWriter writer) throws IOException {
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");
316
for (final Workspace workspace : getWorkspaces()) {
317
workspace.writeElement(writer);
320
writer.endElement(APP_NAMESPACE, "service");
321
writer.endDocument();
322
} catch (SAXException se) {
323
throw new IOException("Couldn't write the service representation: "