Here's some code I wrote up using XStream to send and receive XML. One
passes a single object and the other passes a collection of objects. You
simply extend one of the classes and provide the getXStream() method
with your own custom XStream configured. The same class is both client
and server. I thought maybe someone would be interested in it, as well
as giving back to the project the best I can at this point (still new
and learning). Plus maybe others could give suggestions on how to
improve it.
#######################################################################################
/*
* XStreamRepresentation.java
*
*
*/
package org.savvylms.api.representation;
import com.thoughtworks.xstream.XStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.restlet.data.MediaType;
import org.restlet.resource.Representation;
import org.restlet.resource.StreamRepresentation;
/**
*
* @author Justin Stanczak
*/
public abstract class XStreamRepresentation<T> extends
StreamRepresentation {
private Class<T> obj;
private Representation representation;
public XStreamRepresentation(MediaType mediaType, Class<T> obj) {
super(mediaType);
this.obj = obj;
}
public XStreamRepresentation(Representation representation) {
super(representation.getMediaType());
this.representation = representation;
}
protected abstract XStream getXStream();
public InputStream getStream() throws IOException {
return representation.getStream();
}
public void write(OutputStream outputStream) throws IOException {
XStream xstream = this.getXStream();
xstream.toXML(this.obj, outputStream);
}
@SuppressWarnings(value = "unchecked")
public Class<T> getObject() throws IOException {
XStream xstream = this.getXStream();
return (Class<T>) xstream.fromXML(this.getStream());
}
public void setObject(Class<T> obj) {
this.obj = obj;
}
}
#######################################################################################
/*
* XStreamCollectionRepresentation.java
*
*
*/
package org.savvylms.api.representation;
import com.thoughtworks.xstream.XStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collection;
import org.restlet.data.MediaType;
import org.restlet.resource.Representation;
import org.restlet.resource.StreamRepresentation;
/**
*
* @author Justin Stanczak
*/
public abstract class XStreamCollectionRepresentation<T> extends
StreamRepresentation {
private Collection<T> obj;
private Representation representation;
public XStreamCollectionRepresentation(MediaType mediaType,
Collection<T> obj) {
super(mediaType);
this.obj = obj;
}
public XStreamCollectionRepresentation(Representation representation) {
super(representation.getMediaType());
this.representation = representation;
}
protected abstract XStream getXStream();
public InputStream getStream() throws IOException {
return representation.getStream();
}
public void write(OutputStream outputStream) throws IOException {
XStream xstream = this.getXStream();
xstream.toXML(this.obj, outputStream);
}
@SuppressWarnings(value = "unchecked")
public Collection<T> getObject() throws IOException {
XStream xstream = this.getXStream();
return (Collection<T>) xstream.fromXML(this.getStream());
}
public void setObject(Collection<T> obj) {
this.obj = obj;
}
}
#######################################################################################
/*
* CourseCollectionRepresentation.java
*
*
*/
package org.savvylms.api.representation;
import com.thoughtworks.xstream.XStream;
import java.util.Collection;
import java.util.List;
import org.restlet.data.MediaType;
import org.restlet.resource.Representation;
import org.savvylms.api.course.Course;
/**
*
* @author Justin Stanczak
*/
public class CourseCollectionRepresentation extends
XStreamCollectionRepresentation<Course> {
public CourseCollectionRepresentation(MediaType mediaType,
Collection<Course> obj) {
super(mediaType, obj);
}
public CourseCollectionRepresentation(Representation representation) {
super(representation);
}
protected XStream getXStream() {
XStream xstream = new XStream();
xstream.alias("courses", List.class);
xstream.alias("course", Course.class);
return xstream;
}
}
#######################################################################################
// Usage example
// Here's how to read input on client side:
CourseCollectionRepresentation courses = new
CourseCollectionRepresentation(response.getEntity());
courses.getObject();
// Here's the server sending data:
public Representation getRepresentation(Variant variant) {
return new CourseCollectionRepresentation(MediaType.APPLICATION_XML,
courses);
}
#######################################################################################
--
Justin Stanczak
Stanczak Group
812-735-3600
"All that is necessary for the triumph of evil is that good men do nothing."
Edmund Burke