Search in sources :

Example 1 with EzyResultCallback

use of com.tvd12.ezyfox.concurrent.callback.EzyResultCallback in project ezyhttp by youngmonkeys.

the class ResourceRequestHandler method handle.

@Override
public Object handle(RequestArguments arguments) throws Exception {
    AsyncContext asyncContext = arguments.getAsyncContext();
    if (defaultTimeout > 0) {
        asyncContext.setTimeout(defaultTimeout);
    }
    HttpServletResponse servletResponse = (HttpServletResponse) asyncContext.getResponse();
    InputStream inputStream = inputStreamLoader.load(resourcePath);
    OutputStream outputStream = servletResponse.getOutputStream();
    try {
        downloadManager.drainAsync(inputStream, outputStream, new EzyResultCallback<Boolean>() {

            @Override
            public void onResponse(Boolean response) {
                processWithLogException(inputStream::close);
                servletResponse.setContentType(getResponseContentType());
                servletResponse.setStatus(StatusCodes.OK);
                asyncContext.complete();
            }

            @Override
            public void onException(Exception e) {
                processWithLogException(inputStream::close);
                servletResponse.setStatus(StatusCodes.INTERNAL_SERVER_ERROR);
                asyncContext.complete();
            }
        });
    } catch (Exception e) {
        processWithLogException(inputStream::close);
        servletResponse.setStatus(StatusCodes.INTERNAL_SERVER_ERROR);
        asyncContext.complete();
    }
    return ResponseEntity.ASYNC;
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) AsyncContext(javax.servlet.AsyncContext) EzyProcessor.processWithLogException(com.tvd12.ezyfox.util.EzyProcessor.processWithLogException)

Example 2 with EzyResultCallback

use of com.tvd12.ezyfox.concurrent.callback.EzyResultCallback in project ezyhttp by youngmonkeys.

the class FileUploaderTest method acceptFirstMaxUploadSizeException.

@SuppressWarnings("unchecked")
@Test
public void acceptFirstMaxUploadSizeException() throws Exception {
    // given
    AsyncContext asyncContext = mock(AsyncContext.class);
    HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getRequestURI()).thenReturn("hello-world");
    when(asyncContext.getRequest()).thenReturn(request);
    HttpServletResponse response = mock(HttpServletResponse.class);
    when(asyncContext.getResponse()).thenReturn(response);
    ServletOutputStream outputStream = mock(ServletOutputStream.class);
    when(response.getOutputStream()).thenReturn(outputStream);
    Part part = mock(Part.class);
    File outputFile = new File("test-output/files");
    EzyExceptionVoid callback = mock(EzyExceptionVoid.class);
    doThrow(new MaxUploadSizeException(100)).when(callback).apply();
    ResourceUploadManager resourceUploadManager = mock(ResourceUploadManager.class);
    doAnswer(it -> {
        EzyResultCallback<Boolean> cb = it.getArgumentAt(3, EzyResultCallback.class);
        cb.onResponse(Boolean.TRUE);
        return null;
    }).when(resourceUploadManager).drainAsync(any(), any(), any(long.class), any());
    FileUploader sut = new FileUploader(resourceUploadManager);
    // when
    sut.accept(asyncContext, part, outputFile, callback);
    // then
    verify(callback, times(1)).apply();
    verify(resourceUploadManager, times(1)).drainAsync(any(), any(), any(long.class), any());
    verify(response, times(1)).setStatus(StatusCodes.BAD_REQUEST);
    verify(response, times(1)).getOutputStream();
    verify(outputStream, times(1)).write(any(byte[].class));
    verify(request, times(1)).getRequestURI();
    verify(asyncContext, times(1)).getRequest();
    verify(asyncContext, times(1)).complete();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) EzyExceptionVoid(com.tvd12.ezyfox.function.EzyExceptionVoid) ServletOutputStream(javax.servlet.ServletOutputStream) FileUploader(com.tvd12.ezyhttp.server.core.resources.FileUploader) Part(javax.servlet.http.Part) MaxUploadSizeException(com.tvd12.ezyhttp.core.exception.MaxUploadSizeException) 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 EzyResultCallback

use of com.tvd12.ezyfox.concurrent.callback.EzyResultCallback in project ezyhttp by youngmonkeys.

the class FileUploaderTest method acceptFirst.

@SuppressWarnings("unchecked")
@Test
public void acceptFirst() throws Exception {
    // given
    AsyncContext asyncContext = mock(AsyncContext.class);
    HttpServletResponse response = mock(HttpServletResponse.class);
    when(asyncContext.getResponse()).thenReturn(response);
    Part part = mock(Part.class);
    File outputFile = new File("test-output/files");
    EzyExceptionVoid callback = mock(EzyExceptionVoid.class);
    ResourceUploadManager resourceUploadManager = mock(ResourceUploadManager.class);
    doAnswer(it -> {
        EzyResultCallback<Boolean> cb = it.getArgumentAt(3, EzyResultCallback.class);
        cb.onResponse(Boolean.TRUE);
        return null;
    }).when(resourceUploadManager).drainAsync(any(), any(), any(long.class), any());
    FileUploader sut = new FileUploader(resourceUploadManager);
    // when
    sut.accept(asyncContext, part, outputFile, callback);
    // then
    verify(callback, times(1)).apply();
    verify(resourceUploadManager, times(1)).drainAsync(any(), any(), any(long.class), any());
    verify(response, times(1)).setStatus(StatusCodes.OK);
    verify(asyncContext, times(1)).complete();
}
Also used : EzyExceptionVoid(com.tvd12.ezyfox.function.EzyExceptionVoid) FileUploader(com.tvd12.ezyhttp.server.core.resources.FileUploader) Part(javax.servlet.http.Part) 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 4 with EzyResultCallback

use of com.tvd12.ezyfox.concurrent.callback.EzyResultCallback in project ezyhttp by youngmonkeys.

the class FileUploaderTest method acceptFirstFailed.

@SuppressWarnings("unchecked")
@Test
public void acceptFirstFailed() throws Exception {
    // given
    AsyncContext asyncContext = mock(AsyncContext.class);
    HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getRequestURI()).thenReturn("hello-world");
    when(asyncContext.getRequest()).thenReturn(request);
    HttpServletResponse response = mock(HttpServletResponse.class);
    when(asyncContext.getResponse()).thenReturn(response);
    Part part = mock(Part.class);
    File outputFile = new File("test-output/files");
    EzyExceptionVoid callback = mock(EzyExceptionVoid.class);
    ResourceUploadManager resourceUploadManager = mock(ResourceUploadManager.class);
    doAnswer(it -> {
        EzyResultCallback<Boolean> cb = it.getArgumentAt(3, EzyResultCallback.class);
        cb.onException(new Exception("just test"));
        return null;
    }).when(resourceUploadManager).drainAsync(any(), any(), any(long.class), any());
    FileUploader sut = new FileUploader(resourceUploadManager);
    // when
    sut.accept(asyncContext, part, outputFile, callback);
    // then
    verify(callback, times(0)).apply();
    verify(resourceUploadManager, times(1)).drainAsync(any(), any(), any(long.class), any());
    verify(response, times(1)).setStatus(StatusCodes.INTERNAL_SERVER_ERROR);
    verify(request, times(1)).getRequestURI();
    verify(asyncContext, times(1)).getRequest();
    verify(asyncContext, times(1)).complete();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) EzyExceptionVoid(com.tvd12.ezyfox.function.EzyExceptionVoid) FileUploader(com.tvd12.ezyhttp.server.core.resources.FileUploader) Part(javax.servlet.http.Part) HttpServletResponse(javax.servlet.http.HttpServletResponse) ResourceUploadManager(com.tvd12.ezyhttp.core.resources.ResourceUploadManager) AsyncContext(javax.servlet.AsyncContext) File(java.io.File) MaxUploadSizeException(com.tvd12.ezyhttp.core.exception.MaxUploadSizeException) IOException(java.io.IOException) Test(org.testng.annotations.Test)

Example 5 with EzyResultCallback

use of com.tvd12.ezyfox.concurrent.callback.EzyResultCallback in project ezyhttp by youngmonkeys.

the class FileUploaderTest method acceptFirstException.

@SuppressWarnings("unchecked")
@Test
public void acceptFirstException() throws Exception {
    // given
    AsyncContext asyncContext = mock(AsyncContext.class);
    HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getRequestURI()).thenReturn("hello-world");
    when(asyncContext.getRequest()).thenReturn(request);
    HttpServletResponse response = mock(HttpServletResponse.class);
    when(asyncContext.getResponse()).thenReturn(response);
    Part part = mock(Part.class);
    File outputFile = new File("test-output/files");
    EzyExceptionVoid callback = mock(EzyExceptionVoid.class);
    doThrow(IllegalStateException.class).when(callback).apply();
    ResourceUploadManager resourceUploadManager = mock(ResourceUploadManager.class);
    doAnswer(it -> {
        EzyResultCallback<Boolean> cb = it.getArgumentAt(3, EzyResultCallback.class);
        cb.onResponse(Boolean.TRUE);
        return null;
    }).when(resourceUploadManager).drainAsync(any(), any(), any(long.class), any());
    FileUploader sut = new FileUploader(resourceUploadManager);
    // when
    sut.accept(asyncContext, part, outputFile, callback);
    // then
    verify(callback, times(1)).apply();
    verify(resourceUploadManager, times(1)).drainAsync(any(), any(), any(long.class), any());
    verify(response, times(1)).setStatus(StatusCodes.INTERNAL_SERVER_ERROR);
    verify(request, times(1)).getRequestURI();
    verify(asyncContext, times(1)).getRequest();
    verify(asyncContext, times(1)).complete();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) EzyExceptionVoid(com.tvd12.ezyfox.function.EzyExceptionVoid) FileUploader(com.tvd12.ezyhttp.server.core.resources.FileUploader) Part(javax.servlet.http.Part) 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)

Aggregations

Test (org.testng.annotations.Test)6 ResourceUploadManager (com.tvd12.ezyhttp.core.resources.ResourceUploadManager)5 AsyncContext (javax.servlet.AsyncContext)5 HttpServletResponse (javax.servlet.http.HttpServletResponse)5 EzyExceptionVoid (com.tvd12.ezyfox.function.EzyExceptionVoid)4 FileUploader (com.tvd12.ezyhttp.server.core.resources.FileUploader)4 File (java.io.File)4 Part (javax.servlet.http.Part)4 MaxUploadSizeException (com.tvd12.ezyhttp.core.exception.MaxUploadSizeException)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 BaseTest (com.tvd12.test.base.BaseTest)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 EzyCallableFutureTask (com.tvd12.ezyfox.concurrent.EzyCallableFutureTask)1 EzyProcessor.processWithLogException (com.tvd12.ezyfox.util.EzyProcessor.processWithLogException)1 ResourceDownloadManager (com.tvd12.ezyhttp.core.resources.ResourceDownloadManager)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1