Search in sources :

Example 51 with HttpDelete

use of org.apache.http.client.methods.HttpDelete in project musicbrainz-android by jdamcd.

the class MusicBrainzWebClient method delete.

private void delete(String url) throws IOException {
    HttpDelete delete = new HttpDelete(url);
    HttpResponse response = httpClient.execute(delete);
    response.getEntity().consumeContent();
}
Also used : HttpDelete(org.apache.http.client.methods.HttpDelete) HttpResponse(org.apache.http.HttpResponse)

Example 52 with HttpDelete

use of org.apache.http.client.methods.HttpDelete in project SmartAndroidSource by jaychou2012.

the class AbstractAjaxCallback method httpDelete.

private void httpDelete(String url, Map<String, String> headers, AjaxStatus status) throws IOException {
    AQUtility.debug("get", url);
    url = patchUrl(url);
    HttpDelete del = new HttpDelete(url);
    httpDo(del, url, headers, status);
}
Also used : HttpDelete(org.apache.http.client.methods.HttpDelete)

Example 53 with HttpDelete

use of org.apache.http.client.methods.HttpDelete in project Fling by entertailion.

the class RampClient method launchApp.

public void launchApp(String app, DialServer dialServer) {
    this.app = app;
    this.isChromeCast = app.equals(FlingFrame.CHROMECAST);
    this.dialServer = dialServer;
    this.activityId = UUID.randomUUID().toString();
    try {
        String device = "http://" + dialServer.getIpAddress().getHostAddress() + ":" + dialServer.getPort();
        Log.d(LOG_TAG, "device=" + device);
        Log.d(LOG_TAG, "apps url=" + dialServer.getAppsUrl());
        // application instance url
        String location = null;
        DefaultHttpClient defaultHttpClient = HttpRequestHelper.createHttpClient();
        CustomRedirectHandler handler = new CustomRedirectHandler();
        defaultHttpClient.setRedirectHandler(handler);
        BasicHttpContext localContext = new BasicHttpContext();
        // check if any app is running
        HttpGet httpGet = new HttpGet(dialServer.getAppsUrl());
        httpGet.setHeader(HEADER_CONNECTION, HEADER_CONNECTION_VALUE);
        httpGet.setHeader(HEADER_USER_AGENT, HEADER_USER_AGENT_VALUE);
        httpGet.setHeader(HEADER_ACCEPT, HEADER_ACCEPT_VALUE);
        httpGet.setHeader(HEADER_DNT, HEADER_DNT_VALUE);
        httpGet.setHeader(HEADER_ACCEPT_ENCODING, HEADER_ACCEPT_ENCODING_VALUE);
        httpGet.setHeader(HEADER_ACCEPT_LANGUAGE, HEADER_ACCEPT_LANGUAGE_VALUE);
        HttpResponse httpResponse = defaultHttpClient.execute(httpGet);
        if (httpResponse != null) {
            int responseCode = httpResponse.getStatusLine().getStatusCode();
            Log.d(LOG_TAG, "get response code=" + httpResponse.getStatusLine().getStatusCode());
            if (responseCode == 204) {
            // nothing is running
            } else if (responseCode == 200) {
                // app is running
                // Need to get real URL after a redirect
                // http://stackoverflow.com/a/10286025/594751
                String lastUrl = dialServer.getAppsUrl();
                if (handler.lastRedirectedUri != null) {
                    lastUrl = handler.lastRedirectedUri.toString();
                    Log.d(LOG_TAG, "lastUrl=" + lastUrl);
                }
                String response = EntityUtils.toString(httpResponse.getEntity());
                Log.d(LOG_TAG, "get response=" + response);
                parseXml(new StringReader(response));
                Header[] headers = httpResponse.getAllHeaders();
                for (int i = 0; i < headers.length; i++) {
                    Log.d(LOG_TAG, headers[i].getName() + "=" + headers[i].getValue());
                }
                // stop the app instance
                HttpDelete httpDelete = new HttpDelete(lastUrl);
                httpResponse = defaultHttpClient.execute(httpDelete);
                if (httpResponse != null) {
                    Log.d(LOG_TAG, "delete response code=" + httpResponse.getStatusLine().getStatusCode());
                    response = EntityUtils.toString(httpResponse.getEntity());
                    Log.d(LOG_TAG, "delete response=" + response);
                } else {
                    Log.d(LOG_TAG, "no delete response");
                }
            }
        } else {
            Log.i(LOG_TAG, "no get response");
            return;
        }
        // Check if app is installed on device
        int responseCode = getAppStatus(defaultHttpClient, dialServer.getAppsUrl() + app);
        if (responseCode != 200) {
            return;
        }
        parseXml(new StringReader(response));
        Log.d(LOG_TAG, "state=" + state);
        // start the app with POST
        HttpPost httpPost = new HttpPost(dialServer.getAppsUrl() + app);
        httpPost.setHeader(HEADER_CONNECTION, HEADER_CONNECTION_VALUE);
        httpPost.setHeader(HEADER_ORIGN, HEADER_ORIGIN_VALUE);
        httpPost.setHeader(HEADER_USER_AGENT, HEADER_USER_AGENT_VALUE);
        httpPost.setHeader(HEADER_DNT, HEADER_DNT_VALUE);
        httpPost.setHeader(HEADER_ACCEPT_ENCODING, HEADER_ACCEPT_ENCODING_VALUE);
        httpPost.setHeader(HEADER_ACCEPT, HEADER_ACCEPT_VALUE);
        httpPost.setHeader(HEADER_ACCEPT_LANGUAGE, HEADER_ACCEPT_LANGUAGE_VALUE);
        httpPost.setHeader(HEADER_CONTENT_TYPE, HEADER_CONTENT_TYPE_TEXT_VALUE);
        if (isChromeCast) {
            //httpPost.setEntity(new StringEntity("v=release-d4fa0a24f89ec5ba83f7bf3324282c8d046bf612&id=local%3A1&idle=windowclose"));
            httpPost.setEntity(new StringEntity("v=release-d4fa0a24f89ec5ba83f7bf3324282c8d046bf612&id=local%3A1"));
        }
        httpResponse = defaultHttpClient.execute(httpPost, localContext);
        if (httpResponse != null) {
            Log.d(LOG_TAG, "post response code=" + httpResponse.getStatusLine().getStatusCode());
            response = EntityUtils.toString(httpResponse.getEntity());
            Log.d(LOG_TAG, "post response=" + response);
            Header[] headers = httpResponse.getHeaders("LOCATION");
            if (headers.length > 0) {
                location = headers[0].getValue();
                Log.d(LOG_TAG, "post response location=" + location);
            }
            headers = httpResponse.getAllHeaders();
            for (int i = 0; i < headers.length; i++) {
                Log.d(LOG_TAG, headers[i].getName() + "=" + headers[i].getValue());
            }
        } else {
            Log.i(LOG_TAG, "no post response");
            return;
        }
        // Keep trying to get the app status until the
        // connection service URL is available
        state = STATE_STOPPED;
        do {
            responseCode = getAppStatus(defaultHttpClient, dialServer.getAppsUrl() + app);
            if (responseCode != 200) {
                break;
            }
            parseXml(new StringReader(response));
            Log.d(LOG_TAG, "state=" + state);
            Log.d(LOG_TAG, "connectionServiceUrl=" + connectionServiceUrl);
            Log.d(LOG_TAG, "protocol=" + protocol);
            try {
                Thread.sleep(1000);
            } catch (Exception e) {
            }
        } while (state.equals(STATE_RUNNING) && connectionServiceUrl == null);
        if (connectionServiceUrl == null) {
            Log.i(LOG_TAG, "connectionServiceUrl is null");
            // oops, something went wrong
            return;
        }
        // get the websocket URL
        String webSocketAddress = null;
        // "http://192.168.0.17:8008/connection/YouTube"
        httpPost = new HttpPost(connectionServiceUrl);
        httpPost.setHeader(HEADER_CONNECTION, HEADER_CONNECTION_VALUE);
        httpPost.setHeader(HEADER_ORIGN, HEADER_ORIGIN_VALUE);
        httpPost.setHeader(HEADER_USER_AGENT, HEADER_USER_AGENT_VALUE);
        httpPost.setHeader(HEADER_DNT, HEADER_DNT_VALUE);
        httpPost.setHeader(HEADER_ACCEPT_ENCODING, HEADER_ACCEPT_ENCODING_VALUE);
        httpPost.setHeader(HEADER_ACCEPT, HEADER_ACCEPT_VALUE);
        httpPost.setHeader(HEADER_ACCEPT_LANGUAGE, HEADER_ACCEPT_LANGUAGE_VALUE);
        httpPost.setHeader(HEADER_CONTENT_TYPE, HEADER_CONTENT_TYPE_JSON_VALUE);
        httpPost.setEntity(new StringEntity("{\"channel\":0,\"senderId\":{\"appName\":\"" + app + "\", \"senderId\":\"" + senderId + "\"}}"));
        httpResponse = defaultHttpClient.execute(httpPost, localContext);
        if (httpResponse != null) {
            responseCode = httpResponse.getStatusLine().getStatusCode();
            Log.d(LOG_TAG, "post response code=" + responseCode);
            if (responseCode == 200) {
                // should return JSON payload
                response = EntityUtils.toString(httpResponse.getEntity());
                Log.d(LOG_TAG, "post response=" + response);
                Header[] headers = httpResponse.getAllHeaders();
                for (int i = 0; i < headers.length; i++) {
                    Log.d(LOG_TAG, headers[i].getName() + "=" + headers[i].getValue());
                }
                // http://code.google.com/p/json-simple/
                JSONParser parser = new JSONParser();
                try {
                    // {"URL":"ws://192.168.0.17:8008/session?33","pingInterval":0}
                    Object obj = parser.parse(new StringReader(response));
                    JSONObject jsonObject = (JSONObject) obj;
                    webSocketAddress = (String) jsonObject.get("URL");
                    Log.d(LOG_TAG, "webSocketAddress: " + webSocketAddress);
                    // TODO
                    long pingInterval = (Long) jsonObject.get("pingInterval");
                } catch (Exception e) {
                    Log.e(LOG_TAG, "parse JSON", e);
                }
            }
        } else {
            Log.i(LOG_TAG, "no post response");
            return;
        }
        // Make a web socket connection for doing RAMP
        // to control media playback
        this.started = false;
        this.closed = false;
        this.gotStatus = false;
        if (webSocketAddress != null) {
            // https://github.com/TooTallNate/Java-WebSocket
            URI uri = URI.create(webSocketAddress);
            rampWebSocketClient = new RampWebSocketClient(uri, this);
            new Thread(new Runnable() {

                public void run() {
                    Thread t = new Thread(rampWebSocketClient);
                    t.start();
                    try {
                        t.join();
                    } catch (InterruptedException e1) {
                        e1.printStackTrace();
                    } finally {
                        rampWebSocketClient.close();
                    }
                }
            }).start();
        } else {
            Log.i(LOG_TAG, "webSocketAddress is null");
        }
    } catch (Exception e) {
        Log.e(LOG_TAG, "launchApp", e);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpDelete(org.apache.http.client.methods.HttpDelete) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) URI(java.net.URI) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) ProtocolException(org.apache.http.ProtocolException) StringEntity(org.apache.http.entity.StringEntity) Header(org.apache.http.Header) JSONObject(org.json.simple.JSONObject) StringReader(java.io.StringReader) JSONParser(org.json.simple.parser.JSONParser) JSONObject(org.json.simple.JSONObject)

Example 54 with HttpDelete

use of org.apache.http.client.methods.HttpDelete in project hbase by apache.

the class Client method delete.

/**
   * Send a DELETE request
   * @param cluster the cluster definition
   * @param path the path or URI
   * @return a Response object with response detail
   * @throws IOException for error
   */
public Response delete(Cluster cluster, String path, Header extraHdr) throws IOException {
    HttpDelete method = new HttpDelete(path);
    try {
        Header[] headers = { extraHdr };
        HttpResponse resp = execute(cluster, method, headers, path);
        headers = resp.getAllHeaders();
        byte[] content = getResponseBody(resp);
        return new Response(resp.getStatusLine().getStatusCode(), headers, content);
    } finally {
        method.releaseConnection();
    }
}
Also used : HttpResponse(org.apache.http.HttpResponse) HttpDelete(org.apache.http.client.methods.HttpDelete) Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) HttpResponse(org.apache.http.HttpResponse)

Example 55 with HttpDelete

use of org.apache.http.client.methods.HttpDelete in project hbase by apache.

the class Client method executePathOnly.

/**
   * Execute a transaction method given only the path. Will select at random
   * one of the members of the supplied cluster definition and iterate through
   * the list until a transaction can be successfully completed. The
   * definition of success here is a complete HTTP transaction, irrespective
   * of result code.
   * @param cluster the cluster definition
   * @param method the transaction method
   * @param headers HTTP header values to send
   * @param path the properly urlencoded path
   * @return the HTTP response code
   * @throws IOException
   */
public HttpResponse executePathOnly(Cluster cluster, HttpUriRequest method, Header[] headers, String path) throws IOException {
    IOException lastException;
    if (cluster.nodes.size() < 1) {
        throw new IOException("Cluster is empty");
    }
    int start = (int) Math.round((cluster.nodes.size() - 1) * Math.random());
    int i = start;
    do {
        cluster.lastHost = cluster.nodes.get(i);
        try {
            StringBuilder sb = new StringBuilder();
            if (sslEnabled) {
                sb.append("https://");
            } else {
                sb.append("http://");
            }
            sb.append(cluster.lastHost);
            sb.append(path);
            URI uri = new URI(sb.toString());
            if (method instanceof HttpPut) {
                HttpPut put = new HttpPut(uri);
                put.setEntity(((HttpPut) method).getEntity());
                put.setHeaders(method.getAllHeaders());
                method = put;
            } else if (method instanceof HttpGet) {
                method = new HttpGet(uri);
            } else if (method instanceof HttpHead) {
                method = new HttpHead(uri);
            } else if (method instanceof HttpDelete) {
                method = new HttpDelete(uri);
            } else if (method instanceof HttpPost) {
                HttpPost post = new HttpPost(uri);
                post.setEntity(((HttpPost) method).getEntity());
                post.setHeaders(method.getAllHeaders());
                method = post;
            }
            return executeURI(method, headers, uri.toString());
        } catch (IOException e) {
            lastException = e;
        } catch (URISyntaxException use) {
            lastException = new IOException(use);
        }
    } while (++i != start && i < cluster.nodes.size());
    throw lastException;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpDelete(org.apache.http.client.methods.HttpDelete) HttpGet(org.apache.http.client.methods.HttpGet) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) HttpPut(org.apache.http.client.methods.HttpPut) HttpHead(org.apache.http.client.methods.HttpHead)

Aggregations

HttpDelete (org.apache.http.client.methods.HttpDelete)108 HttpResponse (org.apache.http.HttpResponse)25 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)22 Test (org.junit.Test)21 HttpGet (org.apache.http.client.methods.HttpGet)16 HttpPut (org.apache.http.client.methods.HttpPut)16 HttpPost (org.apache.http.client.methods.HttpPost)15 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)14 IOException (java.io.IOException)12 Deployment (org.activiti.engine.test.Deployment)12 ProcessInstance (org.activiti.engine.runtime.ProcessInstance)11 Task (org.activiti.engine.task.Task)9 StringEntity (org.apache.http.entity.StringEntity)9 URI (java.net.URI)7 RequestExecutor (org.apache.stanbol.commons.testing.http.RequestExecutor)7 URISyntaxException (java.net.URISyntaxException)6 Header (org.apache.http.Header)6 HttpEntity (org.apache.http.HttpEntity)6 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)6 User (org.activiti.engine.identity.User)5