Search in sources :

Example 1 with RenderStaticResponse

use of org.webpieces.router.impl.dto.RenderStaticResponse 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 2 with RenderStaticResponse

use of org.webpieces.router.impl.dto.RenderStaticResponse in project webpieces by deanhiller.

the class RouteInvokerStatic method invokeStatic.

public RouterStreamRef invokeStatic(RequestContext ctx, ProxyStreamHandle handler, RouteInfoForStatic data) {
    boolean isOnClassPath = data.isOnClassPath();
    RenderStaticResponse resp = new RenderStaticResponse(data.getTargetCacheLocation(), isOnClassPath);
    // we do have a test for this now if you try to fix it
    if (data.isRouteAFile()) {
        resp.setFilePath(data.getFileSystemPath());
    } else {
        String relativeUrl = ctx.getPathParams().get("resource");
        if (// make it truly relative so it does not start with /
        relativeUrl.startsWith("/"))
            relativeUrl = relativeUrl.substring(1);
        VirtualFile fullPath = data.getFileSystemPath().child(relativeUrl);
        resp.setFileAndRelativePath(fullPath, relativeUrl);
    }
    ResponseStaticProcessor processor = new ResponseStaticProcessor(reader, pool, futureUtil, ctx, handler);
    return processor.renderStaticResponse(resp);
}
Also used : VirtualFile(org.webpieces.util.file.VirtualFile) RenderStaticResponse(org.webpieces.router.impl.dto.RenderStaticResponse)

Aggregations

RenderStaticResponse (org.webpieces.router.impl.dto.RenderStaticResponse)2 VirtualFile (org.webpieces.util.file.VirtualFile)2 Http2Response (com.webpieces.http2.api.dto.highlevel.Http2Response)1 DataFrame (com.webpieces.http2.api.dto.lowlevel.DataFrame)1 Http2Header (com.webpieces.http2.api.dto.lowlevel.lib.Http2Header)1 Http2HeaderName (com.webpieces.http2.api.dto.lowlevel.lib.Http2HeaderName)1 StreamWriter (com.webpieces.http2.api.streaming.StreamWriter)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 BufferPool (org.webpieces.data.api.BufferPool)1 DataWrapper (org.webpieces.data.api.DataWrapper)1 DataWrapperGenerator (org.webpieces.data.api.DataWrapperGenerator)1 DataWrapperGeneratorFactory (org.webpieces.data.api.DataWrapperGeneratorFactory)1 StatusCode (org.webpieces.http.StatusCode)1 NotFoundException (org.webpieces.http.exception.NotFoundException)1 RouterConfig (org.webpieces.router.api.RouterConfig)1 ProxyStreamHandle (org.webpieces.router.impl.proxyout.ProxyStreamHandle)1 ResponseCreator (org.webpieces.router.impl.proxyout.ResponseCreator)1