예제 #1
1
    /**
     * Called for an incoming message.
     *
     * @param inMessage
     */
    public void onMessage(Message inMessage) {
      // disposable exchange, swapped with real Exchange on correlation
      inMessage.setExchange(new ExchangeImpl());
      inMessage.getExchange().put(Bus.class, bus);
      inMessage.put(Message.DECOUPLED_CHANNEL_MESSAGE, Boolean.TRUE);
      inMessage.put(Message.RESPONSE_CODE, HttpURLConnection.HTTP_OK);

      // remove server-specific properties
      // inMessage.remove(AbstractHTTPDestination.HTTP_REQUEST);
      // inMessage.remove(AbstractHTTPDestination.HTTP_RESPONSE);
      inMessage.remove(Message.ASYNC_POST_RESPONSE_DISPATCH);
      updateResponseCode(inMessage);

      // cache this inputstream since it's defer to use in case of async
      try {
        InputStream in = inMessage.getContent(InputStream.class);
        if (in != null) {
          CachedOutputStream cos = new CachedOutputStream();
          IOUtils.copy(in, cos);
          inMessage.setContent(InputStream.class, cos.getInputStream());
        }
        observer.onMessage(inMessage);
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
예제 #2
0
 private void checkCachedStream(Message m, OutputStream osOriginal, boolean enabled)
     throws Exception {
   XMLStreamWriter writer = null;
   if (enabled) {
     writer = m.getContent(XMLStreamWriter.class);
   } else {
     writer = (XMLStreamWriter) m.get(XMLStreamWriter.class.getName());
   }
   if (writer instanceof CachingXmlEventWriter) {
     CachingXmlEventWriter cache = (CachingXmlEventWriter) writer;
     if (cache.getEvents().size() != 0) {
       XMLStreamWriter origWriter = null;
       try {
         origWriter = StaxUtils.createXMLStreamWriter(osOriginal);
         for (XMLEvent event : cache.getEvents()) {
           StaxUtils.writeEvent(event, origWriter);
         }
       } finally {
         StaxUtils.close(origWriter);
       }
     }
     m.setContent(XMLStreamWriter.class, null);
     return;
   }
   if (enabled) {
     OutputStream os = m.getContent(OutputStream.class);
     if (os != osOriginal && os instanceof CachedOutputStream) {
       CachedOutputStream cos = (CachedOutputStream) os;
       if (cos.size() != 0) {
         cos.writeCacheTo(osOriginal);
       }
     }
   }
 }
예제 #3
0
  protected void handle(Message message) throws SequenceFault, RMException {
    LOG.entering(getClass().getName(), "handleMessage");
    // This message capturing mechanism will need to be changed at some point.
    // Until then, we keep this interceptor here and utilize the robust
    // option to avoid the unnecessary message capturing/caching.
    if (!MessageUtils.isTrue(message.getContextualProperty(Message.ROBUST_ONEWAY))) {
      InputStream is = message.getContent(InputStream.class);
      if (is != null) {
        CachedOutputStream saved = new CachedOutputStream();
        try {
          IOUtils.copy(is, saved);

          saved.flush();
          is.close();
          saved.lockOutputStream();

          LOG.fine("Capturing the original RM message");
          RewindableInputStream ris = RewindableInputStream.makeRewindable(saved.getInputStream());
          message.setContent(InputStream.class, ris);
          message.put(RMMessageConstants.SAVED_CONTENT, ris);
        } catch (Exception e) {
          throw new Fault(e);
        }
      }
    }
  }
예제 #4
0
    @Override
    public void onClose(CachedOutputStream cos) {
      long l = LOG.logStart();
      byte[] buf = new byte[1024];
      int len;
      InputStream is;
      try {
        is = cos.getInputStream();
        while ((len = is.read(buf)) > 0) {
          logOutputStream.write(buf, 0, len);
        }
      } catch (IOException ex) {
        LOG.logError(l, ex);
      }
      LoggingMessage buffer = setupBuffer(message, fout);
      LOG.log(buffer.toString());

      try {
        // empty out the cache
        cos.lockOutputStream();
        cos.resetOut(null, false);
      } catch (Exception ex) {
        LOG.logWarn(l, "Error clearing cache: " + ex.getMessage(), null);
      }
      message.setContent(OutputStream.class, origStream);
    }
 protected void transformOS(Message message, OutputStream out) {
   CachedOutputStream wrapper = new CachedOutputStream();
   CachedOutputStreamCallback callback =
       new XSLTCachedOutputStreamCallback(getXSLTTemplate(), out);
   wrapper.registerCallback(callback);
   message.setContent(OutputStream.class, wrapper);
 }
예제 #6
0
  private void doFromSoapMessage(Message message, Object sm) {
    SOAPMessage m = (SOAPMessage) sm;
    MessageContentsList list = (MessageContentsList) message.getContent(List.class);
    if (list == null) {
      list = new MessageContentsList();
      message.setContent(List.class, list);
    }
    Object o = m;

    if (StreamSource.class.isAssignableFrom(type)) {
      try {
        try (CachedOutputStream out = new CachedOutputStream()) {
          XMLStreamWriter xsw = StaxUtils.createXMLStreamWriter(out);
          StaxUtils.copy(new DOMSource(m.getSOAPPart()), xsw);
          xsw.close();
          o = new StreamSource(out.getInputStream());
        }
      } catch (Exception e) {
        throw new Fault(e);
      }
    } else if (SAXSource.class.isAssignableFrom(type)) {
      o = new StaxSource(new W3CDOMStreamReader(m.getSOAPPart()));
    } else if (Source.class.isAssignableFrom(type)) {
      o = new DOMSource(m.getSOAPPart());
    }
    list.set(0, o);
  }
예제 #7
0
 private InputStream copyIn(InputStream in) throws Exception {
   try (CachedOutputStream bos = new CachedOutputStream()) {
     IOUtils.copyAndCloseInput(in, bos);
     in = bos.getInputStream();
     bos.close();
     return in;
   }
 }
예제 #8
0
 protected void writePayload(
     StringBuilder builder, CachedOutputStream cos, String encoding, String contentType)
     throws Exception {
   if (StringUtils.isEmpty(encoding)) {
     cos.writeCacheTo(builder, lim);
   } else {
     cos.writeCacheTo(builder, encoding, lim);
   }
 }
  @Override
  protected void marshalToWriter(
      Marshaller ms, Object obj, XMLStreamWriter writer, Annotation[] anns, MediaType mt)
      throws Exception {
    CachedOutputStream out = new CachedOutputStream();
    marshalToOutputStream(ms, obj, out, anns, mt);

    StaxUtils.copy(new StreamSource(out.getInputStream()), writer);
  }
  public static OutputStream getAttachmentStream(InputStream inputStream) throws IOException {

    if (inputStream != null) {
      CachedOutputStream cachedOutputStream = new CachedOutputStream();
      IOUtils.copy(inputStream, cachedOutputStream);
      cachedOutputStream.close();

      return cachedOutputStream.getOut();
    }

    return null;
  }
 private void handleOutputStream(final LogEvent event, Message message, CachedOutputStream cos)
     throws IOException {
   String encoding = (String) message.get(Message.ENCODING);
   if (StringUtils.isEmpty(encoding)) {
     encoding = "UTF-8";
   }
   StringBuilder payload = new StringBuilder();
   cos.writeCacheTo(payload, encoding, limit);
   cos.close();
   event.setPayload(payload.toString());
   boolean isTruncated = cos.size() > limit && limit != -1;
   event.setTruncated(isTruncated);
   event.setFullContentFile(cos.getTempFile());
 }
예제 #12
0
 @Override
 protected Object unmarshalFromReader(
     Unmarshaller unmarshaller, XMLStreamReader reader, Annotation[] anns, MediaType mt)
     throws JAXBException {
   CachedOutputStream out = new CachedOutputStream();
   try {
     XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(out);
     StaxUtils.copy(new StaxSource(reader), writer);
     writer.writeEndDocument();
     writer.flush();
     writer.close();
     return unmarshalFromInputStream(unmarshaller, out.getInputStream(), anns, mt);
   } catch (Exception ex) {
     throw ExceptionUtils.toBadRequestException(ex, null);
   }
 }
예제 #13
0
  @Override
  public void close(Message msg) throws IOException {
    CachedOutputStream output = (CachedOutputStream) msg.getContent(OutputStream.class);
    if (targetNode != null && output != null) {
      StringBuilder soapEnvelope = new StringBuilder();
      output.writeCacheTo(soapEnvelope);

      SimplePayload payload =
          new SimplePayload(
              "Envelope", "http://www.w3.org/2003/05/soap-envelope", soapEnvelope.toString());

      PayloadItem<SimplePayload> pi = new PayloadItem<SimplePayload>(payload);
      try {
        targetNode.send(pi);
      } catch (XMPPException e) {
        throw new IOException(e);
      }
    }
  }
예제 #14
0
    @Override
    public void close(Message message) throws IOException {
      if (Boolean.TRUE.equals(message.getExchange().get(LocalConduit.DIRECT_DISPATCH))) {
        final Exchange exchange = (Exchange) message.getExchange().get(LocalConduit.IN_EXCHANGE);

        MessageImpl copy = new MessageImpl();
        copy.putAll(message);
        MessageImpl.copyContent(message, copy);
        CachedOutputStream stream = (CachedOutputStream) message.getContent(OutputStream.class);
        copy.setContent(InputStream.class, stream.getInputStream());
        if (exchange != null && exchange.getInMessage() == null) {
          exchange.setInMessage(copy);
        }
        conduit.getMessageObserver().onMessage(copy);
        return;
      }

      super.close(message);
    }
예제 #15
0
    public void onClose(CachedOutputStream cos) {
      final LogEvent event = new DefaultLogEventMapper().map(message);
      try {
        String encoding = (String) message.get(Message.ENCODING);
        StringBuilder payload = new StringBuilder();
        writePayload(payload, cos, encoding, event.getContentType());
        event.setPayload(payload.toString());
      } catch (Exception ex) {
        // ignore
      }

      sender.send(event);
      try {
        // empty out the cache
        cos.lockOutputStream();
        cos.resetOut(null, false);
      } catch (Exception ex) {
        // ignore
      }
      message.setContent(OutputStream.class, origStream);
    }
 @Override
 public void onClose(CachedOutputStream wrapper) {
   InputStream transformedStream = null;
   try {
     transformedStream = XSLTUtils.transform(xsltTemplate, wrapper.getInputStream());
     IOUtils.copyAndCloseInput(transformedStream, origStream);
   } catch (IOException e) {
     throw new Fault("STREAM_COPY", LOG, e, e.getMessage());
   } finally {
     try {
       origStream.close();
     } catch (IOException e) {
       LOG.warning("Cannot close stream after transformation: " + e.getMessage());
     }
   }
 }
예제 #17
0
    public void onClose(CachedOutputStream cos) {
      LoggingMessage buffer = setupBuffer(message);

      String ct = (String) message.get(Message.CONTENT_TYPE);
      if (!isShowBinaryContent() && isBinaryContent(ct)) {
        buffer.getMessage().append(BINARY_CONTENT_MESSAGE).append('\n');
        log(logger, formatLoggingMessage(buffer));
        return;
      }
      if (!isShowMultipartContent() && isMultipartContent(ct)) {
        buffer.getMessage().append(MULTIPART_CONTENT_MESSAGE).append('\n');
        log(logger, formatLoggingMessage(buffer));
        return;
      }

      if (cos.getTempFile() == null) {
        // buffer.append("Outbound Message:\n");
        if (cos.size() >= lim) {
          buffer.getMessage().append("(message truncated to " + lim + " bytes)\n");
        }
      } else {
        buffer.getMessage().append("Outbound Message (saved to tmp file):\n");
        buffer.getMessage().append("Filename: " + cos.getTempFile().getAbsolutePath() + "\n");
        if (cos.size() >= lim) {
          buffer.getMessage().append("(message truncated to " + lim + " bytes)\n");
        }
      }
      try {
        String encoding = (String) message.get(Message.ENCODING);
        writePayload(buffer.getPayload(), cos, encoding, ct);
      } catch (Exception ex) {
        // ignore
      }

      log(logger, formatLoggingMessage(buffer));
      try {
        // empty out the cache
        cos.lockOutputStream();
        cos.resetOut(null, false);
      } catch (Exception ex) {
        // ignore
      }
      message.setContent(OutputStream.class, origStream);
    }