Skip to content

Commit

Permalink
SWITCHYARD-2045 Refactor camel-sap quickstart
Browse files Browse the repository at this point in the history
Refactored quickstart - split camel routes into indivisual services and convert some CDI beans into MessageComposer
  • Loading branch information
igarashitm authored and rcernich committed Apr 10, 2014
1 parent a3570c1 commit 4ddd0d5
Show file tree
Hide file tree
Showing 23 changed files with 685 additions and 758 deletions.
4 changes: 4 additions & 0 deletions camel-sap-binding/pom.xml
Expand Up @@ -72,6 +72,10 @@
<groupId>org.switchyard</groupId>
<artifactId>switchyard-validate</artifactId>
</dependency>
<dependency>
<groupId>org.switchyard.components</groupId>
<artifactId>switchyard-component-bean</artifactId>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
Expand Down
Expand Up @@ -14,14 +14,18 @@
* permissions and limitations under the License.
*
*/
package org.switchyard.quickstarts.camel.sap.binding.processor;
package org.switchyard.quickstarts.camel.sap.binding;

import javax.inject.Named;

import org.apache.camel.Exchange;
import org.apache.camel.processor.aggregate.AggregationStrategy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.switchyard.quickstarts.camel.sap.binding.bean.FlightConnectionInfo;
import org.switchyard.quickstarts.camel.sap.binding.bean.FlightCustomerInfo;
import org.switchyard.quickstarts.camel.sap.binding.bean.FlightTripRequestInfo;
import org.switchyard.quickstarts.camel.sap.binding.bean.PassengerInfo;

/**
* @author William Collins <punkhornsw@gmail.com>
Expand All @@ -31,16 +35,22 @@
public class AggregateFlightBookingStrategy implements AggregationStrategy {

private static final Logger LOG = LoggerFactory.getLogger(AggregateFlightBookingStrategy.class);
public static final String FLIGHT_CONNECTION_INFO = "flightConnectionInfo";
public static final String FLIGHT_CUSTOMER_INFO = "flightCustomerInfo";
public static final String PASSENGER_INFO = "passengerInfo";

/**
* Merges the message headers of sub-routes.
* <p>{@inheritDoc}
*/
@Override
public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
Exchange answer = oldExchange == null ? newExchange : oldExchange;
FlightTripRequestInfo flightTripRequestInfo;
Object payload = answer.getIn().getBody();
if (payload instanceof FlightTripRequestInfo) {
flightTripRequestInfo = FlightTripRequestInfo.class.cast(payload);
} else {
flightTripRequestInfo = new FlightTripRequestInfo();
}

String to = newExchange.getProperty(Exchange.TO_ENDPOINT, String.class);
if (LOG.isDebugEnabled()) {
LOG.debug("To endpoint = {}", to);
Expand All @@ -49,38 +59,19 @@ public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
if (LOG.isDebugEnabled()) {
LOG.debug("Adding Flight Connection Info to exchange.");
}
return mergeHeaderIntoOldExchange(FLIGHT_CONNECTION_INFO, oldExchange, newExchange);
flightTripRequestInfo.setFlightConnectionInfo(newExchange.getIn().getBody(FlightConnectionInfo.class));
} else if (to.contains("FlightCustomerInfo")){
if (LOG.isDebugEnabled()) {
LOG.debug("Adding Flight Customer Info to exchange.");
}
return mergeHeaderIntoOldExchange(FLIGHT_CUSTOMER_INFO, oldExchange, newExchange);
flightTripRequestInfo.setFlightCustomerInfo(newExchange.getIn().getBody(FlightCustomerInfo.class));
} else {
if (LOG.isDebugEnabled()) {
LOG.debug("Adding Passenger Info to exchange.");
}
return mergeHeaderIntoOldExchange(PASSENGER_INFO, oldExchange, newExchange);
flightTripRequestInfo.setPassengerInfo(newExchange.getIn().getBody(PassengerInfo.class));
}
}

/**
* Merges the message header of input message in new exchange
* into exchange property of old exchange.
*
* @param messageHeader
* - the name of message header to merge.
* @param oldExchange
* - the old exchange.
* @param newExchange
* - the new exchange.
* @return The merged exchange.
*/
public Exchange mergeHeaderIntoOldExchange(String messageHeader, Exchange oldExchange, Exchange newExchange) {

Exchange answer = oldExchange == null ? newExchange : oldExchange;

answer.setProperty(messageHeader, newExchange.getIn().getHeader(messageHeader));

answer.getIn().setBody(flightTripRequestInfo);
return answer;
}

Expand Down
@@ -1,7 +1,8 @@
package org.switchyard.quickstarts.camel.sap.binding;

import org.fusesource.camel.component.sap.model.rfc.Structure;
import org.switchyard.quickstarts.camel.sap.binding.bean.FlightTripRequestInfo;
import org.switchyard.quickstarts.camel.sap.binding.jaxb.BookFlightResponse;

public interface CreateFlightTripService {
Structure invoke(Structure request);
BookFlightResponse invoke(FlightTripRequestInfo request);
}
@@ -0,0 +1,8 @@
package org.switchyard.quickstarts.camel.sap.binding;

import org.switchyard.quickstarts.camel.sap.binding.bean.FlightConnectionInfo;
import org.switchyard.quickstarts.camel.sap.binding.jaxb.BookFlightRequest;

public interface FlightConnectionInfoService {
FlightConnectionInfo getFlightConnectionInfo(BookFlightRequest request);
}
@@ -0,0 +1,8 @@
package org.switchyard.quickstarts.camel.sap.binding;

import org.switchyard.quickstarts.camel.sap.binding.bean.FlightCustomerInfo;
import org.switchyard.quickstarts.camel.sap.binding.jaxb.BookFlightRequest;

public interface FlightCustomerInfoService {
FlightCustomerInfo getFlightCustomerInfo(BookFlightRequest request);
}
@@ -1,7 +1,8 @@
package org.switchyard.quickstarts.camel.sap.binding;

import org.fusesource.camel.component.sap.model.rfc.Structure;
import org.switchyard.quickstarts.camel.sap.binding.bean.FlightConnectionInfo;

public interface GetFlightConnectionDetailService {
Structure invoke(Structure request);
FlightConnectionInfo invoke(Structure request);
}
@@ -1,7 +1,8 @@
package org.switchyard.quickstarts.camel.sap.binding;

import org.fusesource.camel.component.sap.model.rfc.Structure;
import org.switchyard.quickstarts.camel.sap.binding.jaxb.BookFlightRequest;

public interface GetFlightConnectionListService {
Structure invoke(Structure request);
Structure invoke(BookFlightRequest request);
}

This file was deleted.

@@ -0,0 +1,8 @@
package org.switchyard.quickstarts.camel.sap.binding;

import org.switchyard.quickstarts.camel.sap.binding.bean.PassengerInfo;
import org.switchyard.quickstarts.camel.sap.binding.jaxb.BookFlightRequest;

public interface PassengerInfoService {
PassengerInfo getPassengerInfo(BookFlightRequest request);
}
Expand Up @@ -14,13 +14,11 @@
* permissions and limitations under the License.
*
*/
package org.switchyard.quickstarts.camel.sap.binding.processor;
package org.switchyard.quickstarts.camel.sap.binding;

import java.util.Date;

import javax.inject.Named;

import org.apache.camel.Exchange;
import org.switchyard.component.bean.Service;
import org.switchyard.quickstarts.camel.sap.binding.bean.PassengerInfo;
import org.switchyard.quickstarts.camel.sap.binding.jaxb.BookFlightRequest;
import org.slf4j.Logger;
Expand All @@ -32,10 +30,10 @@
* @author William Collins <punkhornsw@gmail.com>
*
*/
@Named("returnPassengerInfo")
public class ReturnPassengerInfo {
@Service(PassengerInfoService.class)
public class PassengerInfoServiceBean implements PassengerInfoService {

private static final Logger LOG = LoggerFactory.getLogger(ReturnPassengerInfo.class);
private static final Logger LOG = LoggerFactory.getLogger(PassengerInfoServiceBean.class);


/**
Expand All @@ -45,16 +43,13 @@ public class ReturnPassengerInfo {
* @param exchange
* @throws Exception
*/
public void createPassengerInfo(Exchange exchange) {

// Retrieve SAP request object from body of exchange message.
BookFlightRequest bookFlightRequest = exchange.getIn().getBody(BookFlightRequest.class);
public PassengerInfo getPassengerInfo(BookFlightRequest request) {

// Create passenger info bean.
PassengerInfo passengerInfo = new PassengerInfo();

// Add passenger form of address to passenger info if set.
String passengerFormOfAddress = bookFlightRequest.getPassengerFormOfAddress();
String passengerFormOfAddress = request.getPassengerFormOfAddress();
if (passengerFormOfAddress != null) {
passengerInfo.setFormOfAddress(passengerFormOfAddress);
if (LOG.isDebugEnabled()) {
Expand All @@ -63,7 +58,7 @@ public void createPassengerInfo(Exchange exchange) {
}

// Add passenger name to passenger info if set.
String passengerName = bookFlightRequest.getPassengerName();
String passengerName = request.getPassengerName();
if (passengerName != null) {
passengerInfo.setName(passengerName);
if (LOG.isDebugEnabled()) {
Expand All @@ -72,16 +67,15 @@ public void createPassengerInfo(Exchange exchange) {
}

// Add passenger date of birth to passenger info if set.
Date passengerDateOfBirth = bookFlightRequest.getPassengerDateOfBirth();
Date passengerDateOfBirth = request.getPassengerDateOfBirth();
if (passengerDateOfBirth != null) {
passengerInfo.setDateOfBirth(passengerDateOfBirth);
if (LOG.isDebugEnabled()) {
LOG.debug("Set passenger date of birth = '{}' in passenger info", passengerDateOfBirth);
}
}

// Put passenger info bean into header of exchange message.
exchange.getIn().setHeader(AggregateFlightBookingStrategy.PASSENGER_INFO, passengerInfo);
return passengerInfo;
}

}
@@ -0,0 +1,52 @@
/**
* Copyright 2013 Red Hat, Inc.
*
* Red Hat licenses this file to you 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.
*
*/
package org.switchyard.quickstarts.camel.sap.binding.bean;

/**
* A composite object contains FlightConnectionInfo, FlightCustomerInfo and PassengerInfo.
* Those 3 objects are needed to book a trip.
*/
public class FlightTripRequestInfo {
FlightConnectionInfo _flightConnectionInfo;
FlightCustomerInfo _flightCustomerInfo;
PassengerInfo _passengerInfo;

public FlightConnectionInfo getFlightConnectionInfo() {
return _flightConnectionInfo;
}

public FlightCustomerInfo getFlightCustomerInfo() {
return _flightCustomerInfo;
}

public PassengerInfo getPassengerInfo() {
return _passengerInfo;
}

public void setFlightConnectionInfo(FlightConnectionInfo _flightConnectionInfo) {
this._flightConnectionInfo = _flightConnectionInfo;
}

public void setFlightCustomerInfo(FlightCustomerInfo _flightCustomerInfo) {
this._flightCustomerInfo = _flightCustomerInfo;
}

public void setPassengerInfo(PassengerInfo _passengerInfo) {
this._passengerInfo = _passengerInfo;
}

}

0 comments on commit 4ddd0d5

Please sign in to comment.