org.apache.camel.model.rest.RestParamType Java Examples

The following examples show how to use org.apache.camel.model.rest.RestParamType. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CamelSagaPaymentService.java    From camel-saga-quickstart with Apache License 2.0 6 votes vote down vote up
@Override
public void configure() throws Exception {

    rest().post("/pay")
            .param().type(RestParamType.query).name("type").required(true).endParam()
            .param().type(RestParamType.header).name("id").required(true).endParam()
            .route()
            .saga()
                .propagation(SagaPropagation.MANDATORY)
                .option("id", header("id"))
                .compensation("direct:cancelPayment")
            .log("Paying ${header.type} for order #${header.id}")
            .choice()
                .when(x -> Math.random() >= 0.85)
                    .throwException(new RuntimeException("Random failure during payment"))
            .end();

    from("direct:cancelPayment")
            .log("Payment #${header.id} has been cancelled");

}
 
Example #2
Source File: CamelSagaFlightService.java    From camel-saga-quickstart with Apache License 2.0 6 votes vote down vote up
@Override
public void configure() throws Exception {

    restConfiguration().port(8383);


    rest().post("/flight/buy")
            .param().type(RestParamType.header).name("id").required(true).endParam()
            .route()
            .saga()
                .propagation(SagaPropagation.MANDATORY)
                .option("id", header("id"))
                .compensation("direct:cancelPurchase")
            .log("Buying flight #${header.id}")
            .to("http4://camel-saga-payment-service:8080/api/pay?bridgeEndpoint=true&type=flight")
            .log("Payment for flight #${header.id} done");

    from("direct:cancelPurchase")
            .log("Flight purchase #${header.id} has been cancelled");

}
 
Example #3
Source File: CamelSagaTrainService.java    From camel-saga-quickstart with Apache License 2.0 6 votes vote down vote up
@Override
public void configure() {

    rest().post("/train/buy/seat")
            .param().type(RestParamType.header).name("id").required(true).endParam()
            .route()
            .saga()
                .propagation(SagaPropagation.SUPPORTS)
                .option("id", header("id"))
                .compensation("direct:cancelPurchase")
            .log("Buying train seat #${header.id}")
            .to("http4://camel-saga-payment-service:8080/api/pay?bridgeEndpoint=true&type=train")
            .log("Payment for train #${header.id} done");

    from("direct:cancelPurchase")
            .log("Train purchase #${header.id} has been cancelled");

}
 
Example #4
Source File: ParamRoute.java    From camel-cookbook-examples with Apache License 2.0 6 votes vote down vote up
@Override
public void configure() throws Exception {
    restConfiguration()
        .component("undertow").host("localhost").port(port1);

    rest("/say")
        .get("/hello")
            .route().transform().constant("Hello World").endRest()
        .get("/hello/{name}")
            .route().transform(simple("Hello ${header.name}")).endRest()
        .get("/hello/query/{name}?verbose={verbose}")
            .param().name("verbose").type(RestParamType.query).defaultValue("false").endParam()
            .to("direct:hello")
        .post("/bye/{name}")
            .toD("mock:${header.name}");

    from("direct:hello")
        .choice()
            .when(header("verbose").isEqualTo(true))
                .transform(simple("Hello there ${header.name}! How are you today?")).endChoice()
            .otherwise()
                .transform(simple("Yo ${header.name}"));
}
 
Example #5
Source File: CafeApiRoute.java    From camel-cookbook-examples with Apache License 2.0 5 votes vote down vote up
@Override
public void configure() throws Exception {
    onException(MenuItemNotFoundException.class)
        .handled(true)
        .setHeader(Exchange.HTTP_RESPONSE_CODE, constant(404))
        .setHeader(Exchange.CONTENT_TYPE, constant("text/plain"))
        .setBody().simple("${exception.message}");

    onException(MenuItemInvalidException.class)
        .handled(true)
        .setHeader(Exchange.HTTP_RESPONSE_CODE, constant(400))
        .setHeader(Exchange.CONTENT_TYPE, constant("text/plain"))
        .setBody().simple("${exception.message}");

    onException(JsonParseException.class)
        .handled(true)
        .setHeader(Exchange.HTTP_RESPONSE_CODE, constant(400))
        .setHeader(Exchange.CONTENT_TYPE, constant("text/plain"))
        .setBody().constant("Invalid json data");

    restConfiguration()
        .component("undertow").port(port1)
        .bindingMode(RestBindingMode.json)
        .apiContextPath("api-doc")
        .apiProperty("api.title", "Cafe Menu")
        .apiProperty("api.version", "1.0.0")
        .apiProperty("api.description", "Cafe Menu Sample API")
        .apiProperty("api.contact.name", "Camel Cookbook")
        .apiProperty("api.contact.url", "http://www.camelcookbook.org")
        .apiProperty("api.license.name", "Apache 2.0")
        .apiProperty("api.license.url", "http://www.apache.org/licenses/LICENSE-2.0.html")
        .enableCORS(true);

    rest("/cafe/menu").description("Cafe Menu Services")
        .get("/items").description("Returns all menu items").outType(MenuItem[].class)
            .responseMessage().code(200).message("All of the menu items").endResponseMessage()
            .to("bean:menuService?method=getMenuItems")
        .get("/items/{id}").description("Returns menu item with matching id").outType(MenuItem.class)
            .param().name("id").type(RestParamType.path).description("The id of the item").dataType("int").endParam()
            .responseMessage().code(200).message("The requested menu item").endResponseMessage()
            .responseMessage().code(404).message("Menu item not found").endResponseMessage()
            .to("bean:menuService?method=getMenuItem(${header.id})")
        .post("/items").description("Creates a new menu item").type(MenuItem.class)
            .param().name("body").type(RestParamType.body).description("The item to create").endParam()
            .responseMessage().code(201).message("Successfully created menu item").endResponseMessage()
            .responseMessage().code(400).message("Invalid menu item").endResponseMessage()
            .route().to("bean:menuService?method=createMenuItem").setHeader(Exchange.HTTP_RESPONSE_CODE, constant(201)).endRest()
        .put("/items/{id}").description("Updates an existing or creates a new menu item").type(MenuItem.class)
            .param().name("id").type(RestParamType.path).description("The id of the item").dataType("int").endParam()
            .param().name("body").type(RestParamType.body).description("The menu item new contents").endParam()
            .responseMessage().code(200).message("Successfully updated item").endResponseMessage()
            .responseMessage().code(400).message("Invalid menu item").endResponseMessage()
            .to("bean:menuService?method=updateMenuItem(${header.id}, ${body})")
        .delete("/items/{id}").description("Deletes the specified item")
            .param().name("id").type(RestParamType.path).description("The id of the item").dataType("int").endParam()
            .responseMessage().code(200).message("Successfully deleted item").endResponseMessage()
            .to("bean:menuService?method=removeMenuItem(${header.id})");
}
 
Example #6
Source File: RestRouteBuilder.java    From wildfly-camel-examples with Apache License 2.0 4 votes vote down vote up
public void configure() throws Exception {

        /**
         * Configure an error handler to trap instances where the data posted to
         * the REST API is invalid
         */
        onException(JsonParseException.class)
            .handled(true)
            .setHeader(Exchange.HTTP_RESPONSE_CODE, constant(400))
            .setHeader(Exchange.CONTENT_TYPE, constant(MediaType.TEXT_PLAIN))
            .setBody().constant("Invalid json data");

        /**
         * Configure Camel REST to use the camel-undertow component
         */
        restConfiguration()
            .bindingMode(RestBindingMode.json)
            .component("undertow")
            .contextPath("rest/api")
            .host("localhost")
            .port(8080)
            .enableCORS(true)
            .apiProperty("api.title", "WildFly Camel REST API")
            .apiProperty("api.version", "1.0")
            .apiContextPath("swagger");

        /**
         * Configure REST API with a base path of /customers
         */
        rest("/customers").description("Customers REST service")
            .get()
                .description("Retrieves all customers")
                .produces(MediaType.APPLICATION_JSON)
                .route()
                    .bean(CustomerService.class, "findAll")
                .endRest()

            .get("/{id}")
                .description("Retrieves a customer for the specified id")
                .param()
                    .name("id")
                    .description("Customer ID")
                    .type(RestParamType.path)
                    .dataType("int")
                .endParam()
                .produces(MediaType.APPLICATION_JSON)
                .route()
                    .bean(CustomerService.class, "findById")
                .endRest()

            .post()
                .description("Creates a new customer")
                .consumes(MediaType.APPLICATION_JSON)
                .produces(MediaType.APPLICATION_JSON)
                .type(Customer.class)
                .route()
                    .bean(CustomerService.class, "create")
                .endRest()

            .put("/{id}")
                .description("Updates the customer relating to the specified id")
                .param()
                    .name("id")
                    .description("Customer ID")
                    .type(RestParamType.path)
                    .dataType("int")
                .endParam()
                .consumes(MediaType.APPLICATION_JSON)
                .type(Customer.class)
                .route()
                    .bean(CustomerService.class, "update")
                .endRest()

            .delete("/{id}")
                .description("Deletes the customer relating to the specified id")
                .param()
                    .name("id")
                    .description("Customer ID")
                    .type(RestParamType.path)
                    .dataType("int")
                .endParam()
                .route()
                    .bean(CustomerService.class, "delete")
                .endRest();
    }