Example usage for org.apache.http.client.methods HttpUriRequest setHeader

List of usage examples for org.apache.http.client.methods HttpUriRequest setHeader

Introduction

In this page you can find the example usage for org.apache.http.client.methods HttpUriRequest setHeader.

Prototype

void setHeader(String str, String str2);

Source Link

Usage

From source file:org.soyatec.windowsazure.management.ServiceManagementRest.java

/**
 * The Reimage Role Instance operation requests a reimage of a role instance
 * that is running in a deployment. The Reimage Role Instance operation is
 * an asynchronous operation.//w w w . j a va  2 s  .  com
 * 
 * @return
 */
public String reimageRoleInstance(String serviceName, DeploymentSlotType deploySlotName,
        String roleInstanceName, AsyncResultCallback callback) {
    if (serviceName == null)
        throw new IllegalArgumentException("Service name is required!");

    if (deploySlotName == null)
        throw new IllegalArgumentException("Deployment slot type is required!");

    if (roleInstanceName == null)
        throw new IllegalArgumentException("Role instance name is required!");

    HttpUriRequest request = HttpUtilities.createServiceHttpRequest(
            URI.create(
                    getBaseUrl() + SERVICES_HOSTEDSERVICES + ConstChars.Slash + serviceName + DEPLOYMENT_SLOTS
                            + ConstChars.Slash + deploySlotName.getLiteral().toLowerCase() + ConstChars.Slash
                            + ROLE_INSTANCES + ConstChars.Slash + roleInstanceName + "?comp=reimage"),
            HttpMethod.Post);
    request.setHeader(HeaderNames.ApiVersion, XmsVersion.VERSION_2010_10_28);
    return sendAsynchronousRequest(request, callback);

}

From source file:com.piusvelte.sonet.core.SonetComments.java

@Override
protected void onListItemClick(ListView list, View view, final int position, long id) {
    super.onListItemClick(list, view, position, id);
    final String sid = mComments.get(position).get(Statuses.SID);
    final String liked = mComments.get(position).get(getString(R.string.like));
    // wait for previous attempts to finish
    if ((liked.length() > 0) && !liked.equals(getString(R.string.loading))) {
        // parse comment body, as in StatusDialog.java
        Matcher m = Sonet.getLinksMatcher(mComments.get(position).get(Statuses.MESSAGE));
        int count = 0;
        while (m.find()) {
            count++;// w  ww .j  a  va2 s .  co m
        }
        // like comments, the first comment is the post itself
        switch (mService) {
        case TWITTER:
            // retweet
            items = new String[count + 1];
            items[0] = getString(R.string.retweet);
            count = 1;
            m.reset();
            while (m.find()) {
                items[count++] = m.group();
            }
            mDialog = (new AlertDialog.Builder(this)).setItems(items, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (which == 0) {
                        AsyncTask<String, Void, String> asyncTask = new AsyncTask<String, Void, String>() {
                            @Override
                            protected String doInBackground(String... arg0) {
                                SonetOAuth sonetOAuth = new SonetOAuth(TWITTER_KEY, TWITTER_SECRET, mToken,
                                        mSecret);
                                HttpPost httpPost = new HttpPost(
                                        String.format(TWITTER_RETWEET, TWITTER_BASE_URL, sid));
                                // resolve Error 417 Expectation by Twitter
                                httpPost.getParams().setBooleanParameter("http.protocol.expect-continue",
                                        false);
                                return SonetHttpClient.httpResponse(mHttpClient,
                                        sonetOAuth.getSignedRequest(httpPost));
                            }

                            @Override
                            protected void onPostExecute(String response) {
                                setCommentStatus(0, getString(R.string.retweet));
                                (Toast.makeText(SonetComments.this,
                                        mServiceName + " "
                                                + getString(
                                                        response != null ? R.string.success : R.string.failure),
                                        Toast.LENGTH_LONG)).show();
                            }
                        };
                        setCommentStatus(0, getString(R.string.loading));
                        asyncTask.execute();
                    } else {
                        if ((which < items.length) && (items[which] != null))
                            // open link
                            startActivity(new Intent(Intent.ACTION_VIEW).setData(Uri.parse(items[which])));
                        else
                            (Toast.makeText(SonetComments.this, getString(R.string.error_status),
                                    Toast.LENGTH_LONG)).show();
                    }
                }
            }).setCancelable(true).setOnCancelListener(this).create();
            mDialog.show();
            break;
        case FACEBOOK:
            items = new String[count + 1];
            items[0] = getString(
                    mComments.get(position).get(getString(R.string.like)) == getString(R.string.like)
                            ? R.string.like
                            : R.string.unlike);
            count = 1;
            m.reset();
            while (m.find()) {
                items[count++] = m.group();
            }
            mDialog = (new AlertDialog.Builder(this)).setItems(items, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (which == 0) {
                        AsyncTask<String, Void, String> asyncTask = new AsyncTask<String, Void, String>() {
                            @Override
                            protected String doInBackground(String... arg0) {
                                if (liked.equals(getString(R.string.like))) {
                                    return SonetHttpClient.httpResponse(mHttpClient,
                                            new HttpPost(String.format(FACEBOOK_LIKES, FACEBOOK_BASE_URL, sid,
                                                    Saccess_token, mToken)));
                                } else {
                                    HttpDelete httpDelete = new HttpDelete(String.format(FACEBOOK_LIKES,
                                            FACEBOOK_BASE_URL, sid, Saccess_token, mToken));
                                    httpDelete.setHeader("Content-Length", "0");
                                    return SonetHttpClient.httpResponse(mHttpClient, httpDelete);
                                }
                            }

                            @Override
                            protected void onPostExecute(String response) {
                                if (response != null) {
                                    setCommentStatus(position,
                                            getString(liked.equals(getString(R.string.like)) ? R.string.unlike
                                                    : R.string.like));
                                    (Toast.makeText(SonetComments.this,
                                            mServiceName + " " + getString(R.string.success),
                                            Toast.LENGTH_LONG)).show();
                                } else {
                                    setCommentStatus(position,
                                            getString(liked.equals(getString(R.string.like)) ? R.string.like
                                                    : R.string.unlike));
                                    (Toast.makeText(SonetComments.this,
                                            mServiceName + " " + getString(R.string.failure),
                                            Toast.LENGTH_LONG)).show();
                                }
                            }
                        };
                        setCommentStatus(position, getString(R.string.loading));
                        asyncTask.execute();
                    } else {
                        if ((which < items.length) && (items[which] != null))
                            // open link
                            startActivity(new Intent(Intent.ACTION_VIEW).setData(Uri.parse(items[which])));
                        else
                            (Toast.makeText(SonetComments.this, getString(R.string.error_status),
                                    Toast.LENGTH_LONG)).show();
                    }
                }
            }).setCancelable(true).setOnCancelListener(this).create();
            mDialog.show();
            break;
        case LINKEDIN:
            if (position == 0) {
                items = new String[count + 1];
                items[0] = getString(
                        mComments.get(position).get(getString(R.string.like)) == getString(R.string.like)
                                ? R.string.like
                                : R.string.unlike);
                count = 1;
                m.reset();
                while (m.find()) {
                    items[count++] = m.group();
                }
                mDialog = (new AlertDialog.Builder(this))
                        .setItems(items, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                if (which == 0) {
                                    AsyncTask<String, Void, String> asyncTask = new AsyncTask<String, Void, String>() {
                                        @Override
                                        protected String doInBackground(String... arg0) {
                                            SonetOAuth sonetOAuth = new SonetOAuth(LINKEDIN_KEY,
                                                    LINKEDIN_SECRET, mToken, mSecret);
                                            HttpPut httpPut = new HttpPut(
                                                    String.format(LINKEDIN_IS_LIKED, LINKEDIN_BASE_URL, mSid));
                                            httpPut.addHeader(
                                                    new BasicHeader("Content-Type", "application/xml"));
                                            try {
                                                httpPut.setEntity(new StringEntity(
                                                        String.format(LINKEDIN_LIKE_BODY, Boolean.toString(
                                                                liked.equals(getString(R.string.like))))));
                                                return SonetHttpClient.httpResponse(mHttpClient,
                                                        sonetOAuth.getSignedRequest(httpPut));
                                            } catch (UnsupportedEncodingException e) {
                                                Log.e(TAG, e.toString());
                                            }
                                            return null;
                                        }

                                        @Override
                                        protected void onPostExecute(String response) {
                                            if (response != null) {
                                                setCommentStatus(position,
                                                        getString(liked.equals(getString(R.string.like))
                                                                ? R.string.unlike
                                                                : R.string.like));
                                                (Toast.makeText(SonetComments.this,
                                                        mServiceName + " " + getString(R.string.success),
                                                        Toast.LENGTH_LONG)).show();
                                            } else {
                                                setCommentStatus(position,
                                                        getString(liked.equals(getString(R.string.like))
                                                                ? R.string.like
                                                                : R.string.unlike));
                                                (Toast.makeText(SonetComments.this,
                                                        mServiceName + " " + getString(R.string.failure),
                                                        Toast.LENGTH_LONG)).show();
                                            }
                                        }
                                    };
                                    setCommentStatus(position, getString(R.string.loading));
                                    asyncTask.execute();
                                } else {
                                    if ((which < items.length) && (items[which] != null))
                                        // open link
                                        startActivity(new Intent(Intent.ACTION_VIEW)
                                                .setData(Uri.parse(items[which])));
                                    else
                                        (Toast.makeText(SonetComments.this, getString(R.string.error_status),
                                                Toast.LENGTH_LONG)).show();
                                }
                            }
                        }).setCancelable(true).setOnCancelListener(this).create();
                mDialog.show();
            } else {
                // no like option here
                items = new String[count];
                count = 1;
                m.reset();
                while (m.find()) {
                    items[count++] = m.group();
                }
                mDialog = (new AlertDialog.Builder(this))
                        .setItems(items, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                if ((which < items.length) && (items[which] != null))
                                    // open link
                                    startActivity(
                                            new Intent(Intent.ACTION_VIEW).setData(Uri.parse(items[which])));
                                else
                                    (Toast.makeText(SonetComments.this, getString(R.string.error_status),
                                            Toast.LENGTH_LONG)).show();
                            }
                        }).setCancelable(true).setOnCancelListener(this).create();
                mDialog.show();
            }
            break;
        case IDENTICA:
            // retweet
            items = new String[count + 1];
            items[0] = getString(R.string.repeat);
            count = 1;
            m.reset();
            while (m.find()) {
                items[count++] = m.group();
            }
            mDialog = (new AlertDialog.Builder(this)).setItems(items, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (which == 0) {
                        AsyncTask<String, Void, String> asyncTask = new AsyncTask<String, Void, String>() {
                            @Override
                            protected String doInBackground(String... arg0) {
                                SonetOAuth sonetOAuth = new SonetOAuth(IDENTICA_KEY, IDENTICA_SECRET, mToken,
                                        mSecret);
                                HttpPost httpPost = new HttpPost(
                                        String.format(IDENTICA_RETWEET, IDENTICA_BASE_URL, sid));
                                // resolve Error 417 Expectation by Twitter
                                httpPost.getParams().setBooleanParameter("http.protocol.expect-continue",
                                        false);
                                return SonetHttpClient.httpResponse(mHttpClient,
                                        sonetOAuth.getSignedRequest(httpPost));
                            }

                            @Override
                            protected void onPostExecute(String response) {
                                setCommentStatus(0, getString(R.string.repeat));
                                (Toast.makeText(SonetComments.this,
                                        mServiceName + " "
                                                + getString(
                                                        response != null ? R.string.success : R.string.failure),
                                        Toast.LENGTH_LONG)).show();
                            }
                        };
                        setCommentStatus(0, getString(R.string.loading));
                        asyncTask.execute();
                    } else {
                        if ((which < items.length) && (items[which] != null))
                            // open link
                            startActivity(new Intent(Intent.ACTION_VIEW).setData(Uri.parse(items[which])));
                        else
                            (Toast.makeText(SonetComments.this, getString(R.string.error_status),
                                    Toast.LENGTH_LONG)).show();
                    }
                }
            }).setCancelable(true).setOnCancelListener(this).create();
            mDialog.show();
            break;
        case GOOGLEPLUS:
            //TODO:
            // plus1
            items = new String[count];
            count = 1;
            m.reset();
            while (m.find()) {
                items[count++] = m.group();
            }
            mDialog = (new AlertDialog.Builder(this)).setItems(items, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if ((which < items.length) && (items[which] != null))
                        // open link
                        startActivity(new Intent(Intent.ACTION_VIEW).setData(Uri.parse(items[which])));
                    else
                        (Toast.makeText(SonetComments.this, getString(R.string.error_status),
                                Toast.LENGTH_LONG)).show();
                }
            }).setCancelable(true).setOnCancelListener(this).create();
            mDialog.show();
            break;
        case CHATTER:
            if (position == 0) {
                items = new String[count + 1];
                items[0] = getString(
                        mComments.get(position).get(getString(R.string.like)) == getString(R.string.like)
                                ? R.string.like
                                : R.string.unlike);
                count = 1;
                m.reset();
                while (m.find()) {
                    items[count++] = m.group();
                }
                mDialog = (new AlertDialog.Builder(this))
                        .setItems(items, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                if (which == 0) {
                                    AsyncTask<String, Void, String> asyncTask = new AsyncTask<String, Void, String>() {
                                        @Override
                                        protected String doInBackground(String... arg0) {
                                            HttpUriRequest httpRequest;
                                            if (liked.equals(getString(R.string.like))) {
                                                httpRequest = new HttpPost(String.format(CHATTER_URL_LIKES,
                                                        mChatterInstance, mSid));
                                            } else {
                                                httpRequest = new HttpDelete(String.format(CHATTER_URL_LIKE,
                                                        mChatterInstance, mChatterLikeId));
                                            }
                                            httpRequest.setHeader("Authorization", "OAuth " + mChatterToken);
                                            return SonetHttpClient.httpResponse(mHttpClient, httpRequest);
                                        }

                                        @Override
                                        protected void onPostExecute(String response) {
                                            if (response != null) {
                                                setCommentStatus(position,
                                                        getString(liked.equals(getString(R.string.like))
                                                                ? R.string.unlike
                                                                : R.string.like));
                                                (Toast.makeText(SonetComments.this,
                                                        mServiceName + " " + getString(R.string.success),
                                                        Toast.LENGTH_LONG)).show();
                                            } else {
                                                setCommentStatus(position,
                                                        getString(liked.equals(getString(R.string.like))
                                                                ? R.string.like
                                                                : R.string.unlike));
                                                (Toast.makeText(SonetComments.this,
                                                        mServiceName + " " + getString(R.string.failure),
                                                        Toast.LENGTH_LONG)).show();
                                            }
                                        }
                                    };
                                    setCommentStatus(position, getString(R.string.loading));
                                    asyncTask.execute();
                                } else {
                                    if ((which < items.length) && (items[which] != null))
                                        // open link
                                        startActivity(new Intent(Intent.ACTION_VIEW)
                                                .setData(Uri.parse(items[which])));
                                    else
                                        (Toast.makeText(SonetComments.this, getString(R.string.error_status),
                                                Toast.LENGTH_LONG)).show();
                                }
                            }
                        }).setCancelable(true).setOnCancelListener(this).create();
                mDialog.show();
            } else {
                // no like option here
                items = new String[count];
                count = 1;
                m.reset();
                while (m.find()) {
                    items[count++] = m.group();
                }
                mDialog = (new AlertDialog.Builder(this))
                        .setItems(items, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                if ((which < items.length) && (items[which] != null))
                                    // open link
                                    startActivity(
                                            new Intent(Intent.ACTION_VIEW).setData(Uri.parse(items[which])));
                                else
                                    (Toast.makeText(SonetComments.this, getString(R.string.error_status),
                                            Toast.LENGTH_LONG)).show();
                            }
                        }).setCancelable(true).setOnCancelListener(this).create();
                mDialog.show();
            }
            break;
        }
    }
}

From source file:com.fdwills.external.http.AsyncHttpClient.java

/**
 * Puts a new request in queue as a new thread in pool to be executed
 *
 * @param client          HttpClient to be used for request, can differ in single requests
 * @param contentType     MIME body type, for POST and PUT requests, may be null
 * @param context         Context of Android application, to hold the reference of request
 * @param httpContext     HttpContext in which the request will be executed
 * @param responseHandler ResponseHandler or its subclass to put the response into
 * @param uriRequest      instance of HttpUriRequest, which means it must be of HttpDelete,
 *                        HttpPost, HttpGet, HttpPut, etc.
 * @return RequestHandle of future request process
 *///from   w  ww  .  j a v  a2s . c  o m
protected RequestHandle sendRequest(DefaultHttpClient client, HttpContext httpContext,
        HttpUriRequest uriRequest, String contentType, ResponseHandlerInterface responseHandler,
        Context context) {
    if (uriRequest == null) {
        throw new IllegalArgumentException("HttpUriRequest must not be null");
    }

    if (responseHandler == null) {
        throw new IllegalArgumentException("ResponseHandler must not be null");
    }

    if (responseHandler.getUseSynchronousMode()) {
        throw new IllegalArgumentException(
                "Synchronous ResponseHandler used in AsyncHttpClient. You should create your response handler in a looper thread or use SyncHttpClient instead.");
    }

    if (contentType != null) {
        if (uriRequest instanceof HttpEntityEnclosingRequestBase
                && ((HttpEntityEnclosingRequestBase) uriRequest).getEntity() != null) {
            Log.w(LOG_TAG, "Passed contentType will be ignored because HttpEntity sets content type");
        } else {
            uriRequest.setHeader(HEADER_CONTENT_TYPE, contentType);
        }
    }

    responseHandler.setRequestHeaders(uriRequest.getAllHeaders());
    responseHandler.setRequestURI(uriRequest.getURI());

    AsyncHttpRequest request = newAsyncHttpRequest(client, httpContext, uriRequest, contentType,
            responseHandler, context);
    threadPool.submit(request);
    RequestHandle requestHandle = new RequestHandle(request);

    if (context != null) {
        // Add request to request map
        List<RequestHandle> requestList = requestMap.get(context);
        synchronized (requestMap) {
            if (requestList == null) {
                requestList = Collections.synchronizedList(new LinkedList<RequestHandle>());
                requestMap.put(context, requestList);
            }
        }

        requestList.add(requestHandle);

        Iterator<RequestHandle> iterator = requestList.iterator();
        while (iterator.hasNext()) {
            if (iterator.next().shouldBeGarbageCollected()) {
                iterator.remove();
            }
        }
    }

    return requestHandle;
}

From source file:com.piusvelte.sonet.SonetComments.java

@Override
protected void onListItemClick(ListView list, View view, final int position, long id) {
    super.onListItemClick(list, view, position, id);
    final String sid = mComments.get(position).get(Statuses.SID);
    final String liked = mComments.get(position).get(getString(R.string.like));
    // wait for previous attempts to finish
    if ((liked.length() > 0) && !liked.equals(getString(R.string.loading))) {
        // parse comment body, as in StatusDialog.java
        Matcher m = Sonet.getLinksMatcher(mComments.get(position).get(Statuses.MESSAGE));
        int count = 0;
        while (m.find()) {
            count++;//  w  w  w.ja v a2  s . com
        }
        // like comments, the first comment is the post itself
        switch (mService) {
        case TWITTER:
            // retweet
            items = new String[count + 1];
            items[0] = getString(R.string.retweet);
            count = 1;
            m.reset();
            while (m.find()) {
                items[count++] = m.group();
            }
            mDialog = (new AlertDialog.Builder(this)).setItems(items, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (which == 0) {
                        AsyncTask<String, Void, String> asyncTask = new AsyncTask<String, Void, String>() {
                            @Override
                            protected String doInBackground(String... arg0) {
                                SonetOAuth sonetOAuth = new SonetOAuth(BuildConfig.TWITTER_KEY,
                                        BuildConfig.TWITTER_SECRET, mToken, mSecret);
                                HttpPost httpPost = new HttpPost(
                                        String.format(TWITTER_RETWEET, TWITTER_BASE_URL, sid));
                                // resolve Error 417 Expectation by Twitter
                                httpPost.getParams().setBooleanParameter("http.protocol.expect-continue",
                                        false);
                                return SonetHttpClient.httpResponse(mHttpClient,
                                        sonetOAuth.getSignedRequest(httpPost));
                            }

                            @Override
                            protected void onPostExecute(String response) {
                                setCommentStatus(0, getString(R.string.retweet));
                                (Toast.makeText(SonetComments.this,
                                        mServiceName + " "
                                                + getString(
                                                        response != null ? R.string.success : R.string.failure),
                                        Toast.LENGTH_LONG)).show();
                            }
                        };
                        setCommentStatus(0, getString(R.string.loading));
                        asyncTask.execute();
                    } else {
                        if ((which < items.length) && (items[which] != null))
                            // open link
                            startActivity(new Intent(Intent.ACTION_VIEW).setData(Uri.parse(items[which])));
                        else
                            (Toast.makeText(SonetComments.this, getString(R.string.error_status),
                                    Toast.LENGTH_LONG)).show();
                    }
                }
            }).setCancelable(true).setOnCancelListener(this).create();
            mDialog.show();
            break;
        case FACEBOOK:
            items = new String[count + 1];
            items[0] = getString(
                    mComments.get(position).get(getString(R.string.like)) == getString(R.string.like)
                            ? R.string.like
                            : R.string.unlike);
            count = 1;
            m.reset();
            while (m.find()) {
                items[count++] = m.group();
            }
            mDialog = (new AlertDialog.Builder(this)).setItems(items, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (which == 0) {
                        AsyncTask<String, Void, String> asyncTask = new AsyncTask<String, Void, String>() {
                            @Override
                            protected String doInBackground(String... arg0) {
                                if (liked.equals(getString(R.string.like))) {
                                    return SonetHttpClient.httpResponse(mHttpClient,
                                            new HttpPost(String.format(FACEBOOK_LIKES, FACEBOOK_BASE_URL, sid,
                                                    Saccess_token, mToken)));
                                } else {
                                    HttpDelete httpDelete = new HttpDelete(String.format(FACEBOOK_LIKES,
                                            FACEBOOK_BASE_URL, sid, Saccess_token, mToken));
                                    httpDelete.setHeader("Content-Length", "0");
                                    return SonetHttpClient.httpResponse(mHttpClient, httpDelete);
                                }
                            }

                            @Override
                            protected void onPostExecute(String response) {
                                if (response != null) {
                                    setCommentStatus(position,
                                            getString(liked.equals(getString(R.string.like)) ? R.string.unlike
                                                    : R.string.like));
                                    (Toast.makeText(SonetComments.this,
                                            mServiceName + " " + getString(R.string.success),
                                            Toast.LENGTH_LONG)).show();
                                } else {
                                    setCommentStatus(position,
                                            getString(liked.equals(getString(R.string.like)) ? R.string.like
                                                    : R.string.unlike));
                                    (Toast.makeText(SonetComments.this,
                                            mServiceName + " " + getString(R.string.failure),
                                            Toast.LENGTH_LONG)).show();
                                }
                            }
                        };
                        setCommentStatus(position, getString(R.string.loading));
                        asyncTask.execute();
                    } else {
                        if ((which < items.length) && (items[which] != null))
                            // open link
                            startActivity(new Intent(Intent.ACTION_VIEW).setData(Uri.parse(items[which])));
                        else
                            (Toast.makeText(SonetComments.this, getString(R.string.error_status),
                                    Toast.LENGTH_LONG)).show();
                    }
                }
            }).setCancelable(true).setOnCancelListener(this).create();
            mDialog.show();
            break;
        case LINKEDIN:
            if (position == 0) {
                items = new String[count + 1];
                items[0] = getString(
                        mComments.get(position).get(getString(R.string.like)) == getString(R.string.like)
                                ? R.string.like
                                : R.string.unlike);
                count = 1;
                m.reset();
                while (m.find()) {
                    items[count++] = m.group();
                }
                mDialog = (new AlertDialog.Builder(this))
                        .setItems(items, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                if (which == 0) {
                                    AsyncTask<String, Void, String> asyncTask = new AsyncTask<String, Void, String>() {
                                        @Override
                                        protected String doInBackground(String... arg0) {
                                            SonetOAuth sonetOAuth = new SonetOAuth(BuildConfig.LINKEDIN_KEY,
                                                    BuildConfig.LINKEDIN_SECRET, mToken, mSecret);
                                            HttpPut httpPut = new HttpPut(
                                                    String.format(LINKEDIN_IS_LIKED, LINKEDIN_BASE_URL, mSid));
                                            httpPut.addHeader(
                                                    new BasicHeader("Content-Type", "application/xml"));
                                            try {
                                                httpPut.setEntity(new StringEntity(
                                                        String.format(LINKEDIN_LIKE_BODY, Boolean.toString(
                                                                liked.equals(getString(R.string.like))))));
                                                return SonetHttpClient.httpResponse(mHttpClient,
                                                        sonetOAuth.getSignedRequest(httpPut));
                                            } catch (UnsupportedEncodingException e) {
                                                Log.e(TAG, e.toString());
                                            }
                                            return null;
                                        }

                                        @Override
                                        protected void onPostExecute(String response) {
                                            if (response != null) {
                                                setCommentStatus(position,
                                                        getString(liked.equals(getString(R.string.like))
                                                                ? R.string.unlike
                                                                : R.string.like));
                                                (Toast.makeText(SonetComments.this,
                                                        mServiceName + " " + getString(R.string.success),
                                                        Toast.LENGTH_LONG)).show();
                                            } else {
                                                setCommentStatus(position,
                                                        getString(liked.equals(getString(R.string.like))
                                                                ? R.string.like
                                                                : R.string.unlike));
                                                (Toast.makeText(SonetComments.this,
                                                        mServiceName + " " + getString(R.string.failure),
                                                        Toast.LENGTH_LONG)).show();
                                            }
                                        }
                                    };
                                    setCommentStatus(position, getString(R.string.loading));
                                    asyncTask.execute();
                                } else {
                                    if ((which < items.length) && (items[which] != null))
                                        // open link
                                        startActivity(new Intent(Intent.ACTION_VIEW)
                                                .setData(Uri.parse(items[which])));
                                    else
                                        (Toast.makeText(SonetComments.this, getString(R.string.error_status),
                                                Toast.LENGTH_LONG)).show();
                                }
                            }
                        }).setCancelable(true).setOnCancelListener(this).create();
                mDialog.show();
            } else {
                // no like option here
                items = new String[count];
                count = 1;
                m.reset();
                while (m.find()) {
                    items[count++] = m.group();
                }
                mDialog = (new AlertDialog.Builder(this))
                        .setItems(items, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                if ((which < items.length) && (items[which] != null))
                                    // open link
                                    startActivity(
                                            new Intent(Intent.ACTION_VIEW).setData(Uri.parse(items[which])));
                                else
                                    (Toast.makeText(SonetComments.this, getString(R.string.error_status),
                                            Toast.LENGTH_LONG)).show();
                            }
                        }).setCancelable(true).setOnCancelListener(this).create();
                mDialog.show();
            }
            break;
        case IDENTICA:
            // retweet
            items = new String[count + 1];
            items[0] = getString(R.string.repeat);
            count = 1;
            m.reset();
            while (m.find()) {
                items[count++] = m.group();
            }
            mDialog = (new AlertDialog.Builder(this)).setItems(items, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (which == 0) {
                        AsyncTask<String, Void, String> asyncTask = new AsyncTask<String, Void, String>() {
                            @Override
                            protected String doInBackground(String... arg0) {
                                SonetOAuth sonetOAuth = new SonetOAuth(BuildConfig.IDENTICA_KEY,
                                        BuildConfig.IDENTICA_SECRET, mToken, mSecret);
                                HttpPost httpPost = new HttpPost(
                                        String.format(IDENTICA_RETWEET, IDENTICA_BASE_URL, sid));
                                // resolve Error 417 Expectation by Twitter
                                httpPost.getParams().setBooleanParameter("http.protocol.expect-continue",
                                        false);
                                return SonetHttpClient.httpResponse(mHttpClient,
                                        sonetOAuth.getSignedRequest(httpPost));
                            }

                            @Override
                            protected void onPostExecute(String response) {
                                setCommentStatus(0, getString(R.string.repeat));
                                (Toast.makeText(SonetComments.this,
                                        mServiceName + " "
                                                + getString(
                                                        response != null ? R.string.success : R.string.failure),
                                        Toast.LENGTH_LONG)).show();
                            }
                        };
                        setCommentStatus(0, getString(R.string.loading));
                        asyncTask.execute();
                    } else {
                        if ((which < items.length) && (items[which] != null))
                            // open link
                            startActivity(new Intent(Intent.ACTION_VIEW).setData(Uri.parse(items[which])));
                        else
                            (Toast.makeText(SonetComments.this, getString(R.string.error_status),
                                    Toast.LENGTH_LONG)).show();
                    }
                }
            }).setCancelable(true).setOnCancelListener(this).create();
            mDialog.show();
            break;
        case GOOGLEPLUS:
            //TODO:
            // plus1
            items = new String[count];
            count = 1;
            m.reset();
            while (m.find()) {
                items[count++] = m.group();
            }
            mDialog = (new AlertDialog.Builder(this)).setItems(items, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if ((which < items.length) && (items[which] != null))
                        // open link
                        startActivity(new Intent(Intent.ACTION_VIEW).setData(Uri.parse(items[which])));
                    else
                        (Toast.makeText(SonetComments.this, getString(R.string.error_status),
                                Toast.LENGTH_LONG)).show();
                }
            }).setCancelable(true).setOnCancelListener(this).create();
            mDialog.show();
            break;
        case CHATTER:
            if (position == 0) {
                items = new String[count + 1];
                items[0] = getString(
                        mComments.get(position).get(getString(R.string.like)) == getString(R.string.like)
                                ? R.string.like
                                : R.string.unlike);
                count = 1;
                m.reset();
                while (m.find()) {
                    items[count++] = m.group();
                }
                mDialog = (new AlertDialog.Builder(this))
                        .setItems(items, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                if (which == 0) {
                                    AsyncTask<String, Void, String> asyncTask = new AsyncTask<String, Void, String>() {
                                        @Override
                                        protected String doInBackground(String... arg0) {
                                            HttpUriRequest httpRequest;
                                            if (liked.equals(getString(R.string.like))) {
                                                httpRequest = new HttpPost(String.format(CHATTER_URL_LIKES,
                                                        mChatterInstance, mSid));
                                            } else {
                                                httpRequest = new HttpDelete(String.format(CHATTER_URL_LIKE,
                                                        mChatterInstance, mChatterLikeId));
                                            }
                                            httpRequest.setHeader("Authorization", "OAuth " + mChatterToken);
                                            return SonetHttpClient.httpResponse(mHttpClient, httpRequest);
                                        }

                                        @Override
                                        protected void onPostExecute(String response) {
                                            if (response != null) {
                                                setCommentStatus(position,
                                                        getString(liked.equals(getString(R.string.like))
                                                                ? R.string.unlike
                                                                : R.string.like));
                                                (Toast.makeText(SonetComments.this,
                                                        mServiceName + " " + getString(R.string.success),
                                                        Toast.LENGTH_LONG)).show();
                                            } else {
                                                setCommentStatus(position,
                                                        getString(liked.equals(getString(R.string.like))
                                                                ? R.string.like
                                                                : R.string.unlike));
                                                (Toast.makeText(SonetComments.this,
                                                        mServiceName + " " + getString(R.string.failure),
                                                        Toast.LENGTH_LONG)).show();
                                            }
                                        }
                                    };
                                    setCommentStatus(position, getString(R.string.loading));
                                    asyncTask.execute();
                                } else {
                                    if ((which < items.length) && (items[which] != null))
                                        // open link
                                        startActivity(new Intent(Intent.ACTION_VIEW)
                                                .setData(Uri.parse(items[which])));
                                    else
                                        (Toast.makeText(SonetComments.this, getString(R.string.error_status),
                                                Toast.LENGTH_LONG)).show();
                                }
                            }
                        }).setCancelable(true).setOnCancelListener(this).create();
                mDialog.show();
            } else {
                // no like option here
                items = new String[count];
                count = 1;
                m.reset();
                while (m.find()) {
                    items[count++] = m.group();
                }
                mDialog = (new AlertDialog.Builder(this))
                        .setItems(items, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                if ((which < items.length) && (items[which] != null))
                                    // open link
                                    startActivity(
                                            new Intent(Intent.ACTION_VIEW).setData(Uri.parse(items[which])));
                                else
                                    (Toast.makeText(SonetComments.this, getString(R.string.error_status),
                                            Toast.LENGTH_LONG)).show();
                            }
                        }).setCancelable(true).setOnCancelListener(this).create();
                mDialog.show();
            }
            break;
        }
    }
}

From source file:derson.com.httpsender.AsyncHttpClient.AsyncHttpClient.java

/**
 * Puts a new request in queue as a new thread in pool to be executed
 *
 * @param client          HttpClient to be used for request, can differ in single requests
 * @param contentType     MIME body type, for POST and PUT requests, may be null
 * @param context         Context of Android application, to hold the reference of request
 * @param httpContext     HttpContext in which the request will be executed
 * @param responseHandler ResponseHandler or its subclass to put the response into
 * @param uriRequest      instance of HttpUriRequest, which means it must be of HttpDelete,
 *                        HttpPost, HttpGet, HttpPut, etc.
 * @return RequestHandle of future request process
 *//*from  w w w.  j  ava2  s.  co  m*/
protected RequestHandle sendRequest(DefaultHttpClient client, HttpContext httpContext,
        HttpUriRequest uriRequest, String contentType, ResponseHandlerInterface responseHandler,
        Context context) {
    if (uriRequest == null) {
        throw new IllegalArgumentException("HttpUriRequest must not be null");
    }

    if (responseHandler == null) {
        throw new IllegalArgumentException("ResponseHandler must not be null");
    }
    //        if (responseHandler.getUseSynchronousMode()) {
    //            throw new IllegalArgumentException("Synchronous ResponseHandler used in AsyncHttpClient. You should create your response handler in a looper thread or use SyncHttpClient instead.");
    //        }
    if (contentType != null) {
        if (uriRequest instanceof HttpEntityEnclosingRequestBase
                && ((HttpEntityEnclosingRequestBase) uriRequest).getEntity() != null) {
            HPLog.w(LOG_TAG, "Passed contentType will be ignored because HttpEntity sets content type");
        } else {
            uriRequest.setHeader(HEADER_CONTENT_TYPE, contentType);
        }
    }

    responseHandler.setRequestHeaders(uriRequest.getAllHeaders());
    responseHandler.setRequestURI(uriRequest.getURI());

    AsyncHttpRequest request = newAsyncHttpRequest(client, httpContext, uriRequest, contentType,
            responseHandler, context);
    threadPool.submit(request);
    RequestHandle requestHandle = new RequestHandle(request);

    if (context != null) {
        // Add request to request map
        List<RequestHandle> requestList = requestMap.get(context);
        synchronized (requestMap) {
            if (requestList == null) {
                requestList = Collections.synchronizedList(new LinkedList<RequestHandle>());
                requestMap.put(context, requestList);
            }
        }

        requestList.add(requestHandle);

        Iterator<RequestHandle> iterator = requestList.iterator();
        while (iterator.hasNext()) {
            if (iterator.next().shouldBeGarbageCollected()) {
                iterator.remove();
            }
        }
    }

    return requestHandle;
}

From source file:org.soyatec.windowsazure.management.ServiceManagementRest.java

@Override
public void createAffinityGroup(String groupName, String label, String description, String location) {
    HttpUriRequest request = HttpUtilities
            .createServiceHttpRequest(URI.create(getBaseUrl() + SERVICES_AFFINITYGROUPS), HttpMethod.Post);
    if (groupName == null)
        throw new IllegalArgumentException("Group name is required!");

    if (label == null)
        throw new IllegalArgumentException("Label is required!");

    if (location == null)
        throw new IllegalArgumentException("Location is required!");

    if (description == null)
        description = "";

    try {//from ww w .j  av  a  2 s . co m
        request.setHeader(HeaderNames.ApiVersion, XmsVersion.VERSION_2011_02_25);
        request.addHeader(HeaderNames.ContentType, APPLICATION_XML);

        String base64Label = Base64.encode(label.getBytes());
        String body = MessageFormat.format(CREATE_AFFINITY_GROUP, groupName, base64Label, description,
                location);

        ((HttpEntityEnclosingRequest) request).setEntity(new ByteArrayEntity(body.getBytes()));
        HttpWebResponse response = HttpUtilities.getSSLReponse(request, getSslSocketFactory());
        if (response.getStatusCode() == HttpStatus.SC_OK || response.getStatusCode() == HttpStatus.SC_CREATED) {
            return;
        } else {
            HttpUtilities.processUnexpectedStatusCode(response);
        }
    } catch (StorageException we) {
        throw HttpUtilities.translateWebException(we);
    } catch (Exception e) {
        throw new IllegalStateException(e);
    }
}

From source file:com.shafiq.myfeedle.core.MyfeedleComments.java

@Override
protected void onListItemClick(ListView list, View view, final int position, long id) {
    super.onListItemClick(list, view, position, id);
    final String sid = mComments.get(position).get(Statuses.SID);
    final String liked = mComments.get(position).get(getString(R.string.like));
    // wait for previous attempts to finish
    if ((liked.length() > 0) && !liked.equals(getString(R.string.loading))) {
        // parse comment body, as in StatusDialog.java
        Matcher m = Myfeedle.getLinksMatcher(mComments.get(position).get(Statuses.MESSAGE));
        int count = 0;
        while (m.find()) {
            count++;//w w w .java  2s  .  c o  m
        }
        // like comments, the first comment is the post itself
        switch (mService) {
        case TWITTER:
            // retweet
            items = new String[count + 1];
            items[0] = getString(R.string.retweet);
            count = 1;
            m.reset();
            while (m.find()) {
                items[count++] = m.group();
            }
            mDialog = (new AlertDialog.Builder(this)).setItems(items, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (which == 0) {
                        AsyncTask<String, Void, String> asyncTask = new AsyncTask<String, Void, String>() {
                            @Override
                            protected String doInBackground(String... arg0) {
                                MyfeedleOAuth myfeedleOAuth = new MyfeedleOAuth(TWITTER_KEY, TWITTER_SECRET,
                                        mToken, mSecret);
                                HttpPost httpPost = new HttpPost(
                                        String.format(TWITTER_RETWEET, TWITTER_BASE_URL, sid));
                                // resolve Error 417 Expectation by Twitter
                                httpPost.getParams().setBooleanParameter("http.protocol.expect-continue",
                                        false);
                                return MyfeedleHttpClient.httpResponse(mHttpClient,
                                        myfeedleOAuth.getSignedRequest(httpPost));
                            }

                            @Override
                            protected void onPostExecute(String response) {
                                setCommentStatus(0, getString(R.string.retweet));
                                (Toast.makeText(MyfeedleComments.this,
                                        mServiceName + " "
                                                + getString(
                                                        response != null ? R.string.success : R.string.failure),
                                        Toast.LENGTH_LONG)).show();
                            }
                        };
                        setCommentStatus(0, getString(R.string.loading));
                        asyncTask.execute();
                    } else {
                        if ((which < items.length) && (items[which] != null))
                            // open link
                            startActivity(new Intent(Intent.ACTION_VIEW).setData(Uri.parse(items[which])));
                        else
                            (Toast.makeText(MyfeedleComments.this, getString(R.string.error_status),
                                    Toast.LENGTH_LONG)).show();
                    }
                }
            }).setCancelable(true).setOnCancelListener(this).create();
            mDialog.show();
            break;
        case FACEBOOK:
            items = new String[count + 1];
            items[0] = getString(
                    mComments.get(position).get(getString(R.string.like)) == getString(R.string.like)
                            ? R.string.like
                            : R.string.unlike);
            count = 1;
            m.reset();
            while (m.find()) {
                items[count++] = m.group();
            }
            mDialog = (new AlertDialog.Builder(this)).setItems(items, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (which == 0) {
                        AsyncTask<String, Void, String> asyncTask = new AsyncTask<String, Void, String>() {
                            @Override
                            protected String doInBackground(String... arg0) {
                                if (liked.equals(getString(R.string.like))) {
                                    return MyfeedleHttpClient.httpResponse(mHttpClient,
                                            new HttpPost(String.format(FACEBOOK_LIKES, FACEBOOK_BASE_URL, sid,
                                                    Saccess_token, mToken)));
                                } else {
                                    HttpDelete httpDelete = new HttpDelete(String.format(FACEBOOK_LIKES,
                                            FACEBOOK_BASE_URL, sid, Saccess_token, mToken));
                                    httpDelete.setHeader("Content-Length", "0");
                                    return MyfeedleHttpClient.httpResponse(mHttpClient, httpDelete);
                                }
                            }

                            @Override
                            protected void onPostExecute(String response) {
                                if (response != null) {
                                    setCommentStatus(position,
                                            getString(liked.equals(getString(R.string.like)) ? R.string.unlike
                                                    : R.string.like));
                                    (Toast.makeText(MyfeedleComments.this,
                                            mServiceName + " " + getString(R.string.success),
                                            Toast.LENGTH_LONG)).show();
                                } else {
                                    setCommentStatus(position,
                                            getString(liked.equals(getString(R.string.like)) ? R.string.like
                                                    : R.string.unlike));
                                    (Toast.makeText(MyfeedleComments.this,
                                            mServiceName + " " + getString(R.string.failure),
                                            Toast.LENGTH_LONG)).show();
                                }
                            }
                        };
                        setCommentStatus(position, getString(R.string.loading));
                        asyncTask.execute();
                    } else {
                        if ((which < items.length) && (items[which] != null))
                            // open link
                            startActivity(new Intent(Intent.ACTION_VIEW).setData(Uri.parse(items[which])));
                        else
                            (Toast.makeText(MyfeedleComments.this, getString(R.string.error_status),
                                    Toast.LENGTH_LONG)).show();
                    }
                }
            }).setCancelable(true).setOnCancelListener(this).create();
            mDialog.show();
            break;
        case LINKEDIN:
            if (position == 0) {
                items = new String[count + 1];
                items[0] = getString(
                        mComments.get(position).get(getString(R.string.like)) == getString(R.string.like)
                                ? R.string.like
                                : R.string.unlike);
                count = 1;
                m.reset();
                while (m.find()) {
                    items[count++] = m.group();
                }
                mDialog = (new AlertDialog.Builder(this))
                        .setItems(items, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                if (which == 0) {
                                    AsyncTask<String, Void, String> asyncTask = new AsyncTask<String, Void, String>() {
                                        @Override
                                        protected String doInBackground(String... arg0) {
                                            MyfeedleOAuth myfeedleOAuth = new MyfeedleOAuth(LINKEDIN_KEY,
                                                    LINKEDIN_SECRET, mToken, mSecret);
                                            HttpPut httpPut = new HttpPut(
                                                    String.format(LINKEDIN_IS_LIKED, LINKEDIN_BASE_URL, mSid));
                                            httpPut.addHeader(
                                                    new BasicHeader("Content-Type", "application/xml"));
                                            try {
                                                httpPut.setEntity(new StringEntity(
                                                        String.format(LINKEDIN_LIKE_BODY, Boolean.toString(
                                                                liked.equals(getString(R.string.like))))));
                                                return MyfeedleHttpClient.httpResponse(mHttpClient,
                                                        myfeedleOAuth.getSignedRequest(httpPut));
                                            } catch (UnsupportedEncodingException e) {
                                                Log.e(TAG, e.toString());
                                            }
                                            return null;
                                        }

                                        @Override
                                        protected void onPostExecute(String response) {
                                            if (response != null) {
                                                setCommentStatus(position,
                                                        getString(liked.equals(getString(R.string.like))
                                                                ? R.string.unlike
                                                                : R.string.like));
                                                (Toast.makeText(MyfeedleComments.this,
                                                        mServiceName + " " + getString(R.string.success),
                                                        Toast.LENGTH_LONG)).show();
                                            } else {
                                                setCommentStatus(position,
                                                        getString(liked.equals(getString(R.string.like))
                                                                ? R.string.like
                                                                : R.string.unlike));
                                                (Toast.makeText(MyfeedleComments.this,
                                                        mServiceName + " " + getString(R.string.failure),
                                                        Toast.LENGTH_LONG)).show();
                                            }
                                        }
                                    };
                                    setCommentStatus(position, getString(R.string.loading));
                                    asyncTask.execute();
                                } else {
                                    if ((which < items.length) && (items[which] != null))
                                        // open link
                                        startActivity(new Intent(Intent.ACTION_VIEW)
                                                .setData(Uri.parse(items[which])));
                                    else
                                        (Toast.makeText(MyfeedleComments.this, getString(R.string.error_status),
                                                Toast.LENGTH_LONG)).show();
                                }
                            }
                        }).setCancelable(true).setOnCancelListener(this).create();
                mDialog.show();
            } else {
                // no like option here
                items = new String[count];
                count = 1;
                m.reset();
                while (m.find()) {
                    items[count++] = m.group();
                }
                mDialog = (new AlertDialog.Builder(this))
                        .setItems(items, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                if ((which < items.length) && (items[which] != null))
                                    // open link
                                    startActivity(
                                            new Intent(Intent.ACTION_VIEW).setData(Uri.parse(items[which])));
                                else
                                    (Toast.makeText(MyfeedleComments.this, getString(R.string.error_status),
                                            Toast.LENGTH_LONG)).show();
                            }
                        }).setCancelable(true).setOnCancelListener(this).create();
                mDialog.show();
            }
            break;
        case IDENTICA:
            // retweet
            items = new String[count + 1];
            items[0] = getString(R.string.repeat);
            count = 1;
            m.reset();
            while (m.find()) {
                items[count++] = m.group();
            }
            mDialog = (new AlertDialog.Builder(this)).setItems(items, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (which == 0) {
                        AsyncTask<String, Void, String> asyncTask = new AsyncTask<String, Void, String>() {
                            @Override
                            protected String doInBackground(String... arg0) {
                                MyfeedleOAuth myfeedleOAuth = new MyfeedleOAuth(IDENTICA_KEY, IDENTICA_SECRET,
                                        mToken, mSecret);
                                HttpPost httpPost = new HttpPost(
                                        String.format(IDENTICA_RETWEET, IDENTICA_BASE_URL, sid));
                                // resolve Error 417 Expectation by Twitter
                                httpPost.getParams().setBooleanParameter("http.protocol.expect-continue",
                                        false);
                                return MyfeedleHttpClient.httpResponse(mHttpClient,
                                        myfeedleOAuth.getSignedRequest(httpPost));
                            }

                            @Override
                            protected void onPostExecute(String response) {
                                setCommentStatus(0, getString(R.string.repeat));
                                (Toast.makeText(MyfeedleComments.this,
                                        mServiceName + " "
                                                + getString(
                                                        response != null ? R.string.success : R.string.failure),
                                        Toast.LENGTH_LONG)).show();
                            }
                        };
                        setCommentStatus(0, getString(R.string.loading));
                        asyncTask.execute();
                    } else {
                        if ((which < items.length) && (items[which] != null))
                            // open link
                            startActivity(new Intent(Intent.ACTION_VIEW).setData(Uri.parse(items[which])));
                        else
                            (Toast.makeText(MyfeedleComments.this, getString(R.string.error_status),
                                    Toast.LENGTH_LONG)).show();
                    }
                }
            }).setCancelable(true).setOnCancelListener(this).create();
            mDialog.show();
            break;
        case GOOGLEPLUS:
            //TODO:
            // plus1
            items = new String[count];
            count = 1;
            m.reset();
            while (m.find()) {
                items[count++] = m.group();
            }
            mDialog = (new AlertDialog.Builder(this)).setItems(items, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if ((which < items.length) && (items[which] != null))
                        // open link
                        startActivity(new Intent(Intent.ACTION_VIEW).setData(Uri.parse(items[which])));
                    else
                        (Toast.makeText(MyfeedleComments.this, getString(R.string.error_status),
                                Toast.LENGTH_LONG)).show();
                }
            }).setCancelable(true).setOnCancelListener(this).create();
            mDialog.show();
            break;
        case CHATTER:
            if (position == 0) {
                items = new String[count + 1];
                items[0] = getString(
                        mComments.get(position).get(getString(R.string.like)) == getString(R.string.like)
                                ? R.string.like
                                : R.string.unlike);
                count = 1;
                m.reset();
                while (m.find()) {
                    items[count++] = m.group();
                }
                mDialog = (new AlertDialog.Builder(this))
                        .setItems(items, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                if (which == 0) {
                                    AsyncTask<String, Void, String> asyncTask = new AsyncTask<String, Void, String>() {
                                        @Override
                                        protected String doInBackground(String... arg0) {
                                            HttpUriRequest httpRequest;
                                            if (liked.equals(getString(R.string.like))) {
                                                httpRequest = new HttpPost(String.format(CHATTER_URL_LIKES,
                                                        mChatterInstance, mSid));
                                            } else {
                                                httpRequest = new HttpDelete(String.format(CHATTER_URL_LIKE,
                                                        mChatterInstance, mChatterLikeId));
                                            }
                                            httpRequest.setHeader("Authorization", "OAuth " + mChatterToken);
                                            return MyfeedleHttpClient.httpResponse(mHttpClient, httpRequest);
                                        }

                                        @Override
                                        protected void onPostExecute(String response) {
                                            if (response != null) {
                                                setCommentStatus(position,
                                                        getString(liked.equals(getString(R.string.like))
                                                                ? R.string.unlike
                                                                : R.string.like));
                                                (Toast.makeText(MyfeedleComments.this,
                                                        mServiceName + " " + getString(R.string.success),
                                                        Toast.LENGTH_LONG)).show();
                                            } else {
                                                setCommentStatus(position,
                                                        getString(liked.equals(getString(R.string.like))
                                                                ? R.string.like
                                                                : R.string.unlike));
                                                (Toast.makeText(MyfeedleComments.this,
                                                        mServiceName + " " + getString(R.string.failure),
                                                        Toast.LENGTH_LONG)).show();
                                            }
                                        }
                                    };
                                    setCommentStatus(position, getString(R.string.loading));
                                    asyncTask.execute();
                                } else {
                                    if ((which < items.length) && (items[which] != null))
                                        // open link
                                        startActivity(new Intent(Intent.ACTION_VIEW)
                                                .setData(Uri.parse(items[which])));
                                    else
                                        (Toast.makeText(MyfeedleComments.this, getString(R.string.error_status),
                                                Toast.LENGTH_LONG)).show();
                                }
                            }
                        }).setCancelable(true).setOnCancelListener(this).create();
                mDialog.show();
            } else {
                // no like option here
                items = new String[count];
                count = 1;
                m.reset();
                while (m.find()) {
                    items[count++] = m.group();
                }
                mDialog = (new AlertDialog.Builder(this))
                        .setItems(items, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                if ((which < items.length) && (items[which] != null))
                                    // open link
                                    startActivity(
                                            new Intent(Intent.ACTION_VIEW).setData(Uri.parse(items[which])));
                                else
                                    (Toast.makeText(MyfeedleComments.this, getString(R.string.error_status),
                                            Toast.LENGTH_LONG)).show();
                            }
                        }).setCancelable(true).setOnCancelListener(this).create();
                mDialog.show();
            }
            break;
        }
    }
}

From source file:com.fujitsu.dc.core.rs.box.DcEngineSvcCollectionResource.java

/**
 * relay??.//from  w  w w. j  a v  a  2s .  c o m
 * @param method 
 * @param uriInfo URI
 * @param path ??
 * @param headers 
 * @param is 
 * @return JAX-RS Response
 */
public Response relaycommon(String method, UriInfo uriInfo, String path, HttpHeaders headers, InputStream is) {

    String cellName = this.davRsCmp.getCell().getName();
    String boxName = this.davRsCmp.getBox().getName();
    String requestUrl = String.format("http://%s:%s/%s/%s/%s/service/%s", DcCoreConfig.getEngineHost(),
            DcCoreConfig.getEnginePort(), DcCoreConfig.getEnginePath(), cellName, boxName, path);

    // baseUrl?
    String baseUrl = uriInfo.getBaseUri().toString();

    // ???
    HttpClient client = new DefaultHttpClient();
    HttpUriRequest req = null;
    if (method.equals(HttpMethod.POST)) {
        HttpPost post = new HttpPost(requestUrl);
        InputStreamEntity ise = new InputStreamEntity(is, -1);
        ise.setChunked(true);
        post.setEntity(ise);
        req = post;
    } else if (method.equals(HttpMethod.PUT)) {
        HttpPut put = new HttpPut(requestUrl);
        InputStreamEntity ise = new InputStreamEntity(is, -1);
        ise.setChunked(true);
        put.setEntity(ise);
        req = put;
    } else if (method.equals(HttpMethod.DELETE)) {
        HttpDelete delete = new HttpDelete(requestUrl);
        req = delete;
    } else {
        HttpGet get = new HttpGet(requestUrl);
        req = get;
    }

    req.addHeader("X-Baseurl", baseUrl);
    req.addHeader("X-Request-Uri", uriInfo.getRequestUri().toString());
    // ?INDEXIDTYPE?
    if (davCmp instanceof DavCmpEsImpl) {
        DavCmpEsImpl test = (DavCmpEsImpl) davCmp;
        req.addHeader("X-Dc-Es-Index", test.getEsColType().getIndex().getName());
        req.addHeader("X-Dc-Es-Id", test.getNodeId());
        req.addHeader("X-Dc-Es-Type", test.getEsColType().getType());
        req.addHeader("X-Dc-Es-Routing-Id", this.davRsCmp.getCell().getId());
        req.addHeader("X-Dc-Box-Schema", this.davRsCmp.getBox().getSchema());
    }

    // ???
    MultivaluedMap<String, String> multivalueHeaders = headers.getRequestHeaders();
    for (Iterator<Entry<String, List<String>>> it = multivalueHeaders.entrySet().iterator(); it.hasNext();) {
        Entry<String, List<String>> entry = it.next();
        String key = (String) entry.getKey();
        if (key.equalsIgnoreCase(HttpHeaders.CONTENT_LENGTH)) {
            continue;
        }
        List<String> valueList = (List<String>) entry.getValue();
        for (Iterator<String> i = valueList.iterator(); i.hasNext();) {
            String value = (String) i.next();
            req.setHeader(key, value);
        }
    }

    if (log.isDebugEnabled()) {
        log.debug("?EngineRelay " + req.getMethod() + "  " + req.getURI());
        Header[] reqHeaders = req.getAllHeaders();
        for (int i = 0; i < reqHeaders.length; i++) {
            log.debug("RelayHeader[" + reqHeaders[i].getName() + "] : " + reqHeaders[i].getValue());
        }
    }

    // Engine??
    HttpResponse objResponse = null;
    try {
        objResponse = client.execute(req);
    } catch (ClientProtocolException e) {
        throw DcCoreException.ServiceCollection.SC_INVALID_HTTP_RESPONSE_ERROR;
    } catch (Exception ioe) {
        throw DcCoreException.ServiceCollection.SC_ENGINE_CONNECTION_ERROR.reason(ioe);
    }

    // 
    ResponseBuilder res = Response.status(objResponse.getStatusLine().getStatusCode());
    Header[] headersResEngine = objResponse.getAllHeaders();
    // ?
    for (int i = 0; i < headersResEngine.length; i++) {
        // Engine????Transfer-Encoding????
        // ?MW????????Content-Length???Transfer-Encoding????
        // 2??????????????????????
        if ("Transfer-Encoding".equalsIgnoreCase(headersResEngine[i].getName())) {
            continue;
        }
        // Engine????Date????
        // Web??MW?Jetty???2?????????
        if (HttpHeaders.DATE.equalsIgnoreCase(headersResEngine[i].getName())) {
            continue;
        }
        res.header(headersResEngine[i].getName(), headersResEngine[i].getValue());
    }

    InputStream isResBody = null;

    // ?
    HttpEntity entity = objResponse.getEntity();
    if (entity != null) {
        try {
            isResBody = entity.getContent();
        } catch (IllegalStateException e) {
            throw DcCoreException.ServiceCollection.SC_UNKNOWN_ERROR.reason(e);
        } catch (IOException e) {
            throw DcCoreException.ServiceCollection.SC_ENGINE_CONNECTION_ERROR.reason(e);
        }
        final InputStream isInvariable = isResBody;
        // ??
        StreamingOutput strOutput = new StreamingOutput() {
            @Override
            public void write(final OutputStream os) throws IOException {
                int chr;
                try {
                    while ((chr = isInvariable.read()) != -1) {
                        os.write(chr);
                    }
                } finally {
                    isInvariable.close();
                }
            }
        };
        res.entity(strOutput);
    }

    // ??
    return res.build();
}

From source file:com.example.pierre.applicompanies.library_http.AsyncHttpClient.java

/**
 * Puts a new request in queue as a new thread in pool to be executed
 *
 * @param client          HttpClient to be used for request, can differ in single requests
 * @param contentType     MIME body type, for POST and PUT requests, may be null
 * @param context         Context of Android application, to hold the reference of request
 * @param httpContext     HttpContext in which the request will be executed
 * @param responseHandler ResponseHandler or its subclass to put the response into
 * @param uriRequest      instance of HttpUriRequest, which means it must be of HttpDelete,
 *                        HttpPost, HttpGet, HttpPut, etc.
 * @return RequestHandle of future request process
 *///from  w ww .j  a  v a 2 s.c o  m
protected RequestHandle sendRequest(DefaultHttpClient client, HttpContext httpContext,
        HttpUriRequest uriRequest, String contentType, ResponseHandlerInterface responseHandler,
        Context context) {
    if (uriRequest == null) {
        throw new IllegalArgumentException("HttpUriRequest must not be null");
    }

    if (responseHandler == null) {
        throw new IllegalArgumentException("ResponseHandler must not be null");
    }

    if (responseHandler.getUseSynchronousMode() && !responseHandler.getUsePoolThread()) {
        throw new IllegalArgumentException(
                "Synchronous ResponseHandler used in AsyncHttpClient. You should create your response handler in a looper thread or use SyncHttpClient instead.");
    }

    if (contentType != null) {
        if (uriRequest instanceof HttpEntityEnclosingRequestBase
                && ((HttpEntityEnclosingRequestBase) uriRequest).getEntity() != null) {
            Log.w(LOG_TAG, "Passed contentType will be ignored because HttpEntity sets content type");
        } else {
            uriRequest.setHeader(HEADER_CONTENT_TYPE, contentType);
        }
    }

    responseHandler.setRequestHeaders(uriRequest.getAllHeaders());
    responseHandler.setRequestURI(uriRequest.getURI());

    AsyncHttpRequest request = newAsyncHttpRequest(client, httpContext, uriRequest, contentType,
            responseHandler, context);
    threadPool.submit(request);
    RequestHandle requestHandle = new RequestHandle(request);

    if (context != null) {
        // Add request to request map
        List<RequestHandle> requestList = requestMap.get(context);
        synchronized (requestMap) {
            if (requestList == null) {
                requestList = Collections.synchronizedList(new LinkedList<RequestHandle>());
                requestMap.put(context, requestList);
            }
        }

        requestList.add(requestHandle);

        Iterator<RequestHandle> iterator = requestList.iterator();
        while (iterator.hasNext()) {
            if (iterator.next().shouldBeGarbageCollected()) {
                iterator.remove();
            }
        }
    }

    return requestHandle;
}

From source file:org.jets3t.service.impl.rest.httpclient.RestStorageService.java

/**
 * Performs an HTTP DELETE request using the {@link #performRequest} method.
 *
 * @param bucketName//w  w  w. j  a v  a2s.  c  o  m
 * the bucket's name
 * @param objectKey
 * the object's key name, may be null if the operation is on a bucket only.
 * @return
 * The HTTP method object used to perform the request.
 *
 * @throws org.jets3t.service.ServiceException
 */
protected HttpResponse performRestDelete(String bucketName, String objectKey,
        Map<String, String> requestParameters, String multiFactorSerialNumber, String multiFactorAuthCode)
        throws ServiceException {

    HttpUriRequest httpMethod = setupConnection(HTTP_METHOD.DELETE, bucketName, objectKey, requestParameters);

    // Set Multi-Factor Serial Number and Authentication code if provided.
    if (multiFactorSerialNumber != null || multiFactorAuthCode != null) {
        httpMethod.setHeader(Constants.AMZ_MULTI_FACTOR_AUTH_CODE,
                multiFactorSerialNumber + " " + multiFactorAuthCode);
    }

    HttpResponse result = performRequest(httpMethod, new int[] { 204, 200 });

    // Release connection after DELETE (there's no response content)
    if (log.isDebugEnabled()) {
        log.debug("Releasing HttpMethod after delete");
    }
    releaseConnection(result);

    return result;
}