HTTPServletRequest equivalent in Apache CXF - java

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.

Related

How to implement squer brackets parameter with Jax-RS client? [duplicate]

I am building a generic web service and need to grab all the query parameters into one string for later parsing. How can I do this?
You can access a single param via #QueryParam("name") or all of the params via the context:
#POST
public Response postSomething(#QueryParam("name") String name, #Context UriInfo uriInfo, String content) {
MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters();
String nameParam = queryParams.getFirst("name");
}
The key is the #Context jax-rs annotation, which can be used to access:
UriInfo, Request, HttpHeaders,
SecurityContext, Providers
The unparsed query part of the request URI can be obtained from the UriInfo object:
#GET
public Representation get(#Context UriInfo uriInfo) {
String query = uriInfo.getRequestUri().getQuery();
...
}
Adding a bit more to the accepted answer. It is also possible to get all the query parameters in the following way without adding an additional parameter to the method which maybe useful when maintaining swagger documentation.
#Context
private UriInfo uriInfo;
#POST
public Response postSomething(#QueryParam("name") String name) {
MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters();
String nameParam = queryParams.getFirst("name");
}
ref

How to get http request and query string params in the controller

I'm new in the play framework. I'm using play 2.8.x framework and I need to get from the controller session object and params from request. But I don't realize how to do that.
My routes file looks like the following:
POST /api/verifyToken/:token controllers.UserController.verifyToken(token: String, request: Request)
and my controller looks like this:
public class UserController extends Controller {
public Result verifyToken(String token, Http.Request request) {
...
}
}
and when I try to send a request to the server I had had an error but if I remove token parameter all is working fine. How can I pass the request and params to the controller?
Your handler is given the Http.Request when it is called:
java.util.Map<java.lang.String,java.lang.String[]> queryParams = request.queryString();
for the session:
Http.Session session = request.session();

JAX-RS #Context HttpHeaders always null

I have a Spring Boot application using jax-rs with resteasy (3.0.24). I'm trying to get the HttpHeaders for a request as such:
#DELETE
#Path("/myendpoint")
public Response myMethod(#Context HttpHeaders headers, #Context HttpServletRequest request) {
// headers is always null
}
The headers param is always null even though I'm making the request with multiple headers. As an alternative, I'm extracting them via the HttpServletRequest.getHeaderNames(), but I'd really like know why headers is not populated.
Found the (embarrassing, although I deflect the blame to the author:)) error. #Context HttpHeaders headers was using Spring's implementation and not that from jax-rs.
You gotta get the headers using the #Context then check if the one one you want is there.
#Path("/users")
public class UserService {
#GET
#Path("/get")
public Response addUser(#Context HttpHeaders headers) {
String userAgent = headers.getRequestHeader("user-agent").get(0);
return Response.status(200)
.entity("addUser is called, userAgent : " + userAgent)
.build();
}
}

jersey. why doesn't parameter pass?

I have following jersey method:
#POST
#Path("path")
#Produces({MediaType.APPLICATION_JSON})
public Response isSellableOnline(#QueryParam("productCodes") final List<String> productCodes,
#QueryParam("storeName") final String storeName,
#Context HttpServletRequest request) {
System.out.println(storeName);
System.out.println(productCodes.size());
...
}
in rest client I sends following data:
in console I see
null
0
What do I wrong?
You're reading the parameters from query string, which go in the form:
http://yourserver/your/service?param1=foo&param2=bar
^ start of query string
But you're sending the parameters as part of the form.
Change the way you consume the parameters in your service:
#POST
#Path("path")
#Produces({MediaType.APPLICATION_JSON})
public Response isSellableOnline(#FormParam("productCodes") final List<String> productCodes,
#FormParam("storeName") final String storeName,
#Context HttpServletRequest request) {
System.out.println(storeName);
System.out.println(productCodes.size());
...
}

How can I grab all query parameters in Jersey JaxRS?

I am building a generic web service and need to grab all the query parameters into one string for later parsing. How can I do this?
You can access a single param via #QueryParam("name") or all of the params via the context:
#POST
public Response postSomething(#QueryParam("name") String name, #Context UriInfo uriInfo, String content) {
MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters();
String nameParam = queryParams.getFirst("name");
}
The key is the #Context jax-rs annotation, which can be used to access:
UriInfo, Request, HttpHeaders,
SecurityContext, Providers
The unparsed query part of the request URI can be obtained from the UriInfo object:
#GET
public Representation get(#Context UriInfo uriInfo) {
String query = uriInfo.getRequestUri().getQuery();
...
}
Adding a bit more to the accepted answer. It is also possible to get all the query parameters in the following way without adding an additional parameter to the method which maybe useful when maintaining swagger documentation.
#Context
private UriInfo uriInfo;
#POST
public Response postSomething(#QueryParam("name") String name) {
MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters();
String nameParam = queryParams.getFirst("name");
}
ref

Categories