001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package loanbroker;
018:
019: import javax.jbi.messaging.ExchangeStatus;
020: import javax.jbi.messaging.Fault;
021: import javax.jbi.messaging.InOut;
022: import javax.jbi.messaging.MessageExchange;
023: import javax.jbi.messaging.MessagingException;
024: import javax.jbi.messaging.NormalizedMessage;
025: import javax.xml.namespace.QName;
026: import javax.xml.transform.TransformerException;
027:
028: import org.apache.servicemix.MessageExchangeListener;
029: import org.apache.servicemix.components.util.ComponentSupport;
030: import org.apache.servicemix.jbi.jaxp.SourceTransformer;
031: import org.apache.servicemix.jbi.jaxp.StringSource;
032: import org.apache.servicemix.jbi.util.DOMUtil;
033: import org.w3c.dom.Document;
034: import org.w3c.dom.Element;
035: import org.w3c.dom.Node;
036: import org.w3c.dom.traversal.NodeIterator;
037:
038: import com.sun.org.apache.xpath.internal.CachedXPathAPI;
039:
040: public class CreditAgency extends ComponentSupport implements
041: MessageExchangeListener {
042:
043: public CreditAgency() {
044: setService(new QName("urn:logicblaze:soa:creditagency",
045: "CreditAgencyService"));
046: setEndpoint("agency");
047: }
048:
049: public void onMessageExchange(MessageExchange exchange)
050: throws MessagingException {
051: InOut inOut = (InOut) exchange;
052: if (inOut.getStatus() == ExchangeStatus.DONE) {
053: return;
054: } else if (inOut.getStatus() == ExchangeStatus.ERROR) {
055: return;
056: }
057: try {
058: Document doc = (Document) new SourceTransformer()
059: .toDOMNode(inOut.getInMessage());
060: String ssn = textValueOfXPath(doc,
061: "//*[local-name()='ssn']");
062: if (ssn == null || ssn.length() == 0) {
063: fail(exchange, new NullPointerException());
064: return;
065: }
066: if (!ssn.startsWith("1")) {
067: Fault fault = inOut.createFault();
068: fault.setContent(new StringSource(
069: "<InvalidSSN xmlns=\"urn:logicblaze:soa:creditagency\"><ssn>"
070: + ssn + "</ssn></InvalidSSN>"));
071: fail(inOut, fault);
072: } else {
073: String operation = null;
074: if (inOut.getOperation() != null) {
075: operation = inOut.getOperation().getLocalPart();
076: } else {
077: operation = doc.getDocumentElement().getLocalName();
078: }
079: String output;
080: if ("getCreditScore".equals(operation)) {
081: output = "<getCreditScoreResponse xmlns=\"urn:logicblaze:soa:creditagency\"><score>"
082: + getCreditScore(ssn)
083: + "</score></getCreditScoreResponse>";
084: } else if ("getCreditHistoryLength".equals(operation)) {
085: output = "<getCreditHistoryLengthResponse xmlns=\"urn:logicblaze:soa:creditagency\"><length>"
086: + getCreditHistoryLength(ssn)
087: + "</length></getCreditHistoryLengthResponse>";
088: } else {
089: throw new UnsupportedOperationException(operation);
090: }
091: NormalizedMessage answer = inOut.createMessage();
092: answer.setContent(new StringSource(output));
093: answer(inOut, answer);
094: }
095: } catch (Exception e) {
096: throw new MessagingException(e);
097: }
098: }
099:
100: int getCreditScore(String ssn) {
101: //return ((int) (Math.random() * 600) + 300);
102: return 1000;
103: }
104:
105: int getCreditHistoryLength(String ssn) {
106: //return ((int) (Math.random() * 19) + 1);
107: return 10;
108: }
109:
110: protected String textValueOfXPath(Node node, String xpath)
111: throws TransformerException {
112: CachedXPathAPI cachedXPathAPI = new CachedXPathAPI();
113: NodeIterator iterator = cachedXPathAPI.selectNodeIterator(node,
114: xpath);
115: Node root = iterator.nextNode();
116: if (root instanceof Element) {
117: Element element = (Element) root;
118: if (element == null) {
119: return "";
120: }
121: String text = DOMUtil.getElementText(element);
122: return text;
123: } else if (root != null) {
124: return root.getNodeValue();
125: } else {
126: return null;
127: }
128: }
129: }
|