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);
    }
  }
  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
      }
    }
  }
Esempio n. 3
0
 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;
 }
    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);
    }
Esempio n. 5
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;
  }
Esempio n. 6
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;
  }
Esempio n. 7
0
 @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;
   }
 }
Esempio n. 8
0
  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);
    }
  }
  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);
    }
  }
Esempio n. 10
0
 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;
 }
Esempio n. 11
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);
    }
  }
Esempio n. 12
0
  @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);
      }
    }
  }
Esempio n. 13
0
  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);
  }
Esempio n. 14
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);
   }
 }
Esempio n. 15
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
   }
 }
  @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);
      }
    }
  }
Esempio n. 17
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;
  }
Esempio n. 18
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);
   }
 }
Esempio n. 19
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);
  }
Esempio n. 20
0
  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);
    }
  }
Esempio n. 21
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;
 }
Esempio n. 22
0
 @Override
 protected int poll() throws Exception {
   log.debug("Started BU353 consumer poll.");
   BufferedReader source = null;
   try {
     log.debug("Opening connection with the GPS serial port.");
     source =
         new BufferedReader(
             new InputStreamReader(getEndpoint().getGpsCoordinatesSource().source()));
     while (true) {
       try {
         String line = source.readLine();
         log.debug("Consuming line: {}", line);
         if (line.startsWith("$GPRMC")) {
           ClientGpsCoordinates coordinates = ClientGpsCoordinates.parseNMEA(line);
           Exchange exchange =
               ExchangeBuilder.anExchange(getEndpoint().getCamelContext())
                   .withBody(coordinates)
                   .build();
           getProcessor().process(exchange);
           return 1;
         } else {
           log.debug("Not supported line read from the NMEA file. Ignoring: {}", line);
         }
       } catch (SatelliteOutOfReachException e) {
         log.info("GPS satellite out of reach.");
       } catch (Exception e) {
         getExceptionHandler().handleException(e);
       }
     }
   } finally {
     log.debug("Closing connection with the GPS serial port.");
     if (source != null) {
       IOHelper.close(source);
     }
   }
 }
Esempio n. 23
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);
   }
 }
  @Override
  public boolean acquireExclusiveReadLock(
      GenericFileOperations<File> operations, GenericFile<File> file, Exchange exchange)
      throws Exception {
    // must call super
    if (!super.acquireExclusiveReadLock(operations, file, exchange)) {
      return false;
    }

    File target = new File(file.getAbsoluteFilePath());

    LOG.trace("Waiting for exclusive read lock to file: {}", target);

    FileChannel channel = null;
    RandomAccessFile randomAccessFile = null;

    boolean exclusive = false;
    FileLock lock = null;

    try {
      randomAccessFile = new RandomAccessFile(target, "rw");
      // try to acquire rw lock on the file before we can consume it
      channel = randomAccessFile.getChannel();

      StopWatch watch = new StopWatch();

      while (!exclusive) {
        // timeout check
        if (timeout > 0) {
          long delta = watch.taken();
          if (delta > timeout) {
            CamelLogger.log(
                LOG,
                readLockLoggingLevel,
                "Cannot acquire read lock within "
                    + timeout
                    + " millis. Will skip the file: "
                    + target);
            // we could not get the lock within the timeout period, so return false
            return false;
          }
        }

        // get the lock using either try lock or not depending on if we are using timeout or not
        try {
          lock = timeout > 0 ? channel.tryLock() : channel.lock();
        } catch (IllegalStateException ex) {
          // Also catch the OverlappingFileLockException here. Do nothing here
        }
        if (lock != null) {
          LOG.trace("Acquired exclusive read lock: {} to file: {}", lock, target);
          exclusive = true;
        } else {
          boolean interrupted = sleep();
          if (interrupted) {
            // we were interrupted while sleeping, we are likely being shutdown so return false
            return false;
          }
        }
      }
    } catch (IOException e) {
      // must handle IOException as some apps on Windows etc. will still somehow hold a lock to a
      // file
      // such as AntiVirus or MS Office that has special locks for it's supported files
      if (timeout == 0) {
        // if not using timeout, then we cant retry, so return false
        return false;
      }
      LOG.debug("Cannot acquire read lock. Will try again.", e);
      boolean interrupted = sleep();
      if (interrupted) {
        // we were interrupted while sleeping, we are likely being shutdown so return false
        return false;
      }
    } finally {
      // close channels if we did not grab the lock
      if (!exclusive) {
        IOHelper.close(channel, "while acquiring exclusive read lock for file: " + target, LOG);
        IOHelper.close(
            randomAccessFile, "while acquiring exclusive read lock for file: " + target, LOG);

        // and also must release super lock
        super.releaseExclusiveReadLock(operations, file, exchange);
      }
    }

    // we grabbed the lock
    exchange.setProperty(Exchange.FILE_LOCK_EXCLUSIVE_LOCK, lock);
    exchange.setProperty(Exchange.FILE_LOCK_RANDOM_ACCESS_FILE, randomAccessFile);

    return true;
  }
 @After
 public void tearDown() throws Exception {
   IOHelper.close(ctx);
 }
  @Override
  public HttpResponse toNettyResponse(Message message, NettyHttpConfiguration configuration)
      throws Exception {
    LOG.trace("toNettyResponse: {}", message);

    // the message body may already be a Netty HTTP response
    if (message.getBody() instanceof HttpResponse) {
      return (HttpResponse) message.getBody();
    }

    // the response code is 200 for OK and 500 for failed
    boolean failed = message.getExchange().isFailed();
    int defaultCode = failed ? 500 : 200;

    int code = message.getHeader(Exchange.HTTP_RESPONSE_CODE, defaultCode, int.class);
    HttpResponse response =
        new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.valueOf(code));
    LOG.trace("HTTP Status Code: {}", code);

    TypeConverter tc = message.getExchange().getContext().getTypeConverter();

    // append headers
    // must use entrySet to ensure case of keys is preserved
    for (Map.Entry<String, Object> entry : message.getHeaders().entrySet()) {
      String key = entry.getKey();
      Object value = entry.getValue();
      // use an iterator as there can be multiple values. (must not use a delimiter)
      final Iterator<?> it = ObjectHelper.createIterator(value, null);
      while (it.hasNext()) {
        String headerValue = tc.convertTo(String.class, it.next());
        if (headerValue != null
            && headerFilterStrategy != null
            && !headerFilterStrategy.applyFilterToCamelHeaders(
                key, headerValue, message.getExchange())) {
          LOG.trace("HTTP-Header: {}={}", key, headerValue);
          response.headers().add(key, headerValue);
        }
      }
    }

    Object body = message.getBody();
    Exception cause = message.getExchange().getException();
    // support bodies as native Netty
    ChannelBuffer buffer;

    // if there was an exception then use that as body
    if (cause != null) {
      if (configuration.isTransferException()) {
        // we failed due an exception, and transfer it as java serialized object
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(cause);
        oos.flush();
        IOHelper.close(oos, bos);

        // the body should be the serialized java object of the exception
        body = ChannelBuffers.copiedBuffer(bos.toByteArray());
        // force content type to be serialized java object
        message.setHeader(
            Exchange.CONTENT_TYPE, NettyHttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT);
      } else {
        // we failed due an exception so print it as plain text
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        cause.printStackTrace(pw);

        // the body should then be the stacktrace
        body = ChannelBuffers.copiedBuffer(sw.toString().getBytes());
        // force content type to be text/plain as that is what the stacktrace is
        message.setHeader(Exchange.CONTENT_TYPE, "text/plain");
      }

      // and mark the exception as failure handled, as we handled it by returning it as the response
      ExchangeHelper.setFailureHandled(message.getExchange());
    }

    if (body instanceof ChannelBuffer) {
      buffer = (ChannelBuffer) body;
    } else {
      // try to convert to buffer first
      buffer = message.getBody(ChannelBuffer.class);
      if (buffer == null) {
        // fallback to byte array as last resort
        byte[] data = message.getBody(byte[].class);
        if (data != null) {
          buffer = ChannelBuffers.copiedBuffer(data);
        } else {
          // and if byte array fails then try String
          String str;
          if (body != null) {
            str = message.getMandatoryBody(String.class);
          } else {
            str = "";
          }
          buffer = ChannelBuffers.copiedBuffer(str.getBytes());
        }
      }
    }
    if (buffer != null) {
      response.setContent(buffer);
      // We just need to reset the readerIndex this time
      if (buffer.readerIndex() == buffer.writerIndex()) {
        buffer.setIndex(0, buffer.writerIndex());
      }
      // TODO How to enable the chunk transport
      int len = buffer.readableBytes();
      // set content-length
      response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, len);
      LOG.trace("Content-Length: {}", len);
    }

    // set the content type in the response.
    String contentType = MessageHelper.getContentType(message);
    if (contentType != null) {
      // set content-type
      response.headers().set(HttpHeaders.Names.CONTENT_TYPE, contentType);
      LOG.trace("Content-Type: {}", contentType);
    }

    // configure connection to accordingly to keep alive configuration
    // favor using the header from the message
    String connection = message.getHeader(HttpHeaders.Names.CONNECTION, String.class);
    // Read the connection header from the exchange property
    if (connection == null) {
      connection = message.getExchange().getProperty(HttpHeaders.Names.CONNECTION, String.class);
    }
    if (connection == null) {
      // fallback and use the keep alive from the configuration
      if (configuration.isKeepAlive()) {
        connection = HttpHeaders.Values.KEEP_ALIVE;
      } else {
        connection = HttpHeaders.Values.CLOSE;
      }
    }
    response.headers().set(HttpHeaders.Names.CONNECTION, connection);
    // Just make sure we close the channel when the connection value is close
    if (connection.equalsIgnoreCase(HttpHeaders.Values.CLOSE)) {
      message.setHeader(NettyConstants.NETTY_CLOSE_CHANNEL_WHEN_COMPLETE, true);
    }
    LOG.trace("Connection: {}", connection);

    return response;
  }
Esempio n. 27
0
 /**
  * Writes the given object as response body to the output stream
  *
  * @param stream output stream
  * @param target object to write
  * @throws IOException is thrown if error writing
  */
 public static void writeObjectToStream(OutputStream stream, Object target) throws IOException {
   ObjectOutputStream oos = new ObjectOutputStream(stream);
   oos.writeObject(target);
   oos.flush();
   IOHelper.close(oos);
 }
Esempio n. 28
0
  @SuppressWarnings("unchecked")
  private boolean retrieveFileToFileInLocalWorkDirectory(String name, Exchange exchange)
      throws GenericFileOperationFailedException {
    File temp;
    File local = new File(endpoint.getLocalWorkDirectory());
    OutputStream os;
    GenericFile<ChannelSftp.LsEntry> file =
        (GenericFile<ChannelSftp.LsEntry>) exchange.getProperty(FileComponent.FILE_EXCHANGE_FILE);
    ObjectHelper.notNull(
        file, "Exchange should have the " + FileComponent.FILE_EXCHANGE_FILE + " set");
    try {
      // use relative filename in local work directory
      String relativeName = file.getRelativeFilePath();

      temp = new File(local, relativeName + ".inprogress");
      local = new File(local, relativeName);

      // create directory to local work file
      local.mkdirs();

      // delete any existing files
      if (temp.exists()) {
        if (!FileUtil.deleteFile(temp)) {
          throw new GenericFileOperationFailedException(
              "Cannot delete existing local work file: " + temp);
        }
      }
      if (local.exists()) {
        if (!FileUtil.deleteFile(local)) {
          throw new GenericFileOperationFailedException(
              "Cannot delete existing local work file: " + local);
        }
      }

      // create new temp local work file
      if (!temp.createNewFile()) {
        throw new GenericFileOperationFailedException("Cannot create new local work file: " + temp);
      }

      // store content as a file in the local work directory in the temp handle
      os = new FileOutputStream(temp);

      // set header with the path to the local work file
      exchange.getIn().setHeader(Exchange.FILE_LOCAL_WORK_PATH, local.getPath());
    } catch (Exception e) {
      throw new GenericFileOperationFailedException("Cannot create new local work file: " + local);
    }
    String currentDir = null;
    try {
      // store the java.io.File handle as the body
      file.setBody(local);

      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);
      }

      channel.get(remoteName, os);

    } catch (SftpException e) {
      LOG.trace(
          "Error occurred during retrieving file: {} to local directory. Deleting local work file: {}",
          name,
          temp);
      // failed to retrieve the file so we need to close streams and delete in progress file
      // must close stream before deleting file
      IOHelper.close(os, "retrieve: " + name, LOG);
      boolean deleted = FileUtil.deleteFile(temp);
      if (!deleted) {
        LOG.warn(
            "Error occurred during retrieving file: "
                + name
                + " to local directory. Cannot delete local work file: "
                + temp);
      }
      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);
      }
    }

    LOG.debug("Retrieve file to local work file result: true");

    // operation went okay so rename temp to local after we have retrieved the data
    LOG.trace("Renaming local in progress file from: {} to: {}", temp, local);
    try {
      if (!FileUtil.renameFile(temp, local, false)) {
        throw new GenericFileOperationFailedException(
            "Cannot rename local work file from: " + temp + " to: " + local);
      }
    } catch (IOException e) {
      throw new GenericFileOperationFailedException(
          "Cannot rename local work file from: " + temp + " to: " + local, e);
    }

    return true;
  }
Esempio n. 29
0
  /**
   * Common work which must be done when we are done multicasting.
   *
   * <p>This logic applies for both running synchronous and asynchronous as there are multiple exist
   * points when using the asynchronous routing engine. And therefore we want the logic in one
   * method instead of being scattered.
   *
   * @param original the original exchange
   * @param subExchange the current sub exchange, can be <tt>null</tt> for the synchronous part
   * @param pairs the pairs with the exchanges to process
   * @param callback the callback
   * @param doneSync the <tt>doneSync</tt> parameter to call on callback
   * @param forceExhaust whether or not error handling is exhausted
   */
  protected void doDone(
      Exchange original,
      Exchange subExchange,
      final Iterable<ProcessorExchangePair> pairs,
      AsyncCallback callback,
      boolean doneSync,
      boolean forceExhaust) {

    // we are done so close the pairs iterator
    if (pairs != null && pairs instanceof Closeable) {
      IOHelper.close((Closeable) pairs, "pairs", LOG);
    }

    AggregationStrategy strategy = getAggregationStrategy(subExchange);
    // invoke the on completion callback
    if (strategy instanceof CompletionAwareAggregationStrategy) {
      ((CompletionAwareAggregationStrategy) strategy).onCompletion(subExchange);
    }

    // cleanup any per exchange aggregation strategy
    removeAggregationStrategyFromExchange(original);

    // we need to know if there was an exception, and if the stopOnException option was enabled
    // also we would need to know if any error handler has attempted redelivery and exhausted
    boolean stoppedOnException = false;
    boolean exception = false;
    boolean exhaust =
        forceExhaust
            || subExchange != null
                && (subExchange.getException() != null
                    || ExchangeHelper.isRedeliveryExhausted(subExchange));
    if (original.getException() != null
        || subExchange != null && subExchange.getException() != null) {
      // there was an exception and we stopped
      stoppedOnException = isStopOnException();
      exception = true;
    }

    // must copy results at this point
    if (subExchange != null) {
      if (stoppedOnException) {
        // if we stopped due an exception then only propagte the exception
        original.setException(subExchange.getException());
      } else {
        // copy the current result to original so it will contain this result of this eip
        ExchangeHelper.copyResults(original, subExchange);
      }
    }

    // .. and then if there was an exception we need to configure the redelivery exhaust
    // for example the noErrorHandler will not cause redelivery exhaust so if this error
    // handled has been in use, then the exhaust would be false (if not forced)
    if (exception) {
      // multicast uses error handling on its output processors and they have tried to redeliver
      // so we shall signal back to the other error handlers that we are exhausted and they should
      // not
      // also try to redeliver as we will then do that twice
      original.setProperty(Exchange.REDELIVERY_EXHAUSTED, exhaust);
    }

    callback.done(doneSync);
  }
Esempio n. 30
0
  private boolean doStoreFile(String name, String targetName, Exchange exchange)
      throws GenericFileOperationFailedException {
    LOG.trace("doStoreFile({})", targetName);

    // if an existing file already exists what should we do?
    if (endpoint.getFileExist() == GenericFileExist.Ignore
        || endpoint.getFileExist() == GenericFileExist.Fail
        || endpoint.getFileExist() == GenericFileExist.Move) {
      boolean existFile = existsFile(targetName);
      if (existFile && endpoint.getFileExist() == GenericFileExist.Ignore) {
        // ignore but indicate that the file was written
        LOG.trace("An existing file already exists: {}. Ignore and do not override it.", name);
        return true;
      } else if (existFile && endpoint.getFileExist() == GenericFileExist.Fail) {
        throw new GenericFileOperationFailedException(
            "File already exist: " + name + ". Cannot write new file.");
      } else if (existFile && endpoint.getFileExist() == GenericFileExist.Move) {
        // move any existing file first
        doMoveExistingFile(name, targetName);
      }
    }

    InputStream is = null;
    if (exchange.getIn().getBody() == null) {
      // Do an explicit test for a null body and decide what to do
      if (endpoint.isAllowNullBody()) {
        LOG.trace("Writing empty file.");
        is = new ByteArrayInputStream(new byte[] {});
      } else {
        throw new GenericFileOperationFailedException("Cannot write null body to file: " + name);
      }
    }

    try {
      if (is == null) {
        String charset = endpoint.getCharset();
        if (charset != null) {
          // charset configured so we must convert to the desired
          // charset so we can write with encoding
          is =
              new ByteArrayInputStream(
                  exchange.getIn().getMandatoryBody(String.class).getBytes(charset));
          LOG.trace("Using InputStream {} with charset {}.", is, charset);
        } else {
          is = exchange.getIn().getMandatoryBody(InputStream.class);
        }
      }

      final StopWatch watch = new StopWatch();
      LOG.debug("About to store file: {} using stream: {}", targetName, is);
      if (endpoint.getFileExist() == GenericFileExist.Append) {
        LOG.trace("Client appendFile: {}", targetName);
        channel.put(is, targetName, ChannelSftp.APPEND);
      } else {
        LOG.trace("Client storeFile: {}", targetName);
        // override is default
        channel.put(is, targetName);
      }
      watch.stop();
      if (LOG.isDebugEnabled()) {
        LOG.debug(
            "Took {} ({} millis) to store file: {} and FTP client returned: true",
            new Object[] {TimeUtils.printDuration(watch.taken()), watch.taken(), targetName});
      }

      // after storing file, we may set chmod on the file
      String mode = endpoint.getConfiguration().getChmod();
      if (ObjectHelper.isNotEmpty(mode)) {
        // parse to int using 8bit mode
        int permissions = Integer.parseInt(mode, 8);
        LOG.trace("Setting chmod: {} on file: {}", mode, targetName);
        channel.chmod(permissions, targetName);
      }

      return true;

    } catch (SftpException e) {
      throw new GenericFileOperationFailedException("Cannot store file: " + name, e);
    } catch (InvalidPayloadException e) {
      throw new GenericFileOperationFailedException("Cannot store file: " + name, e);
    } catch (UnsupportedEncodingException e) {
      throw new GenericFileOperationFailedException("Cannot store file: " + name, e);
    } finally {
      IOHelper.close(is, "store: " + name, LOG);
    }
  }