use of org.webpieces.http.exception.NotFoundException in project webpieces by deanhiller.
the class TestAsynchronousErrors method testNotFoundHandlerThrowsNotFound.
@Test
public void testNotFoundHandlerThrowsNotFound() {
XFuture<Integer> future = new XFuture<Integer>();
mockNotFoundLib.queueFuture(future);
HttpFullRequest req = Requests.createRequest(KnownHttpMethod.GET, "/route/that/does/not/exist");
XFuture<HttpFullResponse> respFuture = http11Socket.send(req);
Assert.assertFalse(respFuture.isDone());
future.completeExceptionally(new NotFoundException("testing notfound from notfound route"));
ResponseWrapper response = ResponseExtract.waitResponseAndWrap(respFuture);
response.assertStatusCode(KnownStatusCode.HTTP_500_INTERNAL_SVR_ERROR);
response.assertContains("There was a bug in our software...sorry about that");
}
use of org.webpieces.http.exception.NotFoundException in project webpieces by deanhiller.
the class TestAsynchronousErrors method testWebappThrowsNotFound.
@Test
public void testWebappThrowsNotFound() {
XFuture<Integer> future = new XFuture<Integer>();
mockNotFoundLib.queueFuture(future);
XFuture<Integer> future2 = new XFuture<Integer>();
mockNotFoundLib.queueFuture(future2);
HttpFullRequest req = Requests.createRequest(KnownHttpMethod.GET, "/throwNotFound");
XFuture<HttpFullResponse> respFuture = http11Socket.send(req);
Assert.assertFalse(respFuture.isDone());
future.completeExceptionally(new NotFoundException("some async NotFound"));
Assert.assertFalse(respFuture.isDone());
future2.complete(55);
ResponseWrapper response = ResponseExtract.waitResponseAndWrap(respFuture);
response.assertStatusCode(KnownStatusCode.HTTP_404_NOTFOUND);
response.assertContains("Your page was not found");
}
use of org.webpieces.http.exception.NotFoundException in project webpieces by deanhiller.
the class XFileReaderFileSystem method fetchFile.
private Path fetchFile(String msg, String fullFilePath) {
Path file = Paths.get(fullFilePath);
File f = file.toFile();
if (!f.exists() || !f.isFile())
throw new NotFoundException(msg + file + " was not found");
return file;
}
use of org.webpieces.http.exception.NotFoundException in project webpieces by deanhiller.
the class XFileReader method runFileRead.
public XFuture<Void> runFileRead(RequestInfo info, RenderStaticResponse renderStatic, ProxyStreamHandle handle) throws IOException {
VirtualFile fullFilePath = renderStatic.getFilePath();
if (!fullFilePath.exists()) {
throw new NotFoundException("File not found=" + fullFilePath);
} else if (fullFilePath.isDirectory()) {
throw new NotFoundException("File not found (it was a directory that can't be rendered)=" + fullFilePath);
}
String fileName = getNameToUse(fullFilePath);
String extension = null;
int lastDot = fileName.lastIndexOf(".");
if (lastDot > 0) {
extension = fileName.substring(lastDot + 1);
}
ResponseCreator.ResponseEncodingTuple tuple = responseCreator.createResponse(info.getRequest(), StatusCode.HTTP_200_OK, extension, "application/octet-stream", false);
Http2Response response = tuple.response;
// On startup, we protect developers from breaking clients. In http, all files that change
// must also change the hash automatically and the %%{ }%% tag generates those hashes so the
// files loaded are always the latest
Long timeSeconds = config.getStaticFileCacheTimeSeconds();
if (timeSeconds != null)
response.addHeader(new Http2Header(Http2HeaderName.CACHE_CONTROL, "max-age=" + timeSeconds));
ChunkReader reader = createFileReader(response, renderStatic, fileName, fullFilePath, info, extension, tuple, handle);
if (log.isDebugEnabled())
log.debug("sending chunked file via async read=" + reader);
ProxyStreamHandle stream = info.getResponseSender();
return futureUtil.finallyBlock(() -> stream.process(response).thenCompose(s -> readLoop(s, info.getPool(), reader, 0)), () -> handleClose(info, reader));
}
use of org.webpieces.http.exception.NotFoundException in project webpieces by deanhiller.
the class EScopedRouter method invokeRouteImpl.
public RouterStreamRef invokeRouteImpl(RequestContext ctx, ProxyStreamHandle handler, String subPath) {
if ("".equals(subPath))
return findAndInvokeRoute(ctx, handler, subPath);
else if (!subPath.startsWith("/"))
throw new IllegalArgumentException("path must start with /");
String prefix = subPath;
int index = subPath.indexOf("/", 1);
if (index == 1) {
XFuture<StreamWriter> future = new XFuture<>();
future.completeExceptionally(new NotFoundException("Bad path=" + ctx.getRequest().relativePath + " request=" + ctx.getRequest()));
return new RouterStreamRef("badPath", future, null);
} else if (index > 1) {
prefix = subPath.substring(0, index);
}
EScopedRouter routeInfo = getPathPrefixToNextRouter().get(prefix);
if (routeInfo != null) {
if (index < 0)
return routeInfo.invokeRoute(ctx, handler, "");
String newRelativePath = subPath.substring(index, subPath.length());
return routeInfo.invokeRoute(ctx, handler, newRelativePath);
}
return findAndInvokeRoute(ctx, handler, subPath);
}
Aggregations