简体   繁体   中英

HTTPServletRequest equivalent in Apache CXF

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 eg:

@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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM