问题
I have a project that's using Camel and running on a ServiceMix server, but I can't seem to get it to access external web services, and I suspect it's because I can't set the proxy authentication properly.
Exchange exchange = producerTemplate.request(url, new Processor() {
public void process(Exchange exchange) throws Exception {
exchange.getIn().setHeader(Exchange.HTTP_METHOD, "POST");
exchange.getIn().setHeader(Exchange.CONTENT_TYPE, "application/json");
}
});
response = exchange.getOut().getBody(String.class);
If I put a breakpoint on the last line, I see a ConnectionTimedOutException in the exchange object and the response is null.
I tried setting the proxies in a multitude of ways.
1) I tried setting the proxy settings in a class that implements CamelContextAware:
camelContext.getProperties().put("http.proxyHost", "...");
camelContext.getProperties().put("http.proxyPort", "8080");
camelContext.getProperties().put("http.proxyUser", "...");
camelContext.getProperties().put("http.proxyPassword", "...");
camelContext.getProperties().put("http.proxySet", "true");
This works in standalone mode, but when I deploy the code in ServiceMix, the camelContext object is null.
2) I tried setting the proxy settings in the etc/system.properties file of ServiceMix.
3) I tried using http-conf:conduit in the camel-context.xml like this:
<http-conf:conduit name="*.http-conduit">
<http-conf:client ProxyServer="..." ProxyServerPort="8080" />
<http-conf:proxyAuthorization>
<conf-sec:UserName>...</conf-sec:UserName>
<conf-sec:Password>...</conf-sec:Password>
</http-conf:proxyAuthorization>
</http-conf:conduit>
However, I think that'd only work if I used a cxf client.
Nothing worked, and I need it to work while deployed on the ServiceMix. Any help would be greatly appreciated.
Thanks.
回答1:
If I'm right, the problem is more like you cannot reach the properties in SMX. ServiceMix supports SpringDM and I'd suggest to use that to request for the properties from Configuration Admin.
0) Assuming you're using Spring, your main-appContext xml must be placed under resources/META-INF/spring/, SMX will look for it there to initialize your app.
1) Add a different property file to ServiceMix/etc (do not use system.properties). It must be named as *.cfg, i.e.: my.properties.cfg
1.5) Ensure the config is loaded. In SMX, enter:
config:update
config:list | grep my.prop.name
2) You'll need this file to be resolved by Spring DM. Add appcontext xml content like the following (ensure you have the namespaces).
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:osgix="http://www.springframework.org/schema/osgi-compendium"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/osgi-compendium
http://www.springframework.org/schema/osgi-compendium/spring-osgi-compendium.xsd">
<osgix:cm-properties id="myProperties" persistent-id="my.properties" /> <!--without the extension-->
<context:property-placeholder ignore-unresolvable="true" properties-ref="myProperties" />
</beans>
3.) import this appcontext as well and use the properties through ${my.prop} placeholders.
Hope this helps!
回答2:
Try this code:
HTTPConduit conduit = (HTTPConduit)outMessage.getExchange().getConduit(outMessage);
HTTPClientPolicy policy = conduit.getClient();
policy.setProxyServer(PROXY_IP);
policy.setProxyServerPort(PROXY_PORT);
conduit.setClient(policy);
来源:https://stackoverflow.com/questions/16984812/proxy-authentication-with-camel-using-producertemplate