All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.bdeploy.jersey.JerseyPathReader Maven / Gradle / Ivy

Go to download

Public API including dependencies, ready to be used for integrations and plugins.

There is a newer version: 7.4.0
Show newest version
package io.bdeploy.jersey;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.nio.file.Files;
import java.nio.file.Path;

import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.ext.MessageBodyReader;
import jakarta.ws.rs.ext.Provider;

/**
 * Allows deserializing {@link Path} objects (parameter or return value). The
 * actual file content is streamed to a local file and a path to the temporary
 * file is injected.
 * 

* The recipient of the {@link Path} is responsible for cleaning up temporary * files. */ @Provider @Consumes(MediaType.APPLICATION_OCTET_STREAM) public class JerseyPathReader implements MessageBodyReader { static final String PATH_SIZE_HDR = "X-File-Size"; // don't use Content-Length = restricted. @Override public boolean isReadable(Class type, Type genericType, Annotation[] annotations, MediaType mediaType) { return Path.class.isAssignableFrom(type); } @Override public Path readFrom(Class type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap httpHeaders, InputStream entityStream) throws IOException { String cl = httpHeaders.getFirst(PATH_SIZE_HDR); long length = -1; try { length = Long.parseLong(cl); } catch (NumberFormatException e) { // no length present or invalid format. } Path tmpFile = Files.createTempFile("dl-", ".bin"); try (OutputStream out = Files.newOutputStream(tmpFile)) { JerseyStreamingHelper.streamWithProgress(entityStream, out, length); } return tmpFile; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy