Coverage Report - org.apache.camel.component.cxf.transport.CamelDestination
 
Classes in this File Line Coverage Branch Coverage Complexity
CamelDestination
0% 
0% 
0
 
 1  
 /**
 2  
  *
 3  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 4  
  * contributor license agreements.  See the NOTICE file distributed with
 5  
  * this work for additional information regarding copyright ownership.
 6  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 7  
  * (the "License"); you may not use this file except in compliance with
 8  
  * the License.  You may obtain a copy of the License at
 9  
  *
 10  
  * http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 package org.apache.camel.component.cxf.transport;
 19  
 
 20  
 import org.apache.camel.CamelContext;
 21  
 import org.apache.camel.Endpoint;
 22  
 import org.apache.camel.Exchange;
 23  
 import org.apache.camel.Processor;
 24  
 import org.apache.camel.Producer;
 25  
 import org.apache.cxf.Bus;
 26  
 import org.apache.cxf.common.logging.LogUtils;
 27  
 import org.apache.cxf.configuration.Configurable;
 28  
 import org.apache.cxf.io.AbstractCachedOutputStream;
 29  
 import org.apache.cxf.message.Message;
 30  
 import org.apache.cxf.message.MessageImpl;
 31  
 import org.apache.cxf.service.model.EndpointInfo;
 32  
 import org.apache.cxf.transport.AbstractConduit;
 33  
 import org.apache.cxf.transport.AbstractDestination;
 34  
 import org.apache.cxf.transport.Conduit;
 35  
 import org.apache.cxf.transport.ConduitInitiator;
 36  
 import org.apache.cxf.transport.MessageObserver;
 37  
 import org.apache.cxf.ws.addressing.EndpointReferenceType;
 38  
 import org.apache.cxf.wsdl.EndpointReferenceUtils;
 39  
 
 40  
 import java.io.ByteArrayInputStream;
 41  
 import java.io.IOException;
 42  
 import java.io.InputStream;
 43  
 import java.io.OutputStream;
 44  
 import java.util.logging.Level;
 45  
 import java.util.logging.Logger;
 46  
 
 47  
 /**
 48  
  * @version $Revision: 535478 $
 49  
  */
 50  0
 public class CamelDestination extends AbstractDestination implements Configurable {
 51  
     protected static final String BASE_BEAN_NAME_SUFFIX = ".camel-destination-base";
 52  0
     private static final Logger LOG = LogUtils.getL7dLogger(CamelDestination.class);
 53  
     CamelContext camelContext;
 54  
     String camelUri;
 55  
     final ConduitInitiator conduitInitiator;
 56  
     private CamelTransportBase base;
 57  
     private Endpoint endpoint;
 58  
 
 59  
     public CamelDestination(CamelContext camelContext, Bus bus,
 60  
             ConduitInitiator ci,
 61  
             EndpointInfo info) throws IOException {
 62  0
         super(getTargetReference(info, bus), info);
 63  0
         this.camelContext = camelContext;
 64  
 
 65  0
         base = new CamelTransportBase(camelContext, bus, endpointInfo, true, BASE_BEAN_NAME_SUFFIX);
 66  
 
 67  0
         conduitInitiator = ci;
 68  
 
 69  0
         initConfig();
 70  0
     }
 71  
 
 72  
     protected Logger getLogger() {
 73  0
         return LOG;
 74  
     }
 75  
 
 76  
     /**
 77  
      * @param inMessage the incoming message
 78  
      * @return the inbuilt backchannel
 79  
      */
 80  
     protected Conduit getInbuiltBackChannel(Message inMessage) {
 81  0
         return new BackChannelConduit(EndpointReferenceUtils.getAnonymousEndpointReference(),
 82  
                 inMessage);
 83  
     }
 84  
 
 85  
     public void activate() {
 86  0
         getLogger().log(Level.INFO, "CamelDestination activate().... ");
 87  
 
 88  
         try {
 89  0
             getLogger().log(Level.FINE, "establishing Camel connection");
 90  0
             endpoint = camelContext.getEndpoint(camelUri);
 91  
         }
 92  0
         catch (Exception ex) {
 93  0
             getLogger().log(Level.SEVERE, "Camel connect failed with EException : ", ex);
 94  0
         }
 95  0
     }
 96  
 
 97  
     public void deactivate() {
 98  0
         base.close();
 99  0
     }
 100  
 
 101  
     public void shutdown() {
 102  0
         getLogger().log(Level.FINE, "CamelDestination shutdown()");
 103  0
         this.deactivate();
 104  0
     }
 105  
 
 106  
     protected void incoming(Exchange exchange) {
 107  0
         getLogger().log(Level.FINE, "server received request: ", exchange);
 108  
 
 109  0
         byte[] bytes = base.unmarshal(exchange);
 110  
 
 111  
         // get the message to be interceptor
 112  0
         MessageImpl inMessage = new MessageImpl();
 113  0
         inMessage.setContent(InputStream.class, new ByteArrayInputStream(bytes));
 114  0
         base.populateIncomingContext(exchange, inMessage, CamelConstants.CAMEL_SERVER_REQUEST_HEADERS);
 115  
         //inMessage.put(CamelConstants.CAMEL_SERVER_RESPONSE_HEADERS, new CamelMessageHeadersType());
 116  0
         inMessage.put(CamelConstants.CAMEL_REQUEST_MESSAGE, exchange);
 117  
 
 118  0
         inMessage.setDestination(this);
 119  
 
 120  
         //handle the incoming message
 121  0
         incomingObserver.onMessage(inMessage);
 122  0
     }
 123  
 
 124  
     public String getBeanName() {
 125  0
         return endpointInfo.getName().toString() + ".camel-destination";
 126  
     }
 127  
 
 128  
     private void initConfig() {
 129  
 /*
 130  
         this.runtimePolicy = endpointInfo.getTraversedExtensor(new ServerBehaviorPolicyType(),
 131  
                                                                ServerBehaviorPolicyType.class);
 132  
         this.serverConfig = endpointInfo.getTraversedExtensor(new ServerConfig(), ServerConfig.class);
 133  
         this.address = endpointInfo.getTraversedExtensor(new AddressType(), AddressType.class);
 134  
         this.sessionPool = endpointInfo.getTraversedExtensor(new SessionPoolType(), SessionPoolType.class);
 135  
 */
 136  0
     }
 137  
 
 138  0
     protected class ConsumerProcessor implements Processor {
 139  
         public void process(Exchange exchange) {
 140  
             try {
 141  0
                 incoming(exchange);
 142  
             }
 143  0
             catch (Throwable ex) {
 144  0
                 getLogger().log(Level.WARNING, "Failed to process incoming message : ", ex);
 145  0
             }
 146  0
         }
 147  
     }
 148  
 
 149  
     // this should deal with the cxf message
 150  
     protected class BackChannelConduit extends AbstractConduit {
 151  
         protected Message inMessage;
 152  
 
 153  0
         BackChannelConduit(EndpointReferenceType ref, Message message) {
 154  0
             super(ref);
 155  0
             inMessage = message;
 156  0
         }
 157  
 
 158  
         /**
 159  
          * Register a message observer for incoming messages.
 160  
          *
 161  
          * @param observer the observer to notify on receipt of incoming
 162  
          */
 163  
         public void setMessageObserver(MessageObserver observer) {
 164  
             // shouldn't be called for a back channel conduit
 165  0
         }
 166  
 
 167  
         /**
 168  
          * Send an outbound message, assumed to contain all the name-value
 169  
          * mappings of the corresponding input message (if any).
 170  
          *
 171  
          * @param message the message to be sent.
 172  
          */
 173  
         public void prepare(Message message) throws IOException {
 174  
             // setup the message to be send back
 175  0
             message.put(CamelConstants.CAMEL_REQUEST_MESSAGE,
 176  
                     inMessage.get(CamelConstants.CAMEL_REQUEST_MESSAGE));
 177  0
             message.setContent(OutputStream.class,
 178  
                     new CamelOutputStream(inMessage));
 179  0
         }
 180  
 
 181  
         protected Logger getLogger() {
 182  0
             return LOG;
 183  
         }
 184  
 
 185  
     }
 186  
 
 187  0
     private class CamelOutputStream extends AbstractCachedOutputStream {
 188  
         private Message inMessage;
 189  
         private Producer<Exchange> replyTo;
 190  
         private Producer<Exchange> sender;
 191  
 
 192  
         // setup the ByteArrayStream
 193  0
         public CamelOutputStream(Message m) {
 194  0
             super();
 195  0
             inMessage = m;
 196  0
         }
 197  
 
 198  
         // prepair the message and get the send out message
 199  
         private void commitOutputMessage() throws IOException {
 200  
 
 201  
             //setup the reply message
 202  0
             final String replyToUri = getReplyToDestination(inMessage);
 203  
 
 204  0
             base.template.send(replyToUri, new Processor() {
 205  0
                 public void process(Exchange reply) {
 206  0
                     base.marshal(currentStream.toString(), replyToUri, reply);
 207  
 
 208  0
                     setReplyCorrelationID(inMessage, reply);
 209  
 
 210  0
                     base.setMessageProperties(inMessage, reply);
 211  
 
 212  0
                     getLogger().log(Level.FINE, "just server sending reply: ", reply);
 213  0
                 }
 214  
             });
 215  0
         }
 216  
 
 217  
         @Override
 218  
         protected void doFlush() throws IOException {
 219  
             // Do nothing here
 220  0
         }
 221  
 
 222  
         @Override
 223  
         protected void doClose() throws IOException {
 224  0
             commitOutputMessage();
 225  0
         }
 226  
 
 227  
         @Override
 228  
         protected void onWrite() throws IOException {
 229  
             // Do nothing here
 230  0
         }
 231  
     }
 232  
 
 233  
     protected String getReplyToDestination(Message inMessage) {
 234  0
         if (inMessage.get(CamelConstants.CAMEL_REBASED_REPLY_TO) != null) {
 235  0
             return (String) inMessage.get(CamelConstants.CAMEL_REBASED_REPLY_TO);
 236  
         }
 237  
         else {
 238  0
             return base.getReplyDestination();
 239  
         }
 240  
     }
 241  
 
 242  
     protected void setReplyCorrelationID(Message inMessage, Exchange reply) {
 243  0
         Object value = inMessage.get(CamelConstants.CAMEL_CORRELATION_ID);
 244  0
         if (value != null) {
 245  0
             reply.getIn().setHeader(CamelConstants.CAMEL_CORRELATION_ID, value);
 246  
         }
 247  0
     }
 248  
 }