Sharing of request object between two web application - java

I have two web applications, WebA andWebB, deployed on different tomcat. I want to send request object fromWebA to WebB. I had used wurfl in WebB application for getting information about browser and operation system etc..or other work.
On WebA application I want to show that data graphically(just like analytics).so for this purpose i had to pass request object to WEbB application.please suggest any way to pass request object from WebA to WebB with same session.

What you need is a proxy, passing everything from webA to webB. The request object contains: request parameters, http headers, cookies. You could try to write a simple proxy yourself (with the help of HttpClient) opening a connection to webB and setting all the necessary data.

Related

Java Jersey - Rest connections through a proxy

I developed an application in java that needs to consume a set of webservices provided by a provider.
Basically I need to call a first rest webservice to connect to the system, in which I pass username and password in the body of the post rest call.
Without using proxies I can make a second rest call without passing username and password, the webservice provider recognises me.
But when I use a proxy this second call does not work any more, I get an HTTP 401 error.
Notice that the first connect call works well also with the proxy.
Is there something conceptual that I'm missing?
Anyone knows what is happening?
Thank you
If you're making only a single login request and then making follow up requests which do not supply credentials, the service you are consuming probably is using cookies to track a user session.
When you put a proxy between yourself and that service, if the proxy is not forwarding all of your HTTP header information (where the cookie is probably being exchanged with the service), then the service will no longer recognize your session and consider you unauthorized
You may need to better understand how the service is maintaining your session (is it using Set-Cookie headers that it expects you to send back?) And you need to understand if the proxy you are using is forwarding all of your headers to the service

Servlets - Send Response Based on Session Only

I get a request from one client, I want to build a response and send it to two clients (one of which is the requester). The only piece of information I know about the other client is its session. Is there a way to construct a HttpResponse object using an HttpSession object?
Im not sure about what youre trying to accomplish, but you may want to contact the second client via "reversal ajax". You may want to take a look at Comet Programming
Comet is a web application model in which a long-held HTTP request allows a web server to push data to a browser, without the browser explicitly requesting it. Wikipedia

Servlet Acting as Proxy: How to forward the session?

I am not too sure about the feasibility of the requirement that I am trying to achieve, but here is how it goes:
I have created a Servlet that acts as proxy. It receives a RESTful call and then invokes another RESTful service on a remote server (node).
The forwarding is achieved via HTTPClient and not with a Request Dispatcher. I basically issue a new HTTP request to the remote server.
When the first server (proxy server) receives the call, the request (HttpServletRequest) has a session associated with it. The isNew() property of the HTTPSession is false.
When the call is forwarded and the remote server receives the call, the session gets to be a brand new one.
I am trying to basically find a way to forward the session to the remote server as well.
To be more precise:
Is it possible simply to get a session from a HttpServletRequest and put it into a session of a newly created HTTP request (via HTTPClient)?
It depends on how your remote WS maintains a session. If for example, it uses cookies (Tomcat does that amongst other techniques), then forwarding the incoming headers should help you achieve that (make sure that you mention that you accept cookies, but I thing HTTPClient does it by default).
Now, if it is based on parameter in the URL, then you should try to reproduce that behaviour.
If the two nodes (proxy and service) are separate processes that are not part of an application cluster, then probably not.
The servlet container typically manages the HttpSession. If you are forwarding a request to another service, hosted by a different container, then you will have a different session object.
If the two nodes are part of a cluster, then typically the session can be shared between nodes in the cluster via a number of mechanisms (in memory replication, database synchronization, etc).
Another option, is to externalize your session data into something like Redis, Memcached, Coherence, etc. Some application servers have pluggable support for such a process. I believe in this scenario the application server nodes would not necessarily have to be party of a cluster to share the session data.
I lacked some of the fundamentals of session handling when I asked this question. After some research and discussion here is what I got:
Basically, the sessions are handled by a JSESSIONID variable.
JSESSIONID is created automatically (can be turned off) when the request hits the server.
It gets sent back with in the response header.
The reason why the remote server thinks it is a new session is because the request doesn't have this JSESSIONID set.
The proxy does the following to ensure that the session is forwarded:
When the incoming request comes in; create and store the JSESSIONID.
Issue a new request to the remote server.
Unpack the response header from the remote server and extract JSESSIONID.
Maintain a mapping of client side JSESSIONID and remote server JSESSIONID.
For any of the following request, use this mapping to send requests to the remote server.
Basically, the proxy does the mapping from the client side's JSESSIONID to the remote server's JSESSIONID. This way, the session gets forwarded to the remote server.

Getting post data from request

I'm writing a server side app in Java using the HttpCore library.
I have an HttpRequest and I'm trying to get the postdata sent from a form. The problem is- when I use request.getEntity() it returns a null object, even though when I look through HTTPFox on what kind of request I'm sending the post data is clearly there.
What am I doing wrong?
There seems to be some confusion. You are sending requests from a browser to the server. The server is likely using the servlet API. There you handle requests using the doPost(..) method of an HttpServlet. You have an HttpServletRequest from which you can get the parameters - request.getParameter("paramName")
HttpCore on the other hand is used to make requests, not to handle requests. It is used as an http client (in the role of the browser).

Call an external web service from a servlet

I'm developing a servlet that gets a name of a web service and could be forward the request to an external web service, for example: http://www.webservice.com/...
I have build a response wrapper that intercept response output but I can't forward request to an external web service, it works only if I redirect the request to a servlet that is on same server.
Example:
request.getRequestDispatcher("aMyServlet").forward(request, response) // WORKS
request.getRequestDispatcher("http://www.webservice.com/...").forward(request, response)
Doesn't because Tomcat searches http://www.webservice.com/... on the server as a local resource.
How can I do external request?
Thanks
forward method that you are using is used for communicating between server resources, (eg: servlet to servlet as you have found out) If you want to redirect to another location, you can use the HttpServletResponse's sendRedirect method.
Better option is to
Perform your own HTTP request and stream the results back to the
browser. This sounds harder than it is. Basically you create a
java.net.HttpURLConnection with the URL of the web site you want to
"redirect" to. This can actually contain the query parameters (as long as
they're not too large) since it will never be sent to the user's browser
either and will not appear in the browser URL bar. Open the connection, get
the content and write it to the Servlet's OutputStream.
To make any request to an outside service, you'll have to explicitly make a new HTTP request and handle its response. Take a look at the HttpUrlConnection class.
You don't mention what kind of service you want to invoke, but either way, your servlet is functioning as a service client, so you should be looking at service client technologies.
For invoking REST style services, java.net.URL or Apache Commons HttpClient can be used to send a request for a URL and fetch the reponse.
For invoking SOAP services, you can use Apache Axis, or Java WSIT.

Categories

Resources