All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bitbucket.rosseti.http.client.Portal Maven / Gradle / Ivy

package org.bitbucket.rosseti.http.client;

import org.bitbucket.javatek.email.Email;
import org.bitbucket.jsoup.form.Form;
import org.jsoup.HttpStatusException;

import java.io.IOException;

import static org.bitbucket.rosseti.http.client.Constants.LOGIN_URL;


/**
 * todo: останавливать обработку по thread.isInterrupted()
 */
public final class Portal {
  public Cabinet login(Email email, String password) throws IOException, LoginFailed, PortalNotAvailable {
    Session session = new Session();

    try {
      session.post(
        LOGIN_URL,
        new Form()
          .with("javax.faces.partial.ajax", "true")
          .with("javax.faces.partial.execute", "")
      );

      PartialResponse response =
        session.post(
          LOGIN_URL,
          new Form()
            .with("javax.faces.partial.ajax", "true")
            .with("javax.faces.source", "workplaceTopForm:loginBtn")
            .with("javax.faces.partial.execute",
              "workplaceTopForm:j_mail_login",
              "workplaceTopForm:j_password",
              "workplaceTopForm:choicePanelAuth",
              "workplaceTopForm:loginBtn"
            )
            .with("workplaceTopForm:loginBtn", "workplaceTopForm:loginBtn")
            .with("workplaceTopForm:choicePanelAuth", "email")
            .with("workplaceTopForm:j_mail_login", email.toString())
            .with("workplaceTopForm:j_phone_login", "")
            .with("workplaceTopForm:j_password", password)
        );

      assertLoginNotFailed(response);
      assertRedirectGiven(response);

      return new Cabinet(session);
    }
    catch (HttpStatusException e) {
      if (e.getStatusCode() >= 500 ||  e.getStatusCode() == 403) {
        throw new PortalNotAvailable(e);
      }
      throw e;
    }
  }

  private void assertLoginNotFailed(PartialResponse response) throws LoginFailed {
    Growl growl = response.growl();
    if (growl != null) {
      String error = growl.error();
      if (error != null) {
        throw new LoginFailed(error);
      }
    }
  }

  private void assertRedirectGiven(PartialResponse response) {
    String redirect = response.redirect();
    if (redirect == null || redirect.isEmpty()) {
      throw new RuntimeException("No redirect given");
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy