Search code examples
javaionic-frameworkcookiesjax-rsangular-httpclient

JAX-RS @CookieParam Ionic client not sending cookies (Capacitor)


I'm sending a cookie as parameter in headers with the annotation @CookieParam.

First step : register/start

    @GET
    @Path("/start")
    @Produces(MediaType.APPLICATION_JSON)
    @NoCache
    public Response start() {
        try {
            String myIdentifier = "abcdefghijkl123456789";

            LOGGER.info(String.format("My identifier: %s ", myIdentifier)); 

            // the identifier is send through the cookies in headers
            NewCookie cookie = new NewCookie("myIdentifierCookie", myIdentifier, null, null, null,
                    1200, false, false);
            return Response.ok().cookie(cookie).build();
        }catch (Exception e) {
            return Response.status(500).build();
        }
    }

From now it's good. When I do the GET, I can read my cookie on Response Headers. Now I'm doing a POST method, to check the email received, that has @CookieParam annotation.

    @POST
    @Path("/email")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @NoCache
    public Response emailCheck(@CookieParam("myIdentifierCookie") Cookie identifierCookie,
                                  CheckEmailRequest request) {

        String identifier = "";
        try {
            identifier = identifierCookie.getValue();
        } catch (Exception e) {
            LOGGER.error("exception e ", e);
            LOGGER.error(String.format("Invalid  request identifier: %s ", identifier));
            return Response.status(500).build();
        }
    }

And my cookie value is empty and I'm receiving :

(executor-thread-62) exception e : java.lang.NullPointerException
 at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:564)
        at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:151)
        at org.jboss.resteasy.core.MethodInjectorImpl.lambda$invoke$3(MethodInjectorImpl.java:122)
        at java.base/java.util.concurrent.CompletableFuture.uniApplyNow(CompletableFuture.java:680)
        at java.base/java.util.concurrent.CompletableFuture.uniApplyStage(CompletableFuture.java:658)
        at java.base/java.util.concurrent.CompletableFuture.thenApply(CompletableFuture.java:2158)
        at java.base/java.util.concurrent.CompletableFuture.thenApply(CompletableFuture.java:143)
        at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:122)
        at org.jboss.resteasy.core.ResourceMethodInvoker.internalInvokeOnTarget(ResourceMethodInvoker.java:594)
        at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTargetAfterFilter(ResourceMethodInvoker.java:468)
        at org.jboss.resteasy.core.ResourceMethodInvoker.lambda$invokeOnTarget$2(ResourceMethodInvoker.java:421)
        at org.jboss.resteasy.core.interception.jaxrs.PreMatchContainerRequestContext.filter(PreMatchContainerRequestContext.java:363)
        at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTarget(ResourceMethodInvoker.java:423)
        at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:391)
        at org.jboss.resteasy.core.ResourceMethodInvoker.lambda$invoke$1(ResourceMethodInvoker.java:365)
        at java.base/java.util.concurrent.CompletableFuture.uniComposeStage(CompletableFuture.java:1183)
        at java.base/java.util.concurrent.CompletableFuture.thenCompose(CompletableFuture.java:2299)
        at java.base/java.util.concurrent.CompletableFuture.thenCompose(CompletableFuture.java:143)
        at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:365)
        at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:477)
        at org.jboss.resteasy.core.SynchronousDispatcher.lambda$invoke$4(SynchronousDispatcher.java:252)
        at org.jboss.resteasy.core.SynchronousDispatcher.lambda$preprocess$0(SynchronousDispatcher.java:153)
        at org.jboss.resteasy.core.interception.jaxrs.PreMatchContainerRequestContext.filter(PreMatchContainerRequestContext.java:363)
        at org.jboss.resteasy.core.SynchronousDispatcher.preprocess(SynchronousDispatcher.java:156)
        at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:238)
        at io.quarkus.resteasy.runtime.standalone.RequestDispatcher.service(RequestDispatcher.java:73)
        at io.quarkus.resteasy.runtime.standalone.VertxRequestHandler.dispatch(VertxRequestHandler.java:120)
        at io.quarkus.resteasy.runtime.standalone.VertxRequestHandler.access$000(VertxRequestHandler.java:36)
        at io.quarkus.resteasy.runtime.standalone.VertxRequestHandler$1.run(VertxRequestHandler.java:85)
        at io.quarkus.runtime.CleanableExecutor$CleaningRunnable.run(CleanableExecutor.java:224)
        at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
        at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
        at org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
        at org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:2011)
        at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1535)
        at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1426)
        at org.jboss.threads.DelegatingRunnable.run(DelegatingRunnable.java:29)
        at org.jboss.threads.ThreadLocalResettingRunnable.run(ThreadLocalResettingRunnable.java:29)
        at java.base/java.lang.Thread.run(Thread.java:832)
        at org.jboss.threads.JBossThread.run(JBossThread.java:479)


Is my implementation wrong?
Thank you very much.

EDIT : @Paul Samsotha

Which client am I using?

I'm using Ionic with Angular, and my plugin is the angular Http module from @angular/common/http. I have a backend service defined like that :

  get(url, options?): Observable<any> {
    options = { ...options, withCredentials: true };
    return this.http.get(myURL, options);
  }

  post(url, body, options?): Observable<any> {
    options = { ...options, withCredentials: true };
    return this.http.post(myURL, body, options);
  }

So on my component i'm using it like this :

  public getRegistrationStart() {
    return this.backendService.get('/register/start', {
      observe: 'response',
    });
  }

  public emailStep(email) {
    const headers = new HttpHeaders().set('Content-Type', 'application/json');
    return this.backendService.postPublic('/register/email', email, {
      observe: 'response',
      headers,
    });
  }

My email is send as a JSON like {"email":"test@test.test"}

EDIT 2 : @Paul

Here a screen of my start request, the server responds with a Set-Cookie containing my cookie (I've set up a random number generator), but my cookie is here.

enter image description here


Solution

  • Problem was not JAX RS but Ionic Capacitor which blocked cookies. Problem solved by adding this line in capacitor.config.json :

    "server":{"hostname":"mydomain"}
    

    Cookie was not intercepted in Android, it was intercepted in iOS, so adding this line in the capacitor.config.json only in the iOS build file solved my problem.

    Huge thanks to Paul Samsotha who showed me the path to the solution.