Example #1
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;
  }