Android Open Source - twawm2 My Http Client






From Project

Back to project page twawm2.

License

The source code is released under:

Copyright (c) 2014, afnf All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistr...

If you think the Android project twawm2 listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.appspot.afnf4199ga.twawm.router;
//  w  ww  .j a va2s.  c  o m
import java.io.IOException;
import java.net.InetAddress;
import java.net.URI;
import java.util.regex.Matcher;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ProtocolException;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.RedirectHandler;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HttpContext;

import android.content.Context;
import android.net.DhcpInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;

import com.appspot.afnf4199ga.twawm.Const;
import com.appspot.afnf4199ga.twawm.app.MyPreferenceActivity;
import com.appspot.afnf4199ga.utils.AndroidUtils;
import com.appspot.afnf4199ga.utils.Logger;
import com.appspot.afnf4199ga.utils.MyStringUtlis;

public class MyHttpClient extends DefaultHttpClient {

    static final String NOT_SITE_LOCAL_ADDR = "NOT_SITE_LOCAL_ADDR";

    private Context context;

    public MyHttpClient(HttpParams httpParams) {
        super(httpParams);
    }

    static MyHttpClient createClient(Context context) {

        // ????????
        HttpParams httpParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParams, Const.ROUTER_HTTP_TIMEOUT);
        HttpConnectionParams.setSoTimeout(httpParams, Const.ROUTER_HTTP_TIMEOUT);

        // ?????????
        MyHttpClient httpClient = new MyHttpClient(httpParams);
        httpClient.context = context;

        // ????????????????????????????
        httpClient.setRedirectHandler(new RedirectHandler() {
            @Override
            public boolean isRedirectRequested(HttpResponse response, HttpContext context) {
                return false;
            }

            @Override
            public URI getLocationURI(HttpResponse response, HttpContext context) throws ProtocolException {
                return null;
            }
        });

        // ??????????????????????????????NAD11?????????????BT???????????????????????????????????
        // ?????????????????????????????????????????????????????????????????
        httpClient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(0, false));

        return httpClient;
    }

    enum AuthType {
        NONE, DEFAULT
    }

    public HttpResponse executeWithAuth(HttpRequestBase method, AuthType authType) throws ClientProtocolException, IOException {

        // ?????
        if (authType == AuthType.DEFAULT) {

            // smart-user??
            boolean smart = false;
            URI uri = method.getURI();
            String path = uri.getPath();

            // RMTMAIN????????????????????????smart-user
            if (isRmtMainPath(path)) {
                smart = true;
            }

            // Credentials???
            String user, pass;
            if (smart) {
                user = Const.ROUTER_BASIC_AUTH_USERNAME;
                pass = Const.ROUTER_BASIC_AUTH_PASSWORD;
            }
            else {
                user = Const.ROUTER_BASIC_AUTH_USERNAME2;
                pass = Const.getPrefRouterControlPassword(context);
            }
            Credentials credentials = new UsernamePasswordCredentials(user, pass);

            // Basic?????
            AuthScope scope = new AuthScope(uri.getHost(), Const.ROUTER_PORT);
            getCredentialsProvider().setCredentials(scope, credentials);
        }
        // ???????
        else {
            getCredentialsProvider().clear();
        }

        // ??
        return execute(method);
    }

    static void close(MyHttpClient client) {
        try {
            if (client != null && client.getConnectionManager() != null) {
                client.getConnectionManager().shutdown();
            }
        }
        catch (Throwable e) {
            // do nothing
        }
    }

    static String getRouterIpAddr(InetLookupWrappter wrapper, String confRouterIpAddr) throws Throwable {
        String routerIpAddr = null;

        // ????IP??????????????????????????
        if (MyStringUtlis.isEmpty(confRouterIpAddr) == false) {
            Matcher m = MyPreferenceActivity.IP_ADDR_PATTERN.matcher(confRouterIpAddr);
            if (m.matches()) {
                routerIpAddr = confRouterIpAddr;
                // ???????????????????????????????????????
            }
            else {
                Logger.w("RouterControlByHttp confRouterIpAddr format is invalid");
                // ????if?????????
            }
        }

        // ??????????
        if (MyStringUtlis.isEmpty(routerIpAddr)) {

            // ??????????????
            InetAddress address = null;
            try {
                address = wrapper.getByName(Const.ROUTER_HOSTNAME);
            }
            catch (Throwable e) {
                // do nothing
            }

            // null??????????NG
            if (address == null) {
                Logger.w("RouterControlByHttp resolved IP Addr is null");
                return null;
            }
            // ???????????????????????NG?????????????????
            else if (address.isSiteLocalAddress() == false) {
                Logger.w("RouterControlByHttp " + Const.ROUTER_HOSTNAME + " is not private address");
                return NOT_SITE_LOCAL_ADDR;
            }
            // OK????routerIpAddr???
            else {
                routerIpAddr = address.getHostAddress();
            }
        }

        return routerIpAddr;
    }

    static String estimateRouterIpAddr(Context context) {
        try {
            WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            if (wifi != null) {
                String ipaddr = estimateRouterIpAddr(wifi);
                if (ipaddr != null) {
                    return ipaddr;
                }
            }
        }
        catch (Exception e) {
            Logger.w("estimateRouterIpAddr failed", e);
        }

        return Const.ROUTER_IPADDR_DEFAULT;
    }

    static String estimateRouterIpAddr(WifiManager wifi) {

        // ??????IP????????
        DhcpInfo dhcpInfo = wifi.getDhcpInfo();
        if (dhcpInfo != null) {
            String ipaddr = AndroidUtils.intToIpaddr(dhcpInfo.gateway);
            //Logger.i("estimateRouterIpAddr 1 " + ipaddr);
            return ipaddr;
        }

        // ??????IP????????
        WifiInfo connectionInfo = wifi.getConnectionInfo();
        if (connectionInfo != null) {
            String ipaddr = AndroidUtils.intToIpaddr(connectionInfo.getIpAddress());
            if (MyStringUtlis.isEmpty(ipaddr) == false) {
                int index = ipaddr.lastIndexOf(".");
                if (index != -1) {
                    ipaddr = ipaddr.substring(0, index) + ".1";
                    //Logger.i("estimateRouterIpAddr 2 " + ipaddr);
                    return ipaddr;
                }
            }
        }

        return null;
    }

    static void discardContent(HttpResponse response) {
        try {
            if (response != null) {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    entity.consumeContent();
                }
            }
        }
        catch (Throwable e) {
            // do nothing
        }
    }

    protected static boolean isRmtMainPath(String path) {
        return path != null && path.indexOf("/info_remote_") != -1;
    }
}




Java Source Code List

com.appspot.afnf4199ga.twawm.BluetoothHelper.java
com.appspot.afnf4199ga.twawm.Const.java
com.appspot.afnf4199ga.twawm.Const.java
com.appspot.afnf4199ga.twawm.HostnameListTest.java
com.appspot.afnf4199ga.twawm.HostnameList.java
com.appspot.afnf4199ga.twawm.IconSelectorTest.java
com.appspot.afnf4199ga.twawm.IconSelector.java
com.appspot.afnf4199ga.twawm.OnlineChecker.java
com.appspot.afnf4199ga.twawm.StateMachineTest.java
com.appspot.afnf4199ga.twawm.StateMachine.java
com.appspot.afnf4199ga.twawm.TwawmUtils.java
com.appspot.afnf4199ga.twawm.app.BackgroundServiceTest.java
com.appspot.afnf4199ga.twawm.app.BackgroundService.java
com.appspot.afnf4199ga.twawm.app.DefaultWidgetProvider.java
com.appspot.afnf4199ga.twawm.app.InfoActivity.java
com.appspot.afnf4199ga.twawm.app.InitialConfigurationWizardActivity.java
com.appspot.afnf4199ga.twawm.app.LogSendActivity.java
com.appspot.afnf4199ga.twawm.app.MainActivity.java
com.appspot.afnf4199ga.twawm.app.MainApp.java
com.appspot.afnf4199ga.twawm.app.MyPreferenceActivity.java
com.appspot.afnf4199ga.twawm.app.NetworkSwitcher.java
com.appspot.afnf4199ga.twawm.app.StaticIntentListener.java
com.appspot.afnf4199ga.twawm.app.UIAct.java
com.appspot.afnf4199ga.twawm.ctl.CustomizeActionsActivityTest.java
com.appspot.afnf4199ga.twawm.ctl.CustomizeActionsActivity.java
com.appspot.afnf4199ga.twawm.ctl.CwacTouchListView.java
com.appspot.afnf4199ga.twawm.ctl.ListItem.java
com.appspot.afnf4199ga.twawm.router.EcoModeControlTest.java
com.appspot.afnf4199ga.twawm.router.EcoModeControl.java
com.appspot.afnf4199ga.twawm.router.InetLookupWrappter.java
com.appspot.afnf4199ga.twawm.router.InetLookupWrappter.java
com.appspot.afnf4199ga.twawm.router.MyHttpClientTest.java
com.appspot.afnf4199ga.twawm.router.MyHttpClient.java
com.appspot.afnf4199ga.twawm.router.MyHttpClient.java
com.appspot.afnf4199ga.twawm.router.RouterControlByHttpTest.java
com.appspot.afnf4199ga.twawm.router.RouterControlByHttp.java
com.appspot.afnf4199ga.twawm.router.RouterControlByHttp.java
com.appspot.afnf4199ga.twawm.router.RouterControl.java
com.appspot.afnf4199ga.twawm.router.RouterControl.java
com.appspot.afnf4199ga.twawm.router.RouterInfo.java
com.appspot.afnf4199ga.twawm.router.RouterInfo.java
com.appspot.afnf4199ga.utils.AndroidUtilsTest.java
com.appspot.afnf4199ga.utils.AndroidUtils.java
com.appspot.afnf4199ga.utils.AndroidUtils.java
com.appspot.afnf4199ga.utils.Logger.java
com.appspot.afnf4199ga.utils.Logger.java
com.appspot.afnf4199ga.utils.MyStringUtlisTest.java
com.appspot.afnf4199ga.utils.MyStringUtlis.java
com.appspot.afnf4199ga.utils.MyStringUtlis.java
com.appspot.afnf4199ga.utils.MyTestUtils.java
com.appspot.afnf4199ga.utils.MyUncaughtExceptionHandler.java
com.appspot.afnf4199ga.utils.MyUncaughtExceptionHandler.java
com.appspot.afnf4199ga.wmgraph.app.FetchThread.java
com.appspot.afnf4199ga.wmgraph.app.InetLookupThread.java
com.appspot.afnf4199ga.wmgraph.app.InfoActivity.java
com.appspot.afnf4199ga.wmgraph.app.MainActivity.java
com.appspot.afnf4199ga.wmgraph.app.MyPreferenceActivity.java
com.appspot.afnf4199ga.wmgraph.app.UIAct.java
net.afnf.and.twawm2.DexmakerInstrumentationTestCase.java
net.afnf.and.twawm2.MyInstrumentationTestRunner.java