Beispiel #1
0
  @RequestMapping(value = "/home")
  public String home(ModelMap modelMap) {
    HttpServletRequest request =
        ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    System.out.println("HomeController: passing through...");
    System.out.println("获取相关renren参数");
    String sessionKey = request.getParameter("xn_sig_session_key");
    String renrenUserId = request.getParameter("xn_sig_user");
    System.out.println("Session Key: " + sessionKey);
    System.out.println("renrenUserId: " + renrenUserId);
    if (sessionKey != null && renrenUserId != null) {
      RenrenApiClient apiClient = new RenrenApiClient(sessionKey);
      SessionKey sessionKeyReal = new SessionKey(sessionKey);
      // 获取用户信息
      JSONArray userInfo = apiClient.getUserService().getInfo(renrenUserId, sessionKeyReal);
      if (userInfo != null && userInfo.size() > 0) {
        JSONObject currentUser = (JSONObject) userInfo.get(0);
        if (currentUser != null) {
          String userName = (String) currentUser.get("name");
          String userHead = (String) currentUser.get("headurl");
          request.setAttribute("userName", userName);
          request.setAttribute("userHead", userHead);
        }
      }
      JSONArray friends = apiClient.getFriendsService().getFriends(2, 80, sessionKeyReal);
      String friendsStr = friends.toJSONString().replace("\"", "\'");
      request.setAttribute("friends", friendsStr);
    }

    // 测试commit

    modelMap.addAttribute("welcome", "Walter's APP");
    return "home";
  }
	public Boolean addFeedUser(String sessionKey,String token){
		RenrenApiClient apiClient = RenrenApiClient.getInstance();
		JSONArray feedInfo = new JSONArray();
		String renrenUserId;
		try {
			
			renrenUserId = String.valueOf(apiClient.getUserService().getLoggedInUser(new AccessToken(sessionKey)));
		} catch (Exception err){
			err.printStackTrace();
			return false;
		}
		try {
			feedInfo = apiClient.getFeedService().getFeed("10", Integer.parseInt(renrenUserId), 1, 30,new AccessToken(sessionKey)); //可能拿不到
		} catch (Exception err){
			err.printStackTrace();
			return false;
		}
		Set<String>	messages = new HashSet<String>();
		if (feedInfo != null && feedInfo.size()>0) {
			for (int i=0;i<feedInfo.size();i++)
			{
				JSONObject currentFeed = (JSONObject) feedInfo.get(i);
				assert (currentFeed != null);
				String message = (String) currentFeed.get("message");
				messages.add(message);		
			}
		}
		UserSpace t = new UserSpace(sessionKey,renrenUserId,messages,token);
		if (!up.offer(t)) return false;
		ApiInitListener.User.put(config.properties.irt.getUserIdByToken(token),t);
		
		return true;
	}
	public void run()
	{
		RenrenApiClient apiClient = RenrenApiClient.getInstance();
		int defaultsleeptime = 100000;
		int sleeptime = defaultsleeptime;
			while (true) {
				try{
					getInfo(apiClient);
					sleeptime = defaultsleeptime;
					Thread.sleep(sleeptime);
				} catch(Exception e) {
					sleeptime = sleeptime * 2;
				}	
		}
	}
	private void getInfo(RenrenApiClient apiClient) {
		if (up.size()!=0){
			Set<String>	messages = new HashSet<String>();
			UserSpace t = up.poll();
			String sessionKey = t.getSessionKey();
			String renrenUserId = t.getRenrenUserId();
			message = t.getMessage();
			
			messages.addAll(message); //保证接收过来的信息也在用户空间中
			//refreshToken()			  //刷新token 保证token有效
			
			JSONArray feedInfo = new JSONArray();
			try {
			feedInfo = apiClient.getFeedService().getFeed("10", Integer.parseInt(renrenUserId), 1, 10,new AccessToken(sessionKey));
						
			if (feedInfo != null && feedInfo.size()>0) {
		
				for (int i=0;i<feedInfo.size();i++)
				{
					JSONObject currentFeed = (JSONObject) feedInfo.get(i);

					if (currentFeed != null){
						String message = (String) currentFeed.get("message");
						messages.add(message);
						System.out.println("[FeedStub : run]: "+"messsage: "+message);
						if (!this.message.contains(message)){
							Map<String,String> map = new HashMap();
							map.put("message", URLEncoder.encode(message));
							map.put("from",URLEncoder.encode("renren"));
							try {
								config.properties.irt.send(t.getToken(),"updatestatus", map);
							} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
							}
							System.out.println("[FeedStub : run]: "+"messsage: "+message);
						}
					}
				}
			}
			t.updateMessage(messages);
			if (!up.offer(t)) System.out.println("[FeedStub : run] Memery Out");
			} catch(Exception e){
				//获取info错误,用户退出
			}
		}
	}