use of org.webpieces.router.api.exceptions.IllegalReturnValueException in project webpieces by deanhiller.
the class ResponseProcessor method createRenderResponse.
public CompletableFuture<Void> createRenderResponse(RenderImpl controllerResponse) {
if (responseSent)
throw new IllegalStateException("You already sent a response. do not call Actions.redirect or Actions.render more than once");
responseSent = true;
RouterRequest request = ctx.getRequest();
Method method = matchedMeta.getMethod();
//not broken)
if (matchedMeta.getRoute().getRouteType() == RouteType.HTML && HttpMethod.POST == request.method) {
throw new IllegalReturnValueException("Controller method='" + method + "' MUST follow the PRG " + "pattern(https://en.wikipedia.org/wiki/Post/Redirect/Get) so " + "users don't have a poor experience using your website with the browser back button. " + "This means on a POST request, you cannot return RenderHtml object and must return Redirects");
}
String controllerName = matchedMeta.getControllerInstance().getClass().getName();
String methodName = matchedMeta.getMethod().getName();
String relativeOrAbsolutePath = controllerResponse.getRelativeOrAbsolutePath();
if (relativeOrAbsolutePath == null) {
relativeOrAbsolutePath = methodName + ".html";
}
Map<String, Object> pageArgs = controllerResponse.getPageArgs();
// Add context as a page arg:
pageArgs.put("_context", ctx);
pageArgs.put("_session", ctx.getSession());
pageArgs.put("_flash", ctx.getFlash());
View view = new View(controllerName, methodName, relativeOrAbsolutePath);
RenderResponse resp = new RenderResponse(view, pageArgs, matchedMeta.getRoute().getRouteType());
return wrapFunctionInContext(() -> responseCb.sendRenderHtml(resp));
}
use of org.webpieces.router.api.exceptions.IllegalReturnValueException in project webpieces by deanhiller.
the class ProxyResponse method createRedirect.
private Http2Response createRedirect(RedirectResponse httpResponse) {
Http2Response response = new Http2Response();
if (httpResponse.isAjaxRedirect) {
response.addHeader(new Http2Header(Http2HeaderName.STATUS, BootstrapModalTag.AJAX_REDIRECT_CODE + ""));
response.addHeader(new Http2Header("reason", "Ajax Redirect"));
} else {
response.addHeader(new Http2Header(Http2HeaderName.STATUS, StatusCode.HTTP_303_SEEOTHER.getCodeString()));
response.addHeader(new Http2Header("reason", StatusCode.HTTP_303_SEEOTHER.getReason()));
}
String url = httpResponse.redirectToPath;
if (url.startsWith("http")) {
//do nothing
} else if (httpResponse.domain != null && httpResponse.isHttps != null) {
String prefix = "http://";
if (httpResponse.isHttps)
prefix = "https://";
String portPostfix = "";
if (httpResponse.port != 443 && httpResponse.port != 80)
portPostfix = ":" + httpResponse.port;
url = prefix + httpResponse.domain + portPostfix + httpResponse.redirectToPath;
} else if (httpResponse.domain != null) {
throw new IllegalReturnValueException("Controller is returning a domain without returning isHttps=true or" + " isHttps=false so we can form the entire redirect. Either drop the domain or set isHttps");
} else if (httpResponse.isHttps != null) {
throw new IllegalReturnValueException("Controller is returning isHttps=" + httpResponse.isHttps + " but there is" + "no domain set so we can't form the full redirect. Either drop setting isHttps or set the domain");
}
Http2Header location = new Http2Header(Http2HeaderName.LOCATION, url);
response.addHeader(location);
responseCreator.addCommonHeaders(request, response, false, true);
//Firefox requires a content length of 0 on redirect(chrome doesn't)!!!...
response.addHeader(new Http2Header(Http2HeaderName.CONTENT_LENGTH, 0 + ""));
return response;
}
use of org.webpieces.router.api.exceptions.IllegalReturnValueException in project webpieces by deanhiller.
the class ObjectToParamTranslator method formMap.
@SuppressWarnings({ "rawtypes", "unchecked" })
public Map<String, String> formMap(Method method, List<String> pathParamNames, Map<String, Object> redirectArgs) {
if (pathParamNames.size() != redirectArgs.size())
throw new IllegalReturnValueException("The Redirect object returned from method='" + method + "' has the wrong number of arguments. args.size=" + redirectArgs.size() + " should be size=" + pathParamNames.size());
Map<String, String> nameToValue = new HashMap<>();
for (int i = 0; i < pathParamNames.size(); i++) {
String key = pathParamNames.get(i);
Object value = redirectArgs.get(key);
// value can't be null on redirect
if (value == null)
throw new IllegalArgumentException("Controller did not set key='" + key + "' or passed in null" + " for '" + key + "' and this is not allowed as you end up with the word 'null' in your url");
ObjectStringConverter function = translator.getConverterFor(value);
nameToValue.put(key, function.objectToString(value));
}
return nameToValue;
}
use of org.webpieces.router.api.exceptions.IllegalReturnValueException in project webpieces by deanhiller.
the class ResponseProcessor method createRedirect.
private CompletableFuture<Void> createRedirect(RouteId id, Map<String, Object> args, boolean isAjaxRedirect) {
if (responseSent)
throw new IllegalStateException("You already sent a response. do not call Actions.redirect or Actions.render more than once");
responseSent = true;
RouterRequest request = ctx.getRequest();
Method method = matchedMeta.getMethod();
RouteMeta nextRequestMeta = reverseRoutes.get(id);
if (nextRequestMeta == null)
throw new IllegalReturnValueException("Route=" + id + " returned from method='" + method + "' was not added in the RouterModules");
else if (!nextRequestMeta.getRoute().matchesMethod(HttpMethod.GET))
throw new IllegalReturnValueException("method='" + method + "' is trying to redirect to routeid=" + id + " but that route is not a GET method route and must be");
Route route = nextRequestMeta.getRoute();
Map<String, String> keysToValues = reverseTranslator.formMap(method, route.getPathParamNames(), args);
Set<String> keySet = keysToValues.keySet();
List<String> argNames = route.getPathParamNames();
if (keySet.size() != argNames.size()) {
throw new IllegalReturnValueException("Method='" + method + "' returns a Redirect action with wrong number of arguments. args=" + keySet.size() + " when it should be size=" + argNames.size());
}
String path = route.getFullPath();
for (String name : argNames) {
String value = keysToValues.get(name);
if (value == null)
throw new IllegalArgumentException("Method='" + method + "' returns a Redirect that is missing argument key=" + name + " to form the url on the redirect");
path = path.replace("{" + name + "}", value);
}
RedirectResponse redirectResponse = new RedirectResponse(isAjaxRedirect, request.isHttps, request.domain, request.port, path);
return wrapFunctionInContext(() -> responseCb.sendRedirect(redirectResponse));
}
use of org.webpieces.router.api.exceptions.IllegalReturnValueException in project webpieces by deanhiller.
the class ResponseProcessorHtml method createRenderResponse.
public XFuture<Void> createRenderResponse(MethodMeta meta, RenderImpl controllerResponse, ProxyStreamHandle handle) {
RequestContext ctx = meta.getCtx();
RouterRequest request = ctx.getRequest();
LoadedController loadedController = meta.getLoadedController();
Method method = loadedController.getControllerMethod();
// not broken)
if (HttpMethod.POST == request.method) {
throw new IllegalReturnValueException("Controller method='" + method + "' MUST follow the PRG " + "pattern(https://en.wikipedia.org/wiki/Post/Redirect/Get) so " + "users don't have a poor experience using your website with the browser back button. " + "This means on a POST request, you cannot return RenderHtml object and must return Redirects");
}
String controllerName = loadedController.getControllerInstance().getClass().getName();
String methodName = loadedController.getControllerMethod().getName();
String relativeOrAbsolutePath = controllerResponse.getRelativeOrAbsolutePath();
if (relativeOrAbsolutePath == null) {
relativeOrAbsolutePath = methodName + ".html";
}
Map<String, Object> pageArgs = controllerResponse.getPageArgs();
// Add context as a page arg:
pageArgs.put("_context", ctx);
pageArgs.put("_session", ctx.getSession());
pageArgs.put("_flash", ctx.getFlash());
pageArgs.put("_appContext", ctx.getApplicationContext());
View view = new View(controllerName, methodName, relativeOrAbsolutePath);
RenderResponse resp = new RenderResponse(view, pageArgs, RouteType.HTML);
return handle.sendRenderHtml(resp);
}
Aggregations