Skip to content
This repository has been archived by the owner on Apr 5, 2022. It is now read-only.

Is it possible to have some progress listener for a POST request using RestTemplate? #35

Open
githubdoramon opened this issue Jan 30, 2017 · 1 comment

Comments

@githubdoramon
Copy link

No description provided.

@steviemo
Copy link

steviemo commented Aug 3, 2017

You can do this already:

final SimpleClientHttpRequestFactory simpleClientHttpRequestFactory = new SimpleClientHttpRequestFactory();
// Important so rest template does not read request into memory so we get sensible progress back.
simpleClientHttpRequestFactory.setBufferRequestBody(false);
final RestTemplate restTemplate = new RestTemplate(simpleClientHttpRequestFactory);
final CallbackFormHttpMessageConverter formConverter = new CallbackFormHttpMessageConverter(tracker);
restTemplate.getMessageConverters().add(0, formConverter);
public class CallbackFormHttpMessageConverter extends FormHttpMessageConverter {

    public CallbackFormHttpMessageConverter(final FileUploadProgressTracker callback) {
        super();

        final List<HttpMessageConverter<?>> partConverters = new ArrayList<>();
        partConverters.add(new CallbackResourceHttpMessageConverter(callback));

        // Continue to add in default for FormHttpMessageConverter.
        partConverters.add(new ByteArrayHttpMessageConverter());
        final StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();
        stringHttpMessageConverter.setWriteAcceptCharset(false);
        partConverters.add(stringHttpMessageConverter);
        partConverters.add(new ResourceHttpMessageConverter());
        setPartConverters(partConverters);
    }

}
public class CallbackResourceHttpMessageConverter extends ResourceHttpMessageConverter {

    private final FileUploadProgressTracker callback;

    public CallbackResourceHttpMessageConverter(final FileUploadProgressTracker callback) {
        this.callback = callback;
    }

    @Override
    protected void writeInternal(final Resource resource, final HttpOutputMessage outputMessage)
            throws IOException, HttpMessageNotWritableException {

        final InputStream in = resource.getInputStream();
        try {
            final long fileSize = resource.contentLength();
            final byte[] buffer = new byte[StreamUtils.BUFFER_SIZE];

            long count = 0;
            int n = 0;
            int percentange = 0;
            while (-1 != (n = in.read(buffer))) {
                outputMessage.getBody().write(buffer, 0, n);
                count += n;
                percentange = (int) ((count * 100) / fileSize);
                callback.trackProgress(percentange);
            }
        } finally {
            try {
                in.close();
            } catch (final IOException ex) {
            }
        }
        outputMessage.getBody().flush();
    }

}
public interface FileUploadProgressTracker {

    void trackProgress(int progress);
}

Implement the interface with your own class - in my example its called 'tracker'

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

2 participants