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 com.renren.api.client.services.RenrenApiException;
import demo.model.User;
/**
* Servlet implementation class FeedServlet
*/
public class FeedServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public FeedServlet() {
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 feeds=apiClient.getFeedService().getFeed("10,11,20,21,22,23,30,31,32,33,34,35,36,40,41,50,51,52,53,54,55", 0, 1, 10,new AccessToken(accessToken));
request.setAttribute("feedlist", feeds);
RequestDispatcher welcomeDispatcher = request.getRequestDispatcher("/views/feed.jsp");
welcomeDispatcher.forward(request, response);
}
/**
* 使用SDK发布人人网新鲜事
*/
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;
}
RenrenApiClient apiClient = RenrenApiClient.getInstance();
String action_name="通过开发者的应用发布";
String action_link="http://dev.renren.com";
String message=(String)request.getParameter("message");
String name=(String)request.getParameter("name");
String description=(String)request.getParameter("description");
String url=(String)request.getParameter("url");
String image=(String)request.getParameter("image");
String caption=(String)request.getParameter("caption");
try{
apiClient.getFeedService().publicFeed(name, description, url, image, caption, action_name, action_link, message,new AccessToken(accessToken));
response.sendRedirect("feed");
}catch(RenrenApiException e){
//TODO 处理发布失败
request.setAttribute("errMsg", e.getMessage());
RequestDispatcher welcomeDispatcher = request.getRequestDispatcher("/views/error.jsp");
welcomeDispatcher.forward(request, response);
}
}
}