Skip to content
This repository was archived by the owner on Sep 23, 2025. It is now read-only.

Commit 98c5937

Browse files
mageshbkcunningt
authored andcommitted
[SWITCHYARD-2313] - Added test for error customization
1 parent 678bdf2 commit 98c5937

File tree

7 files changed

+200
-1
lines changed

7 files changed

+200
-1
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* JBoss, Home of Professional Open Source
3+
* Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual
4+
* contributors by the @authors tag. See the copyright.txt in the
5+
* distribution for a full listing of individual contributors.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.switchyard.quickstarts.rest.binding;
18+
19+
import java.io.InputStream;
20+
import java.io.IOException;
21+
22+
import javax.ws.rs.core.Response;
23+
import javax.ws.rs.WebApplicationException;
24+
25+
import org.jboss.resteasy.client.ClientResponse;
26+
import org.jboss.resteasy.client.ClientResponseFailure;
27+
import org.jboss.resteasy.client.core.BaseClientResponse;
28+
import org.jboss.resteasy.client.core.ClientErrorInterceptor;
29+
30+
public class MyClientErrorInterceptor implements ClientErrorInterceptor {
31+
32+
public void handle(ClientResponse response) throws RuntimeException {
33+
try {
34+
BaseClientResponse r = (BaseClientResponse) response;
35+
36+
InputStream stream = r.getStreamFactory().getInputStream();
37+
if (stream != null) {
38+
stream.reset();
39+
}
40+
if ((response.getResponseStatus() != null) && (response.getResponseStatus().getStatusCode() == 404)
41+
&& !(r.getException() instanceof ItemNotFoundException)) {
42+
throw new WebApplicationException(Response
43+
.status(Response.Status.INTERNAL_SERVER_ERROR)
44+
.entity(new ApiError("Error at " + response.getHeaders().get("full-path")))
45+
.build());
46+
}
47+
48+
} catch (IOException e){
49+
//...
50+
}
51+
}
52+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* JBoss, Home of Professional Open Source
3+
* Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual
4+
* contributors by the @authors tag. See the copyright.txt in the
5+
* distribution for a full listing of individual contributors.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.switchyard.quickstarts.rest.binding;
18+
19+
import java.util.Map;
20+
21+
import javax.ws.rs.core.MultivaluedMap;
22+
import javax.ws.rs.ext.Provider;
23+
24+
import org.jboss.resteasy.annotations.interception.ClientInterceptor;
25+
import org.jboss.resteasy.client.ClientResponse;
26+
import org.jboss.resteasy.client.ClientRequest;
27+
import org.jboss.resteasy.client.core.BaseClientResponse;
28+
import org.jboss.resteasy.spi.interception.ClientExecutionContext;
29+
import org.jboss.resteasy.spi.interception.ClientExecutionInterceptor;
30+
31+
@Provider
32+
@ClientInterceptor
33+
public class MyClientExecutionInterceptor implements ClientExecutionInterceptor {
34+
35+
public ClientResponse execute(ClientExecutionContext ctx) throws Exception {
36+
ClientRequest request = ctx.getRequest();
37+
ClientResponse response = null;
38+
39+
response = ctx.proceed();
40+
if ((response.getResponseStatus() != null) && (response.getResponseStatus().getStatusCode() == 404)) {
41+
BaseClientResponse r = (BaseClientResponse) response;
42+
MultivaluedMap<String, String> headers = r.getHeaders();
43+
headers.add("full-path", request.getUri());
44+
r.setHeaders(headers);
45+
}
46+
return response;
47+
}
48+
}

rest-binding/src/main/java/org/switchyard/quickstarts/rest/binding/OrderServiceImpl.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,13 @@ public class OrderServiceImpl implements OrderService {
3939
private ConcurrentMap<Integer, Order> _orders = new ConcurrentHashMap<Integer, Order>();
4040

4141
@Inject
42-
@Reference
42+
@Reference("Warehouse")
4343
private Warehouse _warehouse;
4444

45+
@Inject
46+
@Reference("WarehouseRef")
47+
private WarehouseRef _warehouseRef;
48+
4549
public OrderServiceImpl() {
4650
}
4751

@@ -55,6 +59,16 @@ public Order newOrder() {
5559

5660
public Order getOrder(Integer orderId) throws Exception {
5761
LOGGER.info("Getting Order with no: " + orderId);
62+
if (orderId == 404) {
63+
Order order = new Order(404);
64+
_orders.put(orderNo, order);
65+
try {
66+
_warehouseRef.get404();
67+
} catch (Exception e) {
68+
throw e;
69+
}
70+
return order;
71+
}
5872
Order corder = _orders.get(orderId);
5973
if (corder == null) {
6074
throw new ItemNotFoundException("Order " + orderId + " not found!");
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* JBoss, Home of Professional Open Source
3+
* Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual
4+
* contributors by the @authors tag. See the copyright.txt in the
5+
* distribution for a full listing of individual contributors.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.switchyard.quickstarts.rest.binding;
18+
19+
/**
20+
* Interface for WarehouseRef.
21+
*/
22+
public interface WarehouseRef {
23+
24+
Integer get404() throws Exception;
25+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* JBoss, Home of Professional Open Source
3+
* Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual
4+
* contributors by the @authors tag. See the copyright.txt in the
5+
* distribution for a full listing of individual contributors.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.switchyard.quickstarts.rest.binding;
18+
19+
import javax.ws.rs.Consumes;
20+
import javax.ws.rs.DELETE;
21+
import javax.ws.rs.GET;
22+
import javax.ws.rs.Path;
23+
import javax.ws.rs.PathParam;
24+
import javax.ws.rs.POST;
25+
import javax.ws.rs.Produces;
26+
import javax.ws.rs.PUT;
27+
import javax.ws.rs.core.Response;
28+
29+
/**
30+
* REST interface for WarehouseService Reference.
31+
*/
32+
@Path("/404")
33+
public interface WarehouseResourceRef {
34+
35+
@GET
36+
@Path("/404/")
37+
@Produces({ "text/plain" })
38+
public Integer get404() throws Exception;
39+
}

rest-binding/src/main/resources/META-INF/switchyard.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
<rest:messageComposer class="org.switchyard.quickstarts.rest.binding.CustomComposer"/>
3232
<rest:interfaces>org.switchyard.quickstarts.rest.binding.WarehouseResource</rest:interfaces>
3333
<rest:contextPath>rest-binding</rest:contextPath>
34+
<rest:contextParams>
35+
<rest:contextParam name="resteasy.providers" value="org.switchyard.quickstarts.rest.binding.ItemNotFoundExceptionMapper"/>
36+
</rest:contextParams>
3437
</rest:binding.rest>
3538
</sca:service>
3639
<sca:reference xmlns:sca="http://docs.oasis-open.org/ns/opencsa/sca/200912" multiplicity="1..1" name="Warehouse" promote="OrderService/Warehouse">
@@ -40,6 +43,17 @@
4043
<rest:address>http://localhost:${restPort}${restPath}</rest:address>
4144
</rest:binding.rest>
4245
</sca:reference>
46+
<sca:reference xmlns:sca="http://docs.oasis-open.org/ns/opencsa/sca/200912" multiplicity="1..1" name="WarehouseRef" promote="OrderService/WarehouseRef">
47+
<rest:binding.rest xmlns:rest="urn:switchyard-component-resteasy:config:2.0">
48+
<rest:messageComposer class="org.switchyard.quickstarts.rest.binding.CustomComposer"/>
49+
<rest:interfaces>org.switchyard.quickstarts.rest.binding.WarehouseResourceRef</rest:interfaces>
50+
<rest:address>http://localhost:${restPort}${restPath}</rest:address>
51+
<rest:contextParams>
52+
<rest:contextParam name="resteasy.providers" value="org.switchyard.quickstarts.rest.binding.MyClientExecutionInterceptor"/>
53+
<rest:contextParam name="resteasy.client.error.interceptors" value="org.switchyard.quickstarts.rest.binding.MyClientErrorInterceptor"/>
54+
</rest:contextParams>
55+
</rest:binding.rest>
56+
</sca:reference>
4357
<component name="OrderService">
4458
<implementation.bean xmlns="urn:switchyard-component-bean:config:2.0" class="org.switchyard.quickstarts.rest.binding.OrderServiceImpl"/>
4559
<service name="OrderService">
@@ -48,6 +62,9 @@
4862
<reference name="Warehouse">
4963
<interface.java interface="org.switchyard.quickstarts.rest.binding.Warehouse"/>
5064
</reference>
65+
<reference name="WarehouseRef">
66+
<interface.java interface="org.switchyard.quickstarts.rest.binding.WarehouseRef"/>
67+
</reference>
5168
</component>
5269
<component name="WarehouseService">
5370
<implementation.bean xmlns="urn:switchyard-component-bean:config:2.0" class="org.switchyard.quickstarts.rest.binding.WarehouseServiceImpl"/>

rest-binding/src/test/java/org/switchyard/quickstarts/rest/binding/RESTEasyBindingTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ public void orderServiceRESTEndpoint() throws Exception {
118118
// Destroy our inventory
119119
response = http.sendString(BASE_URL + "/inventory/remove", "", HTTPMixIn.HTTP_OPTIONS);
120120
Assert.assertEquals(SUCCESS, response);
121+
122+
// Test 404 returned with complete url
123+
response = http.sendString(BASE_URL + "/order/404", "", HTTPMixIn.HTTP_GET);
124+
Assert.assertTrue(response.contains("Error at [http://localhost:8081/404/404/]"));
121125
}
122126

123127
private static String BASE_URL = "http://localhost:8081/rest-binding";

0 commit comments

Comments
 (0)