示例#1
0
  public void send(MessageContext context, OutMessage message) throws XFireException {
    if (message.getUri().equals(Channel.BACKCHANNEL_URI)) {
      final OutputStream out = (OutputStream) context.getProperty(Channel.BACKCHANNEL_URI);
      if (out != null) {
        final XMLStreamWriter writer =
            STAXUtils.createXMLStreamWriter(out, message.getEncoding(), context);

        message.getSerializer().writeMessage(message, writer, context);
      } else {
        throw new XFireRuntimeException("No backchannel exists for message");
      }

      try {
        Attachments atts = message.getAttachments();
        if (atts != null && atts.size() > 0) {
          writeAttachmentBody(context, message);
          // TODO response.setContentType(atts.getContentType());
          atts.write(out);
        } else {
          // TODO response.setContentType(getSoapMimeType(message));
          writeWithoutAttachments(context, message, out);
        }
      } catch (IOException e) {
        throw new XFireException("Couldn't send message.", e);
      }
    } else {
      try {
        sendViaClient(context, message);
      } catch (Exception e) {
        throw new XFireException("Failed to Send via MuleUniversalChannel: " + e.getMessage(), e);
      }
    }
  }
示例#2
0
  String getMimeType(AbstractMessage msg) {
    Attachments atts = msg.getAttachments();

    if (atts != null && atts.size() > 0) {
      return atts.getContentType();
    } else {
      return getSoapMimeType(msg);
    }
  }
示例#3
0
  void writeAttachmentBody(MessageContext context, OutMessage message) throws XFireException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    writeWithoutAttachments(context, message, bos);

    Attachments atts = message.getAttachments();

    ByteDataSource ds = new ByteDataSource(bos.toByteArray());
    ds.setContentType(getSoapMimeType(message));
    DataHandler dh = new DataHandler(ds);

    SimpleAttachment att = new SimpleAttachment("soap-message.xml", dh);

    atts.setSoapMessage(att);
  }
示例#4
0
  private void sendViaClient(final MessageContext context, final OutMessage message)
      throws Exception {
    OutputHandler handler =
        new OutputHandler() {
          public void write(UMOEvent event, OutputStream out) throws IOException {
            try {
              Attachments atts = message.getAttachments();
              if (atts != null && atts.size() > 0) {
                atts.write(out);
              } else {
                XMLStreamWriter writer =
                    STAXUtils.createXMLStreamWriter(out, message.getEncoding(), context);
                message.getSerializer().writeMessage(message, writer, context);
                try {
                  writer.flush();
                } catch (XMLStreamException e) {
                  logger.error(e);
                  throw new XFireException("Couldn't send message.", e);
                }
              }
            } catch (XFireException e) {
              logger.error("Couldn't send message.", e);
              throw new IOException(e.getMessage());
            }
          }

          public Map getHeaders(UMOEvent event) {
            Map headers = new HashMap();
            headers.put(HttpConstants.HEADER_CONTENT_TYPE, getSoapMimeType(message));
            headers.put(SoapConstants.SOAP_ACTION, message.getProperty(SoapConstants.SOAP_ACTION));
            UMOMessage msg = event.getMessage();
            for (Iterator iterator = msg.getPropertyNames().iterator(); iterator.hasNext(); ) {
              String headerName = (String) iterator.next();
              Object headerValue = msg.getStringProperty(headerName, null);

              // let us filter only MULE properties except MULE_USER,
              // Content-Type and Content-Lenght; all other properties are
              // allowed through including custom headers
              if ((!headerName.startsWith(MuleProperties.PROPERTY_PREFIX)
                      || (MuleProperties.MULE_USER_PROPERTY.compareTo(headerName) == 0))
                  && (!HttpConstants.HEADER_CONTENT_TYPE.equalsIgnoreCase(headerName))
                  && (!HttpConstants.HEADER_CONTENT_LENGTH.equalsIgnoreCase(headerName))) {
                headers.put(headerName, headerValue);
              }
            }

            return headers;
          }
        };

    // We can create a generic StreamMessageAdapter here as the underlying
    // transport will create one specific to the transport
    UMOStreamMessageAdapter sp = new StreamMessageAdapter(handler);
    sp.setProperty(HttpConnector.HTTP_METHOD_PROPERTY, HttpConstants.METHOD_POST);

    // set all properties on the message adapter
    UMOMessage msg = RequestContext.getEvent().getMessage();
    for (Iterator i = msg.getPropertyNames().iterator(); i.hasNext(); ) {
      String propertyName = (String) i.next();
      sp.setProperty(propertyName, msg.getProperty(propertyName));
    }

    UMOStreamMessageAdapter result = null;

    try {
      result = sendStream(getUri(), sp);
      if (result != null) {
        InMessage inMessage;
        String contentType = sp.getStringProperty(HttpConstants.HEADER_CONTENT_TYPE, "text/xml");
        InputStream in = result.getInputStream();
        if (contentType.toLowerCase().indexOf("multipart/related") != -1) {
          try {
            Attachments atts = new JavaMailAttachments(in, contentType);
            InputStream msgIs = atts.getSoapMessage().getDataHandler().getInputStream();
            inMessage =
                new InMessage(
                    STAXUtils.createXMLStreamReader(msgIs, message.getEncoding(), context),
                    getUri());
            inMessage.setAttachments(atts);
          } catch (MessagingException e) {
            throw new IOException(e.getMessage());
          }
        } else {
          inMessage =
              new InMessage(
                  STAXUtils.createXMLStreamReader(in, message.getEncoding(), context), getUri());
        }
        getEndpoint().onReceive(context, inMessage);
      }
    } finally {
      sp.release();
      if (result != null) {
        result.release();
      }
    }
  }