/**
   * 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;
  }
  private TriggerKey createTriggerKey(String uri, String remaining, QuartzEndpoint endpoint)
      throws Exception {
    // Parse uri for trigger name and group
    URI u = new URI(uri);
    String path = ObjectHelper.after(u.getPath(), "/");
    String host = u.getHost();

    // host can be null if the uri did contain invalid host characters such as an underscore
    if (host == null) {
      host = ObjectHelper.before(remaining, "/");
    }

    // Trigger group can be optional, if so set it to this context's unique name
    String name;
    String group;
    if (ObjectHelper.isNotEmpty(path) && ObjectHelper.isNotEmpty(host)) {
      group = host;
      name = path;
    } else {
      String camelContextName = getCamelContext().getManagementName();
      group = camelContextName == null ? "Camel" : "Camel_" + camelContextName;
      name = host;
    }

    if (prefixJobNameWithEndpointId) {
      name = endpoint.getId() + "_" + name;
    }

    return new TriggerKey(name, group);
  }
    @Override
    public void run() {
      ClientMixedOperation<
              ReplicationController,
              ReplicationControllerList,
              DoneableReplicationController,
              ClientRollableScallableResource<ReplicationController, DoneableReplicationController>>
          w = getEndpoint().getKubernetesClient().replicationControllers();
      if (ObjectHelper.isNotEmpty(getEndpoint().getKubernetesConfiguration().getNamespace())) {
        w.inNamespace(getEndpoint().getKubernetesConfiguration().getNamespace());
      }
      if (ObjectHelper.isNotEmpty(getEndpoint().getKubernetesConfiguration().getLabelKey())
          && ObjectHelper.isNotEmpty(getEndpoint().getKubernetesConfiguration().getLabelValue())) {
        w.withLabel(
            getEndpoint().getKubernetesConfiguration().getLabelKey(),
            getEndpoint().getKubernetesConfiguration().getLabelValue());
      }
      if (ObjectHelper.isNotEmpty(getEndpoint().getKubernetesConfiguration().getResourceName())) {
        w.withName(getEndpoint().getKubernetesConfiguration().getResourceName());
      }
      w.watch(
          new Watcher<ReplicationController>() {

            @Override
            public void eventReceived(
                io.fabric8.kubernetes.client.Watcher.Action action,
                ReplicationController resource) {
              ReplicationControllerEvent rce = new ReplicationControllerEvent(action, resource);
              Exchange exchange = getEndpoint().createExchange();
              exchange.getIn().setBody(rce.getReplicationController());
              exchange
                  .getIn()
                  .setHeader(KubernetesConstants.KUBERNETES_EVENT_ACTION, rce.getAction());
              exchange
                  .getIn()
                  .setHeader(
                      KubernetesConstants.KUBERNETES_EVENT_TIMESTAMP, System.currentTimeMillis());
              try {
                processor.process(exchange);
              } catch (Exception e) {
                getExceptionHandler().handleException("Error during processing", exchange, e);
              }
            }

            @Override
            public void onClose(KubernetesClientException cause) {
              if (cause != null) {
                LOG.error(cause.getMessage(), cause);
              }
            }
          });
    }
示例#4
0
 /** Optional suffix appended to property names before resolution. */
 public void setPropertySuffix(String propertySuffix) {
   this.propertySuffix = propertySuffix;
   this.propertySuffixResolved = propertySuffix;
   if (ObjectHelper.isNotEmpty(this.propertySuffix)) {
     this.propertySuffixResolved = FilePathResolver.resolvePath(this.propertySuffix);
   }
 }
示例#5
0
 protected void outputRoutesToFile() throws IOException, JAXBException {
   if (ObjectHelper.isNotEmpty(getRoutesOutputFile())) {
     LOG.info("Generating routes as XML in the file named: " + getRoutesOutputFile());
     ModelFileGenerator generator = createModelFileGenerator();
     generator.marshalRoutesUsingJaxb(getRoutesOutputFile(), getRouteDefinitions());
   }
 }
示例#6
0
 private void addDependsOnToRouteBuilder(
     Element childElement, ParserContext parserContext, String contextId) {
   // setting the depends-on explicitly is required since Spring 3.0
   String routeBuilderName = childElement.getAttribute("ref");
   if (ObjectHelper.isNotEmpty(routeBuilderName)) {
     // set depends-on to the context for a routeBuilder bean
     try {
       BeanDefinition definition = parserContext.getRegistry().getBeanDefinition(routeBuilderName);
       Method getDependsOn = definition.getClass().getMethod("getDependsOn", new Class[] {});
       String[] dependsOn = (String[]) getDependsOn.invoke(definition);
       if (dependsOn == null || dependsOn.length == 0) {
         dependsOn = new String[] {contextId};
       } else {
         String[] temp = new String[dependsOn.length + 1];
         System.arraycopy(dependsOn, 0, temp, 0, dependsOn.length);
         temp[dependsOn.length] = contextId;
         dependsOn = temp;
       }
       Method method = definition.getClass().getMethod("setDependsOn", String[].class);
       method.invoke(definition, (Object) dependsOn);
     } catch (Exception e) {
       // Do nothing here
     }
   }
 }
示例#7
0
 protected String getFabricPath(String name) {
   String path = name;
   if (ObjectHelper.isNotEmpty(zkRoot)) {
     path = zkRoot + "/" + name;
   }
   return path;
 }
 /** Returns the endpoint URI or the name of the reference to it */
 public Object getUriOrRef() {
   if (ObjectHelper.isNotEmpty(uri)) {
     return uri;
   } else if (endpoint != null) {
     return endpoint.getEndpointUri();
   }
   return ref;
 }
示例#9
0
 /** Does the given context match this camel context */
 private boolean matchContext(String context) {
   if (ObjectHelper.isNotEmpty(context)) {
     if (!camelContext.getName().equals(context)) {
       return false;
     }
   }
   return true;
 }
示例#10
0
  /** Get parameters defined in @CsvRecord annotation */
  private void initCsvRecordParameters() {
    if (separator == null) {
      for (Class<?> cl : models) {

        // Get annotation @CsvRecord from the class
        CsvRecord record = cl.getAnnotation(CsvRecord.class);

        // Get annotation @Section from the class
        Section section = cl.getAnnotation(Section.class);

        if (record != null) {
          LOG.debug("Csv record: {}", record);

          // Get skipFirstLine parameter
          skipFirstLine = record.skipFirstLine();
          LOG.debug("Skip First Line parameter of the CSV: {}" + skipFirstLine);

          // Get generateHeaderColumnNames parameter
          generateHeaderColumnNames = record.generateHeaderColumns();
          LOG.debug(
              "Generate header column names parameter of the CSV: {}", generateHeaderColumnNames);

          // Get Separator parameter
          ObjectHelper.notNull(
              record.separator(), "No separator has been defined in the @Record annotation");
          separator = record.separator();
          LOG.debug("Separator defined for the CSV: {}", separator);

          // Get carriage return parameter
          crlf = record.crlf();
          LOG.debug("Carriage return defined for the CSV: {}", crlf);

          // Get isOrdered parameter
          messageOrdered = record.isOrdered();
          LOG.debug("Must CSV record be ordered: {}", messageOrdered);

          if (ObjectHelper.isNotEmpty(record.quote())) {
            quote = record.quote();
            LOG.debug("Quoting columns with: {}", quote);
          }

          quoting = record.quoting();
          LOG.debug("CSV will be quoted: {}", messageOrdered);

          autospanLine = record.autospanLine();
          LOG.debug("Autospan line in last record: {}", autospanLine);
        }

        if (section != null) {
          // Test if section number is not null
          ObjectHelper.notNull(section.number(), "No number has been defined for the section");

          // Get section number and add it to the sections
          sections.put(cl.getName(), section.number());
        }
      }
    }
  }
示例#11
0
 public boolean retrieveFile(String name, Exchange exchange)
     throws GenericFileOperationFailedException {
   LOG.trace("retrieveFile({})", name);
   if (ObjectHelper.isNotEmpty(endpoint.getLocalWorkDirectory())) {
     // local work directory is configured so we should store file content as files in this local
     // directory
     return retrieveFileToFileInLocalWorkDirectory(name, exchange);
   } else {
     // store file content directory as stream on the body
     return retrieveFileToStreamInBody(name, exchange);
   }
 }
示例#12
0
  @Override
  public void process(Exchange exchange) throws Exception {
    NatsConfiguration config = getEndpoint().getNatsConfiguration();
    String body = exchange.getIn().getMandatoryBody(String.class);

    LOG.debug("Publishing to topic: {}", config.getTopic());

    if (ObjectHelper.isNotEmpty(config.getReplySubject())) {
      String replySubject = config.getReplySubject();
      connection.publish(config.getTopic(), replySubject, body.getBytes());
    } else {
      connection.publish(config.getTopic(), body.getBytes());
    }
  }
 private String getInputUri() {
   // find the xsd with relative path
   if (ObjectHelper.isNotEmpty(relatedURI)) {
     try {
       ResourceHelper.resolveMandatoryResourceAsInputStream(
           camelContext.getClassResolver(), relatedURI);
       return relatedURI;
     } catch (IOException e) {
       // ignore the exception
     }
   }
   // don't use the relative path
   return getUri(systemId);
 }
示例#14
0
 protected void addDependsOn(CamelContextFactoryBean factoryBean, BeanDefinitionBuilder builder) {
   String dependsOn = factoryBean.getDependsOn();
   if (ObjectHelper.isNotEmpty(dependsOn)) {
     // comma, whitespace and semi colon is valid separators in Spring depends-on
     String[] depends = dependsOn.split(",|;|\\s");
     if (depends == null) {
       throw new IllegalArgumentException("Cannot separate depends-on, was: " + dependsOn);
     } else {
       for (String depend : depends) {
         depend = depend.trim();
         LOG.debug("Adding dependsOn {} to CamelContext({})", depend, factoryBean.getId());
         builder.addDependsOn(depend);
       }
     }
   }
 }
  public void process(Exchange exchange) throws Exception {
    // send direct message
    String toUsername = te.getProperties().getUser();
    if (ObjectHelper.isNotEmpty(
        exchange.getIn().getHeader(TwitterConstants.TWITTER_USER, String.class))) {
      toUsername = exchange.getIn().getHeader(TwitterConstants.TWITTER_USER, String.class);
    }
    String text = exchange.getIn().getBody(String.class);

    if (toUsername.isEmpty()) {
      throw new CamelExchangeException("Username not configured on TwitterEndpoint", exchange);
    } else {
      log.debug("Sending to: {} message: {}", toUsername, text);
      te.getProperties().getTwitter().sendDirectMessage(toUsername, text);
    }
  }
示例#16
0
  public static boolean hasProperties(Map<String, Object> properties, String optionPrefix) {
    ObjectHelper.notNull(properties, "properties");

    if (ObjectHelper.isNotEmpty(optionPrefix)) {
      for (Object o : properties.keySet()) {
        String name = (String) o;
        if (name.startsWith(optionPrefix)) {
          return true;
        }
      }
      // no parameters with this prefix
      return false;
    } else {
      return !properties.isEmpty();
    }
  }
示例#17
0
 protected void generateDot(String name, CamelContext camelContext, int size) throws IOException {
   String outputDir = dotOutputDir;
   if (ObjectHelper.isNotEmpty(outputDir)) {
     if (size > 1) {
       outputDir += "/" + name;
     }
     RouteDotGenerator generator = new RouteDotGenerator(outputDir);
     LOG.info(
         "Generating DOT file for routes: "
             + outputDir
             + " for: "
             + camelContext
             + " with name: "
             + name);
     generator.drawRoutes(camelContext);
   }
 }
示例#18
0
 private void registerEndpoint(
     Element childElement, ParserContext parserContext, String contextId) {
   String id = childElement.getAttribute("id");
   // must have an id to be registered
   if (ObjectHelper.isNotEmpty(id)) {
     BeanDefinition definition = endpointParser.parse(childElement, parserContext);
     definition
         .getPropertyValues()
         .addPropertyValue("camelContext", new RuntimeBeanReference(contextId));
     // Need to add this dependency of CamelContext for Spring 3.0
     try {
       Method method = definition.getClass().getMethod("setDependsOn", String[].class);
       method.invoke(definition, (Object) new String[] {contextId});
     } catch (Exception e) {
       // Do nothing here
     }
     parserContext.registerBeanComponent(new BeanComponentDefinition(definition, id));
   }
 }
  /** Configures the given message with the file which sets the body to the file object. */
  public void configureMessage(GenericFile<T> file, Message message) {
    message.setBody(file);

    if (flatten) {
      // when flatten the file name should not contain any paths
      message.setHeader(Exchange.FILE_NAME, file.getFileNameOnly());
    } else {
      // compute name to set on header that should be relative to starting directory
      String name = file.isAbsolute() ? file.getAbsoluteFilePath() : file.getRelativeFilePath();

      // skip leading endpoint configured directory
      String endpointPath = getConfiguration().getDirectory() + getFileSeparator();
      if (ObjectHelper.isNotEmpty(endpointPath) && name.startsWith(endpointPath)) {
        name = ObjectHelper.after(name, endpointPath);
      }

      // adjust filename
      message.setHeader(Exchange.FILE_NAME, name);
    }
  }
  private OutputStream createOutputStream(File file) throws IOException {
    OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
    if (ObjectHelper.isNotEmpty(strategy.getSpoolChiper())) {
      try {
        if (ciphers == null) {
          ciphers = new CipherPair(strategy.getSpoolChiper());
        }
      } catch (GeneralSecurityException e) {
        throw new IOException(e.getMessage(), e);
      }
      out =
          new CipherOutputStream(out, ciphers.getEncryptor()) {
            boolean closed;

            public void close() throws IOException {
              if (!closed) {
                super.close();
                closed = true;
              }
            }
          };
    }
    return out;
  }
示例#21
0
  private List<PropertiesLocation> parseLocations(List<PropertiesLocation> locations) {
    List<PropertiesLocation> answer = new ArrayList<>();

    for (PropertiesLocation location : locations) {
      LOG.trace("Parsing location: {} ", location);

      try {
        String path = FilePathResolver.resolvePath(location.getPath());
        LOG.debug("Parsed location: {} ", path);
        if (ObjectHelper.isNotEmpty(path)) {
          answer.add(new PropertiesLocation(location.getResolver(), path, location.isOptional()));
        }
      } catch (IllegalArgumentException e) {
        if (!ignoreMissingLocation && !location.isOptional()) {
          throw e;
        } else {
          LOG.debug("Ignored missing location: {}", location);
        }
      }
    }

    // must return a not-null answer
    return answer;
  }
示例#22
0
  private String[] parseLocations(String[] locations) {
    List<String> answer = new ArrayList<String>();

    for (String location : locations) {
      LOG.trace("Parsing location: {} ", location);

      try {
        location = FilePathResolver.resolvePath(location);
        LOG.debug("Parsed location: {} ", location);
        if (ObjectHelper.isNotEmpty(location)) {
          answer.add(location);
        }
      } catch (IllegalArgumentException e) {
        if (!ignoreMissingLocation) {
          throw e;
        } else {
          LOG.debug("Ignored missing location: {}", location);
        }
      }
    }

    // must return a not-null answer
    return answer.toArray(new String[answer.size()]);
  }
示例#23
0
  /**
   * Read the REST-DSL definition's and parse that as a Swagger model representation
   *
   * @param rests the rest-dsl
   * @param route optional route path to filter the rest-dsl to only include from the chose route
   * @param config the swagger configuration
   * @param classResolver class resolver to use
   * @return the swagger model
   */
  public Swagger read(
      List<RestDefinition> rests,
      String route,
      BeanConfig config,
      String camelContextId,
      ClassResolver classResolver) {
    Swagger swagger = new Swagger();

    for (RestDefinition rest : rests) {

      if (ObjectHelper.isNotEmpty(route) && !route.equals("/")) {
        // filter by route
        if (!rest.getPath().equals(route)) {
          continue;
        }
      }

      parse(swagger, rest, camelContextId, classResolver);
    }

    // configure before returning
    swagger = config.configure(swagger);
    return swagger;
  }
示例#24
0
  @Override
  public Processor createProcessor(RouteContext routeContext) throws Exception {
    BeanProcessor answer;
    Class<?> clazz = bean != null ? bean.getClass() : null;
    BeanHolder beanHolder;

    if (ObjectHelper.isNotEmpty(ref)) {
      if (cache != null && cache) {
        // cache the registry lookup which avoids repeat lookup in the registry
        beanHolder = new RegistryBean(routeContext.getCamelContext(), ref).createCacheHolder();
      } else {
        beanHolder = new RegistryBean(routeContext.getCamelContext(), ref);
      }
      // bean holder will check if the bean exists
      bean = beanHolder.getBean();
      answer = new BeanProcessor(beanHolder);
    } else {
      if (bean == null) {

        if (beanType == null && beanClass == null) {
          throw new IllegalArgumentException("bean, ref or beanType must be provided");
        }

        // the clazz is either from beanType or beanClass
        if (beanType != null) {
          try {
            clazz =
                routeContext.getCamelContext().getClassResolver().resolveMandatoryClass(beanType);
          } catch (ClassNotFoundException e) {
            throw ObjectHelper.wrapRuntimeCamelException(e);
          }
        } else {
          clazz = beanClass;
        }

        // create a bean if there is a default public no-arg constructor
        if (ObjectHelper.hasDefaultPublicNoArgConstructor(clazz)) {
          bean = CamelContextHelper.newInstance(routeContext.getCamelContext(), clazz);
          ObjectHelper.notNull(bean, "bean", this);
        }
      }

      // validate the bean type is not from java so you by mistake think its a reference
      // to a bean name but the String is being invoke instead
      if (bean instanceof String) {
        throw new IllegalArgumentException(
            "The bean instance is a java.lang.String type: "
                + bean
                + ". We suppose you want to refer to a bean instance by its id instead. Please use beanRef.");
      }

      // the holder should either be bean or type based
      beanHolder =
          bean != null
              ? new ConstantBeanHolder(bean, routeContext.getCamelContext())
              : new ConstantTypeBeanHolder(clazz, routeContext.getCamelContext());
      answer = new BeanProcessor(beanHolder);
    }

    // check for method exists
    if (method != null) {
      answer.setMethod(method);

      // check there is a method with the given name, and leverage BeanInfo for that
      BeanInfo beanInfo = beanHolder.getBeanInfo();
      if (bean != null) {
        // there is a bean instance, so check for any methods
        if (!beanInfo.hasMethod(method)) {
          throw ObjectHelper.wrapRuntimeCamelException(
              new MethodNotFoundException(null, bean, method));
        }
      } else if (clazz != null) {
        // there is no bean instance, so check for static methods only
        if (!beanInfo.hasStaticMethod(method)) {
          throw ObjectHelper.wrapRuntimeCamelException(
              new MethodNotFoundException(null, clazz, method, true));
        }
      }
    }

    return answer;
  }
示例#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;
  }
  @Override
  public void populateCamelHeaders(
      HttpRequest request,
      Map<String, Object> headers,
      Exchange exchange,
      NettyHttpConfiguration configuration)
      throws Exception {
    LOG.trace("populateCamelHeaders: {}", request);

    // NOTE: these headers is applied using the same logic as camel-http/camel-jetty to be
    // consistent

    headers.put(Exchange.HTTP_METHOD, request.getMethod().getName());
    // strip query parameters from the uri
    String s = request.getUri();
    if (s.contains("?")) {
      s = ObjectHelper.before(s, "?");
    }

    // we want the full path for the url, as the client may provide the url in the HTTP headers as
    // absolute or relative, eg
    //   /foo
    //   http://servername/foo
    String http = configuration.isSsl() ? "https://" : "http://";
    if (!s.startsWith(http)) {
      if (configuration.getPort() != 80) {
        s = http + configuration.getHost() + ":" + configuration.getPort() + s;
      } else {
        s = http + configuration.getHost() + s;
      }
    }

    headers.put(Exchange.HTTP_URL, s);
    // uri is without the host and port
    URI uri = new URI(request.getUri());
    // uri is path and query parameters
    headers.put(Exchange.HTTP_URI, uri.getPath());
    headers.put(Exchange.HTTP_QUERY, uri.getQuery());
    headers.put(Exchange.HTTP_RAW_QUERY, uri.getRawQuery());

    // strip the starting endpoint path so the path is relative to the endpoint uri
    String path = uri.getPath();
    if (configuration.getPath() != null && path.startsWith(configuration.getPath())) {
      path = path.substring(configuration.getPath().length());
    }
    headers.put(Exchange.HTTP_PATH, path);

    if (LOG.isTraceEnabled()) {
      LOG.trace("HTTP-Method {}", request.getMethod().getName());
      LOG.trace("HTTP-Uri {}", request.getUri());
    }

    for (String name : request.headers().names()) {
      // mapping the content-type
      if (name.toLowerCase(Locale.US).equals("content-type")) {
        name = Exchange.CONTENT_TYPE;
      }

      if (name.toLowerCase(Locale.US).equals("authorization")) {
        String value = request.headers().get(name);
        // store a special header that this request was authenticated using HTTP Basic
        if (value != null && value.trim().startsWith("Basic")) {
          if (headerFilterStrategy != null
              && !headerFilterStrategy.applyFilterToExternalHeaders(
                  NettyHttpConstants.HTTP_AUTHENTICATION, "Basic", exchange)) {
            NettyHttpHelper.appendHeader(headers, NettyHttpConstants.HTTP_AUTHENTICATION, "Basic");
          }
        }
      }

      // add the headers one by one, and use the header filter strategy
      List<String> values = request.headers().getAll(name);
      Iterator<?> it = ObjectHelper.createIterator(values);
      while (it.hasNext()) {
        Object extracted = it.next();
        Object decoded = shouldUrlDecodeHeader(configuration, name, extracted, "UTF-8");
        LOG.trace("HTTP-header: {}", extracted);
        if (headerFilterStrategy != null
            && !headerFilterStrategy.applyFilterToExternalHeaders(name, decoded, exchange)) {
          NettyHttpHelper.appendHeader(headers, name, decoded);
        }
      }
    }

    // add uri parameters as headers to the Camel message
    if (request.getUri().contains("?")) {
      String query = ObjectHelper.after(request.getUri(), "?");
      Map<String, Object> uriParameters = URISupport.parseQuery(query, false, true);

      for (Map.Entry<String, Object> entry : uriParameters.entrySet()) {
        String name = entry.getKey();
        Object values = entry.getValue();
        Iterator<?> it = ObjectHelper.createIterator(values);
        while (it.hasNext()) {
          Object extracted = it.next();
          Object decoded = shouldUrlDecodeHeader(configuration, name, extracted, "UTF-8");
          LOG.trace("URI-Parameter: {}", extracted);
          if (headerFilterStrategy != null
              && !headerFilterStrategy.applyFilterToExternalHeaders(name, decoded, exchange)) {
            NettyHttpHelper.appendHeader(headers, name, decoded);
          }
        }
      }
    }

    // if body is application/x-www-form-urlencoded then extract the body as query string and append
    // as headers
    // if it is a bridgeEndpoint we need to skip this part of work
    if (request.getMethod().getName().equals("POST")
        && request.headers().get(Exchange.CONTENT_TYPE) != null
        && request
            .headers()
            .get(Exchange.CONTENT_TYPE)
            .startsWith(NettyHttpConstants.CONTENT_TYPE_WWW_FORM_URLENCODED)
        && !configuration.isBridgeEndpoint()) {

      String charset = "UTF-8";

      // Push POST form params into the headers to retain compatibility with DefaultHttpBinding
      String body = request.getContent().toString(Charset.forName(charset));
      if (ObjectHelper.isNotEmpty(body)) {
        for (String param : body.split("&")) {
          String[] pair = param.split("=", 2);
          if (pair.length == 2) {
            String name = shouldUrlDecodeHeader(configuration, "", pair[0], charset);
            String value = shouldUrlDecodeHeader(configuration, name, pair[1], charset);
            if (headerFilterStrategy != null
                && !headerFilterStrategy.applyFilterToExternalHeaders(name, value, exchange)) {
              NettyHttpHelper.appendHeader(headers, name, value);
            }
          } else {
            throw new IllegalArgumentException(
                "Invalid parameter, expected to be a pair but was " + param);
          }
        }
      }
    }
  }
示例#27
0
  /**
   * Processes any custom {@link org.apache.camel.component.http.UrlRewrite}.
   *
   * @param exchange the exchange
   * @param url the url
   * @param endpoint the http endpoint
   * @param producer the producer
   * @return the rewritten url, or <tt>null</tt> to use original url
   * @throws Exception is thrown if any error during rewriting url
   */
  public static String urlRewrite(
      Exchange exchange, String url, HttpEndpoint endpoint, Producer producer) throws Exception {
    String answer = null;

    String relativeUrl;
    if (endpoint.getUrlRewrite() != null) {
      // we should use the relative path if possible
      String baseUrl;
      relativeUrl = endpoint.getHttpUri().toASCIIString();
      if (url.startsWith(relativeUrl)) {
        baseUrl = url.substring(0, relativeUrl.length());
        relativeUrl = url.substring(relativeUrl.length());
      } else {
        baseUrl = null;
        relativeUrl = url;
      }
      // mark it as null if its empty
      if (ObjectHelper.isEmpty(relativeUrl)) {
        relativeUrl = null;
      }

      String newUrl;
      if (endpoint.getUrlRewrite() instanceof HttpServletUrlRewrite) {
        // its servlet based, so we need the servlet request
        HttpServletRequest request = exchange.getIn().getBody(HttpServletRequest.class);
        if (request == null) {
          HttpMessage msg = exchange.getIn(HttpMessage.class);
          if (msg != null) {
            request = msg.getRequest();
          }
        }
        if (request == null) {
          throw new IllegalArgumentException(
              "UrlRewrite "
                  + endpoint.getUrlRewrite()
                  + " requires the message body to be a"
                  + "HttpServletRequest instance, but was: "
                  + ObjectHelper.className(exchange.getIn().getBody()));
        }
        // we need to adapt the context-path to be the path from the endpoint, if it came from a
        // http based endpoint
        // as eg camel-jetty have hardcoded context-path as / for all its servlets/endpoints
        // we have the actual context-path stored as a header with the key CamelServletContextPath
        String contextPath = exchange.getIn().getHeader("CamelServletContextPath", String.class);
        request = new UrlRewriteHttpServletRequestAdapter(request, contextPath);
        newUrl =
            ((HttpServletUrlRewrite) endpoint.getUrlRewrite())
                .rewrite(url, relativeUrl, producer, request);
      } else {
        newUrl = endpoint.getUrlRewrite().rewrite(url, relativeUrl, producer);
      }

      if (ObjectHelper.isNotEmpty(newUrl) && newUrl != url) {
        // we got a new url back, that can either be a new absolute url
        // or a new relative url
        if (newUrl.startsWith("http:") || newUrl.startsWith("https:")) {
          answer = newUrl;
        } else if (baseUrl != null) {
          // avoid double // when adding the urls
          if (baseUrl.endsWith("/") && newUrl.startsWith("/")) {
            answer = baseUrl + newUrl.substring(1);
          } else {
            answer = baseUrl + newUrl;
          }
        } else {
          // use the new url as is
          answer = newUrl;
        }
        if (LOG.isDebugEnabled()) {
          LOG.debug(
              "Using url rewrite to rewrite from url {} to {} -> {}",
              new Object[] {relativeUrl != null ? relativeUrl : url, newUrl, answer});
        }
      }
    }

    return answer;
  }
  /**
   * Perform the work to process the fileExchange
   *
   * @param exchange fileExchange
   * @param target the target filename
   * @throws Exception is thrown if some error
   */
  protected void processExchange(Exchange exchange, String target) throws Exception {
    log.trace("Processing file: {} for exchange: {}", target, exchange);

    try {
      preWriteCheck();

      // should we write to a temporary name and then afterwards rename to real target
      boolean writeAsTempAndRename = ObjectHelper.isNotEmpty(endpoint.getTempFileName());
      String tempTarget = null;
      // remember if target exists to avoid checking twice
      Boolean targetExists = null;
      if (writeAsTempAndRename) {
        // compute temporary name with the temp prefix
        tempTarget = createTempFileName(exchange, target);

        log.trace("Writing using tempNameFile: {}", tempTarget);

        // if we should eager delete target file before deploying temporary file
        if (endpoint.getFileExist() != GenericFileExist.TryRename
            && endpoint.isEagerDeleteTargetFile()) {

          // cater for file exists option on the real target as
          // the file operations code will work on the temp file

          // if an existing file already exists what should we do?
          targetExists = operations.existsFile(target);
          if (targetExists) {

            log.trace("EagerDeleteTargetFile, target exists");

            if (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.", target);
              return;
            } else if (endpoint.getFileExist() == GenericFileExist.Fail) {
              throw new GenericFileOperationFailedException(
                  "File already exist: " + target + ". Cannot write new file.");
            } else if (endpoint.isEagerDeleteTargetFile()
                && endpoint.getFileExist() == GenericFileExist.Override) {
              // we override the target so we do this by deleting it so the temp file can be renamed
              // later
              // with success as the existing target file have been deleted
              log.trace("Eagerly deleting existing file: {}", target);
              if (!operations.deleteFile(target)) {
                throw new GenericFileOperationFailedException("Cannot delete file: " + target);
              }
            }
          }
        }

        // delete any pre existing temp file
        if (operations.existsFile(tempTarget)) {
          log.trace("Deleting existing temp file: {}", tempTarget);
          if (!operations.deleteFile(tempTarget)) {
            throw new GenericFileOperationFailedException("Cannot delete file: " + tempTarget);
          }
        }
      }

      // write/upload the file
      writeFile(exchange, tempTarget != null ? tempTarget : target);

      // if we did write to a temporary name then rename it to the real
      // name after we have written the file
      if (tempTarget != null) {
        // if we did not eager delete the target file
        if (endpoint.getFileExist() != GenericFileExist.TryRename
            && !endpoint.isEagerDeleteTargetFile()) {

          // if an existing file already exists what should we do?
          targetExists = operations.existsFile(target);
          if (targetExists) {

            log.trace("Not using EagerDeleteTargetFile, target exists");

            if (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.", target);
              return;
            } else if (endpoint.getFileExist() == GenericFileExist.Fail) {
              throw new GenericFileOperationFailedException(
                  "File already exist: " + target + ". Cannot write new file.");
            } else if (endpoint.getFileExist() == GenericFileExist.Override) {
              // we override the target so we do this by deleting it so the temp file can be renamed
              // later
              // with success as the existing target file have been deleted
              log.trace("Deleting existing file: {}", target);
              if (!operations.deleteFile(target)) {
                throw new GenericFileOperationFailedException("Cannot delete file: " + target);
              }
            }
          }
        }

        // now we are ready to rename the temp file to the target file
        log.trace("Renaming file: [{}] to: [{}]", tempTarget, target);
        boolean renamed = operations.renameFile(tempTarget, target);
        if (!renamed) {
          throw new GenericFileOperationFailedException(
              "Cannot rename file from: " + tempTarget + " to: " + target);
        }
      }

      // any done file to write?
      if (endpoint.getDoneFileName() != null) {
        String doneFileName = endpoint.createDoneFileName(target);
        ObjectHelper.notEmpty(doneFileName, "doneFileName", endpoint);

        // create empty exchange with empty body to write as the done file
        Exchange empty = new DefaultExchange(exchange);
        empty.getIn().setBody("");

        log.trace("Writing done file: [{}]", doneFileName);
        // delete any existing done file
        if (operations.existsFile(doneFileName)) {
          if (!operations.deleteFile(doneFileName)) {
            throw new GenericFileOperationFailedException(
                "Cannot delete existing done file: " + doneFileName);
          }
        }
        writeFile(empty, doneFileName);
      }

      // let's store the name we really used in the header, so end-users
      // can retrieve it
      exchange.getIn().setHeader(Exchange.FILE_NAME_PRODUCED, target);
    } catch (Exception e) {
      handleFailedWrite(exchange, e);
    }

    postWriteCheck();
  }
示例#29
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);
    }
  }
示例#30
0
  @SuppressWarnings("deprecation")
  public MethodInfo(
      CamelContext camelContext,
      Class<?> type,
      Method method,
      List<ParameterInfo> parameters,
      List<ParameterInfo> bodyParameters,
      boolean hasCustomAnnotation,
      boolean hasHandlerAnnotation) {
    this.camelContext = camelContext;
    this.type = type;
    this.method = method;
    this.parameters = parameters;
    this.bodyParameters = bodyParameters;
    this.hasCustomAnnotation = hasCustomAnnotation;
    this.hasHandlerAnnotation = hasHandlerAnnotation;
    this.parametersExpression = createParametersExpression();

    Map<Class<?>, Annotation> collectedMethodAnnotation = collectMethodAnnotations(type, method);

    Pattern oneway = findOneWayAnnotation(method);
    if (oneway != null) {
      pattern = oneway.value();
    }

    org.apache.camel.RoutingSlip routingSlipAnnotation =
        (org.apache.camel.RoutingSlip)
            collectedMethodAnnotation.get(org.apache.camel.RoutingSlip.class);
    if (routingSlipAnnotation != null && matchContext(routingSlipAnnotation.context())) {
      routingSlip = new RoutingSlip(camelContext);
      routingSlip.setDelimiter(routingSlipAnnotation.delimiter());
      routingSlip.setIgnoreInvalidEndpoints(routingSlipAnnotation.ignoreInvalidEndpoints());
      // add created routingSlip as a service so we have its lifecycle managed
      try {
        camelContext.addService(routingSlip);
      } catch (Exception e) {
        throw ObjectHelper.wrapRuntimeCamelException(e);
      }
    }

    org.apache.camel.DynamicRouter dynamicRouterAnnotation =
        (org.apache.camel.DynamicRouter)
            collectedMethodAnnotation.get(org.apache.camel.DynamicRouter.class);
    if (dynamicRouterAnnotation != null && matchContext(dynamicRouterAnnotation.context())) {
      dynamicRouter = new DynamicRouter(camelContext);
      dynamicRouter.setDelimiter(dynamicRouterAnnotation.delimiter());
      dynamicRouter.setIgnoreInvalidEndpoints(dynamicRouterAnnotation.ignoreInvalidEndpoints());
      // add created dynamicRouter as a service so we have its lifecycle managed
      try {
        camelContext.addService(dynamicRouter);
      } catch (Exception e) {
        throw ObjectHelper.wrapRuntimeCamelException(e);
      }
    }

    org.apache.camel.RecipientList recipientListAnnotation =
        (org.apache.camel.RecipientList)
            collectedMethodAnnotation.get(org.apache.camel.RecipientList.class);
    if (recipientListAnnotation != null && matchContext(recipientListAnnotation.context())) {
      recipientList = new RecipientList(camelContext, recipientListAnnotation.delimiter());
      recipientList.setStopOnException(recipientListAnnotation.stopOnException());
      recipientList.setIgnoreInvalidEndpoints(recipientListAnnotation.ignoreInvalidEndpoints());
      recipientList.setParallelProcessing(recipientListAnnotation.parallelProcessing());
      recipientList.setParallelAggregate(recipientListAnnotation.parallelAggregate());
      recipientList.setStreaming(recipientListAnnotation.streaming());
      recipientList.setTimeout(recipientListAnnotation.timeout());
      recipientList.setShareUnitOfWork(recipientListAnnotation.shareUnitOfWork());

      if (ObjectHelper.isNotEmpty(recipientListAnnotation.executorServiceRef())) {
        ExecutorService executor =
            camelContext
                .getExecutorServiceManager()
                .newDefaultThreadPool(this, recipientListAnnotation.executorServiceRef());
        recipientList.setExecutorService(executor);
      }

      if (recipientListAnnotation.parallelProcessing()
          && recipientList.getExecutorService() == null) {
        // we are running in parallel so we need a thread pool
        ExecutorService executor =
            camelContext.getExecutorServiceManager().newDefaultThreadPool(this, "@RecipientList");
        recipientList.setExecutorService(executor);
      }

      if (ObjectHelper.isNotEmpty(recipientListAnnotation.strategyRef())) {
        AggregationStrategy strategy =
            CamelContextHelper.mandatoryLookup(
                camelContext, recipientListAnnotation.strategyRef(), AggregationStrategy.class);
        recipientList.setAggregationStrategy(strategy);
      }

      if (ObjectHelper.isNotEmpty(recipientListAnnotation.onPrepareRef())) {
        Processor onPrepare =
            CamelContextHelper.mandatoryLookup(
                camelContext, recipientListAnnotation.onPrepareRef(), Processor.class);
        recipientList.setOnPrepare(onPrepare);
      }

      // add created recipientList as a service so we have its lifecycle managed
      try {
        camelContext.addService(recipientList);
      } catch (Exception e) {
        throw ObjectHelper.wrapRuntimeCamelException(e);
      }
    }
  }