public void writeFile(Exchange exchange, String fileName)
      throws GenericFileOperationFailedException {
    // build directory if auto create is enabled
    if (endpoint.isAutoCreate()) {
      // we must normalize it (to avoid having both \ and / in the name which confuses java.io.File)
      String name = FileUtil.normalizePath(fileName);

      // use java.io.File to compute the file path
      File file = new File(name);
      String directory = file.getParent();
      boolean absolute = FileUtil.isAbsolute(file);
      if (directory != null) {
        if (!operations.buildDirectory(directory, absolute)) {
          log.debug(
              "Cannot build directory [{}] (could be because of denied permissions)", directory);
        }
      }
    }

    // upload
    if (log.isTraceEnabled()) {
      log.trace(
          "About to write [{}] to [{}] from exchange [{}]",
          new Object[] {fileName, getEndpoint(), exchange});
    }

    boolean success = operations.storeFile(fileName, exchange);
    if (!success) {
      throw new GenericFileOperationFailedException("Error writing file [" + fileName + "]");
    }
    log.debug("Wrote [{}] to [{}]", fileName, getEndpoint());
  }
Exemple #2
0
  public boolean storeFile(String name, Exchange exchange)
      throws GenericFileOperationFailedException {
    // must normalize name first
    name = endpoint.getConfiguration().normalizePath(name);

    LOG.trace("storeFile({})", name);

    boolean answer = false;
    String currentDir = null;
    String path = FileUtil.onlyPath(name);
    String targetName = name;

    try {
      if (path != null && endpoint.getConfiguration().isStepwise()) {
        // must remember current dir so we stay in that directory after the write
        currentDir = getCurrentDirectory();

        // change to path of name
        changeCurrentDirectory(path);

        // the target name should be without path, as we have changed directory
        targetName = FileUtil.stripPath(name);
      }

      // store the file
      answer = doStoreFile(name, targetName, exchange);
    } finally {
      // change back to current directory if we changed directory
      if (currentDir != null) {
        changeCurrentDirectory(currentDir);
      }
    }

    return answer;
  }
  /**
   * Creates the associated name of the done file based on the given file name.
   *
   * <p>This method should only be invoked if a done filename property has been set on this
   * endpoint.
   *
   * @param fileName the file name
   * @return name of the associated done file name
   */
  protected String createDoneFileName(String fileName) {
    String pattern = getDoneFileName();
    ObjectHelper.notEmpty(pattern, "doneFileName", pattern);

    // we only support ${file:name} or ${file:name.noext} as dynamic placeholders for done files
    String path = FileUtil.onlyPath(fileName);
    String onlyName = FileUtil.stripPath(fileName);

    pattern = pattern.replaceFirst("\\$\\{file:name\\}", onlyName);
    pattern = pattern.replaceFirst("\\$simple\\{file:name\\}", onlyName);
    pattern = pattern.replaceFirst("\\$\\{file:name.noext\\}", FileUtil.stripExt(onlyName));
    pattern = pattern.replaceFirst("\\$simple\\{file:name.noext\\}", FileUtil.stripExt(onlyName));

    // must be able to resolve all placeholders supported
    if (SimpleLanguage.hasStartToken(pattern)) {
      throw new ExpressionIllegalSyntaxException(
          fileName + ". Cannot resolve reminder: " + pattern);
    }

    String answer = pattern;
    if (ObjectHelper.isNotEmpty(path) && ObjectHelper.isNotEmpty(pattern)) {
      // done file must always be in same directory as the real file name
      answer = path + File.separator + pattern;
    }

    if (getConfiguration().needToNormalize()) {
      // must normalize path to cater for Windows and other OS
      answer = FileUtil.normalizePath(answer);
    }

    return answer;
  }
 @Override
 public LSInput resolveResource(
     String type, String namespaceURI, String publicId, String systemId, String baseURI) {
   // systemId should be mandatory
   if (systemId == null) {
     throw new IllegalArgumentException(
         String.format(
             "Resource: %s refers an invalid resource without SystemId."
                 + " Invalid resource has type: %s, namespaceURI: %s, publicId: %s, systemId: %s, baseURI: %s",
             resourceUri, type, namespaceURI, publicId, systemId, baseURI));
   }
   String resourceURI = null;
   // Build up the relative path for using relatedURI and baseURI
   if (baseURI == null) {
     relatedURI = FileUtil.compactPath(getUri(systemId), '/');
     resourceURI = relatedURI;
   } else {
     String relatedPath = relatedURIMap.get(baseURI);
     if (relatedPath == null) {
       relatedPath = FileUtil.onlyPath(relatedURI);
       if (relatedPath == null) {
         relatedPath = "";
       }
       relatedURI = FileUtil.compactPath(FileUtil.onlyPath(relatedURI) + "/" + systemId, '/');
       resourceURI = relatedURI;
       relatedURIMap.put(baseURI, relatedPath);
     } else {
       resourceURI = FileUtil.compactPath(relatedPath + "/" + systemId, '/');
       relatedURI = resourceURI;
     }
   }
   return new DefaultLSInput(publicId, systemId, baseURI, resourceURI);
 }
  @Override
  public Source resolve(String href, String base) throws TransformerException {
    // supports the empty href
    if (ObjectHelper.isEmpty(href)) {
      href = location;
    }
    if (ObjectHelper.isEmpty(href)) {
      throw new TransformerException("include href is empty");
    }

    LOG.trace("Resolving URI with href: {} and base: {}", href, base);

    String scheme = ResourceHelper.getScheme(href);
    if (scheme != null) {
      // need to compact paths for file/classpath as it can be relative paths using .. to go
      // backwards
      if ("file:".equals(scheme)) {
        // compact path use file OS separator
        href = FileUtil.compactPath(href);
      } else if ("classpath:".equals(scheme)) {
        // for classpath always use /
        href = FileUtil.compactPath(href, '/');
      }
      LOG.debug("Resolving URI from {}: {}", scheme, href);

      InputStream is;
      try {
        is = ResourceHelper.resolveMandatoryResourceAsInputStream(context, href);
      } catch (IOException e) {
        throw new TransformerException(e);
      }
      return new StreamSource(is);
    }

    // if href and location is the same, then its the initial resolve
    if (href.equals(location)) {
      String path = baseScheme + href;
      return resolve(path, base);
    }

    // okay then its relative to the starting location from the XSLT component
    String path = FileUtil.onlyPath(location);
    if (ObjectHelper.isEmpty(path)) {
      path = baseScheme + href;
      return resolve(path, base);
    } else {
      if (ResourceHelper.hasScheme(path)) {
        path = path + "/" + href;
      } else {
        path = baseScheme + path + "/" + href;
      }
      return resolve(path, base);
    }
  }
Exemple #6
0
  public void changeCurrentDirectory(String path) throws GenericFileOperationFailedException {
    LOG.trace("changeCurrentDirectory({})", path);
    if (ObjectHelper.isEmpty(path)) {
      return;
    }

    // must compact path so SFTP server can traverse correctly, make use of the '/'
    // separator because JSch expects this as the file separator even on Windows
    String before = path;
    char separatorChar = '/';
    path = FileUtil.compactPath(path, separatorChar);
    if (LOG.isTraceEnabled()) {
      LOG.trace(
          "Compacted path: {} -> {} using separator: {}",
          new Object[] {before, path, separatorChar});
    }

    // not stepwise should change directory in one operation
    if (!endpoint.getConfiguration().isStepwise()) {
      doChangeDirectory(path);
      return;
    }
    if (getCurrentDirectory().startsWith(path)) {
      // extract the path segment relative to the target path and make sure it keeps the preceding
      // '/' for the regex op
      String p = getCurrentDirectory().substring(path.length() - (path.endsWith("/") ? 1 : 0));
      if (p.length() == 0) {
        return;
      }
      // the first character must be '/' and hence removed
      path = UP_DIR_PATTERN.matcher(p).replaceAll("/..").substring(1);
    }

    // if it starts with the root path then a little special handling for that
    if (FileUtil.hasLeadingSeparator(path)) {
      // change to root path
      doChangeDirectory(path.substring(0, 1));
      path = path.substring(1);
    }

    // split into multiple dirs
    final String[] dirs = path.split("/|\\\\");

    if (dirs == null || dirs.length == 0) {
      // path was just a relative single path
      doChangeDirectory(path);
      return;
    }

    // there are multiple dirs so do this in chunks
    for (String dir : dirs) {
      doChangeDirectory(dir);
    }
  }
Exemple #7
0
  /** Moves any existing file due fileExists=Move is in use. */
  private void doMoveExistingFile(String name, String targetName)
      throws GenericFileOperationFailedException {
    // need to evaluate using a dummy and simulate the file first, to have access to all the file
    // attributes
    // create a dummy exchange as Exchange is needed for expression evaluation
    // we support only the following 3 tokens.
    Exchange dummy = endpoint.createExchange();
    // we only support relative paths for the ftp component, so dont provide any parent
    String parent = null;
    String onlyName = FileUtil.stripPath(targetName);
    dummy.getIn().setHeader(Exchange.FILE_NAME, targetName);
    dummy.getIn().setHeader(Exchange.FILE_NAME_ONLY, onlyName);
    dummy.getIn().setHeader(Exchange.FILE_PARENT, parent);

    String to = endpoint.getMoveExisting().evaluate(dummy, String.class);
    // we only support relative paths for the ftp component, so strip any leading paths
    to = FileUtil.stripLeadingSeparator(to);
    // normalize accordingly to configuration
    to = endpoint.getConfiguration().normalizePath(to);
    if (ObjectHelper.isEmpty(to)) {
      throw new GenericFileOperationFailedException(
          "moveExisting evaluated as empty String, cannot move existing file: " + name);
    }

    // do we have a sub directory
    String dir = FileUtil.onlyPath(to);
    if (dir != null) {
      // ensure directory exists
      buildDirectory(dir, false);
    }

    // deal if there already exists a file
    if (existsFile(to)) {
      if (endpoint.isEagerDeleteTargetFile()) {
        LOG.trace("Deleting existing file: {}", to);
        deleteFile(to);
      } else {
        throw new GenericFileOperationFailedException(
            "Cannot moved existing file from: "
                + name
                + " to: "
                + to
                + " as there already exists a file: "
                + to);
      }
    }

    LOG.trace("Moving existing file: {} to: {}", name, to);
    if (!renameFile(targetName, to)) {
      throw new GenericFileOperationFailedException(
          "Cannot rename file from: " + name + " to: " + to);
    }
  }
Exemple #8
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);
      }
    }
  }
 private void cleanUpTempFile() {
   // cleanup temporary file
   if (tempFile != null) {
     FileUtil.deleteFile(tempFile);
     tempFile = null;
   }
 }
  public String createTempFileName(Exchange exchange, String fileName) {
    String answer = fileName;

    String tempName;
    if (exchange.getIn().getHeader(Exchange.FILE_NAME) == null) {
      // its a generated filename then add it to header so we can evaluate the expression
      exchange.getIn().setHeader(Exchange.FILE_NAME, FileUtil.stripPath(fileName));
      tempName = endpoint.getTempFileName().evaluate(exchange, String.class);
      // and remove it again after evaluation
      exchange.getIn().removeHeader(Exchange.FILE_NAME);
    } else {
      tempName = endpoint.getTempFileName().evaluate(exchange, String.class);
    }

    // check for both windows and unix separators
    int pos = Math.max(answer.lastIndexOf("/"), answer.lastIndexOf("\\"));
    if (pos == -1) {
      // no path so use temp name as calculated
      answer = tempName;
    } else {
      // path should be prefixed before the temp name
      StringBuilder sb = new StringBuilder(answer.substring(0, pos + 1));
      sb.append(tempName);
      answer = sb.toString();
    }

    if (endpoint.getConfiguration().needToNormalize()) {
      // must normalize path to cater for Windows and other OS
      answer = normalizePath(answer);
    }

    return answer;
  }
 private String getUri(String systemId) {
   if (resourcePath != null) {
     return FileUtil.onlyPath(resourceUri) + "/" + systemId;
   } else {
     return systemId;
   }
 }
  /**
   * Copied from CamelBlueprintHelper since there it is also a private method.
   *
   * <p>Gets the bundle descriptors as {@link URL} resources.
   *
   * @param descriptors the bundle descriptors, can be separated by comma
   * @return the bundle descriptors.
   * @throws FileNotFoundException is thrown if a bundle descriptor cannot be found
   */
  private static Collection<URL> getBlueprintDescriptors(String descriptors)
      throws FileNotFoundException, MalformedURLException {
    List<URL> answer = new ArrayList<URL>();
    String descriptor = descriptors;
    if (descriptor != null) {
      // there may be more resources separated by comma
      Iterator<Object> it = ObjectHelper.createIterator(descriptor);
      while (it.hasNext()) {
        String s = (String) it.next();
        LOG.trace("Resource descriptor: {}", s);

        // remove leading / to be able to load resource from the classpath
        s = FileUtil.stripLeadingSeparator(s);

        // if there is wildcards for *.xml then we need to find the urls from the package
        if (s.endsWith("*.xml")) {
          String packageName = s.substring(0, s.length() - 5);
          // remove trailing / to be able to load resource from the classpath
          Enumeration<URL> urls = ObjectHelper.loadResourcesAsURL(packageName);
          while (urls.hasMoreElements()) {
            URL url = urls.nextElement();
            File dir = new File(url.getFile());
            if (dir.isDirectory()) {
              File[] files = dir.listFiles();
              if (files != null) {
                for (File file : files) {
                  if (file.isFile() && file.exists() && file.getName().endsWith(".xml")) {
                    String name = packageName + file.getName();
                    LOG.debug("Resolving resource: {}", name);
                    URL xmlUrl = ObjectHelper.loadResourceAsURL(name);
                    if (xmlUrl != null) {
                      answer.add(xmlUrl);
                    }
                  }
                }
              }
            }
          }
        } else {
          LOG.debug("Resolving resource: {}", s);
          URL url = ResourceHelper.resolveMandatoryResourceAsUrl(RESOLVER, s);
          if (url == null) {
            throw new FileNotFoundException("Resource " + s + " not found");
          }
          answer.add(url);
        }
      }
    } else {
      throw new IllegalArgumentException(
          "No bundle descriptor configured. Override getBlueprintDescriptor() or getBlueprintDescriptors() method");
    }

    if (answer.isEmpty()) {
      throw new IllegalArgumentException(
          "Cannot find any resources in classpath from descriptor " + descriptors);
    }
    return answer;
  }
Exemple #13
0
  public void changeToParentDirectory() throws GenericFileOperationFailedException {
    LOG.trace("changeToParentDirectory()");
    String current = getCurrentDirectory();

    String parent = FileUtil.compactPath(current + "/..");
    // must start with absolute
    if (!parent.startsWith("/")) {
      parent = "/" + parent;
    }

    changeCurrentDirectory(parent);
  }
Exemple #14
0
  public boolean existsFile(String name) throws GenericFileOperationFailedException {
    LOG.trace("existsFile({})", name);
    if (endpoint.isFastExistsCheck()) {
      return fastExistsFile(name);
    }
    // check whether a file already exists
    String directory = FileUtil.onlyPath(name);
    if (directory == null) {
      // assume current dir if no path could be extracted
      directory = ".";
    }
    String onlyName = FileUtil.stripPath(name);

    try {
      @SuppressWarnings("rawtypes")
      Vector files = channel.ls(directory);
      // can return either null or an empty list depending on FTP servers
      if (files == null) {
        return false;
      }
      for (Object file : files) {
        ChannelSftp.LsEntry entry = (ChannelSftp.LsEntry) file;
        String existing = entry.getFilename();
        LOG.trace("Existing file: {}, target file: {}", existing, name);
        existing = FileUtil.stripPath(existing);
        if (existing != null && existing.equals(onlyName)) {
          return true;
        }
      }
      return false;
    } catch (SftpException e) {
      // or an exception can be thrown with id 2 which means file does not exists
      if (ChannelSftp.SSH_FX_NO_SUCH_FILE == e.id) {
        return false;
      }
      // otherwise its a more serious error so rethrow
      throw new GenericFileOperationFailedException(e.getMessage(), e);
    }
  }
Exemple #15
0
  @Override
  protected boolean isMatched(GenericFile<FTPFile> file, String doneFileName, List<FTPFile> files) {
    String onlyName = FileUtil.stripPath(doneFileName);

    for (FTPFile f : files) {
      if (f.getName().equals(onlyName)) {
        return true;
      }
    }

    log.trace("Done file: {} does not exist", doneFileName);
    return false;
  }
Exemple #16
0
  private RemoteFile<FTPFile> asRemoteFile(String absolutePath, FTPFile file, String charset) {
    RemoteFile<FTPFile> answer = new RemoteFile<FTPFile>();

    answer.setCharset(charset);
    answer.setEndpointPath(endpointPath);
    answer.setFile(file);
    answer.setFileNameOnly(file.getName());
    answer.setFileLength(file.getSize());
    answer.setDirectory(file.isDirectory());
    if (file.getTimestamp() != null) {
      answer.setLastModified(file.getTimestamp().getTimeInMillis());
    }
    answer.setHostname(((RemoteFileConfiguration) endpoint.getConfiguration()).getHost());

    // absolute or relative path
    boolean absolute = FileUtil.hasLeadingSeparator(absolutePath);
    answer.setAbsolute(absolute);

    // create a pseudo absolute name
    String dir = FileUtil.stripTrailingSeparator(absolutePath);
    String absoluteFileName = FileUtil.stripLeadingSeparator(dir + "/" + file.getName());
    // if absolute start with a leading separator otherwise let it be relative
    if (absolute) {
      absoluteFileName = "/" + absoluteFileName;
    }
    answer.setAbsoluteFilePath(absoluteFileName);

    // the relative filename, skip the leading endpoint configured path
    String relativePath = ObjectHelper.after(absoluteFileName, endpointPath);
    // skip leading /
    relativePath = FileUtil.stripLeadingSeparator(relativePath);
    answer.setRelativeFilePath(relativePath);

    // the file name should be the relative path
    answer.setFileName(answer.getRelativeFilePath());

    return answer;
  }
  @Override
  public void setUp() throws Exception {
    File dir = new File("target/reports/dosnovol");
    deleteDirectory(dir);
    path = dir.getAbsolutePath();
    if (FileUtil.isWindows()) {
      int dp = path.indexOf(":\\");
      if (dp > 0) {
        path = path.substring(dp + 1).replace('\\', '/');
      }
    }

    super.setUp();
  }
  private void pageToFileStream() throws IOException {
    flush();

    ByteArrayOutputStream bout = (ByteArrayOutputStream) currentStream;
    tempFile = FileUtil.createTempFile("cos", ".tmp", strategy.getSpoolDirectory());

    LOG.trace("Creating temporary stream cache file: {}", tempFile);

    try {
      currentStream = createOutputStream(tempFile);
      bout.writeTo(currentStream);
    } finally {
      // ensure flag is flipped to file based
      inMemory = false;
    }
  }
Exemple #19
0
  @Override
  protected boolean pollDirectory(String fileName, List<GenericFile<FTPFile>> fileList, int depth) {
    String currentDir = null;
    if (isStepwise()) {
      // must remember current dir so we stay in that directory after the poll
      currentDir = operations.getCurrentDirectory();
    }

    // strip trailing slash
    fileName = FileUtil.stripTrailingSeparator(fileName);

    boolean answer = doPollDirectory(fileName, null, fileList, depth);
    if (currentDir != null) {
      operations.changeCurrentDirectory(currentDir);
    }

    return answer;
  }
  public void testFileUsingAlternativeStartToken() throws Exception {
    assertExpression("$simple{file:ext}", "txt");
    assertExpression("$simple{file:name.ext}", "txt");
    assertExpression("$simple{file:name}", "test" + File.separator + file.getName());
    assertExpression("$simple{file:name.noext}", "test" + File.separator + "hello");
    assertExpression("$simple{file:onlyname}", file.getName());
    assertExpression("$simple{file:onlyname.noext}", "hello");
    assertExpression("$simple{file:parent}", file.getParent());
    assertExpression("$simple{file:path}", file.getPath());
    assertExpression("$simple{file:absolute}", FileUtil.isAbsolute(file));
    assertExpression("$simple{file:absolute.path}", file.getAbsolutePath());
    assertExpression("$simple{file:length}", file.length());
    assertExpression("$simple{file:size}", file.length());

    // modified is a long object
    long modified = SimpleLanguage.simple("${file:modified}").evaluate(exchange, long.class);
    assertEquals(file.lastModified(), modified);
  }
  public void testLoadFileWithSpace() throws Exception {
    CamelContext context = new DefaultCamelContext();
    context.start();

    createDirectory("target/my space");
    FileUtil.copyFile(
        new File("src/test/resources/log4j2.properties"),
        new File("target/my space/log4j2.properties"));

    InputStream is =
        ResourceHelper.resolveMandatoryResourceAsInputStream(
            context, "file:target/my%20space/log4j2.properties");
    assertNotNull(is);

    String text = context.getTypeConverter().convertTo(String.class, is);
    assertNotNull(text);
    assertTrue(text.contains("log4j"));
    is.close();

    context.stop();
  }
  /**
   * Strategy to configure the move or premove option based on a String input.
   *
   * <p>
   *
   * @param expression the original string input
   * @return configured string or the original if no modifications is needed
   */
  protected String configureMoveOrPreMoveExpression(String expression) {
    // if the expression already have ${ } placeholders then pass it unmodified
    if (SimpleLanguage.hasStartToken(expression)) {
      return expression;
    }

    // remove trailing slash
    expression = FileUtil.stripTrailingSeparator(expression);

    StringBuilder sb = new StringBuilder();

    // if relative then insert start with the parent folder
    if (!isAbsolute(expression)) {
      sb.append("${file:parent}");
      sb.append(getFileSeparator());
    }
    // insert the directory the end user provided
    sb.append(expression);
    // append only the filename (file:name can contain a relative path, so we must use onlyname)
    sb.append(getFileSeparator());
    sb.append("${file:onlyname}");

    return sb.toString();
  }
Exemple #23
0
  Consumer doCreateConsumer(
      CamelContext camelContext,
      Processor processor,
      String verb,
      String basePath,
      String uriTemplate,
      String consumes,
      String produces,
      RestConfiguration configuration,
      Map<String, Object> parameters,
      boolean api)
      throws Exception {

    String path = basePath;
    if (uriTemplate != null) {
      // make sure to avoid double slashes
      if (uriTemplate.startsWith("/")) {
        path = path + uriTemplate;
      } else {
        path = path + "/" + uriTemplate;
      }
    }
    path = FileUtil.stripLeadingSeparator(path);

    RestConfiguration config = configuration;
    if (config == null) {
      config = getCamelContext().getRestConfiguration("spark-rest", true);
    }

    Map<String, Object> map = new HashMap<String, Object>();
    if (consumes != null) {
      map.put("accept", consumes);
    }

    // setup endpoint options
    if (config.getEndpointProperties() != null && !config.getEndpointProperties().isEmpty()) {
      map.putAll(config.getEndpointProperties());
    }

    if (ObjectHelper.isNotEmpty(path)) {
      // spark-rest uses :name syntax instead of {name} so we need to replace those
      Matcher matcher = pattern.matcher(path);
      path = matcher.replaceAll(":$1");
    }

    // prefix path with context-path if configured in rest-dsl configuration
    String contextPath = config.getContextPath();
    if (ObjectHelper.isNotEmpty(contextPath)) {
      contextPath = FileUtil.stripTrailingSeparator(contextPath);
      contextPath = FileUtil.stripLeadingSeparator(contextPath);
      if (ObjectHelper.isNotEmpty(contextPath)) {
        path = contextPath + "/" + path;
      }
    }

    String url;
    if (api) {
      url = "spark-rest:%s:%s?matchOnUriPrefix=true";
    } else {
      url = "spark-rest:%s:%s";
    }

    url = String.format(url, verb, path);

    String query = URISupport.createQueryString(map);
    if (!query.isEmpty()) {
      url = url + "?" + query;
    }

    // get the endpoint
    SparkEndpoint endpoint = camelContext.getEndpoint(url, SparkEndpoint.class);
    setProperties(endpoint, parameters);

    // configure consumer properties
    Consumer consumer = endpoint.createConsumer(processor);
    if (config.getConsumerProperties() != null && !config.getConsumerProperties().isEmpty()) {
      setProperties(consumer, config.getConsumerProperties());
    }

    return consumer;
  }
 public DefaultLSResourceResolver(CamelContext camelContext, String resourceUri) {
   this.camelContext = camelContext;
   this.resourceUri = resourceUri;
   this.resourcePath = FileUtil.onlyPath(resourceUri);
 }
Exemple #25
0
  protected boolean doPollDirectory(
      String absolutePath, String dirName, List<GenericFile<FTPFile>> fileList, int depth) {
    log.trace("doPollDirectory from absolutePath: {}, dirName: {}", absolutePath, dirName);

    depth++;

    // remove trailing /
    dirName = FileUtil.stripTrailingSeparator(dirName);

    // compute dir depending on stepwise is enabled or not
    String dir;
    if (isStepwise()) {
      dir = ObjectHelper.isNotEmpty(dirName) ? dirName : absolutePath;
      operations.changeCurrentDirectory(dir);
    } else {
      dir = absolutePath;
    }

    log.trace("Polling directory: {}", dir);
    List<FTPFile> files = null;
    if (isUseList()) {
      if (isStepwise()) {
        files = operations.listFiles();
      } else {
        files = operations.listFiles(dir);
      }
    } else {
      // we cannot use the LIST command(s) so we can only poll a named file
      // so created a pseudo file with that name
      FTPFile file = new FTPFile();
      file.setType(FTPFile.FILE_TYPE);
      fileExpressionResult = evaluateFileExpression();
      if (fileExpressionResult != null) {
        file.setName(fileExpressionResult);
        files = new ArrayList<FTPFile>(1);
        files.add(file);
      }
    }

    if (files == null || files.isEmpty()) {
      // no files in this directory to poll
      log.trace("No files found in directory: {}", dir);
      return true;
    } else {
      // we found some files
      log.trace("Found {} in directory: {}", files.size(), dir);
    }

    for (FTPFile file : files) {

      if (log.isTraceEnabled()) {
        log.trace(
            "FtpFile[name={}, dir={}, file={}]",
            new Object[] {file.getName(), file.isDirectory(), file.isFile()});
      }

      // check if we can continue polling in files
      if (!canPollMoreFiles(fileList)) {
        return false;
      }

      if (file.isDirectory()) {
        RemoteFile<FTPFile> remote = asRemoteFile(absolutePath, file, getEndpoint().getCharset());
        if (endpoint.isRecursive()
            && depth < endpoint.getMaxDepth()
            && isValidFile(remote, true, files)) {
          // recursive scan and add the sub files and folders
          String subDirectory = file.getName();
          String path = absolutePath + "/" + subDirectory;
          boolean canPollMore = pollSubDirectory(path, subDirectory, fileList, depth);
          if (!canPollMore) {
            return false;
          }
        }
      } else if (file.isFile()) {
        RemoteFile<FTPFile> remote = asRemoteFile(absolutePath, file, getEndpoint().getCharset());
        if (depth >= endpoint.getMinDepth() && isValidFile(remote, false, files)) {
          // matched file so add
          fileList.add(remote);
        }
      } else {
        log.debug("Ignoring unsupported remote file type: " + file);
      }
    }

    return true;
  }
 public String normalizePath(String name) {
   return FileUtil.normalizePath(name);
 }
  private void parse(
      Swagger swagger, RestDefinition rest, String camelContextId, ClassResolver classResolver) {
    List<VerbDefinition> verbs = new ArrayList<>(rest.getVerbs());
    // must sort the verbs by uri so we group them together when an uri has multiple operations
    Collections.sort(verbs, new VerbOrdering());

    // we need to group the operations within the same tag, so use the path as default if not
    // configured
    String pathAsTag =
        rest.getTag() != null ? rest.getTag() : FileUtil.stripLeadingSeparator(rest.getPath());
    String summary = rest.getDescriptionText();

    if (ObjectHelper.isNotEmpty(pathAsTag)) {
      // add rest as tag
      Tag tag = new Tag();
      tag.description(summary);
      tag.name(pathAsTag);
      swagger.addTag(tag);
    }

    // gather all types in use
    Set<String> types = new LinkedHashSet<>();
    for (VerbDefinition verb : verbs) {
      String type = verb.getType();
      if (ObjectHelper.isNotEmpty(type)) {
        if (type.endsWith("[]")) {
          type = type.substring(0, type.length() - 2);
        }
        types.add(type);
      }
      type = verb.getOutType();
      if (ObjectHelper.isNotEmpty(type)) {
        if (type.endsWith("[]")) {
          type = type.substring(0, type.length() - 2);
        }
        types.add(type);
      }
      // there can also be types in response messages
      if (verb.getResponseMsgs() != null) {
        for (RestOperationResponseMsgDefinition def : verb.getResponseMsgs()) {
          type = def.getResponseModel();
          if (ObjectHelper.isNotEmpty(type)) {
            if (type.endsWith("[]")) {
              type = type.substring(0, type.length() - 2);
            }
            types.add(type);
          }
        }
      }
    }

    // use annotation scanner to find models (annotated classes)
    for (String type : types) {
      Class<?> clazz = classResolver.resolveClass(type);
      appendModels(clazz, swagger);
    }

    // used during gathering of apis
    List<Path> paths = new ArrayList<>();

    String basePath = rest.getPath();

    for (VerbDefinition verb : verbs) {

      // the method must be in lower case
      String method = verb.asVerb().toLowerCase(Locale.US);
      // operation path is a key
      String opPath = SwaggerHelper.buildUrl(basePath, verb.getUri());

      Operation op = new Operation();
      if (ObjectHelper.isNotEmpty(pathAsTag)) {
        // group in the same tag
        op.addTag(pathAsTag);
      }

      // add id as vendor extensions
      op.getVendorExtensions().put("x-camelContextId", camelContextId);
      op.getVendorExtensions().put("x-routeId", verb.getRouteId());

      Path path = swagger.getPath(opPath);
      if (path == null) {
        path = new Path();
        paths.add(path);
      }
      path = path.set(method, op);

      String consumes = verb.getConsumes() != null ? verb.getConsumes() : rest.getConsumes();
      if (consumes != null) {
        String[] parts = consumes.split(",");
        for (String part : parts) {
          op.addConsumes(part);
        }
      }

      String produces = verb.getProduces() != null ? verb.getProduces() : rest.getProduces();
      if (produces != null) {
        String[] parts = produces.split(",");
        for (String part : parts) {
          op.addProduces(part);
        }
      }

      if (verb.getDescriptionText() != null) {
        op.summary(verb.getDescriptionText());
      }

      for (RestOperationParamDefinition param : verb.getParams()) {
        Parameter parameter = null;
        if (param.getType().equals(RestParamType.body)) {
          parameter = new BodyParameter();
        } else if (param.getType().equals(RestParamType.form)) {
          parameter = new FormParameter();
        } else if (param.getType().equals(RestParamType.header)) {
          parameter = new HeaderParameter();
        } else if (param.getType().equals(RestParamType.path)) {
          parameter = new PathParameter();
        } else if (param.getType().equals(RestParamType.query)) {
          parameter = new QueryParameter();
        }

        if (parameter != null) {
          parameter.setName(param.getName());
          parameter.setAccess(param.getAccess());
          parameter.setDescription(param.getDescription());
          parameter.setRequired(param.getRequired());

          // set type on parameter
          if (parameter instanceof SerializableParameter) {
            SerializableParameter sp = (SerializableParameter) parameter;

            if (param.getDataType() != null) {
              sp.setType(param.getDataType());
            }
            if (param.getAllowableValues() != null) {
              sp.setEnum(param.getAllowableValues());
            }
          }

          // set schema on body parameter
          if (parameter instanceof BodyParameter) {
            BodyParameter bp = (BodyParameter) parameter;

            if (verb.getType() != null) {
              String ref = modelTypeAsRef(verb.getType(), swagger);
              if (ref != null) {
                bp.setSchema(new RefModel(ref));
              }
            }
          }

          op.addParameter(parameter);
        }
      }

      // if we have an out type then set that as response message
      if (verb.getOutType() != null) {
        Response response = new Response();
        Property prop = modelTypeAsProperty(verb.getOutType(), swagger);
        response.setSchema(prop);
        response.setDescription("Output type");
        op.addResponse("200", response);
      }

      // enrich with configured response messages from the rest-dsl
      for (RestOperationResponseMsgDefinition msg : verb.getResponseMsgs()) {
        Response response = null;
        if (op.getResponses() != null) {
          response = op.getResponses().get(msg.getCode());
        }
        if (response == null) {
          response = new Response();
        }
        if (ObjectHelper.isNotEmpty(msg.getResponseModel())) {
          Property prop = modelTypeAsProperty(msg.getResponseModel(), swagger);
          response.setSchema(prop);
        }
        response.setDescription(msg.getMessage());
        op.addResponse(msg.getCode(), response);
      }

      // add path
      swagger.path(opPath, path);
    }
  }
Exemple #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;
  }
Exemple #29
0
 @Override
 public void onDone(Exchange exchange) {
   FileUtil.deleteFile(new File(fileName));
 }