/**
* Define the inbound conditions and conversion mechanisms to be used when handling inbound requests.
*/
.when(Method.isGet()
.and(Path.matches("/store/product/{pid}")))
.perform(new HttpOperation() {
@Override
public void performHttp(final HttpServletRewrite event, final EvaluationContext context)
{
/**
* Extract the stored {pid} from our Path and load the Product. This is an example of how we can
* use a converter to directly bind and store the object we want into a binding. {@link Evaluation}
* is an low-level construct, and binds array values that must be dereferenced. If using other
* bindings such as {@link El}, the value will be bound directly to the type of the referenced
* property type, and this array downcast is not necessary.
*/
ParameterStore store = (ParameterStore) context.get(ParameterStore.class);
Product product = (Product) Evaluation.property("pid").retrieveConverted(event, context,
store.get("pid"));
/**
* Marshal the Product into XML using JAXB. This has been extracted into a utility class.
*/
try {
XMLUtil.streamFromObject(Product.class, product, event.getResponse()
.getOutputStream());
}
catch (IOException e) {
throw new RuntimeException(e);
}
/**
* Set the content type and status code of the response, this again could be extracted into a REST
* utility class.
*/
event.getResponse().setContentType("application/xml");
((HttpInboundServletRewrite) event).sendStatusCode(200);
}
}).where("pid").matches("\\d+")
.constrainedBy(new IntegerConstraint())
.convertedBy(productConverter)
.validatedBy(productValidator)
.addRule()
.when(Path.matches("/store/products").and(Method.isGet()))
.perform(new HttpOperation() {
@Override
public void performHttp(final HttpServletRewrite event, final EvaluationContext context)
{
try {
XMLUtil.streamFromObject(ProductRegistry.class, products, event.getResponse().getOutputStream());
event.getResponse().setContentType("application/xml");
((HttpInboundServletRewrite) event).sendStatusCode(200);
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
})
.addRule()
.when(Path.matches("/store/products").and(Method.isPost()))
.perform(new PostOperation())
.addRule().when(Path.matches("/")).perform(new HttpOperation() {
@Override
public void performHttp(final HttpServletRewrite event, final EvaluationContext context)
{
try {