Search in sources :

Example 1 with FileUploadCallback

use of com.tvd12.ezyhttp.server.core.resources.FileUploadCallback in project ezyhttp by youngmonkeys.

the class FileUploaderTest method acceptSecondFailed.

@SuppressWarnings("unchecked")
@Test
public void acceptSecondFailed() throws IOException {
    // given
    AsyncContext asyncContext = mock(AsyncContext.class);
    Part part = mock(Part.class);
    when(part.getInputStream()).thenThrow(IOException.class);
    File outputFile = new File("test-output/files");
    FileUploadCallback callback = mock(FileUploadCallback.class);
    ResourceUploadManager resourceUploadManager = mock(ResourceUploadManager.class);
    FileUploader sut = new FileUploader(resourceUploadManager);
    // when
    sut.accept(asyncContext, part, outputFile, callback);
    // then
    verify(callback, times(1)).onFailure(any());
    verify(asyncContext, times(1)).complete();
}
Also used : FileUploadCallback(com.tvd12.ezyhttp.server.core.resources.FileUploadCallback) FileUploader(com.tvd12.ezyhttp.server.core.resources.FileUploader) Part(javax.servlet.http.Part) ResourceUploadManager(com.tvd12.ezyhttp.core.resources.ResourceUploadManager) AsyncContext(javax.servlet.AsyncContext) File(java.io.File) Test(org.testng.annotations.Test)

Example 2 with FileUploadCallback

use of com.tvd12.ezyhttp.server.core.resources.FileUploadCallback in project ezyhttp by youngmonkeys.

the class FileUploaderTest method acceptThirdFileNotFound.

@Test
public void acceptThirdFileNotFound() {
    // given
    HttpServletResponse response = mock(HttpServletResponse.class);
    AsyncContext asyncContext = mock(AsyncContext.class);
    when(asyncContext.getResponse()).thenReturn(response);
    InputStream inputStream = mock(InputStream.class);
    File outputFile = new File("");
    FileUploadCallback callback = mock(FileUploadCallback.class);
    ResourceUploadManager resourceUploadManager = mock(ResourceUploadManager.class);
    FileUploader sut = new FileUploader(resourceUploadManager);
    // when
    sut.accept(asyncContext, inputStream, outputFile, callback);
    // then
    verify(callback, times(1)).onFailure(any());
    verify(asyncContext, times(1)).complete();
}
Also used : FileUploadCallback(com.tvd12.ezyhttp.server.core.resources.FileUploadCallback) FileUploader(com.tvd12.ezyhttp.server.core.resources.FileUploader) InputStream(java.io.InputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) ResourceUploadManager(com.tvd12.ezyhttp.core.resources.ResourceUploadManager) AsyncContext(javax.servlet.AsyncContext) File(java.io.File) Test(org.testng.annotations.Test)

Example 3 with FileUploadCallback

use of com.tvd12.ezyhttp.server.core.resources.FileUploadCallback in project ezyhttp by youngmonkeys.

the class FileUploaderTest method acceptFourthFailed.

@Test
public void acceptFourthFailed() {
    // given
    HttpServletResponse response = mock(HttpServletResponse.class);
    AsyncContext asyncContext = mock(AsyncContext.class);
    when(asyncContext.getResponse()).thenReturn(response);
    InputStream inputStream = mock(InputStream.class);
    OutputStream outputStream = mock(OutputStream.class);
    FileUploadCallback callback = mock(FileUploadCallback.class);
    ResourceUploadManager resourceUploadManager = mock(ResourceUploadManager.class);
    doThrow(IllegalStateException.class).when(resourceUploadManager).drainAsync(any(), any(), any(long.class), any());
    int timeout = RandomUtil.randomSmallInt() + 1;
    FileUploader sut = new FileUploader(resourceUploadManager, timeout);
    // when
    sut.accept(asyncContext, inputStream, outputStream, callback);
    // then
    verify(callback, times(1)).onFailure(any());
    verify(resourceUploadManager, times(1)).drainAsync(any(), any(), any(long.class), any());
    verify(asyncContext, times(1)).setTimeout(timeout);
    verify(asyncContext, times(1)).complete();
}
Also used : FileUploadCallback(com.tvd12.ezyhttp.server.core.resources.FileUploadCallback) FileUploader(com.tvd12.ezyhttp.server.core.resources.FileUploader) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ServletOutputStream(javax.servlet.ServletOutputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) ResourceUploadManager(com.tvd12.ezyhttp.core.resources.ResourceUploadManager) AsyncContext(javax.servlet.AsyncContext) Test(org.testng.annotations.Test)

Example 4 with FileUploadCallback

use of com.tvd12.ezyhttp.server.core.resources.FileUploadCallback in project ezyhttp by youngmonkeys.

the class FileUploader method accept.

public void accept(AsyncContext asyncContext, InputStream inputStream, File outputFile, long maxUploadSize, FileUploadCallback callback) {
    try {
        EzyFileUtil.createFileIfNotExists(outputFile);
        FileOutputStream outputStream = new FileOutputStream(outputFile);
        accept(asyncContext, inputStream, outputStream, maxUploadSize, new FileUploadCallback() {

            @Override
            public void onSuccess() {
                processWithLogException(outputStream::close);
                callback.onSuccess();
            }

            @Override
            public void onFailure(Exception e) {
                processWithLogException(outputStream::close);
                callback.onFailure(e);
            }
        });
    } catch (Exception e) {
        try {
            callback.onFailure(e);
        } finally {
            processWithLogException(asyncContext::complete);
        }
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) MaxUploadSizeException(com.tvd12.ezyhttp.core.exception.MaxUploadSizeException) EzyProcessor.processWithLogException(com.tvd12.ezyfox.util.EzyProcessor.processWithLogException)

Example 5 with FileUploadCallback

use of com.tvd12.ezyhttp.server.core.resources.FileUploadCallback in project ezyhttp by youngmonkeys.

the class FileUploader method accept.

public void accept(AsyncContext asyncContext, Part part, File outputFile, long maxUploadSize, EzyExceptionVoid callback) {
    HttpServletResponse response = (HttpServletResponse) asyncContext.getResponse();
    accept(asyncContext, part, outputFile, maxUploadSize, new FileUploadCallback() {

        @Override
        public void onSuccess() {
            try {
                callback.apply();
                response.setStatus(StatusCodes.OK);
            } catch (Exception e) {
                onFailure(e);
            }
        }

        @Override
        public void onFailure(Exception e) {
            if (e instanceof MaxUploadSizeException) {
                processWithLogException(() -> response.getOutputStream().write(OVER_UPLOAD_SIZE_MESSAGE));
                response.setStatus(StatusCodes.BAD_REQUEST);
            } else {
                response.setStatus(StatusCodes.INTERNAL_SERVER_ERROR);
            }
            HttpServletRequest request = (HttpServletRequest) asyncContext.getRequest();
            logger.info("accept request: {} error", request.getRequestURI(), e);
        }
    });
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MaxUploadSizeException(com.tvd12.ezyhttp.core.exception.MaxUploadSizeException) HttpServletResponse(javax.servlet.http.HttpServletResponse) MaxUploadSizeException(com.tvd12.ezyhttp.core.exception.MaxUploadSizeException) EzyProcessor.processWithLogException(com.tvd12.ezyfox.util.EzyProcessor.processWithLogException)

Aggregations

ResourceUploadManager (com.tvd12.ezyhttp.core.resources.ResourceUploadManager)3 FileUploadCallback (com.tvd12.ezyhttp.server.core.resources.FileUploadCallback)3 FileUploader (com.tvd12.ezyhttp.server.core.resources.FileUploader)3 AsyncContext (javax.servlet.AsyncContext)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 Test (org.testng.annotations.Test)3 EzyProcessor.processWithLogException (com.tvd12.ezyfox.util.EzyProcessor.processWithLogException)2 MaxUploadSizeException (com.tvd12.ezyhttp.core.exception.MaxUploadSizeException)2 File (java.io.File)2 InputStream (java.io.InputStream)2 FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 Part (javax.servlet.http.Part)1