View Javadoc

1   /*
2    * $Id: MuleUniversalDestination.java 11646 2008-04-25 20:28:53Z dandiep $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.transport.cxf.transport;
12  
13  import org.mule.transport.cxf.support.DelegatingOutputStream;
14  
15  import java.io.ByteArrayOutputStream;
16  import java.io.IOException;
17  import java.io.OutputStream;
18  import java.net.HttpURLConnection;
19  import java.util.logging.Logger;
20  
21  import org.apache.cxf.message.Message;
22  import org.apache.cxf.service.model.EndpointInfo;
23  import org.apache.cxf.transport.AbstractConduit;
24  import org.apache.cxf.transport.AbstractDestination;
25  import org.apache.cxf.transport.Conduit;
26  import org.apache.cxf.transport.ConduitInitiator;
27  import org.apache.cxf.ws.addressing.EndpointReferenceType;
28  
29  public class MuleUniversalDestination extends AbstractDestination
30  {
31      public static final String RESPONSE_OBSERVER = "mule.destination.response.observer";
32  
33      private static final Logger LOGGER = Logger.getLogger(MuleUniversalDestination.class.getName());
34      private MuleUniversalTransport transport;
35  
36      public MuleUniversalDestination(MuleUniversalTransport transport,
37                                      EndpointReferenceType ref,
38                                      EndpointInfo ei)
39      {
40          super(ref, ei); 
41          this.transport = transport;
42      }
43  
44      @Override
45      protected Conduit getInbuiltBackChannel(Message inMessage)
46      {
47          return new ResponseConduit(null);
48      }
49  
50      @Override
51      protected Logger getLogger()
52      {
53          return LOGGER;
54      }
55  
56      @Override
57      public void shutdown()
58      {
59          transport.remove(this);
60  
61          super.shutdown();
62      }
63  
64      @Override
65      protected boolean markPartialResponse(Message partialResponse, EndpointReferenceType decoupledTarget)
66      {
67          // setup the outbound message to for 202 Accepted
68          partialResponse.put(Message.RESPONSE_CODE, HttpURLConnection.HTTP_ACCEPTED);
69          partialResponse.getExchange().put(EndpointReferenceType.class, decoupledTarget);
70          return true;
71      }
72  
73      /**
74       * @return the associated conduit initiator, or null if decoupled mode not
75       *         supported.
76       */
77      @Override
78      protected ConduitInitiator getConduitInitiator()
79      {
80          return transport;
81      }
82  
83      public class ResponseConduit extends AbstractConduit
84      {
85  
86          public ResponseConduit(EndpointReferenceType arg0)
87          {
88              super(arg0);
89          }
90  
91          public void prepare(Message message) throws IOException {
92              // set an outputstream which will be used for things like attachment headers.
93              // we'll stream the body later on down the line via the OutputHandler in CxfServiceComponent
94              message.setContent(OutputStream.class, new DelegatingOutputStream(new ByteArrayOutputStream()));
95          }
96  
97          @Override
98          public void close(Message message) throws IOException
99          {
100             message.getContent(OutputStream.class).close();
101         }
102 
103         @Override
104         protected Logger getLogger()
105         {
106             return LOGGER;
107         }
108 
109     }
110 }