jp.gihyo.wicket.AppSession.java Source code

Java tutorial

Introduction

Here is the source code for jp.gihyo.wicket.AppSession.java

Source

/*
 *  Copyright 2009 Tsutomu YANO.
 * 
 *  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.
 *  under the License.
 */

package jp.gihyo.wicket;

import javax.servlet.http.HttpServletResponse;
import org.apache.wicket.Application;
//import org.apache.wicket.Request;
import org.apache.wicket.Session;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.protocol.http.WebSession;
import org.apache.wicket.request.Request;

import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.User;

/**
 *
 * @author Tsutomu YANO
 */
public class AppSession extends WebSession {
    private static final long serialVersionUID = 8771090050951362338L;

    private Twitter twitterSession;
    private User twitterUser;
    private String lastUnauthorizedMessage;

    public AppSession(Request request) {
        super(request);
    }

    //    @SuppressWarnings("deprecation")
    //    public AppSession(WebApplication application, Request request) {
    //        super(application, request);
    //    }
    //
    //    @SuppressWarnings("deprecation")
    //    public AppSession(Application application, Request request) {
    //        super(application, request);
    //    }

    public boolean login(String userName, String password) {
        Twitter client = new TwitterClient(userName, password);
        try {
            User user = client.verifyCredentials();
            if (user != null) {
                twitterUser = user;
                twitterSession = client;
                return true;
            }
        } catch (TwitterException ex) {
            if (ex.getStatusCode() == HttpServletResponse.SC_UNAUTHORIZED) {
                lastUnauthorizedMessage = ex.getMessage();
                return false;
            }
        }
        return false;
    }

    public boolean isLoggedIn() {
        return twitterSession != null && twitterUser != null;
    }

    public Twitter getTwitterSession() {
        return twitterSession;
    }

    public User getTwitterUser() {
        return twitterUser;
    }

    public String getLastUnauthorizedMessage() {
        return lastUnauthorizedMessage;
    }

    public static AppSession get() {
        return (AppSession) Session.get();
    }
}