List of usage examples for org.apache.http.util EntityUtils consumeQuietly
public static void consumeQuietly(HttpEntity httpEntity)
From source file:org.eclipse.rdf4j.http.client.RDF4JProtocolSession.java
public synchronized void rollbackTransaction() throws RDF4JException, IOException, UnauthorizedException { checkRepositoryURL();/*from w w w . j a v a2 s . c o m*/ if (transactionURL == null) { throw new IllegalStateException("Transaction URL has not been set"); } String requestURL = transactionURL; HttpDelete method = new HttpDelete(requestURL); try { final HttpResponse response = execute(method); try { int code = response.getStatusLine().getStatusCode(); if (code == HttpURLConnection.HTTP_NO_CONTENT) { // we're done. transactionURL = null; } else { throw new RepositoryException("unable to rollback transaction. HTTP error code " + code); } } finally { EntityUtils.consumeQuietly(response.getEntity()); } } finally { method.reset(); } }
From source file:io.openvidu.java.client.OpenVidu.java
/** * Lists all existing recordings/*from ww w . j a va 2 s . c o m*/ * * @return A {@link java.util.List} with all existing recordings * * @throws OpenViduJavaClientException * @throws OpenViduHttpException */ @SuppressWarnings("unchecked") public List<Recording> listRecordings() throws OpenViduJavaClientException, OpenViduHttpException { HttpGet request = new HttpGet(OpenVidu.urlOpenViduServer + API_RECORDINGS); HttpResponse response; try { response = OpenVidu.httpClient.execute(request); } catch (IOException e) { throw new OpenViduJavaClientException(e.getMessage(), e.getCause()); } try { int statusCode = response.getStatusLine().getStatusCode(); if ((statusCode == org.apache.http.HttpStatus.SC_OK)) { List<Recording> recordings = new ArrayList<>(); JSONObject json = httpResponseToJson(response); JSONArray array = (JSONArray) json.get("items"); array.forEach(item -> { recordings.add(new Recording((JSONObject) item)); }); return recordings; } else { throw new OpenViduHttpException(statusCode); } } finally { EntityUtils.consumeQuietly(response.getEntity()); } }
From source file:eu.europa.esig.dss.client.http.commons.CommonsDataLoader.java
/** * This method retrieves data using HTTP or HTTPS protocol and 'get' method. * * @param url// w w w . j a v a2 s .co m * to access * @return {@code byte} array of obtained data or null */ protected byte[] httpGet(final String url) { HttpGet httpRequest = null; HttpResponse httpResponse = null; try { final URI uri = new URI(url.trim()); httpRequest = new HttpGet(uri); if (contentType != null) { httpRequest.setHeader(CONTENT_TYPE, contentType); } httpResponse = getHttpResponse(httpRequest, url); final byte[] returnedBytes = readHttpResponse(url, httpResponse); return returnedBytes; } catch (URISyntaxException e) { throw new DSSException(e); } finally { if (httpRequest != null) { httpRequest.releaseConnection(); } if (httpResponse != null) { EntityUtils.consumeQuietly(httpResponse.getEntity()); } } }
From source file:org.apache.olingo.server.example.TripPinServiceTest.java
@Test public void testCreateReadDeleteEntity() throws Exception { String payload = "{\n" + " \"UserName\":\"olingodude\",\n" + " \"FirstName\":\"Olingo\",\n" + " \"LastName\":\"Apache\",\n" + " \"Emails\":[\n" + " \"olingo@apache.org\"\n" + " ],\n" + " \"AddressInfo\":[\n" + " {\n" + " \"Address\":\"100 apache Ln.\",\n" + " \"City\":{\n" + " \"CountryRegion\":\"United States\",\n" + " \"Name\":\"Boise\",\n" + " \"Region\":\"ID\"\n" + " }\n" + " }\n" + " ],\n" + " \"Gender\":\"0\",\n" + " \"Concurrency\":635585295719432047\n" + "}"; HttpPost postRequest = new HttpPost(baseURL + "/People"); postRequest.setEntity(new StringEntity(payload, ContentType.APPLICATION_JSON)); postRequest.addHeader("Prefer", "return=minimal"); HttpResponse response = httpSend(postRequest, 204); // the below would be 204, if minimal was not supplied assertEquals(baseURL + "/People('olingodude')", getHeader(response, "Location")); assertEquals("return=minimal", getHeader(response, "Preference-Applied")); String location = getHeader(response, "Location"); response = httpGET(location, 200);/*w w w.j a v a 2 s . c om*/ EntityUtils.consumeQuietly(response.getEntity()); HttpDelete deleteRequest = new HttpDelete(location); response = httpSend(deleteRequest, 204); EntityUtils.consumeQuietly(response.getEntity()); response = httpGET(location, 404); EntityUtils.consumeQuietly(response.getEntity()); }
From source file:eu.europa.esig.dss.client.http.commons.CommonsDataLoader.java
@Override public byte[] post(final String url, final byte[] content) throws DSSException { LOG.debug("Fetching data via POST from url " + url); HttpPost httpRequest = null;/*w w w . j a v a2 s . c o m*/ HttpResponse httpResponse = null; try { final URI uri = URI.create(url.trim()); httpRequest = new HttpPost(uri); // The length for the InputStreamEntity is needed, because some receivers (on the other side) need this information. // To determine the length, we cannot read the content-stream up to the end and re-use it afterwards. // This is because, it may not be possible to reset the stream (= go to position 0). // So, the solution is to cache temporarily the complete content data (as we do not expect much here) in a byte-array. final ByteArrayInputStream bis = new ByteArrayInputStream(content); final HttpEntity httpEntity = new InputStreamEntity(bis, content.length); final HttpEntity requestEntity = new BufferedHttpEntity(httpEntity); httpRequest.setEntity(requestEntity); if (contentType != null) { httpRequest.setHeader(CONTENT_TYPE, contentType); } httpResponse = getHttpResponse(httpRequest, url); final byte[] returnedBytes = readHttpResponse(url, httpResponse); return returnedBytes; } catch (IOException e) { throw new DSSException(e); } finally { if (httpRequest != null) { httpRequest.releaseConnection(); } if (httpResponse != null) { EntityUtils.consumeQuietly(httpResponse.getEntity()); } } }
From source file:org.fcrepo.test.api.TestRESTAPI.java
@Test public void testGETMethodBuiltInBadMethod() throws Exception { URI url = getURI(//from w ww .j a v a 2 s .c o m String.format("/objects/%s/methods/fedora-system:3/noSuchMethod", DEMO_REST_PID.toString())); verifyNoAuthFailOnAPIAAuth(url); HttpGet get = new HttpGet(url); HttpResponse response; int status = 0; response = getOrDelete(get, getAuthAccess(), false); status = response.getStatusLine().getStatusCode(); EntityUtils.consumeQuietly(response.getEntity()); assertFalse(SC_OK == status); }
From source file:fi.okm.mpass.shibboleth.attribute.resolver.dc.impl.RestDataConnector.java
/** * Fetch school name from external API./*from w ww . j av a2 s. c o m*/ * @param clientBuilder The HTTP client builder. * @param id The school id whose information is fetched. * @param baseUrl The base URL for the external API. It is appended with the ID of the school. * @return The name of the school. */ public static synchronized String getSchoolName(final HttpClientBuilder clientBuilder, final String id, final String baseUrl) { final Logger log = LoggerFactory.getLogger(RestDataConnector.class); if (StringSupport.trimOrNull(id) == null || !StringUtils.isNumeric(id) || id.length() > 6) { return null; } final HttpResponse response; try { final HttpUriRequest get = RequestBuilder.get().setUri(baseUrl + id).build(); response = clientBuilder.buildClient().execute(get); } catch (Exception e) { log.error("Could not get school information with id {}", id, e); return null; } if (response == null) { log.error("Could not get school information with id {}", id); return null; } final String output; try { output = EntityUtils.toString(response.getEntity(), "UTF-8"); } catch (ParseException | IOException e) { log.error("Could not parse school information response with id {}", id, e); return null; } finally { EntityUtils.consumeQuietly(response.getEntity()); } log.trace("Fetched the following response body: {}", output); final Gson gson = new Gson(); try { final OpintopolkuOppilaitosDTO[] oResponse = gson.fromJson(output, OpintopolkuOppilaitosDTO[].class); if (oResponse.length == 1 && oResponse[0].getMetadata() != null && oResponse[0].getMetadata().length == 1) { log.debug("Successfully fetched name for id {}", id); return oResponse[0].getMetadata()[0].getName(); } } catch (JsonSyntaxException | IllegalStateException e) { log.warn("Could not parse the response", e); log.debug("The unparseable response was {}", output); } log.warn("Could not find name for id {}", id); return null; }
From source file:org.fcrepo.test.api.TestRESTAPI.java
@Test public void testGETMethodBuiltInBadUserArg() throws Exception { URI url = getURI(String.format("/objects/%s/methods/fedora-system:3/viewMethodIndex?noSuchArg=foo", DEMO_REST_PID.toString()));//from w ww . j ava 2s . c om verifyNoAuthFailOnAPIAAuth(url); HttpGet get = new HttpGet(url); HttpResponse response = getOrDelete(get, getAuthAccess(), false); int status = response.getStatusLine().getStatusCode(); EntityUtils.consumeQuietly(response.getEntity()); assertFalse(SC_OK == status); }
From source file:io.openvidu.java.client.OpenVidu.java
/** * Deletes a recording. The recording must have status * {@link io.openvidu.java.client.Recording.Status#stopped} or * {@link io.openvidu.java.client.Recording.Status#available} * * @param recordingId The id property of the recording you want to delete * //from w w w. j a v a 2s . c om * @throws OpenViduJavaClientException * @throws OpenViduHttpException Value returned from * {@link io.openvidu.java.client.OpenViduHttpException#getStatus()} * <ul> * <li><code>404</code>: no recording exists * for the passed <i>recordingId</i></li> * <li><code>409</code>: the recording has * <i>started</i> status. Stop it before * deletion</li> * </ul> */ public void deleteRecording(String recordingId) throws OpenViduJavaClientException, OpenViduHttpException { HttpDelete request = new HttpDelete(OpenVidu.urlOpenViduServer + API_RECORDINGS + "/" + recordingId); HttpResponse response; try { response = OpenVidu.httpClient.execute(request); } catch (IOException e) { throw new OpenViduJavaClientException(e.getMessage(), e.getCause()); } try { int statusCode = response.getStatusLine().getStatusCode(); if (!(statusCode == org.apache.http.HttpStatus.SC_NO_CONTENT)) { throw new OpenViduHttpException(statusCode); } } finally { EntityUtils.consumeQuietly(response.getEntity()); } }
From source file:org.apache.olingo.server.example.TripPinServiceTest.java
@Test public void testDeleteEntity() throws Exception { // fail because no key predicates supplied HttpDelete deleteRequest = new HttpDelete(baseURL + "/People"); HttpResponse response = httpSend(deleteRequest, 405); EntityUtils.consumeQuietly(response.getEntity()); }