Version 4

    Searchable Wiki Title: How can I access the HttpServletRequest and HttpServletReponse from within my endpoint?

     

    By implementing the 'javax.xml.rpc.server.ServiceLifecycle' interface the endpoint will have a 'javax.xml.rpc.server.ServletEndpointContext' object passed into the init method before the business method of the endpoint is invoked.

     

    The 'javax.xml.rpc.server.ServletEndpointContext' can be used to get the 'javax.xml.rpc.handler.MessageContext' which has a getProperty method that can be used to get certain properties.

     

    The following properties are available by default with JBossWS: -

     

    • javax.xml.ws.servlet.context for javax.servlet.ServletContext

    • javax.xml.ws.servlet.request for javax.servlet.http.HttpServletRequest

    • javax.xml.ws.servlet.response for javax.servlet.http.HttpServletResponse

    • javax.xml.ws.servlet.session for javax.servlet.http.HttpSession

     

    See the following endpoint example: -

     

    
    package org.jboss.support.ws.phonebook;
    
    import java.rmi.RemoteException;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.xml.rpc.ServiceException;
    import javax.xml.rpc.handler.MessageContext;
    import javax.xml.rpc.server.ServiceLifecycle;
    import javax.xml.rpc.server.ServletEndpointContext;
    
    public class PhoneBookImpl implements PhoneBook, ServiceLifecycle
    {
    
       private ServletEndpointContext ctx;
    
       public void init(final Object ctx) throws ServiceException
       {
          if (ctx instanceof ServletEndpointContext)
          {
             this.ctx = (ServletEndpointContext) ctx;
          }
          else
          {
             throw new ServiceException("Object passed into init is not a 'ServletEndpointContext'");
          }
       }
    
       public void destroy()
       {
    
       }
    
       public TelephoneNumber lookup(Person person) throws RemoteException
       {
          MessageContext msgCtx = ctx.getMessageContext();
          HttpServletRequest request = (HttpServletRequest) msgCtx.getProperty("javax.xml.ws.servlet.request");
          HttpServletResponse response = (HttpServletResponse) msgCtx.getProperty("javax.xml.ws.servlet.response");
    
          System.out.println("Called from - " + request.getRemoteAddr());
    
          TelephoneNumber tn = new TelephoneNumber();
          tn.setAreaCode("01234");
          tn.setNumber("567890");
    
          return tn;
       }
    
    }