コード例 #1
0
ファイル: BoonDataFormat.java プロジェクト: jonmcewen/camel
 @Override
 public void marshal(Exchange exchange, Object graph, OutputStream stream) throws Exception {
   BufferedWriter writer =
       IOHelper.buffered(new OutputStreamWriter(stream, IOHelper.getCharsetName(exchange)));
   objectMapper.toJson(graph, writer);
   writer.close();
 }
コード例 #2
0
ファイル: BoonDataFormat.java プロジェクト: jonmcewen/camel
 @Override
 public Object unmarshal(Exchange exchange, InputStream stream) throws Exception {
   BufferedReader reader =
       IOHelper.buffered(new InputStreamReader(stream, IOHelper.getCharsetName(exchange)));
   Object result = objectMapper.fromJson(reader, this.unmarshalType);
   reader.close();
   return result;
 }
コード例 #3
0
  /**
   * Creates a holder object for the data to send to the remote server.
   *
   * @param exchange the exchange with the IN message with data to send
   * @return the data holder
   * @throws CamelExchangeException is thrown if error creating RequestEntity
   */
  protected RequestEntity createRequestEntity(Exchange exchange) throws CamelExchangeException {
    Message in = exchange.getIn();
    if (in.getBody() == null) {
      return null;
    }

    RequestEntity answer = in.getBody(RequestEntity.class);
    if (answer == null) {
      try {
        Object data = in.getBody();
        if (data != null) {
          String contentType = ExchangeHelper.getContentType(exchange);

          if (contentType != null
              && HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT.equals(contentType)) {
            // serialized java object
            Serializable obj = in.getMandatoryBody(Serializable.class);
            // write object to output stream
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            HttpHelper.writeObjectToStream(bos, obj);
            answer =
                new ByteArrayRequestEntity(
                    bos.toByteArray(), HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT);
            IOHelper.close(bos);
          } else if (data instanceof File || data instanceof GenericFile) {
            // file based (could potentially also be a FTP file etc)
            File file = in.getBody(File.class);
            if (file != null) {
              answer = new FileRequestEntity(file, contentType);
            }
          } else if (data instanceof String) {
            // be a bit careful with String as any type can most likely be converted to String
            // so we only do an instanceof check and accept String if the body is really a String
            // do not fallback to use the default charset as it can influence the request
            // (for example application/x-www-form-urlencoded forms being sent)
            String charset = IOHelper.getCharsetName(exchange, false);
            answer = new StringRequestEntity((String) data, contentType, charset);
          }
          // fallback as input stream
          if (answer == null) {
            // force the body as an input stream since this is the fallback
            InputStream is = in.getMandatoryBody(InputStream.class);
            answer = new InputStreamRequestEntity(is, contentType);
          }
        }
      } catch (UnsupportedEncodingException e) {
        throw new CamelExchangeException(
            "Error creating RequestEntity from message body", exchange, e);
      } catch (IOException e) {
        throw new CamelExchangeException("Error serializing message body", exchange, e);
      }
    }
    return answer;
  }
コード例 #4
0
  public void marshal(Exchange exchange, Object graph, OutputStream stream) throws Exception {
    InputStream is =
        exchange.getContext().getTypeConverter().mandatoryConvertTo(InputStream.class, graph);

    GZIPOutputStream zipOutput = new GZIPOutputStream(stream);
    try {
      IOHelper.copy(is, zipOutput);
    } finally {
      IOHelper.close(is);
      IOHelper.close(zipOutput);
    }
  }
コード例 #5
0
  public Object unmarshal(Exchange exchange, InputStream stream) throws Exception {
    InputStream is = ExchangeHelper.getMandatoryInBody(exchange, InputStream.class);
    GZIPInputStream unzipInput = new GZIPInputStream(is);

    // Create an expandable byte array to hold the inflated data
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try {
      IOHelper.copy(unzipInput, bos);
      return bos.toByteArray();
    } finally {
      IOHelper.close(unzipInput);
    }
  }
コード例 #6
0
ファイル: SftpOperations.java プロジェクト: nkukhar/camel
  @SuppressWarnings("unchecked")
  private boolean retrieveFileToStreamInBody(String name, Exchange exchange)
      throws GenericFileOperationFailedException {
    OutputStream os = null;
    String currentDir = null;
    try {
      GenericFile<ChannelSftp.LsEntry> target =
          (GenericFile<ChannelSftp.LsEntry>) exchange.getProperty(FileComponent.FILE_EXCHANGE_FILE);
      ObjectHelper.notNull(
          target, "Exchange should have the " + FileComponent.FILE_EXCHANGE_FILE + " set");

      String remoteName = name;
      if (endpoint.getConfiguration().isStepwise()) {
        // remember current directory
        currentDir = getCurrentDirectory();

        // change directory to path where the file is to be retrieved
        // (must do this as some FTP servers cannot retrieve using absolute path)
        String path = FileUtil.onlyPath(name);
        if (path != null) {
          changeCurrentDirectory(path);
        }
        // remote name is now only the file name as we just changed directory
        remoteName = FileUtil.stripPath(name);
      }

      // use input stream which works with Apache SSHD used for testing
      InputStream is = channel.get(remoteName);

      if (endpoint.getConfiguration().isStreamDownload()) {
        target.setBody(is);
        exchange.getIn().setHeader(RemoteFileComponent.REMOTE_FILE_INPUT_STREAM, is);
      } else {
        os = new ByteArrayOutputStream();
        target.setBody(os);
        IOHelper.copyAndCloseInput(is, os);
      }

      return true;
    } catch (IOException e) {
      throw new GenericFileOperationFailedException("Cannot retrieve file: " + name, e);
    } catch (SftpException e) {
      throw new GenericFileOperationFailedException("Cannot retrieve file: " + name, e);
    } finally {
      IOHelper.close(os, "retrieve: " + name, LOG);
      // change back to current directory if we changed directory
      if (currentDir != null) {
        changeCurrentDirectory(currentDir);
      }
    }
  }
  public void handleMessage(Message message) throws Fault {
    // check the fault from the message
    Throwable ex = message.getContent(Throwable.class);
    if (ex != null) {
      if (ex instanceof Fault) {
        throw (Fault) ex;
      } else {
        throw new Fault(ex);
      }
    }

    List<?> params = message.getContent(List.class);
    if (null != params) {
      InputStream is = (InputStream) params.get(0);
      OutputStream os = message.getContent(OutputStream.class);
      try {
        if (is instanceof StreamCache) {
          ((StreamCache) is).writeTo(os);
        } else {
          IOUtils.copy(is, os);
        }
      } catch (Exception e) {
        throw new Fault(e);
      } finally {
        IOHelper.close(is, "input stream", null);
        // Should not close the output stream as the interceptor chain will close it
      }
    }
  }
コード例 #8
0
  protected void populateResponse(
      Exchange exchange,
      HttpMethod method,
      Message in,
      HeaderFilterStrategy strategy,
      int responseCode)
      throws IOException, ClassNotFoundException {
    // We just make the out message is not create when extractResponseBody throws exception,
    Object response = extractResponseBody(method, exchange);
    Message answer = exchange.getOut();

    answer.setHeader(Exchange.HTTP_RESPONSE_CODE, responseCode);
    answer.setBody(response);

    // propagate HTTP response headers
    Header[] headers = method.getResponseHeaders();
    for (Header header : headers) {
      String name = header.getName();
      String value = header.getValue();
      if (name.toLowerCase().equals("content-type")) {
        name = Exchange.CONTENT_TYPE;
        exchange.setProperty(Exchange.CHARSET_NAME, IOHelper.getCharsetNameFromContentType(value));
      }

      // use http helper to extract parameter value as it may contain multiple values
      Object extracted = HttpHelper.extractHttpParameterValue(value);
      if (strategy != null && !strategy.applyFilterToExternalHeaders(name, extracted, exchange)) {
        HttpHelper.appendHeader(answer.getHeaders(), name, extracted);
      }
    }

    // preserve headers from in by copying any non existing headers
    // to avoid overriding existing headers with old values
    MessageHelper.copyHeaders(exchange.getIn(), answer, false);
  }
コード例 #9
0
ファイル: Activator.java プロジェクト: chicagozer/rheosoft
 protected static Set<String> getConverterPackages(URL resource) {
   Set<String> packages = new LinkedHashSet<String>();
   if (resource != null) {
     BufferedReader reader = null;
     try {
       reader = new BufferedReader(new InputStreamReader(resource.openStream()));
       while (true) {
         String line = reader.readLine();
         if (line == null) {
           break;
         }
         line = line.trim();
         if (line.startsWith("#") || line.length() == 0) {
           continue;
         }
         StringTokenizer iter = new StringTokenizer(line, ",");
         while (iter.hasMoreTokens()) {
           String name = iter.nextToken().trim();
           if (name.length() > 0) {
             packages.add(name);
           }
         }
       }
     } catch (Exception ignore) {
       // Do nothing here
     } finally {
       IOHelper.close(reader, null, LOG);
     }
   }
   return packages;
 }
コード例 #10
0
 private static byte[] getKeyRing(String fileName) throws IOException {
   InputStream is = PGPDataFormatTest.class.getClassLoader().getResourceAsStream(fileName);
   ByteArrayOutputStream output = new ByteArrayOutputStream();
   IOHelper.copyAndCloseInput(is, output);
   output.close();
   return output.toByteArray();
 }
コード例 #11
0
 public StreamCache copy(Exchange exchange) throws IOException {
   if (byteArrayForCopy == null) {
     ByteArrayOutputStream baos = new ByteArrayOutputStream(in.available());
     IOHelper.copy(in, baos);
     // reset so that the stream can be reused
     reset();
     // cache the byte array, in order not to copy the byte array in the next call again
     byteArrayForCopy = baos.toByteArray();
   }
   return new InputStreamCache(byteArrayForCopy);
 }
コード例 #12
0
    public void run() {
      log.debug("ReloadStrategy is starting watching folder: {}", folder);

      // allow to run while starting Camel
      while (isStarting() || isRunAllowed()) {
        running = true;

        WatchKey key;
        try {
          log.trace("ReloadStrategy is polling for file changes in directory: {}", folder);
          // wait for a key to be available
          key = watcher.poll(2, TimeUnit.SECONDS);
        } catch (InterruptedException ex) {
          break;
        }

        if (key != null) {
          for (WatchEvent<?> event : key.pollEvents()) {
            WatchEvent<Path> we = (WatchEvent<Path>) event;
            Path path = we.context();
            String name = folder.resolve(path).toAbsolutePath().toFile().getAbsolutePath();
            log.trace("Modified/Created file: {}", name);

            // must be an .xml file
            if (name.toLowerCase(Locale.US).endsWith(".xml")) {
              log.debug("Modified/Created XML file: {}", name);
              try {
                FileInputStream fis = new FileInputStream(name);
                onReloadXml(getCamelContext(), name, fis);
                IOHelper.close(fis);
              } catch (Exception e) {
                log.warn(
                    "Error reloading routes from file: "
                        + name
                        + " due "
                        + e.getMessage()
                        + ". This exception is ignored.",
                    e);
              }
            }
          }

          // the key must be reset after processed
          boolean valid = key.reset();
          if (!valid) {
            break;
          }
        }
      }

      running = false;

      log.info("ReloadStrategy is stopping watching folder: {}", folder);
    }
コード例 #13
0
ファイル: HdfsProducer.java プロジェクト: aaronwalker/camel
 @Override
 protected void doStop() throws Exception {
   super.doStop();
   if (scheduler != null) {
     getEndpoint().getCamelContext().getExecutorServiceManager().shutdown(scheduler);
     scheduler = null;
   }
   if (ostream != null) {
     IOHelper.close(ostream, "output stream", log);
     ostream = null;
   }
 }
コード例 #14
0
  /**
   * Resolves the script.
   *
   * @param script script or uri for a script to load
   * @return the script
   * @throws IOException is thrown if error loading the script
   */
  protected String resolveScript(String script) throws IOException {
    String answer;
    if (ResourceHelper.hasScheme(script)) {
      InputStream is = loadResource(script);
      answer = currentExchange.get().getContext().getTypeConverter().convertTo(String.class, is);
      IOHelper.close(is);
    } else {
      answer = script;
    }

    return answer;
  }
コード例 #15
0
ファイル: XsltBuilder.java プロジェクト: pax95/camel
  public void process(Exchange exchange) throws Exception {
    notNull(getTemplate(), "template");

    if (isDeleteOutputFile()) {
      // add on completion so we can delete the file when the Exchange is done
      String fileName =
          ExchangeHelper.getMandatoryHeader(exchange, Exchange.XSLT_FILE_NAME, String.class);
      exchange.addOnCompletion(new XsltBuilderOnCompletion(fileName));
    }

    Transformer transformer = getTransformer();
    configureTransformer(transformer, exchange);

    ResultHandler resultHandler = resultHandlerFactory.createResult(exchange);
    Result result = resultHandler.getResult();
    // let's copy the headers before we invoke the transform in case they modify them
    Message out = exchange.getOut();
    out.copyFrom(exchange.getIn());

    // the underlying input stream, which we need to close to avoid locking files or other resources
    InputStream is = null;
    try {
      Source source;
      // only convert to input stream if really needed
      if (isInputStreamNeeded(exchange)) {
        is = exchange.getIn().getBody(InputStream.class);
        source = getSource(exchange, is);
      } else {
        Object body = exchange.getIn().getBody();
        source = getSource(exchange, body);
      }

      if (source instanceof StAXSource) {
        // Always convert StAXSource to SAXSource.
        // * Xalan and Saxon-B don't support StAXSource.
        // * The JDK default implementation (XSLTC) doesn't handle CDATA events
        //   (see com.sun.org.apache.xalan.internal.xsltc.trax.StAXStream2SAX).
        // * Saxon-HE/PE/EE seem to support StAXSource, but don't advertise this
        //   officially (via TransformerFactory.getFeature(StAXSource.FEATURE))
        source = new StAX2SAXSource(((StAXSource) source).getXMLStreamReader());
      }

      LOG.trace("Using {} as source", source);
      transformer.transform(source, result);
      LOG.trace("Transform complete with result {}", result);
      resultHandler.setBody(out);
    } finally {
      releaseTransformer(transformer);
      // IOHelper can handle if is is null
      IOHelper.close(is);
    }
  }
コード例 #16
0
  void createEncryptedNonCompressedData(ByteArrayOutputStream bos, String keyringPath)
      throws Exception, IOException, PGPException, UnsupportedEncodingException {
    PGPEncryptedDataGenerator encGen =
        new PGPEncryptedDataGenerator(
            new JcePGPDataEncryptorBuilder(SymmetricKeyAlgorithmTags.CAST5)
                .setSecureRandom(new SecureRandom())
                .setProvider(getProvider()));
    encGen.addMethod(new JcePublicKeyKeyEncryptionMethodGenerator(readPublicKey(keyringPath)));
    OutputStream encOut = encGen.open(bos, new byte[512]);
    PGPLiteralDataGenerator litData = new PGPLiteralDataGenerator();
    OutputStream litOut =
        litData.open(
            encOut, PGPLiteralData.BINARY, PGPLiteralData.CONSOLE, new Date(), new byte[512]);

    try {
      litOut.write("Test Message Without Compression".getBytes("UTF-8"));
      litOut.flush();
    } finally {
      IOHelper.close(litOut);
      IOHelper.close(encOut, bos);
    }
  }
コード例 #17
0
ファイル: Activator.java プロジェクト: chicagozer/rheosoft
 protected static Properties loadProperties(URL url) {
   Properties properties = new Properties();
   BufferedInputStream reader = null;
   try {
     reader = new BufferedInputStream(url.openStream());
     properties.load(reader);
   } catch (IOException e) {
     throw new RuntimeException(e);
   } finally {
     IOHelper.close(reader, "properties", LOG);
   }
   return properties;
 }
コード例 #18
0
ファイル: HdfsProducer.java プロジェクト: aaronwalker/camel
  void doProcess(Exchange exchange) throws Exception {
    Object body = exchange.getIn().getBody();
    Object key = exchange.getIn().getHeader(HdfsHeader.KEY.name());

    // must have ostream
    if (ostream == null) {
      ostream = setupHdfs(false);
    }

    boolean split = false;
    List<SplitStrategy> strategies = config.getSplitStrategies();
    for (SplitStrategy splitStrategy : strategies) {
      split |= splitStrategy.getType().split(ostream, splitStrategy.value, this);
    }

    if (split) {
      if (ostream != null) {
        IOHelper.close(ostream, "output stream", log);
      }
      StringBuilder actualPath = newFileName();
      ostream = HdfsOutputStream.createOutputStream(actualPath.toString(), config);
    }

    String path = ostream.getActualPath();
    log.trace("Writing body to hdfs-file {}", path);
    ostream.append(key, body, exchange.getContext().getTypeConverter());

    idle.set(false);

    // close if we do not have idle checker task to do this for us
    boolean close = scheduler == null;
    // but user may have a header to explict control the close
    Boolean closeHeader = exchange.getIn().getHeader(HdfsConstants.HDFS_CLOSE, Boolean.class);
    if (closeHeader != null) {
      close = closeHeader;
    }

    // if no idle checker then we need to explicit close the stream after usage
    if (close) {
      try {
        HdfsProducer.this.log.trace("Closing stream");
        ostream.close();
        ostream = null;
      } catch (IOException e) {
        // ignore
      }
    }

    log.debug("Wrote body to hdfs-file {}", path);
  }
コード例 #19
0
 @Override
 public long append(
     HdfsOutputStream hdfsostr, Object key, Object value, TypeConverter typeConverter) {
   InputStream is = null;
   try {
     is = typeConverter.convertTo(InputStream.class, value);
     return copyBytes(
         is, (FSDataOutputStream) hdfsostr.getOut(), HdfsConstants.DEFAULT_BUFFERSIZE, false);
   } catch (IOException ex) {
     throw new RuntimeCamelException(ex);
   } finally {
     IOHelper.close(is);
   }
 }
コード例 #20
0
 @After
 public void tearDown() {
   try {
     super.tearDown();
     if (applicationContext != null) {
       if (applicationContext.isActive()) {
         IOHelper.close(applicationContext);
       }
       applicationContext = null;
     }
   } catch (Exception exception) {
     // Don't throw the exception in the tearDown method
   }
 }
コード例 #21
0
  @Override
  public void releaseExclusiveReadLock(
      GenericFileOperations<File> operations, GenericFile<File> file, Exchange exchange)
      throws Exception {

    // must call super
    super.releaseExclusiveReadLock(operations, file, exchange);

    String target = file.getFileName();
    FileLock lock = exchange.getProperty(Exchange.FILE_LOCK_EXCLUSIVE_LOCK, FileLock.class);
    RandomAccessFile rac =
        exchange.getProperty(Exchange.FILE_LOCK_RANDOM_ACCESS_FILE, RandomAccessFile.class);

    if (lock != null) {
      Channel channel = lock.acquiredBy();
      try {
        lock.release();
      } finally {
        // close channel as well
        IOHelper.close(channel, "while releasing exclusive read lock for file: " + target, LOG);
        IOHelper.close(rac, "while releasing exclusive read lock for file: " + target, LOG);
      }
    }
  }
コード例 #22
0
 public static String getCharsetFromContentType(String contentType) {
   if (contentType != null) {
     // find the charset and set it to the Exchange
     int index = contentType.indexOf("charset=");
     if (index > 0) {
       String charset = contentType.substring(index + 8);
       // there may be another parameter after a semi colon, so skip that
       if (charset.contains(";")) {
         charset = ObjectHelper.before(charset, ";");
       }
       return IOHelper.normalizeCharset(charset);
     }
   }
   return null;
 }
コード例 #23
0
  /**
   * Reads the response body from the given input stream.
   *
   * @param is the input stream
   * @param exchange the exchange
   * @return the response body, can be <tt>null</tt> if no body
   * @throws IOException is thrown if error reading response body
   */
  public static Object readResponseBodyFromInputStream(InputStream is, Exchange exchange)
      throws IOException {
    if (is == null) {
      return null;
    }

    // convert the input stream to StreamCache if the stream cache is not disabled
    if (exchange.getProperty(Exchange.DISABLE_HTTP_STREAM_CACHE, Boolean.FALSE, Boolean.class)) {
      return is;
    } else {
      CachedOutputStream cos = new CachedOutputStream(exchange);
      IOHelper.copyAndCloseInput(is, cos);
      return cos.newStreamCache();
    }
  }
コード例 #24
0
 private static InputStream doExtractResponseBodyAsStream(InputStream is, Exchange exchange)
     throws IOException {
   // As httpclient is using a AutoCloseInputStream, it will be closed when the connection is
   // closed
   // we need to cache the stream for it.
   CachedOutputStream cos = null;
   try {
     // This CachedOutputStream will not be closed when the exchange is onCompletion
     cos = new CachedOutputStream(exchange, false);
     IOHelper.copy(is, cos);
     // When the InputStream is closed, the CachedOutputStream will be closed
     return cos.getWrappedInputStream();
   } catch (IOException ex) {
     // try to close the CachedOutputStream when we get the IOException
     try {
       cos.close();
     } catch (IOException ignore) {
       // do nothing here
     }
     throw ex;
   } finally {
     IOHelper.close(is, "Extracting response body", LOG);
   }
 }
コード例 #25
0
  public static Object deserializeJavaObjectFromStream(InputStream is)
      throws ClassNotFoundException, IOException {
    if (is == null) {
      return null;
    }

    Object answer = null;
    ObjectInputStream ois = new ObjectInputStream(is);
    try {
      answer = ois.readObject();
    } finally {
      IOHelper.close(ois);
    }

    return answer;
  }
コード例 #26
0
 @Override
 public Writable create(Object value, TypeConverter typeConverter, Holder<Integer> size) {
   InputStream is = null;
   try {
     is = typeConverter.convertTo(InputStream.class, value);
     ByteArrayOutputStream bos = new ByteArrayOutputStream();
     IOUtils.copyBytes(is, bos, HdfsConstants.DEFAULT_BUFFERSIZE, false);
     BytesWritable writable = new BytesWritable();
     writable.set(bos.toByteArray(), 0, bos.toByteArray().length);
     size.value = bos.toByteArray().length;
     return writable;
   } catch (IOException ex) {
     throw new RuntimeCamelException(ex);
   } finally {
     IOHelper.close(is);
   }
 }
コード例 #27
0
 @Test
 public void testNextPatientIdInResultMessage() throws Exception {
   BufferedReader in =
       IOHelper.buffered(
           new InputStreamReader(this.getClass().getResourceAsStream("/oru_r01.txt")));
   String line = "";
   String message = "";
   while (line != null) {
     if ((line = in.readLine()) != null) {
       message += line + "\r";
     }
   }
   MockEndpoint mock = getMockEndpoint("mock:test6");
   mock.expectedMessageCount(1);
   mock.expectedBodiesReceived("V208155");
   template.sendBody("direct:test6", message);
   assertMockEndpointsSatisfied();
 }
コード例 #28
0
  public static Exception populateNettyHttpOperationFailedException(
      Exchange exchange,
      String url,
      HttpResponse response,
      int responseCode,
      boolean transferException) {
    String uri = url;
    String statusText = response.getStatus().getReasonPhrase();

    if (responseCode >= 300 && responseCode < 400) {
      String redirectLocation = response.headers().get("location");
      if (redirectLocation != null) {
        return new NettyHttpOperationFailedException(
            uri, responseCode, statusText, redirectLocation, response);
      } else {
        // no redirect location
        return new NettyHttpOperationFailedException(uri, responseCode, statusText, null, response);
      }
    }

    if (transferException) {
      String contentType = response.headers().get(Exchange.CONTENT_TYPE);
      if (NettyHttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT.equals(contentType)) {
        // if the response was a serialized exception then use that
        InputStream is =
            exchange.getContext().getTypeConverter().convertTo(InputStream.class, response);
        if (is != null) {
          try {
            Object body = deserializeJavaObjectFromStream(is);
            if (body instanceof Exception) {
              return (Exception) body;
            }
          } catch (Exception e) {
            return e;
          } finally {
            IOHelper.close(is);
          }
        }
      }
    }

    // internal server error (error code 500)
    return new NettyHttpOperationFailedException(uri, responseCode, statusText, null, response);
  }
コード例 #29
0
ファイル: XsltBuilder.java プロジェクト: JSantosP/camel
  public void process(Exchange exchange) throws Exception {
    notNull(getTemplate(), "template");

    if (isDeleteOutputFile()) {
      // add on completion so we can delete the file when the Exchange is done
      String fileName =
          ExchangeHelper.getMandatoryHeader(exchange, Exchange.XSLT_FILE_NAME, String.class);
      exchange.addOnCompletion(new XsltBuilderOnCompletion(fileName));
    }

    Transformer transformer = getTransformer();
    configureTransformer(transformer, exchange);

    ResultHandler resultHandler = resultHandlerFactory.createResult(exchange);
    Result result = resultHandler.getResult();
    exchange.setProperty("isXalanTransformer", isXalanTransformer(transformer));
    // let's copy the headers before we invoke the transform in case they modify them
    Message out = exchange.getOut();
    out.copyFrom(exchange.getIn());

    // the underlying input stream, which we need to close to avoid locking files or other resources
    InputStream is = null;
    try {
      Source source;
      // only convert to input stream if really needed
      if (isInputStreamNeeded(exchange)) {
        is = exchange.getIn().getBody(InputStream.class);
        source = getSource(exchange, is);
      } else {
        Object body = exchange.getIn().getBody();
        source = getSource(exchange, body);
      }
      LOG.trace("Using {} as source", source);
      transformer.transform(source, result);
      LOG.trace("Transform complete with result {}", result);
      resultHandler.setBody(out);
    } finally {
      releaseTransformer(transformer);
      // IOHelper can handle if is is null
      IOHelper.close(is);
    }
  }
コード例 #30
0
 public static long copyBytes(InputStream in, OutputStream out, int buffSize, boolean close)
     throws IOException {
   long numBytes = 0;
   @SuppressWarnings("resource")
   PrintStream ps = out instanceof PrintStream ? (PrintStream) out : null;
   byte buf[] = new byte[buffSize];
   try {
     int bytesRead = in.read(buf);
     while (bytesRead >= 0) {
       out.write(buf, 0, bytesRead);
       numBytes += bytesRead;
       if ((ps != null) && ps.checkError()) {
         throw new IOException("Unable to write to output stream.");
       }
       bytesRead = in.read(buf);
     }
   } finally {
     if (close) {
       IOHelper.close(out, in);
     }
   }
   return numBytes;
 }