use of org.webpieces.router.impl.proxyout.ProxyStreamHandle 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.router.impl.proxyout.ProxyStreamHandle in project webpieces by deanhiller.
the class DScopedRouter method invokeRouteCatchNotFound.
/**
* NOTE: We have to catch any exception from the method processNotFound so we can't catch and call internalServerError in this
* method without nesting even more!!! UGH, more nesting sucks
*/
private RouterStreamRef invokeRouteCatchNotFound(RequestContext ctx, ProxyStreamHandle handler, String subPath) {
RouterStreamRef streamRef = super.invokeRoute(ctx, handler, subPath);
XFuture<StreamWriter> writer = streamRef.getWriter().handle((r, t) -> {
if (t == null)
return XFuture.completedFuture(r);
if (t instanceof NotFoundException)
return notFound((NotFoundException) t, ctx, handler);
return futureUtil.failedFuture(t);
}).thenCompose(Function.identity());
return new RouterStreamRef("DScopedNotFoundCheck", writer, streamRef);
}
use of org.webpieces.router.impl.proxyout.ProxyStreamHandle in project webpieces by deanhiller.
the class DScopedRouter method invokeRoute.
@Override
public RouterStreamRef invokeRoute(RequestContext ctx, ProxyStreamHandle handler, String subPath) {
RouterStreamRef streamRef = invokeRouteCatchNotFound(ctx, handler, subPath);
XFuture<StreamWriter> writer = streamRef.getWriter().handle((r, t) -> {
if (t == null)
return XFuture.completedFuture(r);
return tryRenderWebAppErrorControllerResult(ctx, handler, t);
}).thenCompose(Function.identity());
XFuture<StreamWriter> proxyWriter = writer.thenApply(w -> createProxy(w, ctx, handler));
return new RouterStreamRef("dScopedRouter", proxyWriter, streamRef);
}
use of org.webpieces.router.impl.proxyout.ProxyStreamHandle in project webpieces by deanhiller.
the class DevRouteInvoker method invokeNotFound.
/**
* This one is definitely special
*/
@Override
public XFuture<Void> invokeNotFound(InvokeInfo invokeInfo, Endpoint info, RouteData data) {
// special case for if stuff didn't compile and we flag it
Throwable exc = (Throwable) invokeInfo.getRequestCtx().getRequest().requestState.get(ERROR_KEY);
if (exc != null) {
log.error("Could not compile your code", exc);
RouteInfoForInternalError error = new RouteInfoForInternalError(exc);
return invokeErrorController(invokeInfo, info, error);
}
RequestContext requestCtx = invokeInfo.getRequestCtx();
ProxyStreamHandle handler = invokeInfo.getHandler();
RouteInfoForNotFound notFoundData = (RouteInfoForNotFound) data;
NotFoundException notFoundExc = notFoundData.getNotFoundException();
RouterRequest req = requestCtx.getRequest();
if (notFoundData.getNotFoundException() == null) {
throw new IllegalArgumentException("must have not found exception to be here");
} else if (req.queryParams.containsKey(DevelopmentController.NOT_FOUND_KEY)) {
// This is a callback so render the original webapp developer's not found page into the iframe
return super.invokeNotFound(invokeInfo, info, data);
}
// ok, in dev mode, we hijack the not found page with one with a route list AND an iframe containing the developers original
// notfound page
log.error("(Development only log message) Route not found!!! Either you(developer) typed the wrong url OR you have a bad route. Either way,\n" + " something needs a'fixin. req=" + req, notFoundExc);
Injector webAppInjector = webInjector.getCurrentInjector();
RouteInfo routeInfo = new RouteInfo(new RouteModuleInfo("", null), "/org/webpieces/devrouter/impl/DevelopmentController.notFound");
SvcProxyFixedRoutes svcProxy = new SvcProxyFixedRoutes(serviceInvoker, futureUtil);
LoadedController newLoadedController = controllerFinder.loadGenericController(webAppInjector, routeInfo).getLoadedController();
Endpoint newInfo = new Endpoint(svcProxy);
String reason = "Your route was not found in routes table";
if (notFoundExc != null)
reason = notFoundExc.getMessage();
RouterRequest newRequest = new RouterRequest();
newRequest.putMultipart("webpiecesError", "Exception message=" + reason);
newRequest.putMultipart("url", req.relativePath);
newRequest.isHttps = req.isHttps;
newRequest.isBackendRequest = req.isBackendRequest;
newRequest.originalRequest = req.originalRequest;
newRequest.requestState.put(DevelopmentController.ORIGINAL_REQUEST, req);
ApplicationContext ctx = webInjector.getAppContext();
RequestContext overridenCtx = new RequestContext(requestCtx.getValidation(), (FlashSub) requestCtx.getFlash(), requestCtx.getSession(), newRequest, ctx);
InvokeInfo newInvokeInfo = new InvokeInfo(overridenCtx, handler, RouteType.NOT_FOUND, newLoadedController, null);
RequestContext oldContext = Current.getContext();
Current.setContext(overridenCtx);
try {
return super.invokeNotFound(newInvokeInfo, newInfo, data);
} finally {
Current.setContext(oldContext);
}
}
use of org.webpieces.router.impl.proxyout.ProxyStreamHandle in project webpieces by deanhiller.
the class RouterServiceImpl method incomingRequest.
@Override
public StreamRef incomingRequest(Http2Request req, RouterResponseHandler handler) {
// ******************************************************************************************
// DO NOT ADD CODE HERE OR ABOVE THIS METHOD in RouterService. This is our CATCH-ALL point so
// ANY code above that is not protected from our catch and respond to clients
// ******************************************************************************************
String txId = generate();
// I do NOT like doing Guice creation on the request path(Dagger creation would probably be ok) BUT this
// is very worth it AND customers can swap out these critical classes if they need to quickly temporarily fix a bug while
// we work on the bug. We can easily give customers bug fixes like add binder.bind(ClassWithBug.class).to(BugFixCode.class)
ProxyStreamHandle proxyHandler = proxyProvider.get();
proxyHandler.init(handler, req);
MDC.put("txId", txId);
// top level handler...
try {
RouterStreamRef streamRef = incomingRequestProtected(req, proxyHandler);
XFuture<StreamWriter> writer = streamRef.getWriter().thenApply(w -> new TxStreamWriter(txId, w));
XFuture<StreamWriter> finalWriter = writer.handle((r, t) -> {
if (t == null)
return XFuture.completedFuture(r);
XFuture<StreamWriter> fut = proxyHandler.topLevelFailure(req, t);
return fut;
}).thenCompose(Function.identity());
return new RouterStreamRef("routerSevcTop", finalWriter, streamRef);
} finally {
MDC.remove("txId");
}
}
Aggregations