View Javadoc

1   /*
2    * $Id: MuleUniversalDestination.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.module.cxf.transport;
12  
13  import org.mule.module.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              DelegatingOutputStream stream = new DelegatingOutputStream(new ByteArrayOutputStream());
95              message.setContent(OutputStream.class, stream);
96              message.setContent(DelegatingOutputStream.class, stream);
97          }
98  
99          @Override
100         public void close(Message message) throws IOException
101         {
102             message.getContent(OutputStream.class).close();
103         }
104 
105         @Override
106         protected Logger getLogger()
107         {
108             return LOGGER;
109         }
110 
111     }
112 }