Example usage for org.apache.thrift.transport THttpClient setCustomHeaders

List of usage examples for org.apache.thrift.transport THttpClient setCustomHeaders

Introduction

In this page you can find the example usage for org.apache.thrift.transport THttpClient setCustomHeaders.

Prototype

public void setCustomHeaders(Map<String, String> headers) 

Source Link

Usage

From source file:io.cslinmiso.line.api.impl.LineApiImpl.java

License:Open Source License

/**
 * Ready.//from ww w. j  av a2 s  .co m
 * 
 * @return the talk service. client
 * @throws TTransportException
 */
public Client ready() throws TTransportException {

    THttpClient transport = new THttpClient(LINE_HTTP_IN_URL);
    transport.setCustomHeaders(headers);
    transport.open();

    TProtocol protocol = new TCompactProtocol(transport);

    return new TalkService.Client(protocol);
}

From source file:io.cslinmiso.line.api.impl.LineApiImpl.java

License:Open Source License

public LoginResult login(String id, String password, String certificate) throws Exception {

    IdentityProvider provider = null;/*  ww  w . j  a  v a2  s .  c o m*/
    Map<String, String> json = null;
    String sessionKey = null;
    boolean keepLoggedIn = true;
    String accessLocation = this.ip;

    // Login to LINE server.
    if (id.matches(EMAIL_REGEX)) {
        provider = IdentityProvider.LINE; // LINE
        json = getCertResult(LINE_SESSION_LINE_URL);
    } else {
        provider = IdentityProvider.NAVER_KR; // NAVER
        json = getCertResult(LINE_SESSION_NAVER_URL);
    }

    if (id != null) {
        this.id = id;
    }

    if (password != null) {
        this.password = password;
    }

    if (StringUtils.isNotEmpty(certificate)) {
        setCertificate(certificate);
    } else {
        // read the certificate file if it exists
        try {
            List<String> readFile = Utility.readFile(LineApiImpl.CERT_FILE);
            String tmpCert = readFile != null ? readFile.get(0) : "";
            if (tmpCert != null) {
                setCertificate(tmpCert);
            }
        } catch (Exception ex) {
            setCertificate("");
        }
    }

    sessionKey = json.get("session_key");
    String tmpMsg = (char) (sessionKey.length()) + sessionKey + (char) (id.length()) + id
            + (char) (password.length()) + password;
    String message = new String(tmpMsg.getBytes(), java.nio.charset.StandardCharsets.UTF_8);
    String[] keyArr = json.get("rsa_key").split(",");
    String keyName = keyArr[0];
    String n = keyArr[1];
    String e = keyArr[2];

    BigInteger modulus = new BigInteger(n, 16);
    BigInteger pubExp = new BigInteger(e, 16);

    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(modulus, pubExp);
    RSAPublicKey publicKey = (RSAPublicKey) keyFactory.generatePublic(pubKeySpec);
    Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
    cipher.init(Cipher.ENCRYPT_MODE, publicKey);
    byte[] enBytes = cipher.doFinal(message.getBytes());
    String encryptString = Hex.encodeHexString(enBytes);

    THttpClient transport = new THttpClient(LINE_HTTP_URL);
    transport.setCustomHeaders(headers);
    transport.open();

    TProtocol protocol = new TCompactProtocol(transport);
    this.client = new TalkService.Client(protocol);

    LoginResult result = this.client.loginWithIdentityCredentialForCertificate(provider, keyName, encryptString,
            keepLoggedIn, accessLocation, this.systemName, this.certificate);

    if (result.getType() == LoginResultType.REQUIRE_DEVICE_CONFIRM) {

        headers.put("X-Line-Access", result.getVerifier());
        String pinCode = result.getPinCode();

        System.out.printf("Enter PinCode '%s' to your mobile phone in 2 minutes.\n", pinCode);
        // await for pinCode to be certified, it will return a verifier afterward.
        loginWithVerifierForCertificate();
    } else if (result.getType() == LoginResultType.SUCCESS) {
        // if param certificate has passed certification
        setAuthToken(result.getAuthToken());
    }

    // Once the client passed the verification, switch connection to HTTP_IN_URL
    this.client = ready();
    return result;
}

From source file:io.cslinmiso.line.api.impl.LineApiImpl.java

License:Open Source License

public void loginWithAuthToken(String authToken) throws Exception {
    if (StringUtils.isNotEmpty(authToken)) {
        setAuthToken(authToken);//from   w  ww.  j  a v  a  2s .c  om
    }
    THttpClient transport = new THttpClient(LINE_HTTP_URL);
    transport.setCustomHeaders(headers);
    transport.open();

    TProtocol protocol = new TCompactProtocol(transport);
    setClient(new TalkService.Client(protocol));
}

From source file:io.cslinmiso.line.api.impl.LineApiImpl.java

License:Open Source License

public AuthQrcode loginWithQrCode() throws Exception {
    // Request QrCode from LINE server.

    // Map<String, String> json = null;
    boolean keepLoggedIn = false;

    THttpClient transport = new THttpClient(LINE_HTTP_URL);
    transport.setCustomHeaders(headers);
    transport.open();/* w w  w. j  ava 2s.  co m*/

    TProtocol protocol = new TCompactProtocol(transport);

    this.client = new TalkService.Client(protocol);

    AuthQrcode result = this.client.getAuthQrcode(keepLoggedIn, systemName);

    headers.put("X-Line-Access", result.getVerifier());

    System.out.println("Retrieved QR Code.");

    return result;
    // await for QR code to be certified, it will return a verifier afterward.
    // loginWithVerifier();
}