Java Code Examples for org.apache.http.client.methods.HttpPut#releaseConnection()

The following examples show how to use org.apache.http.client.methods.HttpPut#releaseConnection() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RestClient.java    From kylin with Apache License 2.0 6 votes vote down vote up
public void announceWipeCache(String entity, String event, String cacheKey) throws IOException {
    String url = baseUrl + "/cache/announce/" + entity + "/" + cacheKey + "/" + event;
    HttpPut request = new HttpPut(url);

    try {
        HttpResponse response = client.execute(request);

        if (response.getStatusLine().getStatusCode() != 200) {
            String msg = EntityUtils.toString(response.getEntity());
            throw new IOException("Invalid response " + response.getStatusLine().getStatusCode()
                    + " with announce cache wipe url " + url + "\n" + msg);
        }
    } catch (Exception ex) {
        throw new IOException(ex);
    } finally {
        request.releaseConnection();
    }
}
 
Example 2
Source File: JAXRSClientServerBookTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testUpdateBook() throws Exception {
    String endpointAddress = "http://localhost:" + PORT + "/bookstore/books";

    File input = new File(getClass().getResource("resources/update_book.txt").toURI());
    CloseableHttpClient client = HttpClientBuilder.create().build();
    HttpPut put = new HttpPut(endpointAddress);
    put.setEntity(new FileEntity(input, ContentType.TEXT_XML));

    try {
        CloseableHttpResponse response = client.execute(put);
        assertEquals(200, response.getStatusLine().getStatusCode());
        InputStream expected = getClass().getResourceAsStream("resources/expected_update_book.txt");
        assertEquals(stripXmlInstructionIfNeeded(getStringFromInputStream(expected)),
                     stripXmlInstructionIfNeeded(EntityUtils.toString(response.getEntity())));
    } finally {
        // Release current connection to the connection pool once you are
        // done
        put.releaseConnection();
    }
}
 
Example 3
Source File: JAXRSClientServerBookTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testUpdateBookWithDom() throws Exception {
    String endpointAddress = "http://localhost:" + PORT + "/bookstore/bookswithdom";

    File input = new File(getClass().getResource("resources/update_book.txt").toURI());
    CloseableHttpClient client = HttpClientBuilder.create().build();
    HttpPut put = new HttpPut(endpointAddress);
    put.setEntity(new FileEntity(input, ContentType.TEXT_XML));
    try {
        CloseableHttpResponse response = client.execute(put);
        assertEquals(200, response.getStatusLine().getStatusCode());
        String resp = EntityUtils.toString(response.getEntity());
        InputStream expected = getClass().getResourceAsStream("resources/update_book.txt");
        String s = getStringFromInputStream(expected);
        //System.out.println(resp);
        //System.out.println(s);
        assertTrue(resp.indexOf(s) >= 0);
    } finally {
        // Release current connection to the connection pool once you are
        // done
        put.releaseConnection();
    }
}
 
Example 4
Source File: JAXRSClientServerBookTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testUpdateBookWithJSON() throws Exception {
    String endpointAddress = "http://localhost:" + PORT + "/bookstore/bookswithjson";

    File input = new File(getClass().getResource("resources/update_book_json.txt").toURI());
    CloseableHttpClient client = HttpClientBuilder.create().build();
    HttpPut put = new HttpPut(endpointAddress);
    put.setEntity(new FileEntity(input, ContentType.APPLICATION_JSON));

    try {
        CloseableHttpResponse response = client.execute(put);
        assertEquals(200, response.getStatusLine().getStatusCode());
        InputStream expected = getClass().getResourceAsStream("resources/expected_update_book.txt");
        assertEquals(stripXmlInstructionIfNeeded(getStringFromInputStream(expected)),
                     stripXmlInstructionIfNeeded(EntityUtils.toString(response.getEntity())));
    } finally {
        // Release current connection to the connection pool once you are
        // done
        put.releaseConnection();
    }
}
 
Example 5
Source File: JAXRSClientServerBookTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testUpdateBookFailed() throws Exception {
    String endpointAddress =
        "http://localhost:" + PORT + "/bookstore/books";

    File input = new File(getClass().getResource("resources/update_book_not_exist.txt").toURI());
    CloseableHttpClient client = HttpClientBuilder.create().build();
    HttpPut put = new HttpPut(endpointAddress);
    put.setEntity(new FileEntity(input, ContentType.TEXT_XML));

    try {
        CloseableHttpResponse response = client.execute(put);
        assertEquals(304, response.getStatusLine().getStatusCode());
    } finally {
        // Release current connection to the connection pool once you are done
        put.releaseConnection();
    }
}
 
Example 6
Source File: HttpClientUtil.java    From redis-manager with Apache License 2.0 5 votes vote down vote up
public static String put(String url, JSONObject data) throws IOException {
    HttpPut httpPut = putForm(url, data);
    HttpResponse response;
    String result = null;
    try {
        response = httpclient.execute(httpPut);
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            result = EntityUtils.toString(entity);
        }
    } finally {
        httpPut.releaseConnection();
    }
    return result;
}
 
Example 7
Source File: Client.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Send a PUT request
 * @param cluster the cluster definition
 * @param path the path or URI
 * @param headers the HTTP headers to include, <tt>Content-Type</tt> must be
 * supplied
 * @param content the content bytes
 * @return a Response object with response detail
 * @throws IOException
 */
public Response put(Cluster cluster, String path, Header[] headers,
    byte[] content) throws IOException {
  HttpPut method = new HttpPut(path);
  try {
    method.setEntity(new InputStreamEntity(new ByteArrayInputStream(content), content.length));
    HttpResponse resp = execute(cluster, method, headers, path);
    headers = resp.getAllHeaders();
    content = getResponseBody(resp);
    return new Response(resp.getStatusLine().getStatusCode(), headers, content);
  } finally {
    method.releaseConnection();
  }
}
 
Example 8
Source File: RemoteConsoleAppender.java    From gocd with Apache License 2.0 5 votes vote down vote up
@Override
public void append(String content) throws IOException {
    HttpPut putMethod = new HttpPut(consoleUri);
    try {
        LOGGER.debug("Appending console to URL -> {}", consoleUri);
        StringEntity entity = new StringEntity(content, charset);
        putMethod.setEntity(entity);
        HttpService.setSizeHeader(putMethod, entity.getContentLength());
        try (CloseableHttpResponse response = httpService.execute(putMethod)) {
            LOGGER.debug("Got {}", response.getStatusLine().getStatusCode());
        }
    } finally {
        putMethod.releaseConnection();
    }
}