I am doing a facebook application in wicket. To get facebook authentication
token, I have written a wicket filter as below:



package com.mycompany.myapp.config;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.visural.common.StringUtil;

public class FBOAuth implements Filter {
    public void init(FilterConfig fc) throws ServletException {
    }

    public void doFilter(ServletRequest sr, ServletResponse sr1, FilterChain
fc)
            throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) sr;
        HttpServletResponse res = (HttpServletResponse) sr1;
        res.sendRedirect(FaceBook.getLoginRedirectURL());
        String code = sr.getParameter("code");
        if (StringUtil.isNotBlankStr(code)) {
            String authURL = FaceBook.getAuthURL(code);
            URL url = new URL(authURL);
            try {
                String result = readURL(url);
                String accessToken = null;
                Integer expires = null;
                String[] pairs = result.split("&");
                for (String pair : pairs) {
                    String[] kv = pair.split("=");
                    if (kv.length != 2) {
                        throw new RuntimeException("Unexpected auth
response");
                    } else {
                        if (kv[0].equals("access_token")) {
                            accessToken = kv[1];
                        }
                        if (kv[0].equals("expires")) {
                            expires = Integer.valueOf(kv[1]);
                        }
                    }
                }
                if (accessToken != null && expires != null)
{
                    HttpSession ses = req.getSession();
                    ses.setAttribute("authToken", accessToken);


                } else {
                    throw new RuntimeException(
                            "Access token and expires not found");
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

        }
    }
    private String readURL(URL url) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        InputStream is = url.openStream();
        int r;
        while ((r = is.read()) != -1) {
            baos.write(r);
        }
        return new String(baos.toByteArray());
    }

    public void destroy() {
    }
}





In the doFilter method, I have done all the redirects required and gotten
the authentication token(accesstoken). At this point, I store the value as
an attribute to the httpSession which i have gotten by
ServleteRequest.getSession();

I want to acess this token in a wicket page class so that I use RestFB to
create a FaceBook Client etc. This is what I have done in the wicket page
class:

HttpSession session = null;

    if (RequestCycle.get()!= null)
        session = ((WebRequest) RequestCycle.get().getRequest())
                .getHttpServletRequest().getSession();

    if (session != null) {

        System.out.print("################# AuthToken: "
                + session.getAttribute("authToken"));

    }


However, this is the output I get and I am totally stuck:

################# AuthToken: null


Where am i going wrong?

Reply via email to