com.threewks.thundr.user.controller.AuthenticatedInterceptor.java Source code

Java tutorial

Introduction

Here is the source code for com.threewks.thundr.user.controller.AuthenticatedInterceptor.java

Source

/*
 * This file is a component of thundr, a software library from 3wks.
 * Read more: http://www.3wks.com.au/thundr
 * Copyright (C) 2015 3wks, <thundr@3wks.com.au>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.threewks.thundr.user.controller;

import org.apache.commons.lang3.StringUtils;

import com.threewks.thundr.http.StatusCode;
import com.threewks.thundr.http.exception.HttpStatusException;
import com.threewks.thundr.request.Request;
import com.threewks.thundr.request.Response;
import com.threewks.thundr.route.controller.Interceptor;
import com.threewks.thundr.session.Session;
import com.threewks.thundr.session.SessionService;
import com.threewks.thundr.view.View;

public class AuthenticatedInterceptor extends BaseAuthenticationFilterceptor implements Interceptor<Authenticated> {

    public AuthenticatedInterceptor(SessionService<?> sessionService) {
        super(sessionService);
    }

    @SuppressWarnings("unchecked")
    @Override
    public View before(Authenticated annotation, Request req, Response resp) {
        Session session = getSession(req, resp);
        if (session != null && session.isAuthenticated()) {
            return null;
        }
        String redirect = StringUtils.trimToNull(annotation.redirect());
        if (redirect != null) {
            return redirectToRouteOrPath(redirect);
        }
        throw new HttpStatusException(StatusCode.Unauthorized, "User not identified");
    }

    @Override
    public <T> T after(Authenticated annotation, Object result, Request req, Response resp) {
        return null;
    }

    @Override
    public <T> T exception(Authenticated annotation, Exception e, Request req, Response resp) {
        return null;
    }
}