/**
  * Creates a {@link org.apache.camel.spi.RestConfiguration} instance based on the definition
  *
  * @param context the camel context
  * @return the configuration
  * @throws Exception is thrown if error creating the configuration
  */
 public RestConfiguration asRestConfiguration(CamelContext context) throws Exception {
   RestConfiguration answer = new RestConfiguration();
   if (component != null) {
     answer.setComponent(CamelContextHelper.parseText(context, component));
   }
   if (scheme != null) {
     answer.setScheme(CamelContextHelper.parseText(context, scheme));
   }
   if (host != null) {
     answer.setHost(CamelContextHelper.parseText(context, host));
   }
   if (port != null) {
     answer.setPort(CamelContextHelper.parseInteger(context, port));
   }
   if (bindingMode != null) {
     answer.setBindingMode(bindingMode.name());
   }
   if (!componentProperties.isEmpty()) {
     Map<String, Object> props = new HashMap<String, Object>();
     for (RestPropertyDefinition prop : componentProperties) {
       String key = prop.getKey();
       String value = CamelContextHelper.parseText(context, prop.getValue());
       props.put(key, value);
     }
     answer.setComponentProperties(props);
   }
   if (!endpointProperties.isEmpty()) {
     Map<String, Object> props = new HashMap<String, Object>();
     for (RestPropertyDefinition prop : endpointProperties) {
       String key = prop.getKey();
       String value = CamelContextHelper.parseText(context, prop.getValue());
       props.put(key, value);
     }
     answer.setEndpointProperties(props);
   }
   if (!consumerProperties.isEmpty()) {
     Map<String, Object> props = new HashMap<String, Object>();
     for (RestPropertyDefinition prop : consumerProperties) {
       String key = prop.getKey();
       String value = CamelContextHelper.parseText(context, prop.getValue());
       props.put(key, value);
     }
     answer.setConsumerProperties(props);
   }
   return answer;
 }
  @Override
  public Producer createProducer() throws Exception {
    RestApiProcessorFactory factory = null;

    RestConfiguration config = getCamelContext().getRestConfiguration(componentName, true);

    // lookup in registry
    Set<RestApiProcessorFactory> factories =
        getCamelContext().getRegistry().findByType(RestApiProcessorFactory.class);
    if (factories != null && factories.size() == 1) {
      factory = factories.iterator().next();
    }

    // lookup on classpath using factory finder to automatic find it (just add camel-swagger-java to
    // classpath etc)
    if (factory == null) {
      String name = apiComponentName != null ? apiComponentName : config.getApiComponent();
      if (name == null) {
        name = DEFAULT_API_COMPONENT_NAME;
      }
      try {
        FactoryFinder finder = getCamelContext().getFactoryFinder(RESOURCE_PATH);
        Object instance = finder.newInstance(name);
        if (instance instanceof RestApiProcessorFactory) {
          factory = (RestApiProcessorFactory) instance;
        }
      } catch (NoFactoryAvailableException e) {
        // ignore
      }
    }

    if (factory != null) {

      // if no explicit port/host configured, then use port from rest configuration
      String host = "";
      int port = 80;

      if (config.getHost() != null) {
        host = config.getHost();
      }
      int num = config.getPort();
      if (num > 0) {
        port = num;
      }

      // if no explicit hostname set then resolve the hostname
      if (ObjectHelper.isEmpty(host)) {
        if (config.getRestHostNameResolver() == RestConfiguration.RestHostNameResolver.allLocalIp) {
          host = "0.0.0.0";
        } else if (config.getRestHostNameResolver()
            == RestConfiguration.RestHostNameResolver.localHostName) {
          host = HostUtils.getLocalHostName();
        } else if (config.getRestHostNameResolver()
            == RestConfiguration.RestHostNameResolver.localIp) {
          host = HostUtils.getLocalIp();
        }

        // no host was configured so calculate a host to use
        // there should be no schema in the host (but only port)
        String targetHost = host + (port != 80 ? ":" + port : "");
        getParameters().put("host", targetHost);
      }

      // the base path should start with a leading slash
      String path = getPath();
      if (path != null && !path.startsWith("/")) {
        path = "/" + path;
      }

      // whether listing of the context id's is enabled or not
      boolean contextIdListing = config.isApiContextListing();

      Processor processor =
          factory.createApiProcessor(
              getCamelContext(),
              path,
              getContextIdPattern(),
              contextIdListing,
              config,
              getParameters());
      return new RestApiProducer(this, processor);
    } else {
      throw new IllegalStateException(
          "Cannot find RestApiProcessorFactory in Registry or classpath (such as the camel-swagger-java component)");
    }
  }
Esempio n. 3
0
  @Override
  protected void doStart() throws Exception {
    super.doStart();

    if (getPort() != 4567) {
      CamelSpark.port(getPort());
    } else {
      // if no explicit port configured, then use port from rest configuration
      RestConfiguration config = getCamelContext().getRestConfiguration("spark-rest", true);
      int port = config.getPort();
      if (port > 0) {
        CamelSpark.port(port);
      }
    }

    String host = getIpAddress();
    if (host != null) {
      CamelSpark.ipAddress(host);
    } else {
      // if no explicit port configured, then use port from rest configuration
      RestConfiguration config = getCamelContext().getRestConfiguration("spark-rest", true);
      host = config.getHost();
      if (ObjectHelper.isEmpty(host)) {
        if (config.getRestHostNameResolver() == RestConfiguration.RestHostNameResolver.allLocalIp) {
          host = "0.0.0.0";
        } else if (config.getRestHostNameResolver()
            == RestConfiguration.RestHostNameResolver.localHostName) {
          host = HostUtils.getLocalHostName();
        } else if (config.getRestHostNameResolver()
            == RestConfiguration.RestHostNameResolver.localIp) {
          host = HostUtils.getLocalIp();
        }
      }
      CamelSpark.ipAddress(host);
    }

    if (keystoreFile != null || truststoreFile != null) {
      CamelSpark.security(keystoreFile, keystorePassword, truststoreFile, truststorePassword);
    }

    // configure component options
    RestConfiguration config = getCamelContext().getRestConfiguration("spark-rest", true);
    // configure additional options on spark configuration
    if (config.getComponentProperties() != null && !config.getComponentProperties().isEmpty()) {
      setProperties(sparkConfiguration, config.getComponentProperties());
    }
  }
Esempio n. 4
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;
  }
 /**
  * Creates a {@link org.apache.camel.spi.RestConfiguration} instance based on the definition
  *
  * @param context the camel context
  * @return the configuration
  * @throws Exception is thrown if error creating the configuration
  */
 public RestConfiguration asRestConfiguration(CamelContext context) throws Exception {
   RestConfiguration answer = new RestConfiguration();
   if (component != null) {
     answer.setComponent(CamelContextHelper.parseText(context, component));
   }
   if (apiComponent != null) {
     answer.setApiComponent(CamelContextHelper.parseText(context, apiComponent));
   }
   if (scheme != null) {
     answer.setScheme(CamelContextHelper.parseText(context, scheme));
   }
   if (host != null) {
     answer.setHost(CamelContextHelper.parseText(context, host));
   }
   if (port != null) {
     answer.setPort(CamelContextHelper.parseInteger(context, port));
   }
   if (apiContextPath != null) {
     answer.setApiContextPath(CamelContextHelper.parseText(context, apiContextPath));
   }
   if (apiContextRouteId != null) {
     answer.setApiContextRouteId(CamelContextHelper.parseText(context, apiContextRouteId));
   }
   if (apiContextIdPattern != null) {
     // special to allow #name# to refer to itself
     if ("#name#".equals(apiComponent)) {
       answer.setApiContextIdPattern(context.getName());
     } else {
       answer.setApiContextIdPattern(CamelContextHelper.parseText(context, apiContextIdPattern));
     }
   }
   if (apiContextListing != null) {
     answer.setApiContextListing(apiContextListing);
   }
   if (contextPath != null) {
     answer.setContextPath(CamelContextHelper.parseText(context, contextPath));
   }
   if (hostNameResolver != null) {
     answer.setRestHostNameResolver(hostNameResolver.name());
   }
   if (bindingMode != null) {
     answer.setBindingMode(bindingMode.name());
   }
   if (skipBindingOnErrorCode != null) {
     answer.setSkipBindingOnErrorCode(skipBindingOnErrorCode);
   }
   if (enableCORS != null) {
     answer.setEnableCORS(enableCORS);
   }
   if (jsonDataFormat != null) {
     answer.setJsonDataFormat(jsonDataFormat);
   }
   if (xmlDataFormat != null) {
     answer.setXmlDataFormat(xmlDataFormat);
   }
   if (!componentProperties.isEmpty()) {
     Map<String, Object> props = new HashMap<String, Object>();
     for (RestPropertyDefinition prop : componentProperties) {
       String key = prop.getKey();
       String value = CamelContextHelper.parseText(context, prop.getValue());
       props.put(key, value);
     }
     answer.setComponentProperties(props);
   }
   if (!endpointProperties.isEmpty()) {
     Map<String, Object> props = new HashMap<String, Object>();
     for (RestPropertyDefinition prop : endpointProperties) {
       String key = prop.getKey();
       String value = CamelContextHelper.parseText(context, prop.getValue());
       props.put(key, value);
     }
     answer.setEndpointProperties(props);
   }
   if (!consumerProperties.isEmpty()) {
     Map<String, Object> props = new HashMap<String, Object>();
     for (RestPropertyDefinition prop : consumerProperties) {
       String key = prop.getKey();
       String value = CamelContextHelper.parseText(context, prop.getValue());
       props.put(key, value);
     }
     answer.setConsumerProperties(props);
   }
   if (!dataFormatProperties.isEmpty()) {
     Map<String, Object> props = new HashMap<String, Object>();
     for (RestPropertyDefinition prop : dataFormatProperties) {
       String key = prop.getKey();
       String value = CamelContextHelper.parseText(context, prop.getValue());
       props.put(key, value);
     }
     answer.setDataFormatProperties(props);
   }
   if (!apiProperties.isEmpty()) {
     Map<String, Object> props = new HashMap<String, Object>();
     for (RestPropertyDefinition prop : apiProperties) {
       String key = prop.getKey();
       String value = CamelContextHelper.parseText(context, prop.getValue());
       props.put(key, value);
     }
     answer.setApiProperties(props);
   }
   if (!corsHeaders.isEmpty()) {
     Map<String, String> props = new HashMap<String, String>();
     for (RestPropertyDefinition prop : corsHeaders) {
       String key = prop.getKey();
       String value = CamelContextHelper.parseText(context, prop.getValue());
       props.put(key, value);
     }
     answer.setCorsHeaders(props);
   }
   return answer;
 }