Announcement Announcement Module
Collapse
No announcement yet.
Access cookie from HttpInvokerServiceExporter Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Access cookie from HttpInvokerServiceExporter

    This is probably very simple as I haven't seen it discussed anywhere but I'm curious how to access cookies from inside a HttpInvokerServiceExporter bean. I have the bean defined as:

    <bean name="/myService" class="org.springframework.remoting.httpinvoker.Ht tpInvokerServiceExporter">
    <property name="service">
    <ref bean="myService"/>
    </property>
    <property name="serviceInterface">
    <value>com.test.MyService</value>
    </property>
    </bean>

    I am then calling the service from a JWS app and have the following client configuration.

    <bean id="httpInvokerRequestExecutor" class="org.springframework.remoting.httpinvoker.Co mmonsHttpInvokerRequestExecutor"/>

    <bean id="myService" class="org.springframework.remoting.httpinvoker.Ht tpInvokerProxyFactoryBean">
    <property name="serviceUrl" value="https://localhost:8443/HTTPInvokerServer/invoker/myService"/>
    <property name="serviceInterface" value="com.test.MyService"/>
    <property name="httpInvokerRequestExecutor">
    <ref bean="httpInvokerRequestExecutor"/>
    </property>
    </bean>

    I have implemented MyService as follows (obviously not really doing anything useful, just a text scenario).

    public class MyServiceImpl implements MyService
    {
    public Object1 getObject1() {
    Object1 o1 = new Object1();
    ArrayList tmpList = new ArrayList();

    o1.setName("Dave");

    Object2 o2;
    for (int x=0;x<4;x++){
    o2 = new Object2();
    o2.setName("dave" + x);
    o2.setO1(o1);
    tmpList.add(o2);
    }
    o1.setObject2s(tmpList);
    return o1;
    }
    }

    I would like to be able to access cookies from inside the getObject1 method of MyServiceImpl. I know I need to somehow access the HttpServletRequest to do this, but I don't know how to get access to it. Is there a simple solution to my problem? Thanks for reading.
    Dave

  • #2
    How I was able to access cookies in HttpInvoker service methods...

    I wanted to update this post to let people know how I was able to access cookies from inside HTTP invoker service calls. It required modifying the HttpInvokerServiceExporter that comes w/ Spring and overriding the handleRequest method to call a method to set the request and response on a base class that all my other services extend.

    Here is the overriden method in HttpInvokerRequestResponseServiceExporter.

    @Override
    public void handleRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    Class[] classes = {HttpServletRequest.class, HttpServletResponse.class};
    Object[] parameters = {request, response};
    RemoteInvocation setRequestAndResponseInvocation = new RemoteInvocation("setRequestAndResponse", classes, parameters);
    try {
    setRequestAndResponseInvocation.invoke(getProxy()) ;
    } catch (NoSuchMethodException ex) {
    throw new NestedServletException("Method setRequestAndResponse(HttpServletRequest request, HttpServletResponse response) not found.", ex);
    } catch (IllegalAccessException ex) {
    throw new NestedServletException("Illegal access exception.", ex);
    } catch (InvocationTargetException ex) {
    throw new NestedServletException("Invocation target exception", ex);
    }
    super.handleRequest(request,response);
    }

    Here is my base service interface.

    package com.easecap.ebxreports.service;

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    public interface Service {
    void setRequestAndResponse(HttpServletRequest request,HttpServletResponse response);
    }

    Here is my base service implementation.

    package com.easecap.ebxreports.server;

    import com.easecap.ebxreports.service.Service;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    public class ServiceImpl implements Service{
    HttpServletRequest request;
    HttpServletResponse response;

    public void setRequestAndResponse(HttpServletRequest request,HttpServletResponse response){
    this.request = request;
    this.response = response;
    }
    }

    I extend this interface and implementation for all my services that I want to be able to access cookies via request and response.

    The only issue that you need to be aware of is that the cookes get their age set to -1 regardless of what you try and set them to. This causes them to go away when your client ApplicationContext is destroyed. This means that the cookies are only passed to the server when you use the same ApplicationContext to make subsequent calls. For this reason I define the client ApplicationContext as a static variable in my JWS app and use that single instance of ApplicationContext for all my HTTPInvoker calls.

    Comment

    Working...
    X