MRS
MRS

Reputation: 11

io.helidon.webserver.ServerResponse.send() throwing error on server but file downloaded on front end

Trying to write one sample for (file upload and download) error in server. Though it downloads the file in the frontend, getting error in server.

For Routing--

WebServer.builder(getRouting()).port(8080).build().start();
    
private static Routing getRouting() throws Exception{
        
        return Routing.builder().register("/filetest", JerseySupport.builder().register(FileController.class).build())
            .build();
    }


@RequestScoped
public class FileController {
     @Context
     ServerRequest req;
     @Context
     ServerResponse res;
    @GET
    @Path("/{fname}")
    public void download(@PathParam("fname") String fname) {
        
        try {
            //Getting the file 
            java.nio.file.Path filepath = Paths.get("c:/"+fname+".txt");
            ResponseHeaders headers = res.headers();
            headers.contentType(io.helidon.common.http.MediaType.APPLICATION_OCTET_STREAM);
            headers.put(Http.Header.CONTENT_DISPOSITION, ContentDisposition.builder()
                    .filename(filepath.getFileName().toString())
                    .build()
                    .toString());
           
           res.send(filepath);         
        }catch(Exception e) {
            
        }
}

Jul 29, 2021 6:20:36 PM io.helidon.webserver.RequestRouting$RoutedRequest defaultHandler WARNING: Default error handler: Unhandled exception encountered. java.util.concurrent.ExecutionException: Unhandled 'cause' of this exception encountered. at io.helidon.webserver.RequestRouting$RoutedRequest.defaultHandler(RequestRouting.java:397) at io.helidon.webserver.RequestRouting$RoutedRequest.nextNoCheck(RequestRouting.java:377) at io.helidon.webserver.RequestRouting$RoutedRequest.next(RequestRouting.java:420) at io.helidon.webserver.jersey.ResponseWriter.failure(ResponseWriter.java:133) at org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRuntime.java:438) at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:263) at org.glassfish.jersey.internal.Errors$1.call(Errors.java:248) at org.glassfish.jersey.internal.Errors$1.call(Errors.java:244) at org.glassfish.jersey.internal.Errors.process(Errors.java:292) at org.glassfish.jersey.internal.Errors.process(Errors.java:274) at org.glassfish.jersey.internal.Errors.process(Errors.java:244) at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:265) at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:234) at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:680) at io.helidon.webserver.jersey.JerseySupport$JerseyHandler.lambda$doAccept$3(JerseySupport.java:299) at io.helidon.common.context.Contexts.runInContext(Contexts.java:117) at io.helidon.common.context.ContextAwareExecutorImpl.lambda$wrap$5(ContextAwareExecutorImpl.java:154) at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) at java.base/java.lang.Thread.run(Thread.java:834) Caused by: io.helidon.common.http.AlreadyCompletedException: Response status code and headers are already completed (sent to the client)! at io.helidon.webserver.HashResponseHeaders$CompletionSupport.runIfNotCompleted(HashResponseHeaders.java:384) at io.helidon.webserver.HashResponseHeaders.httpStatus(HashResponseHeaders.java:251) at io.helidon.webserver.Response.status(Response.java:122) at io.helidon.webserver.Response.status(Response.java:48) at io.helidon.webserver.jersey.ResponseWriter.writeResponseStatusAndHeaders(ResponseWriter.java:81) at org.glassfish.jersey.server.ServerRuntime$Responder.writeResponse(ServerRuntime.java:607) at org.glassfish.jersey.server.ServerRuntime$Responder.processResponse(ServerRuntime.java:373) at org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRuntime.java:363) at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:258) ... 14 more

Jul 29, 2021 6:20:36 PM io.helidon.webserver.RequestRouting$RoutedRequest defaultHandler WARNING: Cannot perform error handling of the throwable (see cause of this exception) because headers were already sent java.lang.IllegalStateException: Headers already sent. Cannot handle the cause of this exception. at io.helidon.webserver.RequestRouting$RoutedRequest.defaultHandler(RequestRouting.java:405) at io.helidon.webserver.RequestRouting$RoutedRequest.nextNoCheck(RequestRouting.java:377) at io.helidon.webserver.RequestRouting$RoutedRequest.next(RequestRouting.java:420) at io.helidon.webserver.jersey.ResponseWriter.failure(ResponseWriter.java:133) at org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRuntime.java:438) at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:263) at org.glassfish.jersey.internal.Errors$1.call(Errors.java:248) at org.glassfish.jersey.internal.Errors$1.call(Errors.java:244) at org.glassfish.jersey.internal.Errors.process(Errors.java:292) at org.glassfish.jersey.internal.Errors.process(Errors.java:274) at org.glassfish.jersey.internal.Errors.process(Errors.java:244) at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:265) at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:234) at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:680) at io.helidon.webserver.jersey.JerseySupport$JerseyHandler.lambda$doAccept$3(JerseySupport.java:299) at io.helidon.common.context.Contexts.runInContext(Contexts.java:117) at io.helidon.common.context.ContextAwareExecutorImpl.lambda$wrap$5(ContextAwareExecutorImpl.java:154) at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) at java.base/java.lang.Thread.run(Thread.java:834) Caused by: io.helidon.common.http.AlreadyCompletedException: Response status code and headers are already completed (sent to the client)! at io.helidon.webserver.HashResponseHeaders$CompletionSupport.runIfNotCompleted(HashResponseHeaders.java:384) at io.helidon.webserver.HashResponseHeaders.httpStatus(HashResponseHeaders.java:251) at io.helidon.webserver.Response.status(Response.java:122) at io.helidon.webserver.Response.status(Response.java:48) at io.helidon.webserver.jersey.ResponseWriter.writeResponseStatusAndHeaders(ResponseWriter.java:81) at org.glassfish.jersey.server.ServerRuntime$Responder.writeResponse(ServerRuntime.java:607) at org.glassfish.jersey.server.ServerRuntime$Responder.processResponse(ServerRuntime.java:373) at org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRuntime.java:363) at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:258) ... 14 more

Upvotes: 1

Views: 996

Answers (1)

Romain Grecourt
Romain Grecourt

Reputation: 525

When using Helidon MP, Jersey is responsible for sending the response.

In the code above you are using the underlying WebServer and sending a response inside the body of a JAXRS resource method, when Jersey sends the response the exception is thrown because it was already sent.

Helidon MP Multipart example

Jersey Multipart documentation

Upvotes: 2

Related Questions