Android Open Source - unicef_gis_mobile C B Lite Http Client






From Project

Back to project page unicef_gis_mobile.

License

The source code is released under:

MIT License

If you think the Android project unicef_gis_mobile 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.couchbase.cblite.ektorp;
//w w  w .  ja  va 2s.co  m
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;

import org.ektorp.http.HttpClient;
import org.ektorp.http.HttpResponse;
import org.ektorp.util.Exceptions;

import android.util.Log;

import com.couchbase.cblite.CBLDatabase;
import com.couchbase.cblite.CBLServer;
import com.couchbase.cblite.router.CBLRouter;
import com.couchbase.cblite.router.CBLURLConnection;

public class CBLiteHttpClient implements HttpClient {
    private CBLServer server;

    public CBLiteHttpClient(CBLServer server) {
        this.server = server;
    }

    protected URL urlFromUri(String uri) {
        URL url = null;
        try {
            url = new URL(String.format("cblite://%s", uri));
        } catch (MalformedURLException e) {
            Log.w(CBLDatabase.TAG, "Unable to build CBLite URL", e);
            throw Exceptions.propagate(e);
        }
        return url;
    }

    protected CBLURLConnection connectionFromUri(String uri) {
        CBLURLConnection conn = null;
        URL url = urlFromUri(uri);
        try {
            conn = (CBLURLConnection) url.openConnection();
            conn.setDoOutput(true);
        } catch (IOException e) {
            Exceptions.propagate(e);
        }
        return conn;
    }

    @Override
    public HttpResponse delete(String uri) {
        CBLURLConnection conn = connectionFromUri(uri);
        try {
            conn.setRequestMethod("DELETE");
            return executeRequest(conn);
        } catch (ProtocolException e) {
            throw Exceptions.propagate(e);
        }
    }

    @Override
    public HttpResponse get(String uri) {
        CBLURLConnection conn = connectionFromUri(uri);
        try {
            conn.setRequestMethod("GET");
            return executeRequest(conn);
        } catch (ProtocolException e) {
            throw Exceptions.propagate(e);
        }
    }

    @Override
    public HttpResponse getUncached(String uri) {
        return get(uri);
    }

    @Override
    public HttpResponse head(String uri) {
        CBLURLConnection conn = connectionFromUri(uri);
        try {
            conn.setRequestMethod("HEAD");
            return executeRequest(conn);
        } catch (ProtocolException e) {
            throw Exceptions.propagate(e);
        }
    }

    @Override
    public HttpResponse post(String uri, String content) {
        CBLURLConnection conn = connectionFromUri(uri);
        try {
            conn.setRequestMethod("POST");

            if (content != null) {
                conn.setDoInput(true);
                conn.setRequestInputStream(new ByteArrayInputStream(content
                        .getBytes()));
            }

            return executeRequest(conn);
        } catch (ProtocolException e) {
            throw Exceptions.propagate(e);
        }
    }

    @Override
    public HttpResponse post(String uri, InputStream contentStream) {
        CBLURLConnection conn = connectionFromUri(uri);
        try {
            conn.setRequestMethod("POST");

            if (contentStream != null) {
                conn.setDoInput(true);
                conn.setRequestInputStream(contentStream);
            }

            return executeRequest(conn);
        } catch (ProtocolException e) {
            throw Exceptions.propagate(e);
        }
    }

    @Override
    public HttpResponse postUncached(String uri, String content) {
        return post(uri, content);
    }

    @Override
    public HttpResponse put(String uri) {
        CBLURLConnection conn = connectionFromUri(uri);
        try {
            conn.setRequestMethod("PUT");

            return executeRequest(conn);
        } catch (ProtocolException e) {
            throw Exceptions.propagate(e);
        }
    }

    @Override
    public HttpResponse put(String uri, String content) {
        CBLURLConnection conn = connectionFromUri(uri);
        try {
            conn.setRequestMethod("PUT");

            if (content != null) {
                conn.setDoInput(true);
                conn.setRequestInputStream(new ByteArrayInputStream(content
                        .getBytes()));
            }

            return executeRequest(conn);
        } catch (ProtocolException e) {
            throw Exceptions.propagate(e);
        }
    }

    @Override
    public HttpResponse put(String uri, InputStream contentStream,
            String contentType, long contentLength) {
        CBLURLConnection conn = connectionFromUri(uri);
        try {
            conn.setRequestMethod("PUT");

            if (contentStream != null) {
                conn.setDoInput(true);
                conn.setRequestProperty("content-type", contentType);
                conn.setRequestInputStream(contentStream);
            }

            return executeRequest(conn);
        } catch (ProtocolException e) {
            throw Exceptions.propagate(e);
        }
    }

    @Override
    public void shutdown() {

    }

    protected HttpResponse executeRequest(CBLURLConnection conn) {
        final CBLRouter router = new CBLRouter(server, conn);
        CBLiteHttpResponse response;
        try {
            response = CBLiteHttpResponse.of(conn, router);
        } catch (IOException e) {
            throw Exceptions.propagate(e);
        }
        router.setCallbackBlock(response);
        ScheduledExecutorService workExecutor = server.getWorkExecutor();
        Future<?> routerFuture = workExecutor.submit(new Runnable() {
            @Override
            public void run() {
                router.start();
            }
        });
        try {
            routerFuture.get();
        } catch (InterruptedException e) {
            throw Exceptions.propagate(e);
        } catch (ExecutionException e) {
            throw Exceptions.propagate(e);
        }
        return response;
    }

}




Java Source Code List

com.couchbase.cblite.ektorp.CBLiteHttpClient.java
com.couchbase.cblite.ektorp.CBLiteHttpResponse.java
edu.mit.mobile.android.utils.StreamUtils.java
org.unicef.gis.auth.AuthenticatorService.java
org.unicef.gis.auth.Authenticator.java
org.unicef.gis.infrastructure.CompileTimeSettings.java
org.unicef.gis.infrastructure.ILocationServiceConsumer.java
org.unicef.gis.infrastructure.LocationService.java
org.unicef.gis.infrastructure.Network.java
org.unicef.gis.infrastructure.Notificator.java
org.unicef.gis.infrastructure.RoutesResolver.java
org.unicef.gis.infrastructure.ServerUrlPreferenceNotSetException.java
org.unicef.gis.infrastructure.UnicefGisApi.java
org.unicef.gis.infrastructure.data.CouchDbLiteStoreAdapter.java
org.unicef.gis.infrastructure.data.UnicefGisContentProvider.java
org.unicef.gis.infrastructure.data.UnicefGisStore.java
org.unicef.gis.infrastructure.image.AsyncDrawable.java
org.unicef.gis.infrastructure.image.BitmapWorkerTask.java
org.unicef.gis.infrastructure.image.Camera.java
org.unicef.gis.model.Report.java
org.unicef.gis.model.Tag.java
org.unicef.gis.model.couchdb.NullReduce.java
org.unicef.gis.model.couchdb.ReportLoader.java
org.unicef.gis.model.couchdb.views.AllReportsByTimestampDesc.java
org.unicef.gis.model.couchdb.views.PendingSyncReports.java
org.unicef.gis.model.couchdb.views.UnicefGisView.java
org.unicef.gis.model.couchdb.views.UploadedReports.java
org.unicef.gis.sync.SyncAdapter.java
org.unicef.gis.sync.SyncService.java
org.unicef.gis.ui.AlertDialogFragment.java
org.unicef.gis.ui.AuthenticatorActivity.java
org.unicef.gis.ui.ConfigureServerUrlActivity.java
org.unicef.gis.ui.DeleteUploadedReportsTask.java
org.unicef.gis.ui.FetchTagsActivity.java
org.unicef.gis.ui.FetchTagsTask.java
org.unicef.gis.ui.MyReportsActivity.java
org.unicef.gis.ui.SettingsActivity.java
org.unicef.gis.ui.SettingsFragment.java
org.unicef.gis.ui.report.ChooseTagsFragment.java
org.unicef.gis.ui.report.CreateReportActivityConstants.java
org.unicef.gis.ui.report.CreateReportActivity.java
org.unicef.gis.ui.report.GetTagsTaskFragment.java
org.unicef.gis.ui.report.GetTagsTask.java
org.unicef.gis.ui.report.IChooseTagsCallbacks.java
org.unicef.gis.ui.report.IGetTagsCallback.java
org.unicef.gis.ui.report.IGetTagsTaskFragmentCallbacks.java
org.unicef.gis.ui.report.IReportSummaryCallbacks.java
org.unicef.gis.ui.report.ReportRowAdapter.java
org.unicef.gis.ui.report.ReportSummaryFragment.java
org.unicef.gis.ui.report.ReportViewModel.java
org.unicef.gis.ui.report.ToggleTagAdapter.java