Search in sources :

Example 6 with IllegalReturnValueException

use of org.webpieces.router.api.exceptions.IllegalReturnValueException in project webpieces by deanhiller.

the class ResponseCreator method createRedirect.

public Http2Response createRedirect(Http2Request request, RedirectResponse httpResponse) {
    Http2Response response;
    if (httpResponse.isAjaxRedirect) {
        response = addCommonHeaders(request, null, true, Constants.AJAX_REDIRECT_CODE, "Ajax Redirect");
    } else {
        response = addCommonHeaders(request, null, true, StatusCode.HTTP_303_SEE_OTHER.getCode(), StatusCode.HTTP_303_SEE_OTHER.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);
    // 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.http2.api.dto.highlevel.Http2Response) IllegalReturnValueException(org.webpieces.router.api.exceptions.IllegalReturnValueException) Http2Header(com.webpieces.http2.api.dto.lowlevel.lib.Http2Header)

Example 7 with IllegalReturnValueException

use of org.webpieces.router.api.exceptions.IllegalReturnValueException in project webpieces by deanhiller.

the class ReverseRoutes method routeToUrl.

// for redirects
public UrlInfo routeToUrl(RouteId routeId, Method method, Map<String, Object> args, RequestContext ctx, HttpPort requestedPort) {
    ReversableRouter routeMeta = get(routeId);
    if (routeMeta == null)
        throw new IllegalReturnValueException("Route=" + routeId + " returned from method='" + method + "' was not added in the RouterModules");
    MatchInfo matchInfo = routeMeta.getMatchInfo();
    if (!matchInfo.matchesMethod(HttpMethod.GET))
        throw new IllegalReturnValueException("method='" + method + "' is trying to redirect to routeid=" + routeId + " but that route is not a GET method route and must be");
    Map<String, String> keysToValues = reverseTranslator.formMap(method, matchInfo.getPathParamNames(), args);
    Set<String> keySet = keysToValues.keySet();
    List<String> argNames = matchInfo.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 = matchInfo.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);
    }
    PortAndIsSecure info = redirectFormation.calculateInfo(matchInfo, requestedPort, ctx.getRequest());
    boolean isSecure = info.isSecure();
    int port = info.getPort();
    return new UrlInfo(isSecure, port, path);
}
Also used : IllegalReturnValueException(org.webpieces.router.api.exceptions.IllegalReturnValueException) MatchInfo(org.webpieces.router.impl.routers.MatchInfo) PortAndIsSecure(org.webpieces.router.impl.routeinvoker.PortAndIsSecure)

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