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();
}
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();
}
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();
}
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);
}
}
}
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);
}
});
}
Aggregations