So I wanted to know my web service's client's locale or ip etc.. How do I get it?
My endpoint method:
@POST
@Produces({MediaType.APPLICATION_JSON})
@Consumes({MediaType.APPLICATION_JSON})
@Path("/{EmployeeID}/Shifts/{ShiftID}/Confirm")
public Response confirmShift(@PathParam("EmployeeID")String employeeId, String params, @PathParam("ShiftID")String tbId);
How I get it in interceptor:
Map<String, List> headers = (Map<String, List>) message.get(Message.PROTOCOL_HEADERS);
I think protocol header must contain this info, I havn't checked it by the way. But how do I get it in web service.
Note: I want to avoid getting/setting stuff in cxf request context.
You need to inject MessageContext into your method, which contains HTTP servlet request.
For e.g.:
@POST
@Produces({MediaType.APPLICATION_JSON})
@Consumes({MediaType.APPLICATION_JSON})
@Path("/{EmployeeID}/Shifts/{ShiftID}/Confirm")
public Response confirmShift(@PathParam("EmployeeID") String employeeId,
String params,
@PathParam("ShiftID") String tbId,
@Context MessageContext context){
HttpServletRequest request = context.getHttpServletRequest();
String ip = request.getRemoteAddr();
/** ..... **/
}
Also there are some other ways of getting HTTP servlet request, one would be:
Message message = PhaseInterceptorChain.getCurrentMessage();
HttpServletRequest httpRequest = (HttpServletRequest) message.get(AbstractHTTPDestination.HTTP_REQUEST);
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With