Ejemplo n.º 1
0
 private Response postToLogin(String username, String password, String[] captchaData)
     throws ConnectionException {
   try {
     Map<String, String> data = new HashMap<>();
     Document loginDocument = Jsoup.connect(Endpoints.LOGIN_URL.url()).get();
     Element loginForm = loginDocument.getElementById("loginForm");
     for (Element input : loginForm.getElementsByTag("input")) {
       data.put(input.attr("name"), input.attr("value"));
     }
     Date now = new Date();
     data.put("timezone_field", new SimpleDateFormat("XXX").format(now).replace(':', '|'));
     data.put("username", username);
     data.put("password", password);
     data.put("js_time", String.valueOf(now.getTime() / 1000));
     if (captchaData.length > 0) {
       data.put("hip_solution", captchaData[0]);
       data.put("hip_token", captchaData[1]);
       data.put("fid", captchaData[2]);
       data.put("hip_type", "visual");
       data.put("captcha_provider", "Hip");
     } else {
       data.remove("hip_solution");
       data.remove("hip_token");
       data.remove("fid");
       data.remove("hip_type");
       data.remove("captcha_provider");
     }
     return Jsoup.connect(Endpoints.LOGIN_URL.url()).data(data).method(Method.POST).execute();
   } catch (IOException e) {
     throw ExceptionHandler.generateException("While submitting credentials", e);
   }
 }
Ejemplo n.º 2
0
 @Override
 public GroupChat createGroupChat(Contact... contacts) throws ConnectionException {
   JsonObject obj = new JsonObject();
   JsonArray allContacts = new JsonArray();
   allContacts.add(new JsonObject().add("id", "8:" + this.getUsername()).add("role", "Admin"));
   for (Contact contact : contacts) {
     allContacts.add(new JsonObject().add("id", "8:" + contact.getUsername()).add("role", "User"));
   }
   obj.add("members", allContacts);
   HttpURLConnection con =
       Endpoints.THREAD_URL
           .open(this)
           .as(HttpURLConnection.class)
           .expect(201, "While creating group chat")
           .post(obj);
   String url = con.getHeaderField("Location");
   Matcher chatMatcher = URL_PATTERN.matcher(url);
   if (chatMatcher.find()) {
     String id = chatMatcher.group(1);
     while (this.getChat(id) == null) ;
     return (GroupChat) this.getChat(id);
   } else {
     throw ExceptionHandler.generateException("No chat location", con);
   }
 }