Example usage for android.net UrlQuerySanitizer UrlQuerySanitizer

List of usage examples for android.net UrlQuerySanitizer UrlQuerySanitizer

Introduction

In this page you can find the example usage for android.net UrlQuerySanitizer UrlQuerySanitizer.

Prototype

public UrlQuerySanitizer(String url) 

Source Link

Document

Constructs a UrlQuerySanitizer and parses a URL.

Usage

From source file:com.playhaven.android.data.Purchase.java

public static ArrayList<Purchase> fromJson(String json) {
    if (json == null)
        return new ArrayList<Purchase>(0);

    String url = JsonUtil.getPath(json, "$.url");
    UrlQuerySanitizer sanitizer = new UrlQuerySanitizer(url);
    final String placementTag = sanitizer.getValue("placement_tag");

    ArrayList<Purchase> toReturn = new ArrayList<Purchase>();
    for (String jsonPurchase : JsonUtil.forEach(json, "$.purchases"))
        toReturn.add(new Purchase(jsonPurchase, placementTag));

    return toReturn;
}

From source file:eu.masconsult.bgbanking.banks.dskbank.DskClient.java

@Override
public String authenticate(String username, String password)
        throws IOException, ParseException, CaptchaException {
    final HttpResponse resp;
    final ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair(PARAM_USERNAME, username));
    params.add(new BasicNameValuePair(PARAM_PASSWORD, password));
    final HttpEntity entity;
    try {//from  w  w w .ja v  a 2  s.c o m
        entity = new UrlEncodedFormEntity(params);
    } catch (final UnsupportedEncodingException e) {
        // this should never happen.
        throw new IllegalStateException(e);
    }
    String uri = BASE_URL + "?"
            + URLEncodedUtils.format(Arrays.asList(new BasicNameValuePair(XML_ID, AUTH_XML_ID)), ENCODING);
    Log.i(TAG, "Authenticating to: " + uri);
    final HttpPost post = new HttpPost(uri);
    post.addHeader(entity.getContentType());
    post.setHeader("Accept", "*/*");
    post.setEntity(entity);
    try {
        resp = getHttpClient().execute(post);

        if (resp.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
            throw new ParseException("login: unhandled http status " + resp.getStatusLine().getStatusCode()
                    + " " + resp.getStatusLine().getReasonPhrase());
        }

        String response = EntityUtils.toString(resp.getEntity());
        Log.v(TAG, "response = " + response);

        Document doc = Jsoup.parse(response, BASE_URL);
        Element mainForm = doc.getElementById("mainForm");
        if (mainForm == null) {
            throw new ParseException("login: missing mainForm");
        }

        String action = BASE_URL + mainForm.attr("action");
        Log.v(TAG, "action=" + action);
        UrlQuerySanitizer sanitizer = new UrlQuerySanitizer(action);
        String user_id = sanitizer.getValue(PARAM_USER_ID);
        String session_id = sanitizer.getValue(PARAM_SESSION_ID);

        if (user_id == null || "".equals(user_id) || session_id == null || "".equals(session_id)) {
            if (doc.getElementsByClass("redtext").size() > 0) {
                // bad authentication
                return null;
            } else {
                // TODO handle captcha
                Elements captcha = doc.select("input[name=captcha_hkey]");
                if (captcha != null && captcha.size() == 1) {
                    String captchaHash = captcha.first().attr("value");
                    String captchaUri = BASE_URL + "?"
                            + URLEncodedUtils
                                    .format(Arrays.asList(new BasicNameValuePair(XML_ID, CAPTCHA_XML_ID),
                                            new BasicNameValuePair("captcha_key", captchaHash)), ENCODING);
                    throw new CaptchaException(captchaUri);
                }
                throw new ParseException("no user_id or session_id: " + action);
            }
        }

        return URLEncodedUtils.format(Arrays.asList(new BasicNameValuePair(PARAM_USER_ID, user_id),
                new BasicNameValuePair(PARAM_SESSION_ID, session_id)), ENCODING);
    } catch (ClientProtocolException e) {
        throw new IOException(e.getMessage());
    }
}

From source file:eu.masconsult.bgbanking.banks.sgexpress.SGExpressClient.java

private void performAuthentication(DefaultHttpClient httpClient, AuthToken authToken)
        throws IOException, AuthenticationException {
    HttpResponse resp;/* ww  w  .  ja v a2s .c  om*/
    final ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair(PARAM_USERNAME, authToken.username));
    params.add(new BasicNameValuePair(PARAM_PASSWORD, authToken.password));
    params.add(new BasicNameValuePair(PARAM_USER_TIME, "dummy"));
    final HttpEntity entity;
    try {
        entity = new UrlEncodedFormEntity(params);
    } catch (final UnsupportedEncodingException e) {
        // this should never happen.
        throw new IllegalStateException(e);
    }
    String uri = BASE_URL + "?"
            + URLEncodedUtils.format(Arrays.asList(new BasicNameValuePair(XML_ID, AUTH_XML_ID)), ENCODING);
    Log.i(TAG, "Authenticating to: " + uri);

    final HttpPost post = new HttpPost(uri);
    post.addHeader(entity.getContentType());
    post.setHeader("Accept", "*/*");
    post.setEntity(entity);
    resp = httpClient.execute(post);

    // check for bad status
    if (resp.getStatusLine().getStatusCode() != HttpStatus.SC_MOVED_TEMPORARILY) {
        throw new ParseException("status after auth: " + resp.getStatusLine().getStatusCode() + " "
                + resp.getStatusLine().getReasonPhrase());
    }

    // check header redirect
    Header[] locations = resp.getHeaders(HEADER_LOCATION);
    if (locations.length != 1) {
        throw new ParseException(locations.length + " header locations received!");
    }

    String location = "https://" + DOMAIN + locations[0].getValue();
    Log.v(TAG, "location=" + location);

    UrlQuerySanitizer sanitizer = new UrlQuerySanitizer(location);
    authToken.userId = sanitizer.getValue(PARAM_USER_ID);
    authToken.sessionId = sanitizer.getValue(PARAM_SESSION_ID);
    String redirectedXmlId = sanitizer.getValue(XML_ID);

    if (authToken.userId == null || authToken.userId.length() == 0 || authToken.sessionId == null
            || authToken.sessionId.length() == 0 || AUTH_XML_ID.equalsIgnoreCase(redirectedXmlId)) {
        checkAuthError(sanitizer);
    }
}