Sometimes we need to obtain the request object and the response object in the webservice. For example, we need to do this when we want to obtain the client's access ip. Here are three ways. Of course, the three ways may be to deploy the webservice in different ways to obtain the request object. Methods.
The first: first configure the injection:
@Resource private WebServiceContext webServiceContext;
followed by the following code:
MessageContext mc = webServiceContext.getMessageContext(); HttpServletRequest request = (HttpServletRequest) (mc.get(MessageContext.SERVLET_REQUEST));
The second:
WebServiceContext context = new WebServiceContextImpl(); MessageContext ctx = context.getMessageContext(); HttpServletRequest request = (HttpServletRequest) ctx.get(AbstractHTTPDestination.HTTP_REQUEST);
The third (with a method to get the client ip address):
Message message = PhaseInterceptorChain.getCurrentMessage(); HttpServletRequest request = (HttpServletRequest) message.get(AbstractHTTPDestination.HTTP_REQUEST);
get ip:
public static String getIpAddr(HttpServletRequest request) { String ip = request.getHeader("x-forwarded-for"); if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("Proxy-Client-IP"); } if (ip == null || ip.length() == 0 || "unknow".equalsIgnoreCase(ip)) { ip = request.getHeader("WL-Proxy-Client-IP"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getRemoteAddr(); } return ip; }
Sometimes we need to obtain the request object and the response object in the webservice. For example, we need to do this when we want to obtain the client's access ip. Here are three ways. Of course, the three ways may be to deploy the webservice in different ways to obtain the request object. Methods.
The first: first configure the injection:
@Resource private WebServiceContext webServiceContext;
followed by the following code:
MessageContext mc = webServiceContext.getMessageContext(); HttpServletRequest request = (HttpServletRequest) (mc.get(MessageContext.SERVLET_REQUEST));
The second:
WebServiceContext context = new WebServiceContextImpl(); MessageContext ctx = context.getMessageContext(); HttpServletRequest request = (HttpServletRequest) ctx.get(AbstractHTTPDestination.HTTP_REQUEST);
The third (with a method to get the client ip address):
Message message = PhaseInterceptorChain.getCurrentMessage(); HttpServletRequest request = (HttpServletRequest) message.get(AbstractHTTPDestination.HTTP_REQUEST);
get ip:
public static String getIpAddr(HttpServletRequest request) { String ip = request.getHeader("x-forwarded-for"); if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("Proxy-Client-IP"); } if (ip == null || ip.length() == 0 || "unknow".equalsIgnoreCase(ip)) { ip = request.getHeader("WL-Proxy-Client-IP"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getRemoteAddr(); } return ip; }