use of org.webpieces.router.api.dto.RenderStaticResponse in project webpieces by deanhiller.
the class StaticFileReader method runAsyncFileRead.
private CompletableFuture<Void> runAsyncFileRead(RequestInfo info, RenderStaticResponse renderStatic) throws IOException {
boolean isFile = true;
String fullFilePath = renderStatic.getFilePath();
if (fullFilePath == null) {
isFile = false;
fullFilePath = renderStatic.getDirectory() + renderStatic.getRelativePath();
}
String extension = null;
int lastDirIndex = fullFilePath.lastIndexOf("/");
int lastDot = fullFilePath.lastIndexOf(".");
if (lastDot > lastDirIndex) {
extension = fullFilePath.substring(lastDot + 1);
}
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 timeMs = config.getStaticFileCacheTimeSeconds();
if (timeMs != null)
response.addHeader(new Http2Header(Http2HeaderName.CACHE_CONTROL, "max-age=" + timeMs));
Path file;
Compression compr = compressionLookup.createCompressionStream(info.getRouterRequest().encodings, extension, tuple.mimeType);
//during startup as I don't feel like paying a cpu penalty for compressing while live
if (compr != null && compr.getCompressionType().equals(routerConfig.getStartupCompression())) {
response.addHeader(new Http2Header(Http2HeaderName.CONTENT_ENCODING, compr.getCompressionType()));
File routesCache = renderStatic.getTargetCache();
File fileReference;
if (isFile) {
String fileName = fullFilePath.substring(lastDirIndex + 1);
fileReference = new File(routesCache, fileName);
} else {
fileReference = new File(routesCache, renderStatic.getRelativePath());
}
fullFilePath = fileReference.getAbsolutePath();
file = fetchFile("Compressed File from cache=", fullFilePath + ".gz");
} else {
file = fetchFile("File=", fullFilePath);
}
AsynchronousFileChannel asyncFile = AsynchronousFileChannel.open(file, options, fileExecutor);
CompletableFuture<StreamWriter> future;
try {
log.info(() -> "sending chunked file via async read=" + file);
long length = file.toFile().length();
AtomicLong remaining = new AtomicLong(length);
future = info.getResponseSender().sendResponse(response).thenCompose(s -> readLoop(s, info.getPool(), file, asyncFile, 0, remaining));
} catch (Throwable e) {
future = new CompletableFuture<StreamWriter>();
future.completeExceptionally(e);
}
return //our finally block for failures
future.handle((s, exc) -> handleClose(info, s, exc)).thenAccept(s -> empty());
}
use of org.webpieces.router.api.dto.RenderStaticResponse in project webpieces by deanhiller.
the class RouteInvoker method invokeImpl.
public CompletableFuture<Void> invokeImpl(MatchResult result, Service<MethodMeta, Action> service, RequestContext requestCtx, ResponseStreamer responseCb) {
RouteMeta meta = result.getMeta();
ResponseProcessor processor = new ResponseProcessor(requestCtx, reverseRoutes, reverseTranslator, meta, responseCb);
if (meta.getRoute().getRouteType() == RouteType.STATIC) {
StaticRoute route = (StaticRoute) meta.getRoute();
boolean isOnClassPath = route.getIsOnClassPath();
RenderStaticResponse resp = new RenderStaticResponse(route.getTargetCacheLocation(), isOnClassPath);
if (route.isFile()) {
resp.setFilePath(route.getFileSystemPath());
} else {
String relativeUrl = result.getPathParams().get("resource");
resp.setRelativeFile(route.getFileSystemPath(), relativeUrl);
}
return processor.renderStaticResponse(resp);
}
Object obj = meta.getControllerInstance();
if (obj == null)
throw new IllegalStateException("Someone screwed up, as controllerInstance should not be null at this point, bug");
Method method = meta.getMethod();
if (service == null)
throw new IllegalStateException("Bug, service should never be null at this point");
Messages messages = new Messages(meta.getI18nBundleName(), "webpieces");
requestCtx.setMessages(messages);
RequestLocalCtx.set(processor);
Current.setContext(requestCtx);
CompletableFuture<Action> response;
try {
response = invokeMethod(service, obj, method, meta);
} finally {
RequestLocalCtx.set(null);
Current.setContext(null);
}
CompletableFuture<Void> future = response.thenCompose(resp -> continueProcessing(processor, resp, responseCb));
return future;
}
Aggregations