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.example.book.restlet.ch04.sec2.sub6;
36
import java.io.IOException;
38
import org.restlet.data.LocalReference;
39
import org.restlet.data.Reference;
40
import org.restlet.ext.xml.DomRepresentation;
41
import org.restlet.representation.Representation;
42
import org.restlet.resource.ClientResource;
43
import org.restlet.resource.Get;
44
import org.restlet.resource.Put;
45
import org.restlet.resource.ResourceException;
46
import org.restlet.resource.ServerResource;
47
import org.w3c.dom.Document;
48
import org.w3c.dom.Node;
49
import org.xml.sax.ErrorHandler;
50
import org.xml.sax.SAXException;
51
import org.xml.sax.SAXParseException;
54
* Resource corresponding to a mail received or sent with the parent mail
55
* account. Leverages XML Schema validation.
57
public class MailServerResource extends ServerResource {
60
public Representation toXml() throws IOException {
61
// Create a new DOM representation
62
DomRepresentation result = new DomRepresentation();
63
result.setIndenting(true);
65
// XML namespace configuration
66
result.setNamespaceAware(true);
68
// Populate the DOM document
69
Document doc = result.getDocument();
71
Node mailElt = doc.createElementNS(
72
"http://www.rmep.org/namespaces/1.0", "mail");
73
doc.appendChild(mailElt);
75
Node statusElt = doc.createElement("status");
76
statusElt.setTextContent("received");
77
mailElt.appendChild(statusElt);
79
Node subjectElt = doc.createElement("subject");
80
subjectElt.setTextContent("Message to self");
81
mailElt.appendChild(subjectElt);
83
Node contentElt = doc.createElement("content");
84
contentElt.setTextContent("Doh!");
85
mailElt.appendChild(contentElt);
87
Node accountRefElt = doc.createElement("accountRef");
88
accountRefElt.setTextContent(new Reference(getReference(), "..")
89
.getTargetRef().toString());
90
mailElt.appendChild(accountRefElt);
95
public void store(DomRepresentation mailRep) throws IOException {
96
// Configure the XML Schema used for validation
97
Representation mailXsd = new ClientResource(
98
LocalReference.createClapReference(getClass().getPackage())
100
mailRep.setSchema(mailXsd);
101
mailRep.setErrorHandler(new ErrorHandler() {
102
public void error(SAXParseException exception) throws SAXException {
103
throw new ResourceException(exception);
106
public void fatalError(SAXParseException exception)
107
throws SAXException {
108
throw new ResourceException(exception);
111
public void warning(SAXParseException exception)
112
throws SAXException {
113
throw new ResourceException(exception);
117
// XML namespace configuration
118
String rmepNs = "http://www.rmep.org/namespaces/1.0";
119
mailRep.setNamespaceAware(true);
120
mailRep.getNamespaces().put("", rmepNs);
121
mailRep.getNamespaces().put("rmep", rmepNs);
123
// Retrieve the XML element using XPath expressions
124
String status = mailRep.getText("/:mail/:status");
125
String subject = mailRep.getText("/rmep:mail/:subject");
126
String content = mailRep.getText("/rmep:mail/rmep:content");
127
String accountRef = mailRep.getText("/:mail/rmep:accountRef");
129
// Output the XML element values
130
System.out.println("Status: " + status);
131
System.out.println("Subject: " + subject);
132
System.out.println("Content: " + content);
133
System.out.println("Account URI: " + accountRef);