Search in sources :

Example 1 with MaxUploadSizeException

use of com.tvd12.ezyhttp.core.exception.MaxUploadSizeException 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 2 with MaxUploadSizeException

use of com.tvd12.ezyhttp.core.exception.MaxUploadSizeException 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)

Example 3 with MaxUploadSizeException

use of com.tvd12.ezyhttp.core.exception.MaxUploadSizeException in project ezyhttp by youngmonkeys.

the class ResourceUploadManager method loop.

private void loop() {
    byte[] buffer = new byte[bufferSize];
    while (active) {
        Entry entry = null;
        boolean done = true;
        boolean isMaxUploaded = false;
        Exception exception = null;
        try {
            entry = queue.take();
            if (entry == POISON) {
                break;
            }
            InputStream inputStream = entry.inputStream;
            OutputStream outputStream = entry.outputStream;
            int read = inputStream.read(buffer);
            if (entry.increaseUploadedSize(read)) {
                if (read > 0) {
                    outputStream.write(buffer, 0, read);
                    done = false;
                }
            } else {
                isMaxUploaded = true;
            }
        } catch (Exception e) {
            exception = e;
            logger.info("upload error", e);
        } catch (Throwable e) {
            exception = new IllegalStateException(e);
            logger.info("upload fatal error", e);
        }
        if (entry == null) {
            continue;
        }
        if (isMaxUploaded) {
            exception = new MaxUploadSizeException(entry.maxUploadSize);
        }
        try {
            if (done) {
                EzyFuture future = futureMap.removeFuture(entry);
                if (future == null) {
                    continue;
                }
                if (exception != null) {
                    future.setException(exception);
                } else {
                    future.setResult(Boolean.TRUE);
                }
            } else {
                if (!queue.offer(entry)) {
                    EzyFuture future = futureMap.removeFuture(entry);
                    if (future != null) {
                        future.setException(new MaxResourceUploadCapacity(capacity));
                    }
                }
            }
        } catch (Throwable e) {
            logger.info("handle upload result error", e);
        }
    }
}
Also used : MaxResourceUploadCapacity(com.tvd12.ezyhttp.core.exception.MaxResourceUploadCapacity) InputStream(java.io.InputStream) MaxUploadSizeException(com.tvd12.ezyhttp.core.exception.MaxUploadSizeException) OutputStream(java.io.OutputStream) MaxUploadSizeException(com.tvd12.ezyhttp.core.exception.MaxUploadSizeException)

Aggregations

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