use of org.apache.camel.spi.HeaderFilterStrategy in project camel by apache.
the class NettyHttpComponent method createEndpoint.
@Override
protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
NettyConfiguration config;
if (getConfiguration() != null) {
config = getConfiguration().copy();
} else {
config = new NettyHttpConfiguration();
}
HeaderFilterStrategy headerFilterStrategy = resolveAndRemoveReferenceParameter(parameters, "headerFilterStrategy", HeaderFilterStrategy.class);
// merge any custom bootstrap configuration on the config
NettyServerBootstrapConfiguration bootstrapConfiguration = resolveAndRemoveReferenceParameter(parameters, "bootstrapConfiguration", NettyServerBootstrapConfiguration.class);
if (bootstrapConfiguration != null) {
Map<String, Object> options = new HashMap<String, Object>();
if (IntrospectionSupport.getProperties(bootstrapConfiguration, options, null, false)) {
IntrospectionSupport.setProperties(getCamelContext().getTypeConverter(), config, options);
}
}
// any custom security configuration
NettyHttpSecurityConfiguration securityConfiguration = resolveAndRemoveReferenceParameter(parameters, "securityConfiguration", NettyHttpSecurityConfiguration.class);
Map<String, Object> securityOptions = IntrospectionSupport.extractProperties(parameters, "securityConfiguration.");
// are we using a shared http server?
int sharedPort = -1;
NettySharedHttpServer shared = resolveAndRemoveReferenceParameter(parameters, "nettySharedHttpServer", NettySharedHttpServer.class);
if (shared != null) {
// use port number from the shared http server
LOG.debug("Using NettySharedHttpServer: {} with port: {}", shared, shared.getPort());
sharedPort = shared.getPort();
}
// we must include the protocol in the remaining
boolean hasProtocol = remaining.startsWith("http://") || remaining.startsWith("http:") || remaining.startsWith("https://") || remaining.startsWith("https:");
if (!hasProtocol) {
// http is the default protocol
remaining = "http://" + remaining;
}
boolean hasSlash = remaining.startsWith("http://") || remaining.startsWith("https://");
if (!hasSlash) {
// must have double slash after protocol
if (remaining.startsWith("http:")) {
remaining = "http://" + remaining.substring(5);
} else {
remaining = "https://" + remaining.substring(6);
}
}
LOG.debug("Netty http url: {}", remaining);
// set port on configuration which is either shared or using default values
if (sharedPort != -1) {
config.setPort(sharedPort);
} else if (config.getPort() == -1 || config.getPort() == 0) {
if (remaining.startsWith("http:")) {
config.setPort(80);
} else if (remaining.startsWith("https:")) {
config.setPort(443);
}
}
if (config.getPort() == -1) {
throw new IllegalArgumentException("Port number must be configured");
}
// configure configuration
config = parseConfiguration(config, remaining, parameters);
setProperties(config, parameters);
// validate config
config.validateConfiguration();
// create the address uri which includes the remainder parameters (which is not configuration parameters for this component)
URI u = new URI(UnsafeUriCharactersEncoder.encodeHttpURI(remaining));
String addressUri = URISupport.createRemainingURI(u, parameters).toString();
NettyHttpEndpoint answer = new NettyHttpEndpoint(addressUri, this, config);
answer.setTimer(getTimer());
// must use a copy of the binding on the endpoint to avoid sharing same instance that can cause side-effects
if (answer.getNettyHttpBinding() == null) {
Object binding = getNettyHttpBinding();
if (binding instanceof RestNettyHttpBinding) {
NettyHttpBinding copy = ((RestNettyHttpBinding) binding).copy();
answer.setNettyHttpBinding(copy);
} else if (binding instanceof DefaultNettyHttpBinding) {
NettyHttpBinding copy = ((DefaultNettyHttpBinding) binding).copy();
answer.setNettyHttpBinding(copy);
}
}
if (headerFilterStrategy != null) {
answer.setHeaderFilterStrategy(headerFilterStrategy);
} else if (answer.getHeaderFilterStrategy() == null) {
answer.setHeaderFilterStrategy(getHeaderFilterStrategy());
}
if (securityConfiguration != null) {
answer.setSecurityConfiguration(securityConfiguration);
} else if (answer.getSecurityConfiguration() == null) {
answer.setSecurityConfiguration(getSecurityConfiguration());
}
// configure any security options
if (securityOptions != null && !securityOptions.isEmpty()) {
securityConfiguration = answer.getSecurityConfiguration();
if (securityConfiguration == null) {
securityConfiguration = new NettyHttpSecurityConfiguration();
answer.setSecurityConfiguration(securityConfiguration);
}
setProperties(securityConfiguration, securityOptions);
validateParameters(uri, securityOptions, null);
}
answer.setNettySharedHttpServer(shared);
return answer;
}
use of org.apache.camel.spi.HeaderFilterStrategy in project camel by apache.
the class NettyHttpComponent method createEndpoint.
@Override
protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
NettyConfiguration config;
if (getConfiguration() != null) {
config = getConfiguration().copy();
} else {
config = new NettyHttpConfiguration();
}
HeaderFilterStrategy headerFilterStrategy = resolveAndRemoveReferenceParameter(parameters, "headerFilterStrategy", HeaderFilterStrategy.class);
// merge any custom bootstrap configuration on the config
NettyServerBootstrapConfiguration bootstrapConfiguration = resolveAndRemoveReferenceParameter(parameters, "bootstrapConfiguration", NettyServerBootstrapConfiguration.class);
if (bootstrapConfiguration != null) {
Map<String, Object> options = new HashMap<String, Object>();
if (IntrospectionSupport.getProperties(bootstrapConfiguration, options, null, false)) {
IntrospectionSupport.setProperties(getCamelContext().getTypeConverter(), config, options);
}
}
// any custom security configuration
NettyHttpSecurityConfiguration securityConfiguration = resolveAndRemoveReferenceParameter(parameters, "securityConfiguration", NettyHttpSecurityConfiguration.class);
Map<String, Object> securityOptions = IntrospectionSupport.extractProperties(parameters, "securityConfiguration.");
NettyHttpBinding bindingFromUri = resolveAndRemoveReferenceParameter(parameters, "nettyHttpBinding", NettyHttpBinding.class);
// are we using a shared http server?
int sharedPort = -1;
NettySharedHttpServer shared = resolveAndRemoveReferenceParameter(parameters, "nettySharedHttpServer", NettySharedHttpServer.class);
if (shared != null) {
// use port number from the shared http server
LOG.debug("Using NettySharedHttpServer: {} with port: {}", shared, shared.getPort());
sharedPort = shared.getPort();
}
// we must include the protocol in the remaining
boolean hasProtocol = remaining.startsWith("http://") || remaining.startsWith("http:") || remaining.startsWith("https://") || remaining.startsWith("https:");
if (!hasProtocol) {
// http is the default protocol
remaining = "http://" + remaining;
}
boolean hasSlash = remaining.startsWith("http://") || remaining.startsWith("https://");
if (!hasSlash) {
// must have double slash after protocol
if (remaining.startsWith("http:")) {
remaining = "http://" + remaining.substring(5);
} else {
remaining = "https://" + remaining.substring(6);
}
}
LOG.debug("Netty http url: {}", remaining);
// set port on configuration which is either shared or using default values
if (sharedPort != -1) {
config.setPort(sharedPort);
} else if (config.getPort() == -1 || config.getPort() == 0) {
if (remaining.startsWith("http:")) {
config.setPort(80);
} else if (remaining.startsWith("https:")) {
config.setPort(443);
}
}
if (config.getPort() == -1) {
throw new IllegalArgumentException("Port number must be configured");
}
// configure configuration
config = parseConfiguration(config, remaining, parameters);
setProperties(config, parameters);
// validate config
config.validateConfiguration();
// create the address uri which includes the remainder parameters (which
// is not configuration parameters for this component)
URI u = new URI(UnsafeUriCharactersEncoder.encodeHttpURI(remaining));
String addressUri = URISupport.createRemainingURI(u, parameters).toString();
NettyHttpEndpoint answer = new NettyHttpEndpoint(addressUri, this, config);
// instance that can cause side-effects
if (answer.getNettyHttpBinding() == null) {
Object binding = null;
if (bindingFromUri != null) {
binding = bindingFromUri;
} else {
binding = getNettyHttpBinding();
}
if (binding instanceof RestNettyHttpBinding) {
NettyHttpBinding copy = ((RestNettyHttpBinding) binding).copy();
answer.setNettyHttpBinding(copy);
} else if (binding instanceof DefaultNettyHttpBinding) {
NettyHttpBinding copy = ((DefaultNettyHttpBinding) binding).copy();
answer.setNettyHttpBinding(copy);
}
}
if (headerFilterStrategy != null) {
answer.setHeaderFilterStrategy(headerFilterStrategy);
} else if (answer.getHeaderFilterStrategy() == null) {
answer.setHeaderFilterStrategy(getHeaderFilterStrategy());
}
if (securityConfiguration != null) {
answer.setSecurityConfiguration(securityConfiguration);
} else if (answer.getSecurityConfiguration() == null) {
answer.setSecurityConfiguration(getSecurityConfiguration());
}
// configure any security options
if (securityOptions != null && !securityOptions.isEmpty()) {
securityConfiguration = answer.getSecurityConfiguration();
if (securityConfiguration == null) {
securityConfiguration = new NettyHttpSecurityConfiguration();
answer.setSecurityConfiguration(securityConfiguration);
}
setProperties(securityConfiguration, securityOptions);
validateParameters(uri, securityOptions, null);
}
answer.setNettySharedHttpServer(shared);
return answer;
}
use of org.apache.camel.spi.HeaderFilterStrategy in project camel by apache.
the class HttpPollingConsumer method doReceive.
protected Exchange doReceive(int timeout) {
Exchange exchange = endpoint.createExchange();
HttpRequestBase method = createMethod(exchange);
HttpClientContext httpClientContext = new HttpClientContext();
// set optional timeout in millis
if (timeout > 0) {
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(timeout).build();
httpClientContext.setRequestConfig(requestConfig);
}
HttpEntity responeEntity = null;
try {
// execute request
HttpResponse response = httpClient.execute(method, httpClientContext);
int responseCode = response.getStatusLine().getStatusCode();
responeEntity = response.getEntity();
Object body = HttpHelper.readResponseBodyFromInputStream(responeEntity.getContent(), exchange);
// lets store the result in the output message.
Message message = exchange.getOut();
message.setBody(body);
// lets set the headers
Header[] headers = response.getAllHeaders();
HeaderFilterStrategy strategy = endpoint.getHeaderFilterStrategy();
for (Header header : headers) {
String name = header.getName();
// mapping the content-type
if (name.toLowerCase().equals("content-type")) {
name = Exchange.CONTENT_TYPE;
}
String value = header.getValue();
if (strategy != null && !strategy.applyFilterToExternalHeaders(name, value, exchange)) {
message.setHeader(name, value);
}
}
message.setHeader(Exchange.HTTP_RESPONSE_CODE, responseCode);
if (response.getStatusLine() != null) {
message.setHeader(Exchange.HTTP_RESPONSE_TEXT, response.getStatusLine().getReasonPhrase());
}
return exchange;
} catch (IOException e) {
throw new RuntimeCamelException(e);
} finally {
if (responeEntity != null) {
try {
EntityUtils.consume(responeEntity);
} catch (IOException e) {
// nothing what we can do
}
}
}
}
use of org.apache.camel.spi.HeaderFilterStrategy in project camel by apache.
the class HttpProducer method process.
public void process(Exchange exchange) throws Exception {
if (getEndpoint().isClearExpiredCookies() && !getEndpoint().isBridgeEndpoint()) {
// create the cookies before the invocation
getEndpoint().getCookieStore().clearExpired(new Date());
}
// if we bridge endpoint then we need to skip matching headers with the HTTP_QUERY to avoid sending
// duplicated headers to the receiver, so use this skipRequestHeaders as the list of headers to skip
Map<String, Object> skipRequestHeaders = null;
if (getEndpoint().isBridgeEndpoint()) {
exchange.setProperty(Exchange.SKIP_GZIP_ENCODING, Boolean.TRUE);
String queryString = exchange.getIn().getHeader(Exchange.HTTP_QUERY, String.class);
if (queryString != null) {
skipRequestHeaders = URISupport.parseQuery(queryString, false, true);
}
}
HttpRequestBase httpRequest = createMethod(exchange);
Message in = exchange.getIn();
String httpProtocolVersion = in.getHeader(Exchange.HTTP_PROTOCOL_VERSION, String.class);
if (httpProtocolVersion != null) {
// set the HTTP protocol version
int[] version = HttpHelper.parserHttpVersion(httpProtocolVersion);
httpRequest.setProtocolVersion(new HttpVersion(version[0], version[1]));
}
HeaderFilterStrategy strategy = getEndpoint().getHeaderFilterStrategy();
// propagate headers as HTTP headers
for (Map.Entry<String, Object> entry : in.getHeaders().entrySet()) {
String key = entry.getKey();
Object headerValue = in.getHeader(key);
if (headerValue != null) {
// use an iterator as there can be multiple values. (must not use a delimiter, and allow empty values)
final Iterator<?> it = ObjectHelper.createIterator(headerValue, null, true);
// the value to add as request header
final List<String> values = new ArrayList<String>();
// should be combined into a single value
while (it.hasNext()) {
String value = exchange.getContext().getTypeConverter().convertTo(String.class, it.next());
// as then we would duplicate headers on both the endpoint uri, and in HTTP headers as well
if (skipRequestHeaders != null && skipRequestHeaders.containsKey(key)) {
continue;
}
if (value != null && strategy != null && !strategy.applyFilterToCamelHeaders(key, value, exchange)) {
values.add(value);
}
}
// add the value(s) as a http request header
if (values.size() > 0) {
// use the default toString of a ArrayList to create in the form [xxx, yyy]
// if multi valued, for a single value, then just output the value as is
String s = values.size() > 1 ? values.toString() : values.get(0);
httpRequest.addHeader(key, s);
}
}
}
if (getEndpoint().getCookieHandler() != null) {
Map<String, List<String>> cookieHeaders = getEndpoint().getCookieHandler().loadCookies(exchange, httpRequest.getURI());
for (Map.Entry<String, List<String>> entry : cookieHeaders.entrySet()) {
String key = entry.getKey();
if (entry.getValue().size() > 0) {
// use the default toString of a ArrayList to create in the form [xxx, yyy]
// if multi valued, for a single value, then just output the value as is
String s = entry.getValue().size() > 1 ? entry.getValue().toString() : entry.getValue().get(0);
httpRequest.addHeader(key, s);
}
}
}
//if this option is set, and the exchange Host header is not null, we will set it's current value on the httpRequest
if (getEndpoint().isPreserveHostHeader()) {
String hostHeader = exchange.getIn().getHeader("Host", String.class);
if (hostHeader != null) {
//HttpClient 4 will check to see if the Host header is present, and use it if it is, see org.apache.http.protocol.RequestTargetHost in httpcore
httpRequest.setHeader("Host", hostHeader);
}
}
if (getEndpoint().isConnectionClose()) {
httpRequest.addHeader("Connection", HTTP.CONN_CLOSE);
}
// lets store the result in the output message.
HttpResponse httpResponse = null;
try {
if (LOG.isDebugEnabled()) {
LOG.debug("Executing http {} method: {}", httpRequest.getMethod(), httpRequest.getURI().toString());
}
httpResponse = executeMethod(httpRequest);
int responseCode = httpResponse.getStatusLine().getStatusCode();
LOG.debug("Http responseCode: {}", responseCode);
if (!throwException) {
// if we do not use failed exception then populate response for all response codes
populateResponse(exchange, httpRequest, httpResponse, in, strategy, responseCode);
} else {
boolean ok = HttpHelper.isStatusCodeOk(responseCode, getEndpoint().getOkStatusCodeRange());
if (ok) {
// only populate response for OK response
populateResponse(exchange, httpRequest, httpResponse, in, strategy, responseCode);
} else {
// operation failed so populate exception to throw
throw populateHttpOperationFailedException(exchange, httpRequest, httpResponse, responseCode);
}
}
} finally {
final HttpResponse response = httpResponse;
if (httpResponse != null && getEndpoint().isDisableStreamCache()) {
// close the stream at the end of the exchange to ensure it gets eventually closed later
exchange.addOnCompletion(new SynchronizationAdapter() {
@Override
public void onDone(Exchange exchange) {
try {
EntityUtils.consume(response.getEntity());
} catch (Throwable e) {
// ignore
}
}
});
} else if (httpResponse != null) {
// close the stream now
try {
EntityUtils.consume(response.getEntity());
} catch (Throwable e) {
// ignore
}
}
}
}
use of org.apache.camel.spi.HeaderFilterStrategy in project camel by apache.
the class HttpProducer method process.
public void process(Exchange exchange) throws Exception {
// if we bridge endpoint then we need to skip matching headers with the HTTP_QUERY to avoid sending
// duplicated headers to the receiver, so use this skipRequestHeaders as the list of headers to skip
Map<String, Object> skipRequestHeaders = null;
if (getEndpoint().isBridgeEndpoint()) {
exchange.setProperty(Exchange.SKIP_GZIP_ENCODING, Boolean.TRUE);
String queryString = exchange.getIn().getHeader(Exchange.HTTP_QUERY, String.class);
if (queryString != null) {
skipRequestHeaders = URISupport.parseQuery(queryString, false, true);
}
// Need to remove the Host key as it should be not used
exchange.getIn().getHeaders().remove("host");
}
HttpMethod method = createMethod(exchange);
Message in = exchange.getIn();
String httpProtocolVersion = in.getHeader(Exchange.HTTP_PROTOCOL_VERSION, String.class);
if (httpProtocolVersion != null) {
// set the HTTP protocol version
HttpMethodParams params = method.getParams();
params.setVersion(HttpVersion.parse(httpProtocolVersion));
}
HeaderFilterStrategy strategy = getEndpoint().getHeaderFilterStrategy();
// propagate headers as HTTP headers
for (Map.Entry<String, Object> entry : in.getHeaders().entrySet()) {
String key = entry.getKey();
Object headerValue = in.getHeader(key);
if (headerValue != null) {
// use an iterator as there can be multiple values. (must not use a delimiter, and allow empty values)
final Iterator<?> it = ObjectHelper.createIterator(headerValue, null, true);
// the value to add as request header
final List<String> values = new ArrayList<String>();
// should be combined into a single value
while (it.hasNext()) {
String value = exchange.getContext().getTypeConverter().convertTo(String.class, it.next());
// as then we would duplicate headers on both the endpoint uri, and in HTTP headers as well
if (skipRequestHeaders != null && skipRequestHeaders.containsKey(key)) {
continue;
}
if (value != null && strategy != null && !strategy.applyFilterToCamelHeaders(key, value, exchange)) {
values.add(value);
}
}
// add the value(s) as a http request header
if (values.size() > 0) {
// use the default toString of a ArrayList to create in the form [xxx, yyy]
// if multi valued, for a single value, then just output the value as is
String s = values.size() > 1 ? values.toString() : values.get(0);
method.addRequestHeader(key, s);
}
}
}
if (getEndpoint().getCookieHandler() != null) {
// disable the per endpoint cookie handling
method.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
Map<String, List<String>> cookieHeaders = getEndpoint().getCookieHandler().loadCookies(exchange, new URI(method.getURI().getEscapedURI()));
for (Map.Entry<String, List<String>> entry : cookieHeaders.entrySet()) {
String key = entry.getKey();
if (entry.getValue().size() > 0) {
// use the default toString of a ArrayList to create in the form [xxx, yyy]
// if multi valued, for a single value, then just output the value as is
String s = entry.getValue().size() > 1 ? entry.getValue().toString() : entry.getValue().get(0);
method.addRequestHeader(key, s);
}
}
}
if (getEndpoint().isConnectionClose()) {
method.addRequestHeader("Connection", "close");
}
// lets store the result in the output message.
try {
if (LOG.isDebugEnabled()) {
LOG.debug("Executing http {} method: {}", method.getName(), method.getURI().toString());
}
int responseCode = executeMethod(method);
LOG.debug("Http responseCode: {}", responseCode);
if (!throwException) {
// if we do not use failed exception then populate response for all response codes
populateResponse(exchange, method, in, strategy, responseCode);
} else {
boolean ok = HttpHelper.isStatusCodeOk(responseCode, getEndpoint().getOkStatusCodeRange());
if (ok) {
// only populate response for OK response
populateResponse(exchange, method, in, strategy, responseCode);
} else {
// operation failed so populate exception to throw
throw populateHttpOperationFailedException(exchange, method, responseCode);
}
}
} finally {
method.releaseConnection();
}
}
Aggregations