Search in sources :

Example 1 with IllegalReturnValueException

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));
}
Also used : IllegalReturnValueException(org.webpieces.router.api.exceptions.IllegalReturnValueException) Method(java.lang.reflect.Method) HttpMethod(org.webpieces.ctx.api.HttpMethod) RenderResponse(org.webpieces.router.api.dto.RenderResponse) View(org.webpieces.router.api.dto.View) RouterRequest(org.webpieces.ctx.api.RouterRequest)

Example 2 with IllegalReturnValueException

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;
}
Also used : Http2Response(com.webpieces.hpack.api.dto.Http2Response) IllegalReturnValueException(org.webpieces.router.api.exceptions.IllegalReturnValueException) Http2Header(com.webpieces.http2parser.api.dto.lib.Http2Header)

Example 3 with IllegalReturnValueException

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;
}
Also used : IllegalReturnValueException(org.webpieces.router.api.exceptions.IllegalReturnValueException) HashMap(java.util.HashMap) ObjectStringConverter(org.webpieces.router.api.extensions.ObjectStringConverter)

Example 4 with IllegalReturnValueException

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));
}
Also used : IllegalReturnValueException(org.webpieces.router.api.exceptions.IllegalReturnValueException) RouteMeta(org.webpieces.router.impl.RouteMeta) RedirectResponse(org.webpieces.router.api.dto.RedirectResponse) Method(java.lang.reflect.Method) HttpMethod(org.webpieces.ctx.api.HttpMethod) Route(org.webpieces.router.impl.Route) RouterRequest(org.webpieces.ctx.api.RouterRequest)

Example 5 with IllegalReturnValueException

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);
}
Also used : IllegalReturnValueException(org.webpieces.router.api.exceptions.IllegalReturnValueException) LoadedController(org.webpieces.router.impl.loader.LoadedController) RequestContext(org.webpieces.ctx.api.RequestContext) HttpMethod(org.webpieces.ctx.api.HttpMethod) Method(java.lang.reflect.Method) RenderResponse(org.webpieces.router.impl.dto.RenderResponse) View(org.webpieces.router.impl.dto.View) RouterRequest(org.webpieces.ctx.api.RouterRequest)

Aggregations

IllegalReturnValueException (org.webpieces.router.api.exceptions.IllegalReturnValueException)7 Method (java.lang.reflect.Method)3 HttpMethod (org.webpieces.ctx.api.HttpMethod)3 RouterRequest (org.webpieces.ctx.api.RouterRequest)3 Http2Response (com.webpieces.hpack.api.dto.Http2Response)1 Http2Response (com.webpieces.http2.api.dto.highlevel.Http2Response)1 Http2Header (com.webpieces.http2.api.dto.lowlevel.lib.Http2Header)1 Http2Header (com.webpieces.http2parser.api.dto.lib.Http2Header)1 HashMap (java.util.HashMap)1 RequestContext (org.webpieces.ctx.api.RequestContext)1 RedirectResponse (org.webpieces.router.api.dto.RedirectResponse)1 RenderResponse (org.webpieces.router.api.dto.RenderResponse)1 View (org.webpieces.router.api.dto.View)1 ObjectStringConverter (org.webpieces.router.api.extensions.ObjectStringConverter)1 Route (org.webpieces.router.impl.Route)1 RouteMeta (org.webpieces.router.impl.RouteMeta)1 RenderResponse (org.webpieces.router.impl.dto.RenderResponse)1 View (org.webpieces.router.impl.dto.View)1 LoadedController (org.webpieces.router.impl.loader.LoadedController)1 PortAndIsSecure (org.webpieces.router.impl.routeinvoker.PortAndIsSecure)1