use of org.apache.camel.http.common.HttpBinding in project camel by apache.
the class ServletComponent method createEndpoint.
@Override
protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
// must extract well known parameters before we create the endpoint
Boolean throwExceptionOnFailure = getAndRemoveParameter(parameters, "throwExceptionOnFailure", Boolean.class);
Boolean transferException = getAndRemoveParameter(parameters, "transferException", Boolean.class);
Boolean bridgeEndpoint = getAndRemoveParameter(parameters, "bridgeEndpoint", Boolean.class);
HttpBinding binding = resolveAndRemoveReferenceParameter(parameters, "httpBinding", HttpBinding.class);
Boolean matchOnUriPrefix = getAndRemoveParameter(parameters, "matchOnUriPrefix", Boolean.class);
String servletName = getAndRemoveParameter(parameters, "servletName", String.class, getServletName());
String httpMethodRestrict = getAndRemoveParameter(parameters, "httpMethodRestrict", String.class);
HeaderFilterStrategy headerFilterStrategy = resolveAndRemoveReferenceParameter(parameters, "headerFilterStrategy", HeaderFilterStrategy.class);
Boolean async = getAndRemoveParameter(parameters, "async", Boolean.class);
Boolean attachmentMultipartBinding = getAndRemoveParameter(parameters, "attachmentMultipartBinding", Boolean.class);
Boolean disableStreamCache = getAndRemoveParameter(parameters, "disableStreamCache", Boolean.class);
if (lenientContextPath()) {
// the uri must have a leading slash for the context-path matching to work with servlet, and it can be something people
// forget to add and then the servlet consumer cannot match the context-path as would have been expected
String scheme = ObjectHelper.before(uri, ":");
String after = ObjectHelper.after(uri, ":");
// rebuild uri to have exactly one leading slash
while (after.startsWith("/")) {
after = after.substring(1);
}
after = "/" + after;
uri = scheme + ":" + after;
}
// restructure uri to be based on the parameters left as we dont want to include the Camel internal options
URI httpUri = URISupport.createRemainingURI(new URI(UnsafeUriCharactersEncoder.encodeHttpURI(uri)), parameters);
ServletEndpoint endpoint = createServletEndpoint(uri, this, httpUri);
endpoint.setServletName(servletName);
if (async != null) {
endpoint.setAsync(async);
}
if (headerFilterStrategy != null) {
endpoint.setHeaderFilterStrategy(headerFilterStrategy);
} else {
setEndpointHeaderFilterStrategy(endpoint);
}
// prefer to use endpoint configured over component configured
if (binding == null) {
// fallback to component configured
binding = getHttpBinding();
}
if (binding != null) {
endpoint.setBinding(binding);
}
// should we use an exception for failed error codes?
if (throwExceptionOnFailure != null) {
endpoint.setThrowExceptionOnFailure(throwExceptionOnFailure);
}
// should we transfer exception as serialized object
if (transferException != null) {
endpoint.setTransferException(transferException);
}
if (bridgeEndpoint != null) {
endpoint.setBridgeEndpoint(bridgeEndpoint);
}
if (matchOnUriPrefix != null) {
endpoint.setMatchOnUriPrefix(matchOnUriPrefix);
}
if (httpMethodRestrict != null) {
endpoint.setHttpMethodRestrict(httpMethodRestrict);
}
if (attachmentMultipartBinding != null) {
endpoint.setAttachmentMultipartBinding(attachmentMultipartBinding);
} else {
endpoint.setAttachmentMultipartBinding(isAttachmentMultipartBinding());
}
if (disableStreamCache != null) {
endpoint.setDisableStreamCache(disableStreamCache);
}
// turn off stream caching if in attachment mode
if (endpoint.isAttachmentMultipartBinding()) {
if (disableStreamCache == null) {
// disableStreamCache not explicit configured so we can automatic change it
LOG.info("Disabling stream caching as attachmentMultipartBinding is enabled");
endpoint.setDisableStreamCache(true);
} else if (!disableStreamCache) {
throw new IllegalArgumentException("The options attachmentMultipartBinding=true and disableStreamCache=false cannot work together." + " Remove disableStreamCache to use AttachmentMultipartBinding");
}
}
setProperties(endpoint, parameters);
return endpoint;
}
use of org.apache.camel.http.common.HttpBinding in project camel by apache.
the class HttpComponent method createEndpoint.
@Override
protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
String addressUri = "http://" + remaining;
if (uri.startsWith("https:")) {
addressUri = "https://" + remaining;
}
Map<String, Object> httpClientParameters = new HashMap<String, Object>(parameters);
// must extract well known parameters before we create the endpoint
HttpBinding binding = resolveAndRemoveReferenceParameter(parameters, "httpBinding", HttpBinding.class);
HeaderFilterStrategy headerFilterStrategy = resolveAndRemoveReferenceParameter(parameters, "headerFilterStrategy", HeaderFilterStrategy.class);
UrlRewrite urlRewrite = resolveAndRemoveReferenceParameter(parameters, "urlRewrite", UrlRewrite.class);
// http client can be configured from URI options
HttpClientParams clientParams = new HttpClientParams();
Map<String, Object> httpClientOptions = IntrospectionSupport.extractProperties(parameters, "httpClient.");
IntrospectionSupport.setProperties(clientParams, httpClientOptions);
// validate that we could resolve all httpClient. parameters as this component is lenient
validateParameters(uri, httpClientOptions, null);
// http client can be configured from URI options
HttpConnectionManagerParams connectionManagerParams = new HttpConnectionManagerParams();
// setup the httpConnectionManagerParams
Map<String, Object> httpConnectionManagerOptions = IntrospectionSupport.extractProperties(parameters, "httpConnectionManager.");
IntrospectionSupport.setProperties(connectionManagerParams, httpConnectionManagerOptions);
// validate that we could resolve all httpConnectionManager. parameters as this component is lenient
validateParameters(uri, httpConnectionManagerOptions, null);
// make sure the component httpConnectionManager is take effect
HttpConnectionManager thisHttpConnectionManager = httpConnectionManager;
if (thisHttpConnectionManager == null) {
// only set the params on the new created http connection manager
thisHttpConnectionManager = new MultiThreadedHttpConnectionManager();
thisHttpConnectionManager.setParams(connectionManagerParams);
}
// create the configurer to use for this endpoint (authMethods contains the used methods created by the configurer)
final Set<AuthMethod> authMethods = new LinkedHashSet<AuthMethod>();
HttpClientConfigurer configurer = createHttpClientConfigurer(parameters, authMethods);
addressUri = UnsafeUriCharactersEncoder.encodeHttpURI(addressUri);
URI endpointUri = URISupport.createRemainingURI(new URI(addressUri), httpClientParameters);
// create the endpoint and connectionManagerParams already be set
HttpEndpoint endpoint = createHttpEndpoint(endpointUri.toString(), this, clientParams, thisHttpConnectionManager, configurer);
// configure the endpoint with the common configuration from the component
if (getHttpConfiguration() != null) {
Map<String, Object> properties = new HashMap<>();
IntrospectionSupport.getProperties(getHttpConfiguration(), properties, null);
setProperties(endpoint, properties);
}
if (headerFilterStrategy != null) {
endpoint.setHeaderFilterStrategy(headerFilterStrategy);
} else {
setEndpointHeaderFilterStrategy(endpoint);
}
if (urlRewrite != null) {
// let CamelContext deal with the lifecycle of the url rewrite
// this ensures its being shutdown when Camel shutdown etc.
getCamelContext().addService(urlRewrite);
endpoint.setUrlRewrite(urlRewrite);
}
// prefer to use endpoint configured over component configured
if (binding == null) {
// fallback to component configured
binding = getHttpBinding();
}
if (binding != null) {
endpoint.setBinding(binding);
}
setProperties(endpoint, parameters);
// restructure uri to be based on the parameters left as we dont want to include the Camel internal options
URI httpUri = URISupport.createRemainingURI(new URI(addressUri), parameters);
// validate http uri that end-user did not duplicate the http part that can be a common error
String part = httpUri.getSchemeSpecificPart();
if (part != null) {
part = part.toLowerCase();
if (part.startsWith("//http//") || part.startsWith("//https//") || part.startsWith("//http://") || part.startsWith("//https://")) {
throw new ResolveEndpointFailedException(uri, "The uri part is not configured correctly. You have duplicated the http(s) protocol.");
}
}
endpoint.setHttpUri(httpUri);
endpoint.setHttpClientOptions(httpClientOptions);
return endpoint;
}
use of org.apache.camel.http.common.HttpBinding in project camel by apache.
the class HttpComponent method createEndpoint.
@Override
protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
Map<String, Object> httpClientParameters = new HashMap<String, Object>(parameters);
final Map<String, Object> httpClientOptions = new HashMap<>();
final HttpClientBuilder clientBuilder = createHttpClientBuilder(uri, parameters, httpClientOptions);
HttpBinding httpBinding = resolveAndRemoveReferenceParameter(parameters, "httpBinding", HttpBinding.class);
HttpContext httpContext = resolveAndRemoveReferenceParameter(parameters, "httpContext", HttpContext.class);
SSLContextParameters sslContextParameters = resolveAndRemoveReferenceParameter(parameters, "sslContextParameters", SSLContextParameters.class);
if (sslContextParameters == null) {
sslContextParameters = getSslContextParameters();
}
String httpMethodRestrict = getAndRemoveParameter(parameters, "httpMethodRestrict", String.class);
HeaderFilterStrategy headerFilterStrategy = resolveAndRemoveReferenceParameter(parameters, "headerFilterStrategy", HeaderFilterStrategy.class);
UrlRewrite urlRewrite = resolveAndRemoveReferenceParameter(parameters, "urlRewrite", UrlRewrite.class);
boolean secure = HttpHelper.isSecureConnection(uri) || sslContextParameters != null;
// need to set scheme on address uri depending on if its secure or not
String addressUri = (secure ? "https://" : "http://") + remaining;
addressUri = UnsafeUriCharactersEncoder.encodeHttpURI(addressUri);
URI uriHttpUriAddress = new URI(addressUri);
// validate http uri that end-user did not duplicate the http part that can be a common error
int pos = uri.indexOf("//");
if (pos != -1) {
String part = uri.substring(pos + 2);
if (part.startsWith("http:") || part.startsWith("https:")) {
throw new ResolveEndpointFailedException(uri, "The uri part is not configured correctly. You have duplicated the http(s) protocol.");
}
}
// create the configurer to use for this endpoint
HttpClientConfigurer configurer = createHttpClientConfigurer(parameters, secure);
URI endpointUri = URISupport.createRemainingURI(uriHttpUriAddress, httpClientParameters);
// the endpoint uri should use the component name as scheme, so we need to re-create it once more
String scheme = ObjectHelper.before(uri, "://");
endpointUri = URISupport.createRemainingURI(new URI(scheme, endpointUri.getUserInfo(), endpointUri.getHost(), endpointUri.getPort(), endpointUri.getPath(), endpointUri.getQuery(), endpointUri.getFragment()), httpClientParameters);
// create the endpoint and set the http uri to be null
String endpointUriString = endpointUri.toString();
LOG.debug("Creating endpoint uri {}", endpointUriString);
final HttpClientConnectionManager localConnectionManager = createConnectionManager(parameters, sslContextParameters);
HttpEndpoint endpoint = new HttpEndpoint(endpointUriString, this, clientBuilder, localConnectionManager, configurer);
// configure the endpoint with the common configuration from the component
if (getHttpConfiguration() != null) {
Map<String, Object> properties = new HashMap<>();
IntrospectionSupport.getProperties(getHttpConfiguration(), properties, null);
setProperties(endpoint, properties);
}
if (urlRewrite != null) {
// let CamelContext deal with the lifecycle of the url rewrite
// this ensures its being shutdown when Camel shutdown etc.
getCamelContext().addService(urlRewrite);
endpoint.setUrlRewrite(urlRewrite);
}
// configure the endpoint
setProperties(endpoint, parameters);
// determine the portnumber (special case: default portnumber)
//int port = getPort(uriHttpUriAddress);
// we can not change the port of an URI, we must create a new one with an explicit port value
URI httpUri = URISupport.createRemainingURI(new URI(uriHttpUriAddress.getScheme(), uriHttpUriAddress.getUserInfo(), uriHttpUriAddress.getHost(), uriHttpUriAddress.getPort(), uriHttpUriAddress.getPath(), uriHttpUriAddress.getQuery(), uriHttpUriAddress.getFragment()), parameters);
endpoint.setHttpUri(httpUri);
if (headerFilterStrategy != null) {
endpoint.setHeaderFilterStrategy(headerFilterStrategy);
} else {
setEndpointHeaderFilterStrategy(endpoint);
}
endpoint.setBinding(getHttpBinding());
if (httpBinding != null) {
endpoint.setBinding(httpBinding);
}
if (httpMethodRestrict != null) {
endpoint.setHttpMethodRestrict(httpMethodRestrict);
}
endpoint.setHttpContext(getHttpContext());
if (httpContext != null) {
endpoint.setHttpContext(httpContext);
}
if (endpoint.getCookieStore() == null) {
endpoint.setCookieStore(getCookieStore());
}
endpoint.setHttpClientOptions(httpClientOptions);
return endpoint;
}
use of org.apache.camel.http.common.HttpBinding in project camel by apache.
the class ServletComponent method doCreateConsumer.
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);
// if no explicit port/host configured, then use port from rest configuration
RestConfiguration config = configuration;
if (config == null) {
config = camelContext.getRestConfiguration("servlet", true);
}
Map<String, Object> map = new HashMap<String, Object>();
// build query string, and append any endpoint configuration properties
if (config.getComponent() == null || config.getComponent().equals("servlet")) {
// setup endpoint options
if (config.getEndpointProperties() != null && !config.getEndpointProperties().isEmpty()) {
map.putAll(config.getEndpointProperties());
}
}
boolean cors = config.isEnableCORS();
if (cors) {
// allow HTTP Options as we want to handle CORS in rest-dsl
map.put("optionsEnabled", "true");
}
// do not append with context-path as the servlet path should be without context-path
String query = URISupport.createQueryString(map);
String url;
if (api) {
url = "servlet:///%s?matchOnUriPrefix=true&httpMethodRestrict=%s";
} else {
url = "servlet:///%s?httpMethodRestrict=%s";
}
// must use upper case for restrict
String restrict = verb.toUpperCase(Locale.US);
if (cors) {
restrict += ",OPTIONS";
}
// get the endpoint
url = String.format(url, path, restrict);
if (!query.isEmpty()) {
url = url + "&" + query;
}
ServletEndpoint endpoint = camelContext.getEndpoint(url, ServletEndpoint.class);
setProperties(camelContext, endpoint, parameters);
if (!map.containsKey("httpBindingRef")) {
// use the rest binding, if not using a custom http binding
HttpBinding binding = new ServletRestHttpBinding();
binding.setHeaderFilterStrategy(endpoint.getHeaderFilterStrategy());
binding.setTransferException(endpoint.isTransferException());
binding.setEagerCheckContentAvailable(endpoint.isEagerCheckContentAvailable());
endpoint.setHttpBinding(binding);
}
// configure consumer properties
Consumer consumer = endpoint.createConsumer(processor);
if (config.getConsumerProperties() != null && !config.getConsumerProperties().isEmpty()) {
setProperties(camelContext, consumer, config.getConsumerProperties());
}
return consumer;
}
use of org.apache.camel.http.common.HttpBinding in project camel by apache.
the class JettyHttpComponent method createEndpoint.
@Override
protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
// must extract well known parameters before we create the endpoint
List<Handler> handlerList = resolveAndRemoveReferenceListParameter(parameters, "handlers", Handler.class);
HttpBinding binding = resolveAndRemoveReferenceParameter(parameters, "httpBindingRef", HttpBinding.class);
JettyHttpBinding jettyBinding = resolveAndRemoveReferenceParameter(parameters, "jettyHttpBindingRef", JettyHttpBinding.class);
Boolean enableJmx = getAndRemoveParameter(parameters, "enableJmx", Boolean.class);
Boolean enableMultipartFilter = getAndRemoveParameter(parameters, "enableMultipartFilter", Boolean.class, true);
Filter multipartFilter = resolveAndRemoveReferenceParameter(parameters, "multipartFilterRef", Filter.class);
List<Filter> filters = resolveAndRemoveReferenceListParameter(parameters, "filtersRef", Filter.class);
Boolean enableCors = getAndRemoveParameter(parameters, "enableCORS", Boolean.class, false);
HeaderFilterStrategy headerFilterStrategy = resolveAndRemoveReferenceParameter(parameters, "headerFilterStrategy", HeaderFilterStrategy.class);
UrlRewrite urlRewrite = resolveAndRemoveReferenceParameter(parameters, "urlRewrite", UrlRewrite.class);
SSLContextParameters sslContextParameters = resolveAndRemoveReferenceParameter(parameters, "sslContextParametersRef", SSLContextParameters.class);
SSLContextParameters ssl = sslContextParameters != null ? sslContextParameters : this.sslContextParameters;
String proxyHost = getAndRemoveParameter(parameters, "proxyHost", String.class, getProxyHost());
Integer proxyPort = getAndRemoveParameter(parameters, "proxyPort", Integer.class, getProxyPort());
Integer httpClientMinThreads = getAndRemoveParameter(parameters, "httpClientMinThreads", Integer.class, this.httpClientMinThreads);
Integer httpClientMaxThreads = getAndRemoveParameter(parameters, "httpClientMaxThreads", Integer.class, this.httpClientMaxThreads);
HttpClient httpClient = resolveAndRemoveReferenceParameter(parameters, "httpClient", HttpClient.class);
Boolean async = getAndRemoveParameter(parameters, "async", Boolean.class);
// extract httpClient. parameters
Map<String, Object> httpClientParameters = IntrospectionSupport.extractProperties(parameters, "httpClient.");
// extract filterInit. parameters
Map<String, String> filterInitParameters = IntrospectionSupport.extractStringProperties(IntrospectionSupport.extractProperties(parameters, "filterInit."));
String address = remaining;
URI addressUri = new URI(UnsafeUriCharactersEncoder.encodeHttpURI(address));
URI endpointUri = URISupport.createRemainingURI(addressUri, parameters);
// need to keep the httpMethodRestrict parameter for the endpointUri
String httpMethodRestrict = getAndRemoveParameter(parameters, "httpMethodRestrict", String.class);
// restructure uri to be based on the parameters left as we dont want to include the Camel internal options
URI httpUri = URISupport.createRemainingURI(addressUri, parameters);
// create endpoint after all known parameters have been extracted from parameters
// include component scheme in the uri
String scheme = ObjectHelper.before(uri, ":");
endpointUri = new URI(scheme + ":" + endpointUri);
JettyHttpEndpoint endpoint = createEndpoint(endpointUri, httpUri);
if (async != null) {
endpoint.setAsync(async);
}
if (headerFilterStrategy != null) {
endpoint.setHeaderFilterStrategy(headerFilterStrategy);
} else {
setEndpointHeaderFilterStrategy(endpoint);
}
// setup the proxy host and proxy port
if (proxyHost != null) {
endpoint.setProxyHost(proxyHost);
endpoint.setProxyPort(proxyPort);
}
if (urlRewrite != null) {
// let CamelContext deal with the lifecycle of the url rewrite
// this ensures its being shutdown when Camel shutdown etc.
getCamelContext().addService(urlRewrite);
endpoint.setUrlRewrite(urlRewrite);
}
if (httpClientParameters != null && !httpClientParameters.isEmpty()) {
endpoint.setHttpClientParameters(httpClientParameters);
}
if (filterInitParameters != null && !filterInitParameters.isEmpty()) {
endpoint.setFilterInitParameters(filterInitParameters);
}
if (handlerList.size() > 0) {
endpoint.setHandlers(handlerList);
}
// prefer to use endpoint configured over component configured
if (binding == null) {
// fallback to component configured
binding = getHttpBinding();
}
if (binding != null) {
endpoint.setBinding(binding);
}
// prefer to use endpoint configured over component configured
if (jettyBinding == null) {
// fallback to component configured
jettyBinding = getJettyHttpBinding();
}
if (jettyBinding != null) {
endpoint.setJettyBinding(jettyBinding);
}
if (enableJmx != null) {
endpoint.setEnableJmx(enableJmx);
} else {
// set this option based on setting of JettyHttpComponent
endpoint.setEnableJmx(isEnableJmx());
}
endpoint.setEnableMultipartFilter(enableMultipartFilter);
if (multipartFilter != null) {
endpoint.setMultipartFilter(multipartFilter);
endpoint.setEnableMultipartFilter(true);
}
if (enableCors) {
endpoint.setEnableCORS(enableCors);
if (filters == null) {
filters = new ArrayList<Filter>(1);
}
filters.add(new CrossOriginFilter());
}
if (filters != null) {
endpoint.setFilters(filters);
}
if (httpMethodRestrict != null) {
endpoint.setHttpMethodRestrict(httpMethodRestrict);
}
if (ssl != null) {
endpoint.setSslContextParameters(ssl);
}
if (httpClientMinThreads != null) {
endpoint.setHttpClientMinThreads(httpClientMinThreads);
}
if (httpClientMaxThreads != null) {
endpoint.setHttpClientMaxThreads(httpClientMaxThreads);
}
if (httpClient != null) {
endpoint.setHttpClient(httpClient);
}
endpoint.setSendServerVersion(isSendServerVersion());
setProperties(endpoint, parameters);
// re-create http uri after all parameters has been set on the endpoint, as the remainders are for http uri
httpUri = URISupport.createRemainingURI(addressUri, parameters);
endpoint.setHttpUri(httpUri);
return endpoint;
}
Aggregations