Set Timeout on CXF Proxy Client - cxf

I have a JAX-RS client in CXF created via JAXRSClientFactoryBean.create. How can I set the connection/receive timeouts?
I assume I need to get hold of the conduit but can't work out how to. This project is not using Spring.

Here's the code I use:
service = JAXRSClientFactory.create(url, serviceClass, providers);
HTTPConduit conduit = WebClient.getConfig(service).getHttpConduit();
HTTPClientPolicy policy = new HTTPClientPolicy();
policy.setReceiveTimeout(300000); //5 minutes
conduit.setClient(policy);

HTTPClientPolicy clientConfig = WebClient.getConfig(service).getHttpConduit().getClient();
clientConfig.setReceiveTimeout(10000);

Related

camel not knowing amount of endpoints in loadbalancer

Is it possible to loadbalance in a camel route without knowing the amount of endpoints before runtime?
As an example, certain incoming requests has to loadbalance over certain servers and the servers are configured.
using .loadBalance().failover().to() how can I dynamically set the amount of to() endpoints?
I have tried it with toD() and sending a string of comma seperated endpoints but it sends the request to all servers and does not loadbalance.
To do this, you'll need to use the Java DSL.
//Setup dynamic list of endpoints
List<String> toDefs = new ArrayList<String>();
toDefs.add("mock:a");
toDefs.add("mock:b");
//We need to modify the definition, so get the reference
LoadBalanceDefinition loadBalanceDefinition =
from("dirct:start")
.loadBalance()
.failover();
//Dynamically add the list of endpoints
for (String toDef: toDefs){
loadBalanceDefinition = loadBalanceDefinition.to(toDef);
}
//Finalize the route
loadBalanceDefinition.to("direct:end");

SolrJ - Can't commit updates when authentication plugin is enabled

With Solr 6.3.0, in cloud mode, and 3 external zookeepers as cluster, and use solrJ as client.
A: Without authentication
Before enabling authentication, I use following code to add/update document:
CloudSolrClient client = cloudClientBuilder.build();
UpdateResponse resp = client.add(doc, 5000);
client.commit();
client.close();
return resp;
It works well, the new document is in searching result immediately.
B: With authentication enabled
Then I enabled basic authentication plugin and rule-based authorization plugin (and SSL if that matters).
In order to set credential information, the code is refactored as following:
// create request,
UpdateRequest req = new UpdateRequest();
// add doc to request,
req.add(doc, 5000);
// set credential,
req.setBasicAuthCredentials(user, password);
// create client,
CloudSolrClient client = cloudClientBuilder.build();
client.setDefaultCollection(ConfigUtil.getProp(ConfigUtil.KEY_SOLR_CORE));
// do request & get response,
UpdateResponse resp = req.process(client);
client.commit();
client.close();
Then it will get error similar as this:
Error 401 require authentication, require authentication.
When debugging, the error occurs at line client.commit();.
Try with curl
I use curl to make an update:
curl -k --user solr:password "https://localhost:8983/solr/corexxx/update?wt=json&indent=true&commit=true" -d '[{"id":"20041", "name":"xxx 41", "location":"xxx", "description":"xxx"}]'
It committed successfully ! And, the updates are visible in searching result immediately.
My guess
Since curl works well, I guess solr cloud itself works fine.
Thus the issue is due to the code from B which is based on SolrJ.
Questions:
Why code B get HTTP 401 error? How can I fix it?
Could I use code from A, and still able to provide credential information,if yes, then how?
Thanks.
You should change client.commit() for req.commit(client, ConfigUtil.getProp(ConfigUtil.KEY_SOLR_CORE)), the credentials are setted in the UpdateRequest.
It worked like this:
SolrClient client = new HttpSolrClient.Builder(urlString).build();
UpdateRequest up = new UpdateRequest();
up.setBasicAuthCredentials(user, pass);
up.add(doc1);
up.process(client, core);
up.commit(client, core);
client.close();

How to fix a AutomaticUrlReservationCreationFailureException when using Nancy FX Self Host

When using Nancy FX, I came across the following exception which was thrown when trying to fire up a web service: AutomaticUrlReservationCreationFailureException
Having looked into it in a bit more detail, I discovered that the way to fix this was to run up a cmd prompt (as an administrator), then run the following command:
netsh http add urlacl url=http://+:1234/ user=DOMAIN\username
where
DOMAIN\username is the id of the user the service will be run under
1234 is the port that the service will be run on
I write this here in case anyone else comes across the same issue and spends a fruitless half hour or so looking for an answer - hopefully they will find this sooner than I did!
If you're creating your own NancyFx host, it may be easier for you to flag your HostConfiguration this way
HostConfiguration hostConfigs = new HostConfiguration()
{
UrlReservations = new UrlReservations() { CreateAutomatically = true }
};
or...
HostConfiguration hostConfigs = new HostConfiguration();
hostConfigs.UrlReservations.CreateAutomatically = true;
And then finally have something like
NancyHost nancyHost = new NancyHost(new Uri("http://+:80"), new DefaultNancyBootstrapper(), hostConfigs);
The Message of the AutomaticUrlReservationCreationFailureException will tell you this
The Nancy self host was unable to start, as no namespace reservation existed for the provided url(s).
Please either enable CreateNamespaceReservations on the HostConfiguration provided to the NancyHost, or create the reservations manually with the (elevated) command(s):
http add urlacl url=http://+:8888/nancy/ user=Everyone
http add urlacl url=http://127.0.0.1:8888/nancy/ user=Everyone
http add urlacl url=http://+:8889/nancytoo/ user=Everyone
The suggested reservations is based on the base URIs that you pass into the host when you create it.
The AutomaticUrlReservationCreationFailureException will also appear if you are running NancyFX from Visual Studio.
So make sure you are running as administrator in order for NancyFX to set up the underlying configurations

Proxy Authentication With Fluent API Request?

I am currently using a Get Request with proxy information:
String result1 = Request.Get("_http://somehost/")
.version(HttpVersion.HTTP_1_1)
.connectTimeout(1000)
.socketTimeout(1000)
.viaProxy(new HttpHost("myproxy", 8080))
.execute().returnContent().asString();
The result is a "Proxy Authentication Required" error. I believe a username and password is required from the server making the request? If so, how do I add that detail? I have never used the Fluent API before.
Here is an example using the Fluent API. The executor can be used to specify credentials for the proxy.
HttpHost proxyHost = new HttpHost("myproxy", 8080);
Executor executor = Executor.newInstance()
.auth(proxyHost, "username", "password");
String result1 = executor.execute(Request.Get("_http://somehost/")
.viaProxy(proxyHost))
.returnContent()
.asString();
You need a CredentialsProvider.
final CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(proxy), new UsernamePasswordCredentials(username, password));
final HttpPost httppost = new HttpPost(uri);
httppost.setConfig(RequestConfig.custom().setProxy(proxy).build());
String line = HttpClientBuilder.create().setDefaultCredentialsProvider(credsProvider).build() .execute(httppost).getStatusLine());
Also, there was a bug in 4.3.1 that impacted authentication. It is fixed in 4.3.2.
https://issues.apache.org/jira/browse/HTTPCLIENT-1435
Executor executor = Executor.newInstance()
.auth(new HttpHost("myproxy", 8080), "username", "password")
.authPreemptive(new HttpHost("myproxy", 8080));
Response response = executor.execute(<your reques>);

How to include proxy auth username and password for proxy?

I do not want to give username/password for proxy setting I have for my office network, I could give PROXY to browser either :
String PROXY = "localhost:8080";
org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();
proxy.setHttpProxy(PROXY)
.setFtpProxy(PROXY)
.setSslProxy(PROXY);
DesiredCapabilities cap = new DesiredCapabailities();
cap.setPreference(CapabilityType.PROXY, proxy);
or
user_pref("network.proxy.http", "127.0.0.1");
user_pref("network.proxy.http_port", 8084);
user_pref("network.proxy.ssl", "127.0.0.1");
user_pref("network.proxy.ssl_port", 8084);
user_pref("network.proxy.no_proxies_on", "localhost:4444");
user_pref("network.proxy.type", 1);
but, what ever I do, its still asking for password for Webdriver.
Note: I could send username/password for htmlunit driver. PLEASE HELP!
Try below
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.proxy.type", 0);
WebDriver driver = new FirefoxDriver(profile);
I have read a lot of posts stating that you have to send the profile as a base64 encoded string.
cap.setPreference(CapabilityType.PROXY, proxy.ToBase64String());
The documentation I've read hasn't ever been conclusive if this is necessary or not, but it's worth a shot.

Resources