summaryrefslogtreecommitdiff
path: root/src/main/java/com/p4square/session/SessionCheckingAuthenticator.java
blob: 489d6a0018926778ba974d0c55debcaba0fd6951 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
 * Copyright 2013 Jesse Morgan
 */

package com.p4square.session;

import org.apache.log4j.Logger;

import org.restlet.Context;
import org.restlet.Request;
import org.restlet.Response;
import org.restlet.security.Authenticator;

/**
 * Authenticator which succeeds if a valid Session exists.
 *
 * @author Jesse Morgan <jesse@jesterpm.net>
 */
public class SessionCheckingAuthenticator extends Authenticator {
    private static final Logger LOG = Logger.getLogger(SessionCheckingAuthenticator.class);

    public SessionCheckingAuthenticator(Context context, boolean optional) {
        super(context, optional);
    }

    protected boolean authenticate(Request request, Response response) {
        Session s = Sessions.getInstance().get(request);

        if (s != null) {
            LOG.debug("Found session for user " + s.getUser());
            request.getClientInfo().setUser(s.getUser());
            return true;

        } else {
            return false;
        }
    }

}