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

« back to all changes in this revision

Viewing changes to org.restlet.example/src/org/restlet/example/book/restlet/ch04/sec2/sub6/MailServerResource.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.example.book.restlet.ch04.sec2.sub6;
 
35
 
 
36
import java.io.IOException;
 
37
 
 
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;
 
52
 
 
53
/**
 
54
 * Resource corresponding to a mail received or sent with the parent mail
 
55
 * account. Leverages XML Schema validation.
 
56
 */
 
57
public class MailServerResource extends ServerResource {
 
58
 
 
59
    @Get
 
60
    public Representation toXml() throws IOException {
 
61
        // Create a new DOM representation
 
62
        DomRepresentation result = new DomRepresentation();
 
63
        result.setIndenting(true);
 
64
 
 
65
        // XML namespace configuration
 
66
        result.setNamespaceAware(true);
 
67
 
 
68
        // Populate the DOM document
 
69
        Document doc = result.getDocument();
 
70
 
 
71
        Node mailElt = doc.createElementNS(
 
72
                "http://www.rmep.org/namespaces/1.0", "mail");
 
73
        doc.appendChild(mailElt);
 
74
 
 
75
        Node statusElt = doc.createElement("status");
 
76
        statusElt.setTextContent("received");
 
77
        mailElt.appendChild(statusElt);
 
78
 
 
79
        Node subjectElt = doc.createElement("subject");
 
80
        subjectElt.setTextContent("Message to self");
 
81
        mailElt.appendChild(subjectElt);
 
82
 
 
83
        Node contentElt = doc.createElement("content");
 
84
        contentElt.setTextContent("Doh!");
 
85
        mailElt.appendChild(contentElt);
 
86
 
 
87
        Node accountRefElt = doc.createElement("accountRef");
 
88
        accountRefElt.setTextContent(new Reference(getReference(), "..")
 
89
                .getTargetRef().toString());
 
90
        mailElt.appendChild(accountRefElt);
 
91
        return result;
 
92
    }
 
93
 
 
94
    @Put
 
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())
 
99
                        + "/Mail.xsd").get();
 
100
        mailRep.setSchema(mailXsd);
 
101
        mailRep.setErrorHandler(new ErrorHandler() {
 
102
            public void error(SAXParseException exception) throws SAXException {
 
103
                throw new ResourceException(exception);
 
104
            }
 
105
 
 
106
            public void fatalError(SAXParseException exception)
 
107
                    throws SAXException {
 
108
                throw new ResourceException(exception);
 
109
            }
 
110
 
 
111
            public void warning(SAXParseException exception)
 
112
                    throws SAXException {
 
113
                throw new ResourceException(exception);
 
114
            }
 
115
        });
 
116
 
 
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);
 
122
 
 
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");
 
128
 
 
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);
 
134
    }
 
135
}