Search in sources :

Example 1 with NotFoundException

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");
}
Also used : HttpFullResponse(org.webpieces.httpclient11.api.HttpFullResponse) HttpFullRequest(org.webpieces.httpclient11.api.HttpFullRequest) XFuture(org.webpieces.util.futures.XFuture) NotFoundException(org.webpieces.http.exception.NotFoundException) ResponseWrapper(org.webpieces.webserver.test.ResponseWrapper) AbstractWebpiecesTest(org.webpieces.webserver.test.AbstractWebpiecesTest) PrivateWebserverForTest(org.webpieces.webserver.PrivateWebserverForTest) Test(org.junit.Test)

Example 2 with NotFoundException

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");
}
Also used : HttpFullResponse(org.webpieces.httpclient11.api.HttpFullResponse) HttpFullRequest(org.webpieces.httpclient11.api.HttpFullRequest) XFuture(org.webpieces.util.futures.XFuture) NotFoundException(org.webpieces.http.exception.NotFoundException) ResponseWrapper(org.webpieces.webserver.test.ResponseWrapper) AbstractWebpiecesTest(org.webpieces.webserver.test.AbstractWebpiecesTest) PrivateWebserverForTest(org.webpieces.webserver.PrivateWebserverForTest) Test(org.junit.Test)

Example 3 with NotFoundException

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;
}
Also used : Path(java.nio.file.Path) NotFoundException(org.webpieces.http.exception.NotFoundException) File(java.io.File) VirtualFile(org.webpieces.util.file.VirtualFile)

Example 4 with NotFoundException

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));
}
Also used : VirtualFile(org.webpieces.util.file.VirtualFile) BufferPool(org.webpieces.data.api.BufferPool) StatusCode(org.webpieces.http.StatusCode) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) RouterConfig(org.webpieces.router.api.RouterConfig) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) FutureHelper(org.webpieces.util.futures.FutureHelper) NotFoundException(org.webpieces.http.exception.NotFoundException) VirtualFile(org.webpieces.util.file.VirtualFile) ProxyStreamHandle(org.webpieces.router.impl.proxyout.ProxyStreamHandle) ResponseCreator(org.webpieces.router.impl.proxyout.ResponseCreator) DataFrame(com.webpieces.http2.api.dto.lowlevel.DataFrame) XFuture(org.webpieces.util.futures.XFuture) Http2Header(com.webpieces.http2.api.dto.lowlevel.lib.Http2Header) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) DataWrapper(org.webpieces.data.api.DataWrapper) DataWrapperGenerator(org.webpieces.data.api.DataWrapperGenerator) Http2HeaderName(com.webpieces.http2.api.dto.lowlevel.lib.Http2HeaderName) RenderStaticResponse(org.webpieces.router.impl.dto.RenderStaticResponse) DataWrapperGeneratorFactory(org.webpieces.data.api.DataWrapperGeneratorFactory) Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response) Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response) ProxyStreamHandle(org.webpieces.router.impl.proxyout.ProxyStreamHandle) Http2Header(com.webpieces.http2.api.dto.lowlevel.lib.Http2Header) NotFoundException(org.webpieces.http.exception.NotFoundException) ResponseCreator(org.webpieces.router.impl.proxyout.ResponseCreator)

Example 5 with NotFoundException

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);
}
Also used : XFuture(org.webpieces.util.futures.XFuture) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) NotFoundException(org.webpieces.http.exception.NotFoundException) RouterStreamRef(org.webpieces.router.impl.routeinvoker.RouterStreamRef)

Aggregations

NotFoundException (org.webpieces.http.exception.NotFoundException)9 XFuture (org.webpieces.util.futures.XFuture)5 StreamWriter (com.webpieces.http2.api.streaming.StreamWriter)3 ProxyStreamHandle (org.webpieces.router.impl.proxyout.ProxyStreamHandle)3 Map (java.util.Map)2 Test (org.junit.Test)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 HttpFullRequest (org.webpieces.httpclient11.api.HttpFullRequest)2 HttpFullResponse (org.webpieces.httpclient11.api.HttpFullResponse)2 RouterStreamRef (org.webpieces.router.impl.routeinvoker.RouterStreamRef)2 FutureHelper (org.webpieces.util.futures.FutureHelper)2 PrivateWebserverForTest (org.webpieces.webserver.PrivateWebserverForTest)2 AbstractWebpiecesTest (org.webpieces.webserver.test.AbstractWebpiecesTest)2 ResponseWrapper (org.webpieces.webserver.test.ResponseWrapper)2 Injector (com.google.inject.Injector)1 Http2Response (com.webpieces.http2.api.dto.highlevel.Http2Response)1 DataFrame (com.webpieces.http2.api.dto.lowlevel.DataFrame)1 RstStreamFrame (com.webpieces.http2.api.dto.lowlevel.RstStreamFrame)1 Http2ErrorCode (com.webpieces.http2.api.dto.lowlevel.lib.Http2ErrorCode)1