Java 类org.apache.camel.spi.RestConfiguration 实例源码

项目:Camel    文件:RestDefinition.java   
/**
 * Transforms this REST definition into a list of {@link org.apache.camel.model.RouteDefinition} which
 * Camel routing engine can add and run. This allows us to define REST services using this
 * REST DSL and turn those into regular Camel routes.
 *
 * @param camelContext The Camel context
 */
public List<RouteDefinition> asRouteDefinition(CamelContext camelContext) {
    ObjectHelper.notNull(camelContext, "CamelContext");

    // sanity check this rest definition do not have duplicates
    validateUniquePaths();

    List<RouteDefinition> answer = new ArrayList<RouteDefinition>();
    if (camelContext.getRestConfigurations().isEmpty()) {
        camelContext.getRestConfiguration();
    }
    for (RestConfiguration config : camelContext.getRestConfigurations()) {
        addRouteDefinition(camelContext, answer, config.getComponent());
    }
    return answer;
}
项目:Camel    文件:DummyRestConsumerFactory.java   
@Override
public Consumer createConsumer(CamelContext camelContext, Processor processor, String verb, String basePath, String uriTemplate,
                               String consumes, String produces, RestConfiguration configuration, Map<String, Object> parameters) throws Exception {
    // just use a seda endpoint for testing purpose
    String id;
    if (uriTemplate != null) {
        id = ActiveMQUuidGenerator.generateSanitizedId(basePath + uriTemplate);
    } else {
        id = ActiveMQUuidGenerator.generateSanitizedId(basePath);
    }
    // remove leading dash as we add that ourselves
    if (id.startsWith("-")) {
        id = id.substring(1);
    }

    if (configuration.getConsumerProperties() != null) {
        String ref = (String) configuration.getConsumerProperties().get("dummy");
        if (ref != null) {
            dummy = CamelContextHelper.mandatoryLookup(camelContext, ref.substring(1));
        }
    }

    SedaEndpoint seda = camelContext.getEndpoint("seda:" + verb + "-" + id, SedaEndpoint.class);
    return seda.createConsumer(processor);
}
项目:Camel    文件:FromRestGetCorsTest.java   
public void testCors() throws Exception {
    // the rest becomes routes and the input is a seda endpoint created by the DummyRestConsumerFactory
    getMockEndpoint("mock:update").expectedMessageCount(1);

    Exchange out = template.request("seda:post-say-bye", new Processor() {
        @Override
        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setBody("I was here");
        }
    });
    assertNotNull(out);

    assertEquals(out.getOut().getHeader("Access-Control-Allow-Origin"), RestConfiguration.CORS_ACCESS_CONTROL_ALLOW_ORIGIN);
    assertEquals(out.getOut().getHeader("Access-Control-Allow-Methods"), RestConfiguration.CORS_ACCESS_CONTROL_ALLOW_METHODS);
    assertEquals(out.getOut().getHeader("Access-Control-Allow-Headers"), RestConfiguration.CORS_ACCESS_CONTROL_ALLOW_HEADERS);
    assertEquals(out.getOut().getHeader("Access-Control-Max-Age"), RestConfiguration.CORS_ACCESS_CONTROL_MAX_AGE);

    assertMockEndpointsSatisfied();
}
项目:Camel    文件:FromRestGetCorsCustomTest.java   
public void testCors() throws Exception {
    // the rest becomes routes and the input is a seda endpoint created by the DummyRestConsumerFactory
    getMockEndpoint("mock:update").expectedMessageCount(1);

    Exchange out = template.request("seda:post-say-bye", new Processor() {
        @Override
        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setBody("I was here");
        }
    });
    assertNotNull(out);

    assertEquals(out.getOut().getHeader("Access-Control-Allow-Origin"), "myserver");
    assertEquals(out.getOut().getHeader("Access-Control-Allow-Methods"), RestConfiguration.CORS_ACCESS_CONTROL_ALLOW_METHODS);
    assertEquals(out.getOut().getHeader("Access-Control-Allow-Headers"), RestConfiguration.CORS_ACCESS_CONTROL_ALLOW_HEADERS);
    assertEquals(out.getOut().getHeader("Access-Control-Max-Age"), "180");

    assertMockEndpointsSatisfied();
}
项目:Camel    文件:DummyRestConsumerFactory.java   
@Override
public Consumer createConsumer(CamelContext camelContext, Processor processor, String verb, String basePath, String uriTemplate,
                               String consumes, String produces, RestConfiguration configuration, Map<String, Object> parameters) throws Exception {
    // just use a seda endpoint for testing purpose
    String id;
    if (uriTemplate != null) {
        id = ActiveMQUuidGenerator.generateSanitizedId(basePath + uriTemplate);
    } else {
        id = ActiveMQUuidGenerator.generateSanitizedId(basePath);
    }
    // remove leading dash as we add that ourselves
    if (id.startsWith("-")) {
        id = id.substring(1);
    }
    SedaEndpoint seda = camelContext.getEndpoint("seda:" + verb + "-" + id, SedaEndpoint.class);
    return seda.createConsumer(processor);
}
项目:Camel    文件:DummyRestConsumerFactory.java   
@Override
public Consumer createConsumer(CamelContext camelContext, Processor processor, String verb, String basePath, String uriTemplate,
                               String consumes, String produces, RestConfiguration configuration, Map<String, Object> parameters) throws Exception {
    // just use a seda endpoint for testing purpose
    String id;
    if (uriTemplate != null) {
        id = ActiveMQUuidGenerator.generateSanitizedId(basePath + uriTemplate);
    } else {
        id = ActiveMQUuidGenerator.generateSanitizedId(basePath);
    }
    // remove leading dash as we add that ourselves
    if (id.startsWith("-")) {
        id = id.substring(1);
    }
    SedaEndpoint seda = camelContext.getEndpoint("seda:" + verb + "-" + id, SedaEndpoint.class);
    return seda.createConsumer(processor);
}
项目:Camel    文件:DummyRestConsumerFactory.java   
@Override
public Consumer createConsumer(CamelContext camelContext, Processor processor, String verb,
                               String basePath, String uriTemplate, String consumes,
                               String produces, RestConfiguration configuration,
                               Map<String, Object> parameters) throws Exception {
    // just use a seda endpoint for testing purpose
    String id;
    if (uriTemplate != null) {
        id = ActiveMQUuidGenerator.generateSanitizedId(basePath + uriTemplate);
    } else {
        id = ActiveMQUuidGenerator.generateSanitizedId(basePath);
    }
    // remove leading dash as we add that ourselves
    if (id.startsWith("-")) {
        id = id.substring(1);
    }

    SedaEndpoint seda = camelContext.getEndpoint("seda:" + verb + "-" + id, SedaEndpoint.class);
    return seda.createConsumer(processor);
}
项目:Camel    文件:RestSwaggerCorsFilter.java   
private static void setupCorsHeaders(HttpServletResponse response, Map<String, String> corsHeaders) {
    // use default value if none has been configured
    String allowOrigin = corsHeaders != null ? corsHeaders.get("Access-Control-Allow-Origin") : null;
    if (allowOrigin == null) {
        allowOrigin = RestConfiguration.CORS_ACCESS_CONTROL_ALLOW_ORIGIN;
    }
    String allowMethods = corsHeaders != null ? corsHeaders.get("Access-Control-Allow-Methods") : null;
    if (allowMethods == null) {
        allowMethods = RestConfiguration.CORS_ACCESS_CONTROL_ALLOW_METHODS;
    }
    String allowHeaders = corsHeaders != null ? corsHeaders.get("Access-Control-Allow-Headers") : null;
    if (allowHeaders == null) {
        allowHeaders = RestConfiguration.CORS_ACCESS_CONTROL_ALLOW_HEADERS;
    }
    String maxAge = corsHeaders != null ? corsHeaders.get("Access-Control-Max-Age") : null;
    if (maxAge == null) {
        maxAge = RestConfiguration.CORS_ACCESS_CONTROL_MAX_AGE;
    }

    response.setHeader("Access-Control-Allow-Origin", allowOrigin);
    response.setHeader("Access-Control-Allow-Methods", allowMethods);
    response.setHeader("Access-Control-Allow-Headers", allowHeaders);
    response.setHeader("Access-Control-Max-Age", maxAge);
}
项目:Camel    文件:DummyRestConsumerFactory.java   
@Override
public Consumer createConsumer(CamelContext camelContext, Processor processor, String verb, String basePath, String uriTemplate,
                               String consumes, String produces, RestConfiguration configuration, Map<String, Object> parameters) throws Exception {
    // just use a seda endpoint for testing purpose
    String id;
    if (uriTemplate != null) {
        id = ActiveMQUuidGenerator.generateSanitizedId(basePath + uriTemplate);
    } else {
        id = ActiveMQUuidGenerator.generateSanitizedId(basePath);
    }
    // remove leading dash as we add that ourselves
    if (id.startsWith("-")) {
        id = id.substring(1);
    }
    SedaEndpoint seda = camelContext.getEndpoint("seda:" + verb + "-" + id, SedaEndpoint.class);
    return seda.createConsumer(processor);
}
项目:camel-cdi    文件:DummyRestConsumerFactory.java   
@Override
public Consumer createConsumer(CamelContext camelContext, Processor processor, String verb,
                               String basePath, String uriTemplate, String consumes,
                               String produces, RestConfiguration configuration,
                               Map<String, Object> parameters) throws Exception {
    // just use a seda endpoint for testing purpose
    String id;
    if (uriTemplate != null)
        id = ActiveMQUuidGenerator.generateSanitizedId(basePath + uriTemplate);
    else
        id = ActiveMQUuidGenerator.generateSanitizedId(basePath);
    // remove leading dash as we add that ourselves
    if (id.startsWith("-"))
        id = id.substring(1);

    SedaEndpoint seda = camelContext.getEndpoint("seda:" + verb + "-" + id, SedaEndpoint.class);
    return seda.createConsumer(processor);
}
项目:Camel    文件:RestBindingDefinition.java   
private void setAdditionalConfiguration(RestConfiguration config, CamelContext context,
                                        DataFormat dataFormat, String prefix) throws Exception {
    if (config.getDataFormatProperties() != null && !config.getDataFormatProperties().isEmpty()) {
        // must use a copy as otherwise the options gets removed during introspection setProperties
        Map<String, Object> copy = new HashMap<String, Object>();

        // filter keys on prefix
        // - either its a known prefix and must match the prefix parameter
        // - or its a common configuration that we should always use
        for (Map.Entry<String, Object> entry : config.getDataFormatProperties().entrySet()) {
            String key = entry.getKey();
            String copyKey;
            boolean known = isKeyKnownPrefix(key);
            if (known) {
                // remove the prefix from the key to use
                copyKey = key.substring(prefix.length());
            } else {
                // use the key as is
                copyKey = key;
            }
            if (!known || key.startsWith(prefix)) {
                copy.put(copyKey, entry.getValue());
            }
        }

        // set reference properties first as they use # syntax that fools the regular properties setter
        EndpointHelper.setReferenceProperties(context, dataFormat, copy);
        EndpointHelper.setProperties(context, dataFormat, copy);
    }
}
项目:Camel    文件:RestDefinition.java   
/**
 * Transforms this REST definition into a list of {@link org.apache.camel.model.RouteDefinition} which
 * Camel routing engine can add and run. This allows us to define REST services using this
 * REST DSL and turn those into regular Camel routes.
 *
 * @param camelContext        The Camel context
 * @param restConfiguration   The rest configuration to use
 */
public List<RouteDefinition> asRouteDefinition(CamelContext camelContext, RestConfiguration restConfiguration) {
    ObjectHelper.notNull(camelContext, "CamelContext");
    ObjectHelper.notNull(restConfiguration, "RestConfiguration");

    // sanity check this rest definition do not have duplicates
    validateUniquePaths();

    List<RouteDefinition> answer = new ArrayList<RouteDefinition>();
    addRouteDefinition(camelContext, answer, restConfiguration.getComponent());
    return answer;
}
项目:Camel    文件:RestDefinition.java   
/**
 * Transforms the rest api configuration into a {@link org.apache.camel.model.RouteDefinition} which
 * Camel routing engine uses to service the rest api docs.
 */
public static RouteDefinition asRouteApiDefinition(CamelContext camelContext, RestConfiguration configuration) {
    RouteDefinition answer = new RouteDefinition();

    // create the from endpoint uri which is using the rest-api component
    String from = "rest-api:" + configuration.getApiContextPath();

    // append options
    Map<String, Object> options = new HashMap<String, Object>();

    String routeId = configuration.getApiContextRouteId();
    if (routeId == null) {
        routeId = answer.idOrCreate(camelContext.getNodeIdFactory());
    }
    options.put("routeId", routeId);
    if (configuration.getComponent() != null && !configuration.getComponent().isEmpty()) {
        options.put("componentName", configuration.getComponent());
    }
    if (configuration.getApiContextIdPattern() != null) {
        options.put("contextIdPattern", configuration.getApiContextIdPattern());
    }

    if (!options.isEmpty()) {
        String query;
        try {
            query = URISupport.createQueryString(options);
        } catch (URISyntaxException e) {
            throw ObjectHelper.wrapRuntimeCamelException(e);
        }
        from = from + "?" + query;
    }

    // we use the same uri as the producer (so we have a little route for the rest api)
    String to = from;
    answer.fromRest(from);
    answer.id(routeId);
    answer.to(to);

    return answer;
}
项目:Camel    文件:RestBindingProcessor.java   
@Override
public void onAfterRoute(Route route, Exchange exchange) {
    // add the CORS headers after routing, but before the consumer writes the response
    Message msg = exchange.hasOut() ? exchange.getOut() : exchange.getIn();

    // use default value if none has been configured
    String allowOrigin = corsHeaders != null ? corsHeaders.get("Access-Control-Allow-Origin") : null;
    if (allowOrigin == null) {
        allowOrigin = RestConfiguration.CORS_ACCESS_CONTROL_ALLOW_ORIGIN;
    }
    String allowMethods = corsHeaders != null ? corsHeaders.get("Access-Control-Allow-Methods") : null;
    if (allowMethods == null) {
        allowMethods = RestConfiguration.CORS_ACCESS_CONTROL_ALLOW_METHODS;
    }
    String allowHeaders = corsHeaders != null ? corsHeaders.get("Access-Control-Allow-Headers") : null;
    if (allowHeaders == null) {
        allowHeaders = RestConfiguration.CORS_ACCESS_CONTROL_ALLOW_HEADERS;
    }
    String maxAge = corsHeaders != null ? corsHeaders.get("Access-Control-Max-Age") : null;
    if (maxAge == null) {
        maxAge = RestConfiguration.CORS_ACCESS_CONTROL_MAX_AGE;
    }

    msg.setHeader("Access-Control-Allow-Origin", allowOrigin);
    msg.setHeader("Access-Control-Allow-Methods", allowMethods);
    msg.setHeader("Access-Control-Allow-Headers", allowHeaders);
    msg.setHeader("Access-Control-Max-Age", maxAge);
}
项目:Camel    文件:DummyRestConsumerFactory.java   
@Override
public Consumer createApiConsumer(CamelContext camelContext, Processor processor, String contextPath,
                                  RestConfiguration configuration, Map<String, Object> parameters) throws Exception {
    // just use a seda endpoint for testing purpose
    String id = ActiveMQUuidGenerator.generateSanitizedId(contextPath);
    // remove leading dash as we add that ourselves
    if (id.startsWith("-")) {
        id = id.substring(1);
    }
    SedaEndpoint seda = camelContext.getEndpoint("seda:api:" + "-" + id, SedaEndpoint.class);
    return seda.createConsumer(processor);
}
项目:Camel    文件:DummyRestProcessorFactory.java   
@Override
public Processor createApiProcessor(CamelContext camelContext, String contextPath, String contextIdPattern, boolean contextIdListing,
                                    RestConfiguration configuration, Map<String, Object> parameters) throws Exception {
    return new Processor() {
        @Override
        public void process(Exchange exchange) throws Exception {
            // noop
        }
    };
}
项目:Camel    文件:NettyHttpComponent.java   
@Override
protected void doStart() throws Exception {
    super.doStart();

    RestConfiguration config = getCamelContext().getRestConfiguration("netty-http", true);
    // configure additional options on netty-http configuration
    if (config.getComponentProperties() != null && !config.getComponentProperties().isEmpty()) {
        setProperties(this, config.getComponentProperties());
    }
}
项目:Camel    文件:ServletComponent.java   
@Override
protected void doStart() throws Exception {
    super.doStart();

    RestConfiguration config = getCamelContext().getRestConfiguration("servlet", true);
    // configure additional options on jetty configuration
    if (config.getComponentProperties() != null && !config.getComponentProperties().isEmpty()) {
        setProperties(this, config.getComponentProperties());
    }
}
项目:Camel    文件:NettyHttpComponent.java   
@Override
protected void doStart() throws Exception {
    super.doStart();

    RestConfiguration config = getCamelContext().getRestConfiguration("netty4-http", true);
    // configure additional options on netty4-http configuration
    if (config.getComponentProperties() != null && !config.getComponentProperties().isEmpty()) {
        setProperties(this, config.getComponentProperties());
    }
}
项目:Camel    文件:RestNettyHttpGetCorsTest.java   
@Test
public void testCors() throws Exception {
    // send OPTIONS first which should not be routed
    getMockEndpoint("mock:input").expectedMessageCount(0);

    Exchange out = template.request("http://localhost:" + getPort() + "/users/123/basic", new Processor() {
        @Override
        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setHeader(Exchange.HTTP_METHOD, "OPTIONS");
        }
    });

    assertEquals(RestConfiguration.CORS_ACCESS_CONTROL_ALLOW_ORIGIN, out.getOut().getHeader("Access-Control-Allow-Origin"));
    assertEquals(RestConfiguration.CORS_ACCESS_CONTROL_ALLOW_METHODS, out.getOut().getHeader("Access-Control-Allow-Methods"));
    assertEquals(RestConfiguration.CORS_ACCESS_CONTROL_ALLOW_HEADERS, out.getOut().getHeader("Access-Control-Allow-Headers"));
    assertEquals(RestConfiguration.CORS_ACCESS_CONTROL_MAX_AGE, out.getOut().getHeader("Access-Control-Max-Age"));

    assertMockEndpointsSatisfied();

    resetMocks();
    getMockEndpoint("mock:input").expectedMessageCount(1);

    // send GET request which should be routed

    String out2 = template.requestBody("http://localhost:" + getPort() + "/users/123/basic", null, String.class);
    assertEquals("123;Donald Duck", out2);

    assertMockEndpointsSatisfied();
}
项目:Camel    文件:JettyHttpComponent.java   
@Override
protected void doStart() throws Exception {
    super.doStart();

    RestConfiguration config = getCamelContext().getRestConfiguration("jetty", true);
    // configure additional options on jetty configuration
    if (config.getComponentProperties() != null && !config.getComponentProperties().isEmpty()) {
        setProperties(this, config.getComponentProperties());
    }

    startMbContainer();
}
项目:Camel    文件:CoAPComponent.java   
@Override
protected void doStart() throws Exception {
    super.doStart();

    RestConfiguration config = getCamelContext().getRestConfiguration("coap", true);
    // configure additional options on coap configuration
    if (config.getComponentProperties() != null && !config.getComponentProperties().isEmpty()) {
        setProperties(this, config.getComponentProperties());
    }
    defaultServer = getServer(config.getPort());

    for (CoapServer s : servers.values()) {
        s.start();
    }
}
项目:Camel    文件:UndertowComponent.java   
@Override
protected void doStart() throws Exception {
    super.doStart();

    RestConfiguration config = getCamelContext().getRestConfiguration("undertow", true);
    // configure additional options on undertow configuration
    if (config.getComponentProperties() != null && !config.getComponentProperties().isEmpty()) {
        setProperties(this, config.getComponentProperties());
    }
}
项目:Camel    文件:RestUndertowHttpGetCorsTest.java   
@Test
public void testCors() throws Exception {
    // send OPTIONS first which should not be routed
    getMockEndpoint("mock:input").expectedMessageCount(0);

    Exchange out = template.request("http://localhost:" + getPort() + "/users/123/basic", new Processor() {
        @Override
        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setHeader(Exchange.HTTP_METHOD, "OPTIONS");
        }
    });

    assertEquals(RestConfiguration.CORS_ACCESS_CONTROL_ALLOW_ORIGIN, out.getOut().getHeader("Access-Control-Allow-Origin"));
    assertEquals(RestConfiguration.CORS_ACCESS_CONTROL_ALLOW_METHODS, out.getOut().getHeader("Access-Control-Allow-Methods"));
    assertEquals(RestConfiguration.CORS_ACCESS_CONTROL_ALLOW_HEADERS, out.getOut().getHeader("Access-Control-Allow-Headers"));
    assertEquals(RestConfiguration.CORS_ACCESS_CONTROL_MAX_AGE, out.getOut().getHeader("Access-Control-Max-Age"));

    assertMockEndpointsSatisfied();

    resetMocks();
    getMockEndpoint("mock:input").expectedMessageCount(1);

    // send GET request which should be routed

    String out2 = template.requestBody("http://localhost:" + getPort() + "/users/123/basic", null, String.class);
    assertEquals("123;Donald Duck", out2);

    assertMockEndpointsSatisfied();
}
项目:Camel    文件:DummyRestConsumerFactory.java   
@Override
public Consumer createApiConsumer(CamelContext camelContext, Processor processor,
                                  String contextPath, RestConfiguration configuration,
                                  Map<String, Object> parameters) throws Exception {
    // just use a seda endpoint for testing purpose
    String id = ActiveMQUuidGenerator.generateSanitizedId(contextPath);
    // remove leading dash as we add that ourselves
    if (id.startsWith("-")) {
        id = id.substring(1);
    }
    SedaEndpoint seda = camelContext.getEndpoint("seda:api:" + "-" + id, SedaEndpoint.class);
    return seda.createConsumer(processor);
}
项目:Camel    文件:RestSwaggerSupport.java   
private static void setupCorsHeaders(RestApiResponseAdapter response, Map<String, String> corsHeaders) {
    // use default value if none has been configured
    String allowOrigin = corsHeaders != null ? corsHeaders.get("Access-Control-Allow-Origin") : null;
    if (allowOrigin == null) {
        allowOrigin = RestConfiguration.CORS_ACCESS_CONTROL_ALLOW_ORIGIN;
    }
    String allowMethods = corsHeaders != null ? corsHeaders.get("Access-Control-Allow-Methods") : null;
    if (allowMethods == null) {
        allowMethods = RestConfiguration.CORS_ACCESS_CONTROL_ALLOW_METHODS;
    }
    String allowHeaders = corsHeaders != null ? corsHeaders.get("Access-Control-Allow-Headers") : null;
    if (allowHeaders == null) {
        allowHeaders = RestConfiguration.CORS_ACCESS_CONTROL_ALLOW_HEADERS;
    }
    String maxAge = corsHeaders != null ? corsHeaders.get("Access-Control-Max-Age") : null;
    if (maxAge == null) {
        maxAge = RestConfiguration.CORS_ACCESS_CONTROL_MAX_AGE;
    }

    if (LOG.isTraceEnabled()) {
        LOG.trace("Using CORS headers[");
        LOG.trace("  Access-Control-Allow-Origin={}", allowOrigin);
        LOG.trace("  Access-Control-Allow-Methods={}", allowMethods);
        LOG.trace("  Access-Control-Allow-Headers={}", allowHeaders);
        LOG.trace("  Access-Control-Max-Age={}", maxAge);
        LOG.trace("]");
    }
    response.setHeader("Access-Control-Allow-Origin", allowOrigin);
    response.setHeader("Access-Control-Allow-Methods", allowMethods);
    response.setHeader("Access-Control-Allow-Headers", allowHeaders);
    response.setHeader("Access-Control-Max-Age", maxAge);
}
项目:Camel    文件:RestSwaggerProcessor.java   
@SuppressWarnings("unchecked")
public RestSwaggerProcessor(String contextIdPattern, boolean contextIdListing, Map<String, Object> parameters, RestConfiguration configuration) {
    this.contextIdPattern = contextIdPattern;
    this.contextIdListing = contextIdListing;
    this.configuration = configuration;
    this.support = new RestSwaggerSupport();
    this.swaggerConfig = new BeanConfig();

    if (parameters == null) {
        parameters = Collections.EMPTY_MAP;
    }
    support.initSwagger(swaggerConfig, parameters);
}
项目:Camel    文件:SwaggerRestApiProcessorFactory.java   
@Override
public Processor createApiProcessor(CamelContext camelContext, String contextPath, String contextIdPattern, boolean contextIdListing,
                                    RestConfiguration configuration, Map<String, Object> parameters) throws Exception {

    Map<String, Object> options = new HashMap<String, Object>(parameters);
    if (configuration.getApiProperties() != null) {
        options.putAll(configuration.getApiProperties());
    }

    // need to include host in options
    String host = (String) options.get("host");
    if (host == null) {
        host = configuration.getHost();
        int port = configuration.getPort();
        if (host != null && port > 0) {
            options.put("host", host + ":" + port);
        } else if (host != null) {
            options.put("host", host);
        } else {
            options.put("host", "localhost");
        }
    }
    // and context path is the base.path
    String path = configuration.getContextPath();
    if (path != null) {
        options.put("base.path", path);
    }

    // is cors enabled?
    Object cors = options.get("cors");
    if (cors == null && configuration.isEnableCORS()) {
        options.put("cors", "true");
    }

    return new RestSwaggerProcessor(contextIdPattern, contextIdListing, options, configuration);
}
项目:Camel    文件:RestletComponent.java   
@Override
protected void doStart() throws Exception {
    super.doStart();

    // configure component options
    RestConfiguration config = getCamelContext().getRestConfiguration("restlet", true);
    // configure additional options on spark configuration
    if (config.getComponentProperties() != null && !config.getComponentProperties().isEmpty()) {
        setProperties(this, config.getComponentProperties());
    }

    cleanupConverters(enabledConverters);

    component.start();
}
项目:Camel    文件:RestRestletGetCorsTest.java   
@Test
public void testCors() throws Exception {
    // send OPTIONS first which should not be routed
    getMockEndpoint("mock:input").expectedMessageCount(0);

    Exchange out = template.request("http://localhost:" + portNum + "/users/123/basic", new Processor() {
        @Override
        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setHeader(Exchange.HTTP_METHOD, "OPTIONS");
        }
    });

    assertEquals(RestConfiguration.CORS_ACCESS_CONTROL_ALLOW_ORIGIN, out.getOut().getHeader("Access-Control-Allow-Origin"));
    assertEquals(RestConfiguration.CORS_ACCESS_CONTROL_ALLOW_METHODS, out.getOut().getHeader("Access-Control-Allow-Methods"));
    assertEquals(RestConfiguration.CORS_ACCESS_CONTROL_ALLOW_HEADERS, out.getOut().getHeader("Access-Control-Allow-Headers"));
    assertEquals(RestConfiguration.CORS_ACCESS_CONTROL_MAX_AGE, out.getOut().getHeader("Access-Control-Max-Age"));

    assertMockEndpointsSatisfied();

    resetMocks();
    getMockEndpoint("mock:input").expectedMessageCount(1);

    // send GET request which should be routed

    String out2 = template.requestBody("http://localhost:" + portNum + "/users/123/basic", null, String.class);
    assertEquals("123;Donald Duck", out2);

    assertMockEndpointsSatisfied();
}
项目:Camel    文件:RestJettyGetCorsTest.java   
@Test
public void testCors() throws Exception {
    // send OPTIONS first which should not be routed
    getMockEndpoint("mock:input").expectedMessageCount(0);

    Exchange out = template.request("http://localhost:" + getPort() + "/users/123/basic", new Processor() {
        @Override
        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setHeader(Exchange.HTTP_METHOD, "OPTIONS");
        }
    });

    assertEquals(RestConfiguration.CORS_ACCESS_CONTROL_ALLOW_ORIGIN, out.getOut().getHeader("Access-Control-Allow-Origin"));
    assertEquals(RestConfiguration.CORS_ACCESS_CONTROL_ALLOW_METHODS, out.getOut().getHeader("Access-Control-Allow-Methods"));
    assertEquals(RestConfiguration.CORS_ACCESS_CONTROL_ALLOW_HEADERS, out.getOut().getHeader("Access-Control-Allow-Headers"));
    assertEquals(RestConfiguration.CORS_ACCESS_CONTROL_MAX_AGE, out.getOut().getHeader("Access-Control-Max-Age"));

    assertMockEndpointsSatisfied();

    resetMocks();
    getMockEndpoint("mock:input").expectedMessageCount(1);

    // send GET request which should be routed

    String out2 = template.requestBody("http://localhost:" + getPort() + "/users/123/basic", null, String.class);
    assertEquals("123;Donald Duck", out2);

    assertMockEndpointsSatisfied();
}
项目:Camel    文件:RestCamelSparkCorsTest.java   
@Test
public void testCors() throws Exception {
    // send OPTIONS first which should not be routed
    getMockEndpoint("mock:input").expectedMessageCount(0);

    Exchange out = template.request("http://localhost:" + getPort() + "/users/123/basic", new Processor() {
        @Override
        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setHeader(Exchange.HTTP_METHOD, "OPTIONS");
        }
    });

    assertEquals(RestConfiguration.CORS_ACCESS_CONTROL_ALLOW_ORIGIN, out.getOut().getHeader("Access-Control-Allow-Origin"));
    assertEquals(RestConfiguration.CORS_ACCESS_CONTROL_ALLOW_METHODS, out.getOut().getHeader("Access-Control-Allow-Methods"));
    assertEquals(RestConfiguration.CORS_ACCESS_CONTROL_ALLOW_HEADERS, out.getOut().getHeader("Access-Control-Allow-Headers"));
    assertEquals(RestConfiguration.CORS_ACCESS_CONTROL_MAX_AGE, out.getOut().getHeader("Access-Control-Max-Age"));

    assertMockEndpointsSatisfied();

    resetMocks();
    getMockEndpoint("mock:input").expectedMessageCount(1);

    // send GET request which should be routed

    String out2 = template.requestBody("http://localhost:" + getPort() + "/users/123/basic", null, String.class);
    assertEquals("123;Donald Duck", out2);

    assertMockEndpointsSatisfied();
}
项目:camel-cdi    文件:DummyRestConsumerFactory.java   
@Override
public Consumer createApiConsumer(CamelContext camelContext, Processor processor,
                                  String contextPath, RestConfiguration configuration,
                                  Map<String, Object> parameters) throws Exception {
    // just use a seda endpoint for testing purpose
    String id = ActiveMQUuidGenerator.generateSanitizedId(contextPath);
    // remove leading dash as we add that ourselves
    if (id.startsWith("-"))
        id = id.substring(1);
    SedaEndpoint seda = camelContext.getEndpoint("seda:api:" + "-" + id, SedaEndpoint.class);
    return seda.createConsumer(processor);
}
项目:beyondj    文件:JettyHttpComponent.java   
@Override
public Consumer createConsumer(CamelContext camelContext, Processor processor, String verb, String basePath, String uriTemplate,
                               String consumes, String produces, Map<String, Object> parameters) 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);

    String scheme = "http";
    String host = "";
    int port = 0;

    // if no explicit port/host configured, then use port from rest configuration
    RestConfiguration config = getCamelContext().getRestConfiguration();
    if (config.getComponent() == null || config.getComponent().equals("jetty")) {
        if (config.getScheme() != null) {
            scheme = config.getScheme();
        }
        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.localHostName) {
            host = HostUtils.getLocalHostName();
        } else if (config.getRestHostNameResolver() == RestConfiguration.RestHostNameResolver.localIp) {
            host = HostUtils.getLocalIp();
        }
    }

    Map<String, Object> map = new HashMap<String, Object>();
    // build query string, and append any endpoint configuration properties
    if (config != null && (config.getComponent() == null || config.getComponent().equals("jetty"))) {
        // setup endpoint options
        if (config.getEndpointProperties() != null && !config.getEndpointProperties().isEmpty()) {
            map.putAll(config.getEndpointProperties());
        }
    }

    String query = URISupport.createQueryString(map);

    String url = "jetty:%s://%s:%s/%s?httpMethodRestrict=%s";
    // must use upper case for restrict
    String restrict = verb.toUpperCase(Locale.US);
    // get the endpoint
    url = String.format(url, scheme, host, port, path, restrict);

    if (!query.isEmpty()) {
        url = url + "&" + query;
    }

    JettyHttpEndpoint endpoint = camelContext.getEndpoint(url, JettyHttpEndpoint.class);
    setProperties(endpoint, parameters);

    // disable this filter as we want to use ours
    endpoint.setEnableMultipartFilter(false);
    // use the rest binding
    endpoint.setBinding(new JettyRestHttpBinding(endpoint));

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

    return consumer;
}
项目:Camel    文件:DefaultCamelContext.java   
public void setRestConfiguration(RestConfiguration restConfiguration) {
    restConfigurations.put("", restConfiguration);
}
项目:Camel    文件:DefaultCamelContext.java   
public Collection<RestConfiguration> getRestConfigurations() {
    return restConfigurations.values();
}
项目:Camel    文件:DefaultCamelContext.java   
public void addRestConfiguration(RestConfiguration restConfiguration) {
    restConfigurations.put(restConfiguration.getComponent(), restConfiguration);
}
项目:Camel    文件:RestApiEndpoint.java   
@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)");
    }
}
项目:Camel    文件:NettyHttpComponent.java   
@Override
public Consumer createConsumer(CamelContext camelContext, Processor processor, String verb, String basePath, String uriTemplate,
                               String consumes, String produces, RestConfiguration configuration, Map<String, Object> parameters) throws Exception {
    return doCreateConsumer(camelContext, processor, verb, basePath, uriTemplate, consumes, produces, configuration, parameters, false);
}
项目:Camel    文件:NettyHttpComponent.java   
@Override
public Consumer createApiConsumer(CamelContext camelContext, Processor processor, String contextPath,
                                  RestConfiguration configuration, Map<String, Object> parameters) throws Exception {
    // reuse the createConsumer method we already have. The api need to use GET and match on uri prefix
    return doCreateConsumer(camelContext, processor, "GET", contextPath, null, null, null, configuration, parameters, true);
}