Skip to content

Commit 673cf37

Browse files
committed
Updated REST showcase to show list results, ParameterizedPattern can now build using list of values
1 parent 5c94cc1 commit 673cf37

4 files changed

Lines changed: 86 additions & 44 deletions

File tree

‎impl-config/src/main/java/com/ocpsoft/rewrite/bind/ParameterizedPattern.java‎

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,68 @@ public Map<String, Parameter<String>> getParameters()
112112
}
113113

114114
/**
115-
* Use this expression to build a {@link String} from the given pattern and values.
115+
* Use this expression to build a {@link String} from the given pattern. Extract needed values from registered
116+
* {@link Binding} instances.
116117
*/
117118
public String build(final Rewrite event, final EvaluationContext context)
118119
{
119120
return build(extractBoundValues(event, context));
120121
}
121122

123+
/**
124+
* Use this expression to build a {@link String} from the given pattern and values.
125+
*/
126+
public String build(final Object... values)
127+
{
128+
if ((values == null) || (params.size() != values.length))
129+
{
130+
throw new IllegalArgumentException("Must supply [" + params.size() + "] values to build output string.");
131+
}
132+
133+
return buildUnsafe(values);
134+
}
135+
136+
/**
137+
* Use this expression to build a {@link String} from the given pattern and values. Enforces that the number of
138+
* values passed must equal the number of expression parameters.
139+
*/
140+
public String buildUnsafe(final Object... values)
141+
{
142+
StringBuilder builder = new StringBuilder();
143+
CapturingGroup last = null;
144+
145+
int index = 0;
146+
for (Entry<String, RegexParameter> entry : params.entrySet())
147+
{
148+
RegexParameter param = entry.getValue();
149+
CapturingGroup capture = param.getCapture();
150+
151+
if ((last != null) && (last.getEnd() < capture.getStart()))
152+
{
153+
builder.append(Arrays.copyOfRange(chars, last.getEnd() + 1, capture.getStart()));
154+
}
155+
else if ((last == null) && (capture.getStart() > 0))
156+
{
157+
builder.append(Arrays.copyOfRange(chars, 0, capture.getStart()));
158+
}
159+
160+
builder.append(values[index]);
161+
162+
last = capture;
163+
}
164+
165+
if ((last != null) && (last.getEnd() < chars.length))
166+
{
167+
builder.append(Arrays.copyOfRange(chars, last.getEnd() + 1, chars.length));
168+
}
169+
else if (last == null)
170+
{
171+
builder.append(chars);
172+
}
173+
174+
return builder.toString();
175+
}
176+
122177
/**
123178
* Use this expression to build a {@link String} from the given pattern and values. Enforces that the number of keys
124179
* passed must equal the number of expression parameters.
@@ -128,7 +183,7 @@ public String build(final Map<String, List<Object>> values)
128183

129184
if ((values == null) || (params.size() != values.size()))
130185
{
131-
throw new IllegalArgumentException("Must supply [" + params.size() + "] values to build path.");
186+
throw new IllegalArgumentException("Must supply [" + params.size() + "] values to build output string.");
132187
}
133188

134189
return buildUnsafe(values);

‎showcase/rest-ws/src/main/java/com/ocpsoft/rewrite/showcase/rest/ProductRegistry.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@
1919
import java.util.List;
2020

2121
import javax.enterprise.context.ApplicationScoped;
22+
import javax.xml.bind.annotation.XmlElement;
23+
import javax.xml.bind.annotation.XmlRootElement;
2224

2325
/**
2426
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
2527
*/
2628
@ApplicationScoped
29+
@XmlRootElement(name = "products")
2730
public class ProductRegistry
2831
{
2932
private final List<Product> products = new ArrayList<Product>();
@@ -46,6 +49,7 @@ public Product getById(final int id)
4649
return products.get(id);
4750
}
4851

52+
@XmlElement(name = "product", type = Product.class)
4953
public List<Product> getProducts()
5054
{
5155
return products;

‎showcase/rest-ws/src/main/java/com/ocpsoft/rewrite/showcase/rest/RestRewriteConfiguration.java‎

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
package com.ocpsoft.rewrite.showcase.rest;
22

3-
import java.util.Arrays;
4-
import java.util.LinkedHashMap;
5-
import java.util.List;
6-
import java.util.Map;
7-
83
import javax.inject.Inject;
94
import javax.servlet.ServletContext;
105
import javax.xml.bind.JAXBContext;
@@ -92,6 +87,30 @@ public void performHttp(final HttpServletRewrite event, final EvaluationContext
9287
}
9388
})
9489

90+
.defineRule()
91+
.when(Path.matches("/store/products").and(Method.isGet()))
92+
.perform(new HttpOperation() {
93+
@Override
94+
public void performHttp(final HttpServletRewrite event, final EvaluationContext context)
95+
{
96+
/*
97+
* Marshal the Product into XML using JAXB
98+
*/
99+
try {
100+
Marshaller marshaller = JAXBContext.newInstance(ProductRegistry.class)
101+
.createMarshaller();
102+
marshaller.setProperty("jaxb.formatted.output", true);
103+
marshaller.marshal(products, event.getResponse().getOutputStream());
104+
105+
event.getResponse().setContentType("application/xml");
106+
((HttpInboundServletRewrite) event).sendStatusCode(200);
107+
}
108+
catch (Exception e) {
109+
throw new RuntimeException(e);
110+
}
111+
}
112+
})
113+
95114
.defineRule()
96115
.when(Path.matches("/store/products").and(Method.isPost()))
97116
.perform(new HttpOperation() {
@@ -108,10 +127,8 @@ public void performHttp(final HttpServletRewrite event, final EvaluationContext
108127

109128
product = products.add(product);
110129

111-
Map<String, List<Object>> values = new LinkedHashMap<String, List<Object>>();
112-
values.put("pid", (List) Arrays.asList(product.getId()));
113130
((HttpInboundServletRewrite) event).redirectPermanent(event.getContextPath()
114-
+ new ParameterizedPattern("/store/product/{pid}").build(values));
131+
+ new ParameterizedPattern("/store/product/{pid}").build(product.getId()));
115132
}
116133
catch (Exception e) {
117134
throw new RuntimeException(e);

‎showcase/rest-ws/src/main/java/com/ocpsoft/rewrite/showcase/rest/XMLObjectConverter.java‎

Lines changed: 0 additions & 34 deletions
This file was deleted.

0 commit comments

Comments
 (0)