-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c625c39
commit b1ca4d4
Showing
39 changed files
with
102,113 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
app/gateway/src/main/java/org/apache/camel/workshop/gateway/GatewayRoutes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
package org.apache.camel.workshop.gateway; | ||
|
||
import org.apache.camel.Exchange; | ||
import org.apache.camel.builder.RouteBuilder; | ||
import org.apache.camel.component.caffeine.CaffeineConstants; | ||
import org.apache.camel.http.common.HttpMethods; | ||
import org.apache.camel.impl.saga.InMemorySagaService; | ||
import org.apache.camel.model.dataformat.JsonLibrary; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
@Component | ||
public class GatewayRoutes extends RouteBuilder { | ||
|
||
|
||
|
||
@Override | ||
public void configure() throws Exception { | ||
|
||
restConfiguration() | ||
.component("servlet") | ||
.enableCORS(true); | ||
|
||
|
||
getContext().addService(new InMemorySagaService(), true); | ||
|
||
/* | ||
* Proxied requests | ||
*/ | ||
|
||
rest().get("/payments") | ||
.route() | ||
.to("undertow:http://{{credit.service}}/api/payments"); | ||
|
||
rest().get("/purchases") | ||
.route() | ||
.to("undertow:http://{{inventory.service}}/api/purchases"); | ||
|
||
/* | ||
* Items | ||
*/ | ||
|
||
rest().get("/items") | ||
.route() | ||
.to("undertow:http://{{inventory.service}}/api/items") | ||
.unmarshal().json(JsonLibrary.Jackson, Catalog.class) | ||
.enrichWith("direct:recommendationHystrix") | ||
.body(Catalog.class, List.class, this::recommend) | ||
.marshal().json(JsonLibrary.Jackson); | ||
|
||
/* | ||
* Recommendations | ||
*/ | ||
|
||
from("direct:recommendationHystrix") | ||
.hystrix() | ||
.to("direct:recommendation") | ||
.setHeader(CaffeineConstants.ACTION, constant(CaffeineConstants.ACTION_PUT)) | ||
.to("caffeine-cache:global?key=recommendation") | ||
.onFallback() | ||
.setHeader(CaffeineConstants.ACTION, constant(CaffeineConstants.ACTION_GET)) | ||
.to("caffeine-cache:global?key=recommendation") | ||
.choice() | ||
.when(header("CamelCaffeineActionHasResult").isNotEqualTo(true)) | ||
.setBody(constant(Collections.emptyList())) | ||
.end() | ||
.end(); | ||
|
||
from("direct:recommendation") | ||
.setHeader(Exchange.HTTP_METHOD, constant(HttpMethods.GET)) | ||
.to("undertow:http://{{recommendation.service}}/api/recommendations") | ||
.unmarshal().json(JsonLibrary.Jackson, List.class); | ||
|
||
|
||
/* | ||
* Orders | ||
*/ | ||
|
||
rest().post("/orders") | ||
.type(Order.class) | ||
.route() | ||
.saga() | ||
.compensation("direct:cancelOrder") | ||
.option("order", simple("${body}")) | ||
.unmarshal().json(JsonLibrary.Jackson, Order.class) | ||
.to("bean-validator:validateOrder") | ||
.multicast().parallelProcessing() | ||
.to("direct:payOrder") | ||
.to("direct:purchaseOrderItems") | ||
.end() | ||
.marshal().json(JsonLibrary.Jackson) | ||
.end(); | ||
|
||
from("direct:payOrder") | ||
.setBody().body(Order.class, this::createPayment) | ||
.marshal().json(JsonLibrary.Jackson) | ||
.to("undertow:http://{{credit.service}}/api/payments"); | ||
|
||
|
||
from("direct:purchaseOrderItems") | ||
.setHeader("reference", simple("${body.reference}")) | ||
.split().simple("${body.items}").parallelProcessing() | ||
.toD("undertow:http://{{inventory.service}}/api/purchases/${header.reference}/items/${body.id}?amount=${body.amount}") | ||
.end(); | ||
|
||
|
||
from("direct:cancelOrder") | ||
.setBody(header("order")).convertBodyTo(String.class) | ||
.unmarshal().json(JsonLibrary.Jackson, Order.class) | ||
.setHeader(Exchange.HTTP_METHOD, constant(HttpMethods.DELETE)) | ||
.multicast().parallelProcessing() | ||
.toD("undertow:http://{{credit.service}}/api/payments/${body.reference}") | ||
.toD("undertow:http://{{inventory.service}}/api/purchases/${body.reference}"); | ||
|
||
|
||
} | ||
|
||
private Payment createPayment(Order order) { | ||
Payment payment = new Payment(); | ||
payment.setUser(order.getUser()); | ||
payment.setReference(order.getReference()); | ||
payment.setAmount(order.getPrice()); | ||
return payment; | ||
} | ||
|
||
private Catalog recommend(Catalog catalog, List<String> recomm) { | ||
if (recomm != null && catalog != null && catalog.getItems() != null) { | ||
for (String recommItem : recomm) { | ||
Item item = catalog.getItems().get(recommItem); | ||
if (item != null) { | ||
item.setRecommended(true); | ||
} | ||
} | ||
} | ||
return catalog; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
camel.component.servlet.mapping.context-path=/api/* | ||
|
||
|
||
inventory.host=localhost | ||
inventory.port=8081 | ||
inventory.service=${inventory.host}:${inventory.port} | ||
|
||
payment.host=localhost | ||
payment.port=8082 | ||
credit.host=localhost | ||
credit.port=8082 | ||
credit.service=${credit.host}:${credit.port} | ||
|
||
recommendation.host=localhost | ||
recommendation.port=8083 | ||
recommendation.port=8083 | ||
recommendation.service=${recommendation.host}:${recommendation.port} |
Oops, something went wrong.