1

I have a restlet-based app running on heroku that I want to use SSL. I have enabled the basic "piggyback" ssl option, so it accepts ssl connections -- almost there! However, I want some (actually, probably all) URLs to only work if the user connects via ssl. As it is now (I am told) Heroku converts SSL to non-ssl calls and sets the HTTP_X_FORWARDED_PROTO to https, so I think I need to deal with this in restlet, but I'm not sure how. I see the Redirector class, but it's not clear to me if I can use this class to solve this problem.

Note: I was previously running my app using GAE, which used a servlet container, so I could specify "CONFIDENTIAL" in web.xml.

Thanks!

1 Answer 1

2

You can get the information out of the HTTP request headers and then decide if you want to throw an error or redirect:

    Form requestHeaders = (Form) this.getRequest().getAttributes().get("org.restlet.http.headers");

    boolean secure = false;
    if (requestHeaders.getValues("x-forwarded-proto") != null) {
        secure = requestHeaders.getValues("x-forwarded-proto").contains("https");
    }

Expanding on this you can create a Filter that can easily be applied to a route. A complete code example is on GitHub. But here is the basic SecureFilter:

public class SecureFilter extends Filter {

    private boolean doRedirect;

    public SecureFilter(Context context, Restlet next) {
        super(context);
        doRedirect = false;
        setNext(next);
    }

    public SecureFilter(Context context, Restlet next, boolean doRedirect) {
        super(context);
        this.doRedirect = doRedirect;
        setNext(next);
    }

    public SecureFilter(Context context, Class<?> next) {
        super(context);
        doRedirect = false;
        setNext(next);
    }

    public SecureFilter(Context context, Class<?> next, boolean doRedirect) {
        super(context);
        this.doRedirect = doRedirect;
        setNext(next);
    }

    public boolean isDoRedirect() {
        return doRedirect;
    }

    public void setDoRedirect(boolean doRedirect) {
        this.doRedirect = doRedirect;
    }

    @Override
    protected int beforeHandle(Request request, Response response) {
        Form requestHeaders = (Form) request.getAttributes().get("org.restlet.http.headers");

        if ((requestHeaders.getValues("x-forwarded-proto") != null) && (requestHeaders.getValues("x-forwarded-proto").indexOf("https") != 0)) {
            if (doRedirect) {
                String target = "https://" + request.getHostRef().getHostDomain() + request.getResourceRef().getPath();
                Redirector redirector = new Redirector(getContext(), target, Redirector.MODE_CLIENT_SEE_OTHER);
                redirector.handle(request, response);
                return STOP;
            }
            else {
                response.setStatus(Status.CLIENT_ERROR_FORBIDDEN);
                return STOP;
            }
        }

        return CONTINUE;
    }

}

To use the SecureFilter, simply wrap the route / resource mappings:

    router.attach("/secure", new SecureFilter(getContext(), HelloSecureResource.class));
    router.attach("/secureWithRedirect", new SecureFilter(getContext(), HelloSecureResource.class, true));
7
  • This is fine for handling one or two calls, but my API has at least a dozen resources, all of which accept at least one method (most accept more). I would like to do this in one place, rather than everywhere where I handle a call. Feb 6, 2012 at 21:28
  • Do you know if RESTlet has a global interceptor / filter?
    – James Ward
    Feb 6, 2012 at 21:37
  • That is what I'm looking for. It has a Redirector class, but I don't see how to use that to accomplish what I want. Perhaps I can use a "org.restlet.service.Service", but, again, not clear how. Feb 6, 2012 at 23:30
  • I figured this out. Check out the SecureFilter and the HelloApplication examples I just added to the ssl_verify branch of my restlet-template example project.
    – James Ward
    Feb 7, 2012 at 1:19
  • Thanks James. Gonna look into this! Feb 7, 2012 at 20:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.