View Javadoc

    1   package org.codehaus.xfire.transport.http;
    2
    3   import java.io.IOException;
    4   import java.io.OutputStream;
    5
    6   import javax.servlet.http.HttpServletResponse;
    7   import javax.xml.stream.XMLStreamWriter;
    8
    9   import org.codehaus.xfire.MessageContext;
    10  import org.codehaus.xfire.XFireException;
    11  import org.codehaus.xfire.XFireRuntimeException;
    12  import org.codehaus.xfire.attachments.Attachments;
    13  import org.codehaus.xfire.exchange.OutMessage;
    14  import org.codehaus.xfire.transport.AbstractChannel;
    15  import org.codehaus.xfire.transport.Channel;
    16  import org.codehaus.xfire.util.STAXUtils;
    17
    18  public class HttpSoapChannel
    19      extends AbstractChannel
    20  {
    21      public HttpSoapChannel(String uri, SoapHttpTransport transport)
    22      {
    23          setTransport(transport);
    24          setUri(uri);
    25      }
    26
    27      public void open()
    28      {
    29      }
    30
    31      public void send(MessageContext context, OutMessage message) throws XFireException
    32      {
    33          if (message.getUri().equals(Channel.BACKCHANNEL_URI))
    34          {
    35              HttpServletResponse response = XFireServletController.getResponse();
    36
    37              if (response == null)
    38              {
    39                  throw new XFireRuntimeException("No backchannel exists for message");
    40              }
    41
    42              Attachments atts = message.getAttachments();
    43              if (atts != null && atts.size() > 0)
    44              {
    45                  throw new UnsupportedOperationException("Sending attachments isn't supported at this time.");
    46              }
    47
    48              try
    49              {
    50                  OutputStream out = response.getOutputStream();
    51                  XMLStreamWriter writer = STAXUtils.createXMLStreamWriter(out, message.getEncoding());
    52
    53                  message.getSerializer().writeMessage(message, writer, context);
    54
    55                  out.flush();
    56                  out.close();
    57              }
    58              catch (Exception e)
    59              {
    60                  if (e instanceof XFireException)
    61                      throw (XFireException) e;
    62
    63                  throw new XFireException("Couldn't send message.", e);
    64              }
    65          }
    66          else
    67          {
    68              sendViaClient(context, message);
    69          }
    70      }
    71
    72      private void sendViaClient(MessageContext context, OutMessage message)
    73          throws XFireException
    74      {
    75          HttpMessageSender sender = new HttpMessageSender(message.getUri(), message.getEncoding());
    76          try
    77          {
    78              sender.setAction(message.getAction());
    79              sender.open();
    80
    81              OutputStream out = sender.getOutputStream();
    82              XMLStreamWriter writer = STAXUtils.createXMLStreamWriter(out, message.getEncoding());
    83
    84              message.getSerializer().writeMessage(message, writer, context);
    85
    86              out.flush();
    87              out.close();
    88
    89              getEndpoint().onReceive(context, sender.getInMessage());
    90          }
    91          catch (IOException e)
    92          {
    93              throw new XFireException("Couldn't send message.", e);
    94          }
    95          finally
    96          {
    97              sender.close();
    98          }
    99      }
    100
    101     public void close()
    102     {
    103     }
    104 }