I'm looking for a way to pass a map that contains param names and values to a GET Web Target. I am expecting RESTEasy to convert my map to a list of URL query params; however, RESTEasy throws an exception saying Caused by: javax.ws.rs.ProcessingException: RESTEASY004565: A GET request cannot have a body.
. How can I tell RESTEasy to convert this map to a URL query parameters?
This is the proxy interface:
#Path("/")
#Consumes(MediaType.APPLICATION_JSON)
public interface ExampleClient {
#GET
#Path("/example/{name}")
#Produces(MediaType.APPLICATION_JSON)
Object getObject(#PathParam("name") String name, MultivaluedMap<String, String> multiValueMap);
}
This is the usage:
#Controller
public class ExampleController {
#Inject
ExampleClient exampleClient; // injected correctly by spring DI
// this runs inside a spring controller
public String action(String objectName) {
MultivaluedMap<String, String> params = new MultivaluedHashMap<>();
// in the real code I get the params and values from a DB
params.add("foo", "bar")
params.add("jar", "car")
//.. keep adding
exampleClient.getObject(objectName, params); // throws exception
}
}
After hours digging down in RESTEasy source code, I found out that there is no way to do that though interface annotation. In short, RESTEasy creates something called a 'processor' from org.jboss.resteasy.client.jaxrs.internal.proxy.processors.ProcessorFactory to map the annotation to the target URI.
However, it is really simple to solve this issue by creating a ClientRequestFilter that takes the Map from the request body (before executing the request of course), and place them inside the URI query param. Check the code below:
The filter:
#Provider
#Component // because I'm using spring boot
public class GetMessageBodyFilter implements ClientRequestFilter {
#Override
public void filter(ClientRequestContext requestContext) throws IOException {
if (requestContext.getEntity() instanceof Map && requestContext.getMethod().equals(HttpMethod.GET)) {
UriBuilder uriBuilder = UriBuilder.fromUri(requestContext.getUri());
Map allParam = (Map)requestContext.getEntity();
for (Object key : allParam.keySet()) {
uriBuilder.queryParam(key.toString(), allParam.get(key));
}
requestContext.setUri(uriBuilder.build());
requestContext.setEntity(null);
}
}
}
PS: I have used Map instead of MultivaluedMap for simplicity
Related
Before everything I tried this two solution but didn't work for me
Equivalent of javax.ws.rs NameBinding in Micronaut?
https://blogs.ashrithgn.com/custom-annotation-to-handle-authorisation-in-micronaut-aop-tutorial/
In my application I have to get a string in the Authorization header and then decode it from base64 and the json transform it into a POJO. Certainly the string is a jwt and I need to decode the public part of the json to get a data from a field.
Technically speaking a client will forward the header to me to take it, decode it and extract the data. (It's very bad practice but that's what I have to do).
For this I am using micronaut 2.4.1 and this is my code:
Interceptor:
public class HeadInterceptor implements MethodInterceptor<Object, Object> {
#SneakyThrows
#Override
public Object intercept(MethodInvocationContext<Object, Object> context) {
Request request = (Request) context.getParameterValueMap().get("request");
// Where do i get Authorization header?
// i.e String token = (String) context.getParameterValueMap().get("Authorization");
String token = "eyJhdWQiOiJ0ZXN0IiwiaXNzIjoidGVzdCIsInN1YiI6InRlc3QiLCJleHAiOjExMTExMTEsImlhdCI6MTExMTExMTEsImRhdGEiOiJ0ZXN0In0=";
ObjectMapper mapper = new ObjectMapper();
Info info = mapper.readValue(new String(Base64.getDecoder().decode(token)), Info.class);
request.setData(info.getSub().toUpperCase());
return context.proceed();
}
}
Controller:
#Controller("/main")
public class MainController {
#Post
#Head
public Single<Response> index(#Body #Valid Request request) {
return Single.just(
Response.builder()
.message(String.format("%s-%s", request.getData(), request.getInfo()))
.build()
);
}
}
Here's a sample app https://github.com/j1cs/micronaut-jacksonxml-error
(ignore the name is for other issue)
In your implementation, the header cannot be shown in the interceptor because your index method doesn't receive it as a parameter.
So, if you add it as a parameter as below:
...
#Post
#Head
public Single<Response> index(#Body #Valid Request request, #Header("Authorization") String authorizationHeader) {
return Single.just(
Response.builder()
.message(String.format("%s-%s", request.getData(), request.getInfo()))
.build()
);
}
...
Then, you can retrieve it in the intercept method via getParameterValues(). Basically, it will be the second argument.
...
#SneakyThrows
#Override
public Object intercept(MethodInvocationContext<Object, Object> context) {
...
String token = (String) context.getParameterValues()[1];
...
}
...
Update
Since you want your Request to contain both body and header, I edited the solution a bit. Basically, the header is added as a member variable to Request as below:
public class Request {
#NotNull
#NotBlank
private String info;
private String data;
#Header("Authorization")
String authorizationHeader;
}
Then, use #RequestBean rather than a #Body annotation on your Request parameter:
...
#Post
#Head
public Single<Response> index(#RequestBean #Valid Request request) {
return Single.just(
Response.builder()
.message(String.format("%s-%s", request.getData(), request.getInfo()))
.build()
);
}
...
Finally, you can access the header easily in your intercept() method as follows:
#SneakyThrows
#Override
public Object intercept(MethodInvocationContext<Object, Object> context) {
...
Request request = (Request) context.getParameterValueMap().get("request");
String token = request.authorizationHeader;
...
}
I created a pull request for this change here, so you can check how it works.
In order to address the problem, you may first break the problem into parts.
Part 1: How to get arbitrary header (or list all headers)?
Try to use request.getHeaders() doc.
Part 2: How to get the header named Authorization ?
Use the way in part 1. In addition, be careful about the case. For example, is Authorization the same as authorization?
Method 2:
In controller (https://github.com/j1cs/micronaut-jacksonxml-error/blob/master/src/main/java/me/jics/MainController.java):
public Single<Response> index(#Body Request request, #Header('Authorization') String authorization) {
...
}
p.s. the "Header" annotation's doc is here: https://docs.micronaut.io/2.0.1/api/io/micronaut/http/annotation/Header.html
In interceptor:
...
String token = context.getParameterValueMap().get("authorization");
...
Why the code looks like this:
Firstly get the auth header you want using parameter injection.
Secondly, recall the fundamental concepts of AOP / AspectJ (which your interceptor class uses). Inside your interceptor, you intercept a method (in your case, the index method in controller. Thus, you can happily get the parameters of that method. In the code above, just the authorization parameter.
Please tell me if you are stuck on somewhere (and paste the code and the outputs).
getParameterMap() of HttpServletRequest returns both the query params and posted form data.
I am able to fetch the query parameters alone from UriInfo.getQueryParameters().
But I need the form parameters separately as a MultivaluedMap similar to query parameters, is there a way to fetch it?
EDITED:
I apologize for not making this clear. I am trying to fetch the form parameters in the filter/interceptor.
You can put the MultivaluedMap as a parameter in the resource method. This will be the body of the request. JAX-RS will put all the parameters in the map for you.
#POST
#Consumes("application/x-www-form-urlencoded")
public Response post(#Context UriInfo uriInfo, MultivaluedMap params) {}
UPDATE (to edited post)
So if you want to get the parameters in a filter, you can get the body from the ContainerRequestContext. With Jersey, instead of getting the InputStream with context.getEntityStream(), you can cast the ContainerRequestContext to Jersey's ContainerRequest implementation. This will give you access to the methods bufferEntity() and readEntity(). These methods will allow you do easily get the form parameters. You will need to buffer the entity so that it can be read later when it needs to be passed on to your resource method.
#Provider
public class MyFilter implements ContainerRequestFilter {
#Override
public void filter(ContainerRequestContext context) throws IOException {
if (MediaTypes.typeEqual(MediaType.APPLICATION_FORM_URLENCODED_TYPE, context.getMediaType())) { {
return;
}
ContainerRequest request = (ContainerRequest) context;
request.bufferEntity();
Form form = request.readEntity(Form.class);
MultivaluedMap params<String, String> = form.asMap();
MultivaluedMap<String, String> query = context.getUriInfo().getQueryParameters();
}
}
If you want to use the filter only with specific resource methods, then you can use Name Binding or Dynamic Binding.
If for some reason the readEntity() returns an empty map (I've seen rare occurrences of people having this problem), you can try to retrieve the Form through an internal property
Object formProperty = request.getProperty(InternalServerProperties.FORM_DECODED_PROPERTY);
if (formProperty != null) {
Form for = (Form) formProperty;
}
I have a Spring 4.3.3 #RestController which manages an entity type via Lists.
#RestController
#RequestMapping("...")
public class EntityRestController {
#PostMapping
public void doSomeWork(#RequestBody final List<Entity> entities) { ... }
}
I discovered sometimes I may receive a request where the body consists not of an array, but a single JSON object.
I'm using Gson as the default serializer/deserializer and obviously it throws an exception.
JSON parse error: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT
What would the better way to tackle this problem be (at the Controller level)?
You could use HttpServletRequest in method parameter, like:
#RestController
#RequestMapping("...")
public class EntityRestController {
#PostMapping
public void doSomeWork(HttpServletRequest request) {
// Do your things here..
}
}
And this question has many answers how to parse request body within HttpServletRequest:
Use apache commons-IO : IOUtils.toString(request.getReader()).
Use request.getParameterMap() :
Map<String, String[]> map = request.getParameterMap();
for(String paramName : map.keySet()) {
String[] paramValues = map.get(paramName);
for(String valueOfParam : paramValues) {
// Do your things..
}
}
Or other techniques based on your need and preferences.
HTH
Since I'm using the CQRS pattern, I'm trying to create a single controller method that accepts every POST call with a command in its request body and send it.
I'm almost there, but I can't get the path variables.
I created a custom HandlerMapping
#Bean
public HandlerMapping requestMappingHandlerMapping() throws NoSuchMethodException {
for (final UrlEnum urlEnumItem : UrlEnum.values()) {
requestMappingHandlerMapping.registerMapping(new RequestMappingInfo(urlEnumItem.getCommandName(),
new PatternsRequestCondition(urlEnumItem.getUrl()),
null,
null,
null,
null,
null,
null),
commandController,
commandController.getClass().getDeclaredMethod("commandHandler", HttpServletRequest.class)
);
}
return requestMappingHandlerMapping;
}
and this is my controller method signature
#RequestMapping(method = {RequestMethod.POST, RequestMethod.PUT}, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public ResponseEntity<Object> commandHandler(final HttpServletRequest request) throws Exception {
// controller code here
}
If the url path is something like /api/test it works, but with something like /api/test/{idEntity} I don't have any PathVariable available in the request.
I tried everything like
String originalUrl = (String) request.getAttribute(
HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
which returns the valued url (i.e. /api/test/1234), not the template, or adding
#PathVariable Map<String, Object> parameters
as a parameter in the method, which is empty.
Debugging the request object it seems there isn't anything useful to identify the path variables.
Maybe I should interrogate the HandlerMapping, but I can't have access to it in the controller method.
Is there a way to extract the pathVariables in the controller method?
It was an error in the configuration. I shouldn't have added the RequestMapping annotation to the controller method because it overrode my configuration.
Now I have
#RestController
public class CommandController extends AbstractController {
private final MappingJackson2JsonView mappingJackson2JsonView = new MappingJackson2JsonView();
#Override
protected ModelAndView handleRequestInternal(final HttpServletRequest request, final HttpServletResponse response) throws Exception {
// controller code here
return new ModelAndView(mappingJackson2JsonView);
}
}
The Jersey docs give an example of how to inject HttpSession on resources. How should I inject (or otherwise gain access to) form parameters sent on requests with "Content-Type: application/x-www-form-urlencoded"? I see that these are passed as parameters on methods, and do not seem to be annotated, letting me believe there is some quirk here?
The (naive) factory I'm currently working with is implemented as follows, JerseyHttpServletRequestWrapper being one of my own classes:
import org.glassfish.hk2.api.Factory;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.MultivaluedMap;
public class JerseyHttpServletRequestWrapperFactory implements Factory<JerseyHttpServletRequestWrapper> {
private final HttpServletRequest request;
private final MultivaluedMap<String, String> formParams;
#Inject
public JerseyHttpServletRequestWrapperFactory(HttpServletRequest request, MultivaluedMap<String, String> formParams) {
this.request = request;
this.formParams = formParams;
}
#Override
public JerseyHttpServletRequestWrapper provide() {
return new JerseyHttpServletRequestWrapper(request, formParams);
}
#Override
public void dispose(JerseyHttpServletRequestWrapper jerseyHttpServletRequestWrapper) {
}
}
I'm thinking here that an entity provider should be injected into the instance so that I can check if there is actually an entity sent with the request. Trying to directly inject MultivaluedMap errors out with:
org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=MultivaluedMap<String,String>,parent=JerseyHttpServletRequestWrapperFactory,qualifiers={},position=1,optional=false,self=false,unqualified=null,2067821943)
When you do
#POST
public Response post(MultivaluedMap<String, String> params) {}
this method parameter injection is handled differently than with regular field/constructor injection. So you can't try to inject the MultivaluedMap into a field.
What you can do though is inject the ContainerRequest, and then read the body. You'll want to check that it's a POST request and the Content-Type is application/x-www-form-urlencoded. If you don't, there's a chance you will get an exception when you try to read the entity.
#Inject
ContainerRequest request;
if (request.getMethod().toUpperCase().equals("POST")
&& request.getMediaType().equals(MediaType.APPLICATION_FORM_URLENCODED_TYPE)) {
request.bufferEntity();
Form form = request.readEntity(Form.class);
MultivaluedMap<String, String> params = form.asMap();
}