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()); CloseableHttpResponse response = httpService.execute(putMethod); LOGGER.debug("Got {}", response.getStatusLine().getStatusCode()); } finally { putMethod.releaseConnection(); } } }
/** * 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(); } }
public ApiResponse put(String requestURL, ApiRequest message) throws IOException, SignatureException { log.debug("PUT url: {}", requestURL); log.debug("PUT content: {}", message == null ? "(empty)" : message.content); HttpPut request = new HttpPut(requestURL); if( message != null && message.content != null ) { request.setEntity(new StringEntity(message.content, ContentType.create(message.contentType.toString(), "UTF-8"))); } HttpResponse httpResponse = httpClient.execute(request); ApiResponse apiResponse = readResponse(httpResponse); request.releaseConnection(); return apiResponse; }
@Override public void storeVideo(InputStream videoStream, long contentLength, String mimeType, String sessionId, SessionInfoBean infoBean) throws Exception { HttpPut request = new HttpPut(url.toExternalForm() + "/" + sessionId + ".webm"); request.setEntity(new InputStreamEntity(videoStream, contentLength, ContentType.create(mimeType))); try { HttpResponse response = client.execute(remoteHost, request, new HttpClientContext(clientContext)); int statusCode = response.getStatusLine().getStatusCode(); if(statusCode != HttpStatus.SC_OK && statusCode != HttpStatus.SC_NO_CONTENT && statusCode != HttpStatus.SC_CREATED) { throw new HttpException("Could not upload video - response code: " + statusCode + " reason: " + response.getStatusLine().getReasonPhrase()); } log.info("Successfully uploaded video for session: " + sessionId); } finally { request.releaseConnection(); } }
private void deploy0(HttpEntity entity, String groupId, String artifactId, String version, String extension) throws IOException { StringBuilder path = new StringBuilder(); path.append(groupId.replace('.', '/')).append('/'); path.append(artifactId).append('/'); path.append(version).append('/'); path.append(artifactId).append('-').append(version).append(extension); HttpPut httpPut = new HttpPut(repoUrl + path); httpPut.setEntity(entity); HttpResponse response; try { response = httpclient.execute(httpPut); try { EntityUtils.consume(response.getEntity()); } finally { httpPut.releaseConnection(); } } catch (IOException e) { throw new IOException("IOException executing " + httpPut.toString(), e); } if (response.getStatusLine().getStatusCode() < 200 || response.getStatusLine().getStatusCode() > 299) { throw new IOException(httpPut.toString() + " : " + response.getStatusLine().toString()); } }
private void register() { try { JsonObject provisioner = new JsonObject(); provisioner.addProperty("id", id); provisioner.addProperty("host", "localhost"); provisioner.addProperty("port", myPort()); provisioner.addProperty("capacityTotal", totalCapacity); registerRequest.setEntity(new StringEntity(GSON.toJson(provisioner))); LOG.debug("registering provisioner {}...", id); CloseableHttpResponse response = httpClient.execute(registerRequest); try { int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == 200) { LOG.info("provisioner {} successfully registered.", id); } else { LOG.info("provisioner {} registration failed. Got a {} status with message:\n {}." + "\n Will retry at a later point.", id, statusCode, getResponseString(response)); } } finally { response.close(); } } catch (Exception e) { LOG.error("Exception while making register call.", e); } finally { registerRequest.releaseConnection(); } }
/** * 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(); } }
/** * 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(); } }
@Override public String put(URI uri, Header[] headers, HttpEntity body) throws IOException, SpotifyWebApiException { assert (uri != null); assert (!uri.toString().equals("")); final HttpPut httpPut = new HttpPut(); httpPut.setURI(uri); httpPut.setHeaders(headers); httpPut.setEntity(body); String responseBody = getResponseBody(execute(httpPut)); httpPut.releaseConnection(); return responseBody; }
private CompletionStage<Boolean> put(String url, Object json) { LOGGER.debug("Executing put: url = {}, data = {}", url, json); CompletableFuture<Boolean> promise = new CompletableFuture<Boolean>(); executor.submit(() -> { HttpPut put = new HttpPut(url); try { put.setEntity(new StringEntity( SerializationUtils.toJsonString(json), ContentType.APPLICATION_JSON)); HttpResponse response = client.execute(put); boolean successful = isSuccessful(response); if (!successful) { LOGGER.error("Put request failed :url = {}, " + "status = {}", url, response.getStatusLine().getStatusCode()); } promise.complete(successful); } catch (Throwable t) { LOGGER.error(String.format("Put request failed: url = %s", url), t); promise.completeExceptionally(t); } finally{ put.releaseConnection(); } }); return promise; }
request.releaseConnection(); System.out.println(response.getStatusLine().getReasonPhrase());
throw new MarmottaClientException("could not encode URI parameter"); } finally { put.releaseConnection();
throw new MarmottaClientException("could not encode URI parameter"); } finally { put.releaseConnection();
httpPost.releaseConnection(); httpClient = null;
@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(); } }
@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(); } }
@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(); } }
throw ex; } finally { put.releaseConnection();
@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(); } }
throw ex; } finally { put.releaseConnection();