// Path: camel-rest-swagger/src/main/java/org/wildfly/camel/examples/rest/model/Customer.java
// @Entity
// @Table(name = "customer")
// public class Customer implements Serializable {
// 
//     private static final long serialVersionUID = -2372350530824400584L;
// 
//     @Id
//     @GeneratedValue(strategy= GenerationType.IDENTITY)
//     private Long id;
//     private String firstName;
//     private String lastName;
// 
//     public Long getId() {
//         return id;
//     }
// 
//     public void setId(Long id) {
//         this.id = id;
//     }
// 
//     public String getFirstName() {
//         return firstName;
//     }
// 
//     public void setFirstName(String firstName) {
//         this.firstName = firstName;
//     }
// 
//     public String getLastName() {
//         return lastName;
//     }
// 
//     public void setLastName(String lastName) {
//         this.lastName = lastName;
//     }
// 
//     @Override
//     public String toString() {
//         return "Customer{" + "id=" + id + ", firstName='" + firstName + '\'' + ", lastName='" + lastName + '\'' + '}';
//     }
// }
// 
// Path: camel-rest-swagger/src/main/java/org/wildfly/camel/examples/rest/service/CustomerService.java
// @ApplicationScoped
// @Named
// public class CustomerService {
// 
//     @Inject
//     CustomerRepository customerRepository;
// 
//     public void findAll(Exchange exchange) {
//         List<Customer> customers = customerRepository.findAll();
//         exchange.getOut().setBody(customers);
//     }
// 
//     public void findById(Exchange exchange) {
//         Long id = exchange.getIn().getHeader("id", Long.class);
//         Customer customer = customerRepository.findById(id);
// 
//         if (customer != null) {
//             exchange.getOut().setBody(customer);
//         } else {
//             exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, 404);
//         }
//     }
// 
//     public void create(Exchange exchange) {
//         Customer customer = customerRepository.save(exchange.getIn().getBody(Customer.class));
//         exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, 201);
// 
//         String URL = exchange.getIn().getHeader(Exchange.HTTP_URL, String.class);
//         exchange.getOut().setHeader("Location", URL + "/" + customer.getId());
// 
//         exchange.getOut().setBody(customer);
//     }
// 
//     public void update(Exchange exchange) {
//         Long id = exchange.getIn().getHeader("id", Long.class);
//         Customer customer = customerRepository.findById(id);
// 
//         if (customer != null) {
//             Customer updatedCustomer = customerRepository.save(exchange.getIn().getBody(Customer.class));
// 
//             String URL = exchange.getIn().getHeader(Exchange.HTTP_URL, String.class);
//             exchange.getOut().setHeader("Location", URL + "/" + updatedCustomer.getId());
//             exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, 204);
//             exchange.getOut().setBody(updatedCustomer);
//         } else {
//             exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, 404);
//         }
//     }
// 
//     public void delete(Exchange exchange) {
//         Long id = exchange.getIn().getHeader("id", Long.class);
//         Customer customer = customerRepository.findById(id);
// 
//         if (customer != null) {
//             customerRepository.delete(customer);
//             exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, 204);
//         } else {
//             exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, 404);
//         }
//     }
// }


import javax.enterprise.context.ApplicationScoped;
import javax.ws.rs.core.MediaType;
import com.fasterxml.jackson.core.JsonParseException;
import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.model.rest.RestBindingMode;
import org.apache.camel.model.rest.RestParamType;
import org.wildfly.camel.examples.rest.model.Customer;
import org.wildfly.camel.examples.rest.service.CustomerService;
/*
 * #%L
 * Wildfly Camel :: Example :: Camel Rest Swagger
 * %%
 * Copyright (C) 2013 - 2017 RedHat
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */
package org.wildfly.camel.examples.rest;




@ApplicationScoped
public class RestRouteBuilder extends RouteBuilder {

    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()
// Path: camel-rest-swagger/src/main/java/org/wildfly/camel/examples/rest/model/Customer.java
// @Entity
// @Table(name = "customer")
// public class Customer implements Serializable {
// 
//     private static final long serialVersionUID = -2372350530824400584L;
// 
//     @Id
//     @GeneratedValue(strategy= GenerationType.IDENTITY)
//     private Long id;
//     private String firstName;
//     private String lastName;
// 
//     public Long getId() {
//         return id;
//     }
// 
//     public void setId(Long id) {
//         this.id = id;
//     }
// 
//     public String getFirstName() {
//         return firstName;
//     }
// 
//     public void setFirstName(String firstName) {
//         this.firstName = firstName;
//     }
// 
//     public String getLastName() {
//         return lastName;
//     }
// 
//     public void setLastName(String lastName) {
//         this.lastName = lastName;
//     }
// 
//     @Override
//     public String toString() {
//         return "Customer{" + "id=" + id + ", firstName='" + firstName + '\'' + ", lastName='" + lastName + '\'' + '}';
//     }
// }
// 
// Path: camel-rest-swagger/src/main/java/org/wildfly/camel/examples/rest/service/CustomerService.java
// @ApplicationScoped
// @Named
// public class CustomerService {
// 
//     @Inject
//     CustomerRepository customerRepository;
// 
//     public void findAll(Exchange exchange) {
//         List<Customer> customers = customerRepository.findAll();
//         exchange.getOut().setBody(customers);
//     }
// 
//     public void findById(Exchange exchange) {
//         Long id = exchange.getIn().getHeader("id", Long.class);
//         Customer customer = customerRepository.findById(id);
// 
//         if (customer != null) {
//             exchange.getOut().setBody(customer);
//         } else {
//             exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, 404);
//         }
//     }
// 
//     public void create(Exchange exchange) {
//         Customer customer = customerRepository.save(exchange.getIn().getBody(Customer.class));
//         exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, 201);
// 
//         String URL = exchange.getIn().getHeader(Exchange.HTTP_URL, String.class);
//         exchange.getOut().setHeader("Location", URL + "/" + customer.getId());
// 
//         exchange.getOut().setBody(customer);
//     }
// 
//     public void update(Exchange exchange) {
//         Long id = exchange.getIn().getHeader("id", Long.class);
//         Customer customer = customerRepository.findById(id);
// 
//         if (customer != null) {
//             Customer updatedCustomer = customerRepository.save(exchange.getIn().getBody(Customer.class));
// 
//             String URL = exchange.getIn().getHeader(Exchange.HTTP_URL, String.class);
//             exchange.getOut().setHeader("Location", URL + "/" + updatedCustomer.getId());
//             exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, 204);
//             exchange.getOut().setBody(updatedCustomer);
//         } else {
//             exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, 404);
//         }
//     }
// 
//     public void delete(Exchange exchange) {
//         Long id = exchange.getIn().getHeader("id", Long.class);
//         Customer customer = customerRepository.findById(id);
// 
//         if (customer != null) {
//             customerRepository.delete(customer);
//             exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, 204);
//         } else {
//             exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, 404);
//         }
//     }
// }


// Path: camel-rest-swagger/src/main/java/org/wildfly/camel/examples/rest/RestRouteBuilder.java
import javax.enterprise.context.ApplicationScoped;
import javax.ws.rs.core.MediaType;
import com.fasterxml.jackson.core.JsonParseException;
import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.model.rest.RestBindingMode;
import org.apache.camel.model.rest.RestParamType;
import org.wildfly.camel.examples.rest.model.Customer;
import org.wildfly.camel.examples.rest.service.CustomerService;

/*
 * #%L
 * Wildfly Camel :: Example :: Camel Rest Swagger
 * %%
 * Copyright (C) 2013 - 2017 RedHat
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */
package org.wildfly.camel.examples.rest;




@ApplicationScoped
public class RestRouteBuilder extends RouteBuilder {

    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")