Android Open Source - Android-SDK Ok Client






From Project

Back to project page Android-SDK.

License

The source code is released under:

Apache License

If you think the Android project Android-SDK 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

/*
 * Copyright (C) 2014./*from   w  w  w .  j  a v a2s  . co m*/
 *
 * BaasBox - info@baasbox.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and limitations under the License.
 */

package com.baasbox.android.net;

import android.content.Context;

import com.baasbox.android.BaasBox;
import com.baasbox.android.BaasException;
import com.baasbox.android.BaasIOException;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Protocol;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;

import org.apache.http.HttpEntity;
import org.apache.http.HttpRequestFactory;
import org.apache.http.HttpResponse;
import org.apache.http.ProtocolVersion;
import org.apache.http.StatusLine;
import org.apache.http.entity.BasicHttpEntity;
import org.apache.http.message.BasicHttpResponse;
import org.apache.http.message.BasicStatusLine;

import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.TimeUnit;

import okio.BufferedSink;
import okio.Okio;
import okio.Source;

/**
 * Created by Andrea Tortorella on 08/07/14.
 */
public class OkClient implements RestClient{

    private static final byte[] ZERO_BYTES=new byte[0];

    private OkHttpClient mOkHttp;
    private String charset;
    public OkClient() { this(new OkHttpClient());}

    @Deprecated
    public OkClient(BaasBox.Config config){
        this(new OkHttpClient());
    }

    public OkClient(OkHttpClient client){
        mOkHttp = client;
    }

    @Override
    public void init(Context context, BaasBox.Config config) {
        this.charset=config.httpCharset;
        mOkHttp.setConnectTimeout(config.httpConnectionTimeout, TimeUnit.MILLISECONDS);
        mOkHttp.setReadTimeout(config.httpSocketTimeout,TimeUnit.MILLISECONDS);
        mOkHttp.setFollowSslRedirects(true);
    }

    private static class InputRequestBody extends RequestBody{
        MediaType media;
        Source in;
        InputRequestBody(String ct,InputStream in){
            this.media=MediaType.parse(ct);
            this.in= Okio.source(in);
        }

        @Override
        public MediaType contentType() {
            return media;
        }

        @Override
        public void writeTo(BufferedSink sink) throws IOException {
            sink.writeAll(in);
        }
    }

    private RequestBody buildBody(String contentType,InputStream bodyData) {
        if (bodyData==null){
            return RequestBody.create(MediaType.parse("application/json;charset=" + charset), "{}");
        } else {
            return new InputRequestBody(contentType,bodyData);
        }
    }

    @Override
    public HttpResponse execute(HttpRequest request) throws BaasException {
        String contentType = request.headers.get("Content-Type");
        Request.Builder okRequestBuilder = new Request.Builder();
        boolean contentLengthSet = false;
        for (String name: request.headers.keySet()){
            if (!contentLengthSet &&"Content-Length".equals(name)){
                contentLengthSet = true;
            }
            okRequestBuilder.addHeader(name,request.headers.get(name));
        }
        if (!contentLengthSet){
            okRequestBuilder.addHeader("Content-Length","0");
        }
        RequestBody rb;
        switch (request.method){
            case HttpRequest.GET:
                okRequestBuilder.get();
                break;
            case HttpRequest.POST:
                rb = buildBody(contentType,request.body);
                //InputRequestBody rb = new InputRequestBody(contentType,request.body);
                okRequestBuilder.post(rb);
                break;
            case HttpRequest.PUT:
                rb = buildBody(contentType,request.body);
                okRequestBuilder.put(rb);
                break;
            case HttpRequest.DELETE:
                okRequestBuilder.delete();
                break;
            case HttpRequest.PATCH:
                rb = buildBody(contentType,request.body);
                okRequestBuilder.patch(rb);
                break;

        }

        okRequestBuilder.url(request.url);
        Request okRequest=okRequestBuilder.build();
        try {
            Response resp = mOkHttp.newCall(okRequest).execute();
            Protocol protocol = resp.protocol();
            ProtocolVersion pv;
            switch (protocol){
                case HTTP_1_0:
                    pv = new ProtocolVersion("HTTP",1,0);
                    break;
                case HTTP_1_1:
                    pv = new ProtocolVersion("HTTP",1,1);
                    break;
                case HTTP_2:
                    pv = new ProtocolVersion("HTTP",2,0);
                    break;
                case SPDY_3:
                    pv = new ProtocolVersion("spdy",3,1);
                    break;
                default:
                    throw new BaasIOException("Invalid protocol");
            }
            StatusLine line = new BasicStatusLine(pv,resp.code(),resp.message());
            BasicHttpResponse bresp = new BasicHttpResponse(line);
            bresp.setEntity(asEntity(resp));

            for (String name:resp.headers().names()){
                String val = resp.headers().get(name);
                bresp.addHeader(name,val);
            }
            return bresp;
        } catch (IOException e) {
            throw new BaasIOException(e);
        }
    }

    private HttpEntity asEntity(Response resp) {
        BasicHttpEntity entity =new BasicHttpEntity();
        InputStream inputStream = resp.body().byteStream();
        entity.setContent(inputStream);
        entity.setContentLength(resp.body().contentLength());
        String ctnt=resp.body().contentType().toString();
        entity.setContentType(ctnt);
        return entity;
    }
}




Java Source Code List

com.baasbox.android.AsyncStream.java
com.baasbox.android.BaasACL.java
com.baasbox.android.BaasApiException.java
com.baasbox.android.BaasAssetId.java
com.baasbox.android.BaasAsset.java
com.baasbox.android.BaasBox.java
com.baasbox.android.BaasCancellationException.java
com.baasbox.android.BaasClientException.java
com.baasbox.android.BaasCloudMessagingService.java
com.baasbox.android.BaasCredentialManager.java
com.baasbox.android.BaasDocument.java
com.baasbox.android.BaasException.java
com.baasbox.android.BaasFile.java
com.baasbox.android.BaasHandler.java
com.baasbox.android.BaasIOException.java
com.baasbox.android.BaasInvalidSessionException.java
com.baasbox.android.BaasLink.java
com.baasbox.android.BaasObject.java
com.baasbox.android.BaasQuery.java
com.baasbox.android.BaasResult.java
com.baasbox.android.BaasRuntimeException.java
com.baasbox.android.BaasServerException.java
com.baasbox.android.BaasStream.java
com.baasbox.android.BaasUser.java
com.baasbox.android.Cache.java
com.baasbox.android.DataStreamHandler.java
com.baasbox.android.ExceptionHandler.java
com.baasbox.android.FixedByteArrayOutputStream.java
com.baasbox.android.Grant.java
com.baasbox.android.HttpUrlConnectionClient.java
com.baasbox.android.JsonWrapper.java
com.baasbox.android.NetworkTask.java
com.baasbox.android.Plugin.java
com.baasbox.android.RequestFactory.java
com.baasbox.android.RequestOptions.java
com.baasbox.android.RequestToken.java
com.baasbox.android.RestImpl.java
com.baasbox.android.Rest.java
com.baasbox.android.Role.java
com.baasbox.android.SaveMode.java
com.baasbox.android.StreamBody.java
com.baasbox.android.StreamRequest.java
com.baasbox.android.impl.Base64.java
com.baasbox.android.impl.Constants.java
com.baasbox.android.impl.DiskLruCache.java
com.baasbox.android.impl.Dispatcher.java
com.baasbox.android.impl.GCMWrapper.java
com.baasbox.android.impl.ImmediateDispatcher.java
com.baasbox.android.impl.Logger.java
com.baasbox.android.impl.StrictLineReader.java
com.baasbox.android.impl.Task.java
com.baasbox.android.impl.Util.java
com.baasbox.android.impl.package-info.java
com.baasbox.android.json.JsonArray.java
com.baasbox.android.json.JsonException.java
com.baasbox.android.json.JsonObject.java
com.baasbox.android.json.JsonReader.java
com.baasbox.android.json.JsonScope.java
com.baasbox.android.json.JsonStructure.java
com.baasbox.android.json.JsonToken.java
com.baasbox.android.json.JsonWriter.java
com.baasbox.android.json.StringPool.java
com.baasbox.android.json.package-info.java
com.baasbox.android.net.HttpRequest.java
com.baasbox.android.net.OkClient.java
com.baasbox.android.net.RestClient.java
com.baasbox.android.plugins.glide.BaasAssetModelLoader.java
com.baasbox.android.plugins.glide.BaasFileModelLoader.java
com.baasbox.android.plugins.glide.BaasImageFetcher.java
com.baasbox.android.plugins.glide.GlidePlugin.java
com.baasbox.android.samples.aloa.Aloa.java
com.baasbox.android.samples.aloa.ApplicationTest.java
com.baasbox.android.samples.aloa.Configuration.java
com.baasbox.android.samples.aloa.Data.java
com.baasbox.android.samples.aloa.activities.CreateChannelFragment.java
com.baasbox.android.samples.aloa.activities.LoginActivity.java
com.baasbox.android.samples.aloa.activities.MainActivity.java
com.baasbox.android.samples.aloa.activities.OnTargetSelectedListener.java
com.baasbox.android.samples.aloa.activities.SendMessageActivity.java
com.baasbox.android.samples.aloa.activities.SendTargetFragment.java
com.baasbox.android.samples.aloa.activities.UserStatusFragment.java
com.baasbox.android.samples.aloa.activities.loaders.BaasUsersLoader.java
com.baasbox.android.samples.aloa.activities.loaders.ChannelsLoader.java
com.baasbox.android.samples.aloa.receive.NewMessageNotification.java
com.baasbox.android.samples.aloa.receive.PushReceiver.java
com.baasbox.android.samples.aloa.utils.BaasLoader.java
com.baasbox.android.samples.aloa.utils.BaseActivity.java
com.baasbox.android.samples.aloa.utils.BaseBaasLoader.java
com.baasbox.android.samples.aloa.utils.BaseFragment.java
com.baasbox.android.samples.phrasebook.Configuration.java
com.baasbox.android.samples.phrasebook.Phrasebook.java
com.baasbox.android.samples.phrasebook.ServerContract.java
com.baasbox.android.samples.phrasebook.common.BaseActivity.java
com.baasbox.android.samples.phrasebook.common.BaseFragment.java
com.baasbox.android.samples.phrasebook.loaders.BaasDocumentsLoader.java
com.baasbox.android.samples.phrasebook.ui.HomeActivity.java
com.baasbox.android.samples.phrasebook.ui.PhraseOfTheDay.java
com.baasbox.android.samples.phrasebook.utils.ThreadLocalRandom.java
com.baasbox.android.package-info.java