Android Open Source - Arbalest Arbalest Client






From Project

Back to project page Arbalest.

License

The source code is released under:

Apache License

If you think the Android project Arbalest 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) 2013 KeithYokoma. All rights reserved.
 *//from  www  .  j a va2 s  .  c  o  m
 * 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.arbalest.http;

import android.app.Application;
import android.util.Log;

import com.amalgam.app.ApplicationUtils;
import com.amalgam.io.DebugInputStream;
import com.amalgam.io.DebugOutputStream;
import com.arbalest.Arbalest;
import com.arbalest.exception.ArbalestAuthenticationException;
import com.arbalest.exception.ArbalestException;
import com.arbalest.exception.ArbalestNetworkException;
import com.arbalest.exception.ArbalestResponseException;
import com.squareup.okhttp.OkHttpClient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
import java.util.Set;

public class ArbalestClient implements IArbalestClient {
    public static final String TAG = ArbalestClient.class.getSimpleName();

    private final Application mApplication;
    private final HttpURLConnection mConnection;
    private final OkHttpClient mClient;

    protected ArbalestClient(Application application, OkHttpClient client, String url) throws ArbalestNetworkException {
        mApplication = application;
        mClient = client;
        mConnection = openConnection(url);
    }

    protected ArbalestClient(Application application, OkHttpClient client, HttpURLConnection connection) throws ArbalestNetworkException {
        mApplication = application;
        mClient = client;
        mConnection = connection;
    }

    @Override
    public void close() throws IOException {
        if (mConnection != null) {
            mConnection.disconnect();
        }
    }

    @SuppressWarnings("resource")
    @Override
    public OutputStream getOutputStream(String method, Map<String, String> headers) throws ArbalestNetworkException {
        if (headers != null) {
            Set<Map.Entry<String, String>> entries = headers.entrySet();
            for (Map.Entry<String, String> entry : entries) {
                mConnection.setRequestProperty(entry.getKey(), entry.getValue());
            }
        }

        try {
            if ("GET".equals(method) || "DELETE".equals(method)) {
                mConnection.setDoOutput(false);
                return null;
            }

            mConnection.setDoOutput(true);
            OutputStream out = mConnection.getOutputStream();
            return ApplicationUtils.isDebuggable(mApplication) ? new DebugOutputStream(out) : out;
        } catch (IOException e) {
            throw new ArbalestNetworkException(e);
        }
    }

    @SuppressWarnings("resource")
    @Override
    public InputStream getInputStream() throws ArbalestNetworkException, ArbalestResponseException, ArbalestAuthenticationException{
        try {
            mConnection.connect();
            InputStream in = mConnection.getInputStream();
            return ApplicationUtils.isDebuggable(mApplication) ? new DebugInputStream(in) : in;
        } catch (IOException e) {
            handleErrorResponse();
        }
        return null; // we cannot take care for the input stream
    }

    @Override
    public int getResponseCode() throws ArbalestNetworkException {
        try {
            return mConnection.getResponseCode();
        } catch (IOException e) {
            throw new ArbalestNetworkException(e);
        }
    }

    protected HttpURLConnection openConnection(String url) {
        try {
            HttpURLConnection connection = mClient.open(new URL(url));
            connection.setUseCaches(false);
            connection.setDoInput(true);
            return connection;
        } catch (MalformedURLException e) {
            Log.e(TAG, "malformed url: ", e);
            throw new IllegalArgumentException(e);
        }
    }

    private ArbalestException handleErrorResponse() throws ArbalestAuthenticationException, ArbalestNetworkException, ArbalestResponseException {
        int code = getResponseCode();
        switch (code) {
            case HttpURLConnection.HTTP_UNAUTHORIZED:
                throw new ArbalestAuthenticationException("", code);
            default:
                throw new ArbalestResponseException(getErrorResponse(), code);
        }
    }

    private String getErrorResponse() throws ArbalestNetworkException {
        StringBuilder builder = new StringBuilder();
        InputStream stream = mConnection.getErrorStream();
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(stream));
            String line = null;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }
            return builder.toString();
        } catch (IOException e) {
            throw new ArbalestNetworkException(e);
        }
    }
}




Java Source Code List

com.arbalest.ArbalestContext.java
com.arbalest.Arbalest.java
com.arbalest.callback.ArbalestCallbackManager.java
com.arbalest.callback.ArbalestCallback.java
com.arbalest.concurrent.AsyncProcessor.java
com.arbalest.constants.ArbalestConstants.java
com.arbalest.exception.ArbalestAuthenticationException.java
com.arbalest.exception.ArbalestConnectionException.java
com.arbalest.exception.ArbalestException.java
com.arbalest.exception.ArbalestNetworkException.java
com.arbalest.exception.ArbalestRequestException.java
com.arbalest.exception.ArbalestResponseException.java
com.arbalest.http.ArbalestClientFactory.java
com.arbalest.http.ArbalestClient.java
com.arbalest.http.IArbalestClientFactory.java
com.arbalest.http.IArbalestClient.java
com.arbalest.net.Binary.java
com.arbalest.net.annotation.ConvertedBy.java
com.arbalest.net.converter.BinaryConverter.java
com.arbalest.net.converter.Converter.java
com.arbalest.net.converter.EntityConversionPolicy.java
com.arbalest.net.converter.EntityConversionStrategy.java
com.arbalest.net.converter.GsonConverter.java
com.arbalest.net.converter.RawJsonConverter.java
com.arbalest.net.json.ArbalestFieldNamingStrategy.java
com.arbalest.utils.ArbalestCallbackUtils.java