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 org.apache.camel.example.cxf.provider;
018    
019    import java.util.List;
020    
021    import javax.xml.namespace.QName;
022    import javax.xml.soap.MessageFactory;
023    import javax.xml.soap.SOAPBody;
024    import javax.xml.soap.SOAPBodyElement;
025    import javax.xml.soap.SOAPElement;
026    import javax.xml.soap.SOAPEnvelope;
027    import javax.xml.soap.SOAPException;
028    import javax.xml.soap.SOAPMessage;
029    import javax.xml.soap.SOAPPart;
030    
031    import org.apache.camel.Exchange;
032    
033    /**
034     * A simple bean demonstrating the processing of a SOAPMessage routed by Camel
035     *
036     */
037    //START SNIPPET: e1
038    public class TesterBean {
039    
040        public SOAPMessage processSOAP(Exchange exchange) {
041            // Since the Camel-CXF endpoint uses a list to store the parameters
042            // and bean component uses the bodyAs expression to get the value
043            // we'll need to deal with the parameters ourself
044            SOAPMessage soapMessage = (SOAPMessage)exchange.getIn().getBody(List.class).get(0);
045            if (soapMessage == null) {
046                System.out.println("Incoming null message detected...");
047                return createDefaultSoapMessage("Greetings from Apache Camel!!!!", "null");
048            }
049    
050            try {
051                SOAPPart sp = soapMessage.getSOAPPart();
052                SOAPEnvelope se = sp.getEnvelope();
053                SOAPBody sb = se.getBody();
054                String requestText = sb.getFirstChild().getTextContent();
055                System.out.println(requestText);
056                return createDefaultSoapMessage("Greetings from Apache Camel!!!!", requestText);
057            } catch (Exception e) {
058                e.printStackTrace();
059                return createDefaultSoapMessage("Greetings from Apache Camel!!!!", e.getClass().getName());
060            }
061        }
062    
063        public static SOAPMessage createDefaultSoapMessage(String responseMessage, String requestMessage) {
064            try {
065                SOAPMessage soapMessage = MessageFactory.newInstance().createMessage();
066                SOAPBody body = soapMessage.getSOAPPart().getEnvelope().getBody();
067    
068                QName payloadName = new QName("http://apache.org/hello_world_soap_http/types", "greetMeResponse",
069                                              "ns1");
070    
071                SOAPBodyElement payload = body.addBodyElement(payloadName);
072    
073                SOAPElement message = payload.addChildElement("responseType");
074    
075                message.addTextNode(responseMessage + " Request was  " + requestMessage);
076                return soapMessage;
077            } catch (SOAPException e) {
078                e.printStackTrace();
079                throw new RuntimeException(e);
080            }
081    
082        }
083    
084    }
085    //END SNIPPET: e1