Defaulthttpclient scheme - httpclient

The request is using the wrong scheme (http instead of https). I can see this when i debug my client and inspect the scheme inside the HttpHost object. I'm using JerseyClient to submit the request, it does so by creating a web resource with a URI. I simply pass a string https://myserver.com:443/some/path. However inside the DefaultHttpRoutePlanner class it decides to use the default settings for HttpRoute and uses http. Can anyone tell me how i can override the default settings of the HttpRoute or RoutePlanner classes?

found the answer -
return new HttpHost(request.getURI().getHost(), request.getURI().getPort(), request.getURI().getScheme());

Related

How to redirect the url from nested site in pencilblue?

I want to 301 redirect the URLs from previous site that are nested, as pencilblue doesn’t support them,
e.g. a/b to page/b
For this I have been experimenting in include/http/request_handler.js but facing some issues.
Call never comes inside RequestHandler.prototype.handleRequest or even RequestHandler.prototype.onSessionRetrieved (seems these methods are not being called from anywhere)
Therefore I placed the code in RequestHandler and after confirming that req is not for public resource or api, I create a new url and execute
return this.doRedirect(newUrl, 301)
This actually works but at the same time I receive
Can’t render headers after they are sent error
#1075 has not helped me much as I’m not sure which specific controller I should modify. I need to catch the req as early as possible and see if it’s a page then redirect to page prefixed url.
Thanks in advance.
There are couple of ways to do redirects. You can do them from a controller or from middleware. You are correct in that, some of the functions in the request handler are not called. These are deprecated despite the fact pencilblue team didn't mark them as such. They replaced a good deal of the request handler functionality with /include/http/router.js and include/http/middleware/index.js. Plugins can register their own middleware to hijack the request pipeline.
See Advanced Routing on wiki for more info about creating your own middleware.
Using the routing framework your plugin would be able to register middleware that would be able to inspect the request and then redirect based on your specific criteria. The Router will be accessible from req.router and from there you could call req.router.redirect (Source).
Reference: #1224

HTTP Options method is not working as expected

I have a Jersey 2.x application running in tomcat. All the method implementations are working as expected, and even I am able to get WADL by navigating to http://{host}:{port}/JerseyRESTWebapp/ws/rest/application.wadl.
Everything is great so far.
Now, Out of curiosity I tried navigating to http://{host}:{port}/JerseyRESTWebapp/ws/rest/employees URL using using HTTP OPTIONS method expecting i will get 405 Method not allowed but i got the 200 OK and response body contains the WADL. Can someone let me know why is this happening? I am using POSTMAN chrome extension as REST client.
Also in the Response Allow Header, i am getting POST,GET,DELETE,OPTIONS,HEAD. I am missing PUT method here. why?
This is how the resource discovery works by default. It's implemented to follow the spec in regards to OPTIONS resource discovery
This method allows the client to determine the options and/or requirements associated with a resource, or the capabilities of a server, without implying a resource action or initiating a resource retrieval.
If you want to disable the WADL, you can by setting the property ServerProperties.WADL_FEATURE_DISABLE to true.
If you're curious about how this is implemented, check out the source for the WadlModelProcessor. It goes through all the resource models and adds an extra OPTIONS resource method. You can read more about the ModelProcessor in the Jersey docs Programmatic API for Building Resources

How to make GWT work with CORS and cookies

I separated GWT - static code is on CDN and dynamic is on a different server.
I configured this CORS filter on Tomcat7 and it works fine:
http://software.dzhuvinov.com/cors-filter-installation.html
However I'm still struggling with getting Cookies to work with GWT and CORS.
I mainly need it for XSRF protection (and few other things):
https://developers.google.com/web-toolkit/doc/latest/DevGuideSecurityRpcXsrf
It looks like I have to set withcredentials=true in XMLHttpRequest.
Does anyone know how to do it in GWT?
By default Cookies are not sent with CORS. If you want them, you have to set the withCredentials attribute to the low-level XMLHttpRequest used by GWT. Unfortunately there is no way to set it from your java code.
A simple hack is that you maintain your own version of XMLHttpRequest.java in your source tree (use the same namespace), and modify the create() method in this way:
private static native XMLHttpRequest create(String responseType) /*-{
[...]
xhr.withCredentials = true;
return xhr;
}-*/;
Note: from server side you have to sent this header as well:
Access-Control-Allow-Credentials: true
Otherwise, the normal way to deal with authentication in GWT is that you use special headers with the auth info and read it in the server side.
builder.setHeader("X-My-Authenticated-Header", "whatever");
EDIT: as t.broyer points in his comment below, a RequestBuilder.setIncludeCredentials method is in 2.5.1-rc1. So if you are using this release, call this method in when creating your builder.

GWTP HttpSessions with Cookies disabled

I have a GWT app that is loaded within an iFrame. The app uses GWTP for client server communication. In the ActionHandler implementation a HttpSession Provider is injected.
#Inject
private final Provider<HttpSession> sessionProvider;
If cookies are disabled in the browser, a new HttpSession is provided each time the provider calls .get()
requestProvider.get();
So, there is no chance to store any information in the http session.
How can this problem be solved?
Is there a way to add the jsessionid to each action request and if so, how do I get the jsessionid?
Are you looking for the approach that is described here. The technique relies on url rewrite for sessionid.
You can also reference section "Using URL Rewriting Instead of Cookies" from oracle documentation here.

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()