package demo.servlet;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.simple.JSONArray;
import com.renren.api.client.RenrenApiClient;
import com.renren.api.client.param.impl.AccessToken;
import demo.model.User;
/**
* Servlet implementation class StatusServlet
*/
public class StatusServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public StatusServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
User user = (User) request.getSession().getAttribute("user");
if (user == null) {
//未登录,跳转到登录页面去
response.sendRedirect("login");
return;
}
String accessToken =(String)request.getSession().getAttribute("access_token");
if(accessToken==null){
response.sendRedirect("login");
return;
}
RenrenApiClient apiClient = RenrenApiClient.getInstance();
//得到当前用户的状态列表
JSONArray statuses=apiClient.getStatusService().getStatuses(0 ,1, 20, new AccessToken(accessToken));
request.setAttribute("statuslist", statuses);
request.setAttribute("user", user);
RequestDispatcher welcomeDispatcher = request.getRequestDispatcher("/views/status.jsp");
welcomeDispatcher.forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("UTF-8");
User user = (User) request.getSession().getAttribute("user");
if (user == null) {
//未登录,跳转到登录页面去
response.sendRedirect("login");
return;
}
String accessToken =(String)request.getSession().getAttribute("access_token");
if(accessToken==null){
response.sendRedirect("login");
return;
}
String status=(String)request.getParameter("status");
RenrenApiClient client=RenrenApiClient.getInstance();
try{
client.getStatusService().setStatus(status, new AccessToken(accessToken));
response.sendRedirect("status");
}catch(Exception e){
request.setAttribute("errMsg", e.getMessage());
RequestDispatcher welcomeDispatcher = request.getRequestDispatcher("/views/error.jsp");
welcomeDispatcher.forward(request, response);
}
}
}