Skip to content
Advertisement

How to get URI of current request?

I need to resolve URI of current request in Quarkus (I use Quarkus 1.13). I use RESTEasy as my rest framework. Current request URI is needed in my case to validate Mandrill webhook call.

Simplified my current implementation looks like:

@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Path("/api/v1/webooks/mandrill")
public class MandrillWebhookResource {

    // code skipped for brevity

    @POST
    public Response mandrill(
            @HeaderParam("x-mandrill-signature") String mandrillSignature,
            @FormParam("mandrill_events") String mandrillEvents
    ) throws JsonProcessingException {
        final var mandrillSignatureValid = mandrillSignatureValidator
          .valid(/* here I need request URI */, mandrillSignature, mandrillEvents);

        // code skipped for brevity

        return Response.ok().build();
    }

Advertisement

Answer

RESTEasy is implementation of JAX-RS and it provides facilities for obtaining and processing information about the context of individual requests.

JAX-RS has UriInfo class for that context. It provides both static and dynamic, per-request information, about the components of a request URI. You just need to use @Context annotation with UriInfo class to inject that context into your POST method.

It could be done like below:

@POST
public Response mandrill(
        @Context UriInfo uriInfo,
        @HeaderParam("x-mandrill-signature") String mandrillSignature,
        @FormParam("mandrill_events") String mandrillEvents
) throws JsonProcessingException {
    final var requestUri = uriInfo.getRequestUri();

    final var mandrillSignatureValid = mandrillSignatureValidator
      .valid(requestUri, mandrillSignature, mandrillEvents);

    // code skipped for brevity

    return Response.ok().build();
}
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement