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;
}
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);
}
Aggregations