setRootRequestMapper(new HttpsMapper(getRootRequestMapper(), new HttpsConfig(8080, 8443)){ @Override public Url mapHandler(IRequestHandler requestHandler) { Url url = super.mapHandler(requestHandler); if ("https".equals(url.getProtocol)){ // Force the HostName for HTTPS requests url.setHost("securepage.example.com"); } return url; } });
@Override public final IRequestHandler mapRequest(Request request) { IRequestHandler handler = delegate.mapRequest(request); Scheme desired = getDesiredSchemeFor(handler); Scheme current = getSchemeOf(request); if (!desired.isCompatibleWith(current)) { // we are currently on the wrong scheme for this handler // construct a url for the handler on the correct scheme String url = createRedirectUrl(handler, request, desired); // replace handler with one that will redirect to the created url handler = createRedirectHandler(url); } return handler; }
/** * Checks if the specified {@code type} has the {@link RequireHttps} annotation * * @param type * @return {@code true} iff {@code type} has the {@link RequireHttps} annotation */ private boolean hasSecureAnnotation(Class<?> type) { if (type.getAnnotation(RequireHttps.class) != null) { return true; } for (Class<?> iface : type.getInterfaces()) { if (hasSecureAnnotation(iface)) { return true; } } if (type.getSuperclass() != null) { return hasSecureAnnotation(type.getSuperclass()); } return false; }
/** * Creates a url for the handler. Modifies it with the correct {@link Scheme} if necessary. * * @param handler * @param request * @return url */ final Url mapHandler(IRequestHandler handler, Request request) { Url url = delegate.mapHandler(handler); Scheme desired = getDesiredSchemeFor(handler); Scheme current = getSchemeOf(request); if (!desired.isCompatibleWith(current)) { // the generated url does not have the correct scheme, set it (which in turn will cause // the url to be rendered in its full representation) url.setProtocol(desired.urlName()); url.setPort(desired.getPort(config)); } return url; }
/** * Figures out which {@link Scheme} should be used to access the request handler * * @param handler * request handler * @return {@link Scheme} */ protected Scheme getDesiredSchemeFor(IRequestHandler handler) { if (handler instanceof IPageClassRequestHandler) { return getDesiredSchemeFor(((IPageClassRequestHandler)handler).getPageClass()); } return Scheme.ANY; }
@Override protected Scheme getDesiredSchemeFor(IRequestHandler handler) { Request request = RequestCycle.get().getRequest(); return super.getSchemeOf(request); } });
@Override public final Url mapHandler(IRequestHandler handler) { return mapHandler(handler, RequestCycle.get().getRequest()); }
/** * Creates a url for the handler. Modifies it with the correct {@link Scheme} if necessary. * * @param handler * @param request * @return url */ final Url mapHandler(IRequestHandler handler, Request request) { Url url = delegate.mapHandler(handler); Scheme desired = getDesiredSchemeFor(handler); Scheme current = getSchemeOf(request); if (!desired.isCompatibleWith(current)) { // the generated url does not have the correct scheme, set it (which in turn will cause // the url to be rendered in its full representation) url.setProtocol(desired.urlName()); url.setPort(desired.getPort(config)); } return url; }
/** * Figures out which {@link Scheme} should be used to access the request handler * * @param handler * request handler * @return {@link Scheme} */ protected Scheme getDesiredSchemeFor(IRequestHandler handler) { if (handler instanceof IPageClassRequestHandler) { return getDesiredSchemeFor(((IPageClassRequestHandler)handler).getPageClass()); } return Scheme.ANY; }
@Override public final Url mapHandler(IRequestHandler handler) { return mapHandler(handler, RequestCycle.get().getRequest()); }
@Override public final IRequestHandler mapRequest(Request request) { IRequestHandler handler = delegate.mapRequest(request); Scheme desired = getDesiredSchemeFor(handler); Scheme current = getSchemeOf(request); if (!desired.isCompatibleWith(current)) { // we are currently on the wrong scheme for this handler // construct a url for the handler on the correct scheme String url = createRedirectUrl(handler, request, desired); // replace handler with one that will redirect to the created url handler = createRedirectHandler(url); } return handler; }
/** * Sets the root request mapper for the given application from the given httpPort and httpsPort. * * @param application * the application * @param httpPort * the http port * @param httpsPort * the https port * @return the i request mapper */ public static IRequestMapper setRootRequestMapper(final Application application, final int httpPort, final int httpsPort) { final IRequestMapper httpsMapper = new HttpsMapper(application.getRootRequestMapper(), new HttpsConfig(httpPort, httpsPort)); application.setRootRequestMapper(httpsMapper); return httpsMapper; }
@Override protected Scheme getDesiredSchemeFor(final Class<? extends IRequestablePage> pageClass) { if (application.getConfigurationType().equals(RuntimeConfigurationType.DEVELOPMENT)) { // is in development mode, returning Scheme.HTTP... return Scheme.HTTP; } else { // not in development mode, letting the mapper decide return super.getDesiredSchemeFor(pageClass); } } });
/** * Checks if the specified {@code type} has the {@link RequireHttps} annotation * * @param type * @return {@code true} iff {@code type} has the {@link RequireHttps} annotation */ private boolean hasSecureAnnotation(Class<?> type) { if (type.getAnnotation(RequireHttps.class) != null) { return true; } for (Class<?> iface : type.getInterfaces()) { if (hasSecureAnnotation(iface)) { return true; } } if (type.getSuperclass() != null) { return hasSecureAnnotation(type.getSuperclass()); } return false; }
final int httpPort, final int httpsPort) application.setRootRequestMapper(new HttpsMapper(application.getRootRequestMapper(), new HttpsConfig(httpPort, httpsPort))
/** * Determines which {@link Scheme} should be used to access the page * * @param pageClass * type of page * @return {@link Scheme} */ protected Scheme getDesiredSchemeFor(Class<? extends IRequestablePage> pageClass) { if (pageClass == null) { return Scheme.ANY; } Scheme SCHEME = cache.get(pageClass); if (SCHEME == null) { if (hasSecureAnnotation(pageClass)) { SCHEME = Scheme.HTTPS; } else { SCHEME = Scheme.HTTP; } cache.put(pageClass, SCHEME); } return SCHEME; }
/** * Determines which {@link Scheme} should be used to access the page * * @param pageClass * type of page * @return {@link Scheme} */ protected Scheme getDesiredSchemeFor(Class<? extends IRequestablePage> pageClass) { if (pageClass == null) { return Scheme.ANY; } Scheme SCHEME = cache.get(pageClass); if (SCHEME == null) { if (hasSecureAnnotation(pageClass)) { SCHEME = Scheme.HTTPS; } else { SCHEME = Scheme.HTTP; } cache.put(pageClass, SCHEME); } return SCHEME; }