Package com.box.restclientv2

Source Code of com.box.restclientv2.BoxBasicRestClient

package com.box.restclientv2;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;

import com.box.boxjavalibv2.BoxConnectionManagerBuilder.BoxConnectionManager;
import com.box.boxjavalibv2.exceptions.AuthFatalFailureException;
import com.box.restclientv2.exceptions.BoxRestException;
import com.box.restclientv2.requestsbase.IBoxRequest;
import com.box.restclientv2.responses.DefaultBoxResponse;
import com.box.restclientv2.responses.IBoxResponse;

/**
* Basic implementation of the {@link IBoxRESTClient}.
*/
public class BoxBasicRestClient implements IBoxRESTClient {

    private final DefaultHttpClient mHttpClient;

    public HttpClient getRawHttpClient() {
        return mHttpClient;
    }

    /**
     * Create a DefaultHttpClient, monitored by the connectionManager.
     *
     * @param connectionManager
     */
    public BoxBasicRestClient(final BoxConnectionManager connectionManager) {
        mHttpClient = connectionManager.getMonitoredRestClient();
    }

    /**
     * Constructor.
     */
    public BoxBasicRestClient() {
        mHttpClient = new DefaultHttpClient();
    }

    @Override
    public IBoxResponse execute(final IBoxRequest boxRequest) throws BoxRestException, AuthFatalFailureException {
        HttpUriRequest httpRequest = boxRequest.prepareRequest();
        HttpResponse response;

        try {
            response = getRawHttpClient().execute(httpRequest);
        }
        catch (IOException e) {
            throw new BoxRestException(e);
        }
        DefaultBoxResponse boxResponse = new DefaultBoxResponse(response);
        boxResponse.setExpectedResponseCode(boxRequest.getExpectedResponseCode());
        return boxResponse;
    }

}
TOP

Related Classes of com.box.restclientv2.BoxBasicRestClient

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.