Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTPServletRequest equivalent in Apache CXF

Tags:

java

spring

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.

like image 976
Sachin Verma Avatar asked Aug 11 '26 11:08

Sachin Verma


1 Answers

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.

like image 115
Paulius Matulionis Avatar answered Aug 13 '26 02:08

Paulius Matulionis