0

A bit of newbie on Webservices, but here's a tricky one I'm struggling with finding a better way to implement. See code below: instead of having to invoke the setClientIPAddress method on each webservice method, is there a way of doing this just once ? i.e. I tried the following:

// initialisation block
{
   WSBean wsBean = new WSBean();
   wsBean.setClientIPAddress(getClientIPAdd);

}

this compiles ok but I get a runtime error. Webservice class doesn't seem to like the initialisation block.

@javax.jws.WebService(targetNamespace = "http://baseentity.com/", serviceName = "WSBeanService", portName = "WSBeanPort", wsdlLocation = "WEB-INF/wsdl/WSBeanService.wsdl")
public class WSBeanDelegate {

    WSBean wsBean = new WSBean();

    public String getBaseInfoList(String baseID) {
      wsBean.setClientIPAddress(getClientIPAdd); // 
        return wsBean.getBaseInfoList(transactionID);
    }

    public String getBaseEntityInfo(String entityID) {
      wsBean.setClientIPAddress(getClientIPAdd);
        return wsBean.getBaseEntityInfo(entityID);
    }

    @WebMethod 
      private String getClientIPAdd()
      {
        MessageContext mc = this.wsContext.getMessageContext();

        HttpServletRequest req = (HttpServletRequest)mc.get("javax.xml.ws.servlet.request");
        return req.getRemoteAddr();
      }

I've tried using @PostContruct, as shown below:

 @PostContruct
        private void init()
        {
              wsBean.setClientIPAddress(getClientIPAdd);
        }

but i get the following error: "illegalaccessexception with modifiers private".

However, declaring the method as public also requires defining the same method in the bean/wsdl file, which is not i want do do. Any suggestions on how to better this code?

Thanks in advance.

Jonathan W
  • 3,759
  • 19
  • 20
stack
  • 115
  • 2
  • 6
  • 19
  • Can't you implement this outside in another Class? and then a static variable clientAddressIP in there must be set only if it is null.... You can set it such that upon getClientAddressIP, check if it is null, then set it and return... otherwise return clientIPAddress. This is a good case of a Singleton Class that will contain your static String clientIPAddress.. and other such variables – mhan May 28 '12 at 10:57
  • Hi Nav, tried a couple of things around using a static variable and that didnt work. – stack May 29 '12 at 13:57
  • what happened when you tried the static variable? – mhan May 30 '12 at 08:51
  • Add the static failure details to your question above. Will further help all, trying in resolving the issue – mhan May 30 '12 at 08:58
  • This looks like a product issue to me. PostConstruct should be usable on non-public methods. – Brett Kail Jun 03 '12 at 14:53

1 Answers1

1

Try:

@PostContruct
@javax.jws.WebMethod(exclude=true)
public void init()
{
    wsBean.setClientIPAddress(getClientIPAdd);
}
Jonathan W
  • 3,759
  • 19
  • 20