Exemplo n.º 1
0
  @Override
  protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> params)
      throws Exception {
    int idx = remaining.indexOf(':');
    if (idx > 0) {
      // we are registering a regular endpoint
      String name = remaining.substring(0, idx);
      String fabricPath = getFabricPath(name);
      // need to replace the "0.0.0.0" with the host and port
      String childUri = replaceAnyIpAddress(remaining.substring(idx + 1));
      // we need to apply the params here
      if (params != null && params.size() > 0) {
        childUri = childUri + "?" + URISupport.createQueryString(params);
      }
      Group group = ZooKeeperGroupFactory.create(getCurator(), fabricPath);
      return new FabricPublisherEndpoint(uri, this, group, childUri);

    } else {
      String fabricPath = getFabricPath(remaining);
      Group group = ZooKeeperGroupFactory.create(getCurator(), fabricPath);
      return new FabricLocatorEndpoint(uri, this, group);
    }
  }
Exemplo n.º 2
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;
  }