Package com.jeck.microblogging.server

Source Code of com.jeck.microblogging.server.SinaAuthImpl

package com.jeck.microblogging.server;

import java.io.IOException;
import java.util.Map;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import weibo4j.Oauth;
import weibo4j.http.AccessToken;
import weibo4j.model.WeiboException;

import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;
import com.jeck.microblogging.persistence.entities.User;
import com.jeck.microblogging.shared.interfaces.IAuth;
import com.jeck.microblogging.shared.interfaces.ICallBackParameter;
import com.jeck.microblogging.shared.interfaces.IWebRedirect;
import com.jeck.microblogging.utils.StoreServiceDAO;

public class SinaAuthImpl extends HttpServlet implements IAuth,IWebRedirect {
  /**
   *
   */
  private static final long serialVersionUID = -5594911738919677488L;
  private static final String FQDN="http://jeck-microblogging.appspot.com";
  private static final String WELCOME_PAGE="main.html";
  private static final String THIS_PAGE="microblogging/sinaAuth";
  private final StoreServiceDAO storeService;
  private static Logger logger = Logger.getLogger(SinaAuthImpl.class.getName());
 
  public SinaAuthImpl() {
    this.storeService = new StoreServiceDAO();
  }
 
  @Override
  public String getAccessToken() {
    return null;
  }
 
  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    logger.info("code:"+req.getParameter("code"));
    if (req.getParameter("code")!=null){
      redirectMainPage(req,resp);
    }else{
      redirectAuthPage(req,resp);
    }
  }
 
  private void redirectMainPage(HttpServletRequest req,
      HttpServletResponse resp){
    logger.info("redirectMainPage");
    ICallBackParameter para=new CallBackParameterImpl(req.getSession());
    Oauth oauth = new Oauth();
    try {
      AccessToken at = oauth.getAccessTokenByCode(req.getParameter("code"));
      para.putParameter(ICallBackParameter.ACCESS_TOKEN, at);
      logger.info("Access Token:"+at);
      User user=storeService.find(User.class,at.getAccessToken());
      if (user == null){
        user = new User();
        user.setAccessToken(at.getAccessToken());
        storeService.store(user);
      }else {
        logger.info("user:"+user);
      }
    } catch (WeiboException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    try {
      resp.sendRedirect(FQDN+"/"+WELCOME_PAGE);
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }

  @Override
  public void redirectAuthPage(HttpServletRequest req,
      HttpServletResponse resp) throws IOException {
    // TODO Auto-generated method stub
    Oauth oauth = new Oauth();
    try {
      resp.sendRedirect(oauth.authorize("code"));
      logger.info("redirectAuthPage");
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
}
TOP

Related Classes of com.jeck.microblogging.server.SinaAuthImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.