In Vertx webclient how to log http request and response - vert.x

What is the simplest way for logging outgoing http request and response in Vertx WebClient. I'm looking for something similar to the httpserver LoggerHandler, but then for the webclient thus outgoing requests.

WebClient has an overloaded create(Vertx, WebClientOptions) method.
WebClientOptions has a setLogActivity() method that accepts a boolean parameter that indicates if network activity should be logged.
(disclaimer: i haven't tried this myself so i can't vouch for what's actually logged, but see if that covers your needs).

Related

Submitting concurrent HTTP requests with RestSharp

I have a RESTful API running as a self hosted OWIN application, and a Client that's making API calls to the server. Both are done using .Net/WebAPI/RestSharp. In a normal scenario the client sends an HTTP request and receives an HTTP response and everything works as expected. However, there are cases when the client needs to send another request before receiving a response from the previous request. Something like:
HTTP POST Request1 <-- this is a long running operation on the server side
HTTP POST Request2 <-- this comes from a different thread
HTTP POST Response2
HTTP POST Response1
The problem is that Request2 doesn't send until Response1 arrives. These requests are blocking calls (synchronous) and I cannot make them asynchronous.
I tried setting System.Net.ServicePointManager.DefaultConnectionLimit to 20 before initializing my RestClient, but that didn't help.
System.Net.ServicePointManager.DefaultConnectionLimit = 20;
var Client = new RestClient("https://someURL");
Any idea on what the problem might be? and what can I do to send those requests concurrently?
This was a thread synchronization issue in my server. I was dealing with a deadlock situation.
Using RestClient.ExecuteAsync() instead of .Execute() may solve the problem.

Management API: batch request > Behavior when one of the nested HTTP request fails

I'm batching GA Management API calls (insert Web Property User Links) as described on this link: https://developers.google.com/analytics/devguides/config/mgmt/v3/batching
Using Pything Client Library:
link = service.management().webpropertyUserLinks().insert(accountId, webPropertyId, body)
batch.add(link)
batch.execute(http=http)
Did I understand correctly, if any of the nested HTTP request fails, all pending HTTP requests in the batch will fail to execute as well? This seems to be the behavior I'm experiencing.
I had expected rest of the calls will still be executed!
Thanks a lot for any clarification
This is how batching works unfortunately it is assumed that the calls are linked and if on fails they should all fail.
Answer: yes if one of the requests within your batching request fails they will all fail.

How to call soap web service from camel rest java DEL

I am trying to call soap web service from camel rest, from java DSL. but getting server error with 500 response code.
I will receive call from a rest with json data and i have to make call to a third party soap service also i need to process the soap response and send back the response in json formate.
here is my code
{
String getCustomerDetailsurl="http://<serverip>/webservice/Service.asmx?op=GetClientDetail&bridgeEndpoint=true";
rest("/customers")
.description("Aviva Mobile sales customer service")
.consumes("application/json")
.produces("application/json")
.post().type(ClientRequest.class) // incomming request data
.route()
.from("direct:start")
//.setHeader(Exchange.HTTP_METHOD, constant("POST"))
.process(new CustomerProcessor()).marshal().xstream()
.to(getCustomerDetailsurl);
Error
org.apache.camel.http.common.HttpOperationFailedException: HTTP operation failed invoking http://<serverip>/webservice/Service.asmx?op=GetClientDetail with statusCode: 500
at org.apache.camel.component.http.HttpProducer.populateHttpOperationFailedException(HttpProducer.java:239) ~[camel-http-2.17.5.jar:2.17.5]
at org.apache.camel.component.http.HttpProducer.process(HttpProducer.java:161) ~[camel-http-2.17.5.jar:2.17.5]
at org.apache.camel.util.AsyncProcessorConverterHelper$ProcessorToAsyncProcessorBridge.process(AsyncProcessorConverterHelper.java:61) ~[camel-core-2.17.5.jar:2.17.5]
at org.apache.camel.processor.SendProcessor.process(SendProcessor.java:145) ~[camel-core-2.17.5.jar:2.17.5]
at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:77) ~[camel-core-2.17.5.jar:2.17.5]
at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:468) ~[camel-core-2.17.5.jar:2.17.5]
}
You have to look at Server side for what actual error is.
You may use some tool like tcp monitor (tcpMon) to see exactly what you send and what you get back.
Actually, To use SOAP service it is much better to use SOAP client, rather than raw call through http. Take a look at Camel-CXF component. Then create a CXF endpoint and use it uri in your .to({cxfEndpointUri}). CXF will do all SOAP work for you. Maybe you will need to make a little work in its interceptors, like authorization if Server requires it.
P.S. In your code what kind of Exchange.body your CustomerProcessor produces? is it a valid for server SOAP Envelope? does it have all what server requires by its contract (WSDL)?

Difference between RequestContext and MessageContext in JAX WS

What is the difference between RequestContext and MessageContext in JAX-WS?
MessageContext provides access to the current inbound or outbound message in a JAX-WS call, specifically for JAX-WS handlers. Inbound to a service provider is the request message while outbound is the response; For a JAX-WS client handler, outbound is the request and inbound is the response.
I had to look up RequestContext - it isn't technically a JAX-WS class. It's a proprietary (read: implementation) class in the JAX-WS reference implementation. I don't think you'd want to couple/compile your code against this, but I do expect you'd see it during debugger sessions if you're using the JAX-WS RI.
If by chance you're referring to BindingProvider.getRequestContext(), this is a Map<String, Object> that is a map that contains values for initializing the outbound request message for a JAX-WS client. For example, to programmatically set the endpoint URL:
Map<String, Object> requestContext = ((BindingProvider)port).getRequestContext();
requestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, myCustomEndpointUrl);

Consuming a RESTful web service using Apache Camel

I am trying to consume a restful Web service using camel.
For that I am configuring dynamic endpoint url as the RESTful url is created at the runtime. Everytime I am checking if the particular endpoint url is registered as a route in my camel context using following method of CamelContext class.
Endpoint hasEndpoint(String uri);
In this case, if the endpoint is not registered then I add a route to my camel context using a custom Route Builder.
I am using camel HTTP component for this. This is working fine for me as of now.
However, I believe performance wise this is not good as everytime I have to check if a route is registered with the camel context and if not then register the same before making the webservice call.
Can some body please tell me if there is a better way to consume RESTful Web services in camel?
I also want to know if the RESTful webservice I am consuming uses OAuth 2.0 protocol, do I need to change anything in my code as I am just consuming it?
Regards, Nilotpal
Thanks for your reply.
I am checking if the route is already exists to make sure I don't end up adding duplicate route(s) to the camel context.
Regarding long lived routes and route dynamics, can u please explain a bit regarding this? How do I implement route dynamics?
It would also be helpful if you could point me to some CXF-RS producer example.. I read the documentation of CXFRS but could not understand it clearly.
Thanks
Nilotpal
Exactly why do you need to check if the route is registred or not before making the call? You should perhaps setup a more long lived route and route dynamic towards resfull resources.
As for Rest with camel, I think the HTTP component does a great job, but there are higher level components to use as well, more designed for REST.
CXFRS and Restlet, producer examples for restlet can be found in the Apache Camel source unit tests, such as this RestletProducerGetTest.java.
As for oAuth 2.0, Camel has some oAuth support built-in, especially for google. Look for the gauth component. There is even a tutorial, however it might not be aligned with your case, it still might give some background so you could solve your issues: http://camel.apache.org/tutorial-oauth.html
CamelContext context = new DefaultCamelContext();
My Aim
I am trying to intercept the incoming request and based on the ip of the incoming request i want to invoke dynamic endpoint of get offers
context.addRoutes(new RouteBuilder(){
public void configure(){
from("jetty:localhost:9000/offers")
.process(new Processor(){
public void process(Exchange exchange) throws Exception {
//getting the request object
HttpServletRequest req = exchange.getIn().getBody(HttpServletRequest.class);
//Extracting information from the request
String requestIP=req.getRemoteAddr();
/**
* After getting the ip address i do necessay processing
* and then add a property to exchange object.
* Destination ip address is the address to which i want to
* send my request
*/
exchange.setProperty("operatorAddress",destinationIpAddress);
}
})
.to("direct:getOffers")
.end();
}
});
Now i will invoke the getOffers endpoint
so first i will register it
context.addRoutes(new RouteBuilder(){
public void configure(){
from("direct:getOffers")
.toD("jetty:${property.operatorAddress}/api/v2.0/offers?
bridgeEndpoint=true")
.end();
}
});
so we can access the operatorAddress property of exchange object as
${property.operatorAddress}
also when we have dynamic routes then we need to call
.toD() and not .to()