Example usage for org.apache.http.impl DefaultHttpRequestFactory DefaultHttpRequestFactory

List of usage examples for org.apache.http.impl DefaultHttpRequestFactory DefaultHttpRequestFactory

Introduction

In this page you can find the example usage for org.apache.http.impl DefaultHttpRequestFactory DefaultHttpRequestFactory.

Prototype

DefaultHttpRequestFactory

Source Link

Usage

From source file:ti.modules.titanium.network.TiHTTPClient.java

public void send(Object userData) throws MethodNotSupportedException {
    aborted = false;/*w w w. ja  va 2 s  . c  o m*/

    // TODO consider using task manager
    int totalLength = 0;
    needMultipart = false;

    if (userData != null) {
        if (userData instanceof HashMap) {
            HashMap<String, Object> data = (HashMap) userData;
            boolean isPostOrPut = method.equals("POST") || method.equals("PUT");
            boolean isGet = !isPostOrPut && method.equals("GET");

            // first time through check if we need multipart for POST
            for (String key : data.keySet()) {
                Object value = data.get(key);

                if (value != null) {
                    // if the value is a proxy, we need to get the actual file object
                    if (value instanceof TiFileProxy) {
                        value = ((TiFileProxy) value).getBaseFile();
                    }

                    if (value instanceof TiBaseFile || value instanceof TiBlob) {
                        needMultipart = true;
                        break;
                    }
                }
            }

            boolean queryStringAltered = false;
            for (String key : data.keySet()) {
                Object value = data.get(key);
                if (isPostOrPut && (value != null)) {
                    // if the value is a proxy, we need to get the actual file object
                    if (value instanceof TiFileProxy) {
                        value = ((TiFileProxy) value).getBaseFile();
                    }

                    if (value instanceof TiBaseFile || value instanceof TiBlob) {
                        totalLength += addTitaniumFileAsPostData(key, value);

                    } else {
                        String str = TiConvert.toString(value);
                        addPostData(key, str);
                        totalLength += str.length();
                    }

                } else if (isGet) {
                    uri = uri.buildUpon().appendQueryParameter(key, TiConvert.toString(value)).build();
                    queryStringAltered = true;
                }
            }

            if (queryStringAltered) {
                this.url = uri.toString();
            }
        } else if (userData instanceof TiFileProxy || userData instanceof TiBaseFile
                || userData instanceof TiBlob) {
            Object value = userData;
            if (value instanceof TiFileProxy) {
                value = ((TiFileProxy) value).getBaseFile();
            }
            if (value instanceof TiBaseFile || value instanceof TiBlob) {
                setRawData(titaniumFileAsPutData(value));
            } else {
                setRawData(TiConvert.toString(value));
            }
        } else {
            setRawData(TiConvert.toString(userData));
        }
    }

    Log.d(TAG, "Instantiating http request with method='" + method + "' and this url:", Log.DEBUG_MODE);
    Log.d(TAG, this.url, Log.DEBUG_MODE);

    request = new DefaultHttpRequestFactory().newHttpRequest(method, this.url);
    request.setHeader(TITANIUM_ID_HEADER, TiApplication.getInstance().getAppGUID());
    for (String header : headers.keySet()) {
        request.setHeader(header, headers.get(header));
    }

    clientThread = new Thread(new ClientRunnable(totalLength),
            "TiHttpClient-" + httpClientThreadCounter.incrementAndGet());
    clientThread.setPriority(Thread.MIN_PRIORITY);
    clientThread.start();

    Log.d(TAG, "Leaving send()", Log.DEBUG_MODE);
}