List of usage examples for org.apache.http.util EntityUtils consumeQuietly
public static void consumeQuietly(HttpEntity httpEntity)
From source file:org.seasar.robot.client.http.HcHttpClient.java
protected ResponseData processHttpMethod(final String url, final HttpUriRequest httpRequest) { try {//from ww w.ja v a 2 s . c o m processRobotsTxt(url); } catch (final RobotCrawlAccessException e) { if (logger.isInfoEnabled()) { final StringBuilder buf = new StringBuilder(); buf.append(e.getMessage()); if (e.getCause() != null) { buf.append(e.getCause().getMessage()); } logger.info(buf.toString()); } else if (logger.isDebugEnabled()) { logger.debug("Crawling Access Exception at " + url, e); } } // request header for (final Header header : requestHeaderList) { httpRequest.addHeader(header); } ResponseData responseData = null; InputStream inputStream = null; HttpEntity httpEntity = null; try { // get a content final HttpResponse response = executeHttpClient(httpRequest); httpEntity = response.getEntity(); final int httpStatusCode = response.getStatusLine().getStatusCode(); // redirect if (isRedirectHttpStatus(httpStatusCode)) { final Header locationHeader = response.getFirstHeader("location"); if (locationHeader == null) { logger.warn("Invalid redirect location at " + url); } else { responseData = new ResponseData(); responseData.setRedirectLocation(locationHeader.getValue()); return responseData; } } long contentLength = 0; String contentEncoding = Constants.UTF_8; if (httpEntity == null) { inputStream = new ByteArrayInputStream(new byte[0]); } else { final InputStream responseBodyStream = httpEntity.getContent(); final File outputFile = File.createTempFile("s2robot-HcHttpClient-", ".out"); DeferredFileOutputStream dfos = null; try { try { dfos = new DeferredFileOutputStream(responseBodyInMemoryThresholdSize, outputFile); StreamUtil.drain(responseBodyStream, dfos); dfos.flush(); } finally { IOUtils.closeQuietly(dfos); } } catch (final Exception e) { if (!outputFile.delete()) { logger.warn("Could not delete " + outputFile.getAbsolutePath()); } throw e; } if (dfos.isInMemory()) { inputStream = new ByteArrayInputStream(dfos.getData()); contentLength = dfos.getData().length; if (!outputFile.delete()) { logger.warn("Could not delete " + outputFile.getAbsolutePath()); } } else { inputStream = new TemporaryFileInputStream(outputFile); contentLength = outputFile.length(); } final Header contentEncodingHeader = httpEntity.getContentEncoding(); if (contentEncodingHeader != null) { contentEncoding = contentEncodingHeader.getValue(); } } String contentType = null; final Header contentTypeHeader = response.getFirstHeader("Content-Type"); if (contentTypeHeader != null) { contentType = contentTypeHeader.getValue(); final int idx = contentType.indexOf(';'); if (idx > 0) { contentType = contentType.substring(0, idx); } } // check file size if (contentLengthHelper != null) { final long maxLength = contentLengthHelper.getMaxLength(contentType); if (contentLength > maxLength) { throw new MaxLengthExceededException("The content length (" + contentLength + " byte) is over " + maxLength + " byte. The url is " + url); } } responseData = new ResponseData(); responseData.setUrl(url); responseData.setCharSet(contentEncoding); if (httpRequest instanceof HttpHead) { responseData.setMethod(Constants.HEAD_METHOD); } else { responseData.setMethod(Constants.GET_METHOD); } responseData.setResponseBody(inputStream); responseData.setHttpStatusCode(httpStatusCode); for (final Header header : response.getAllHeaders()) { responseData.addMetaData(header.getName(), header.getValue()); } if (contentType == null) { responseData.setMimeType(defaultMimeType); } else { responseData.setMimeType(contentType); } final Header contentLengthHeader = response.getFirstHeader("Content-Length"); if (contentLengthHeader == null) { responseData.setContentLength(contentLength); } else { final String value = contentLengthHeader.getValue(); try { responseData.setContentLength(Long.parseLong(value)); } catch (final Exception e) { responseData.setContentLength(contentLength); } } final Header lastModifiedHeader = response.getFirstHeader("Last-Modified"); if (lastModifiedHeader != null) { final String value = lastModifiedHeader.getValue(); if (StringUtil.isNotBlank(value)) { final Date d = parseLastModified(value); if (d != null) { responseData.setLastModified(d); } } } if (responseData.getLastModified() == null) { responseData.setLastModified(new Date()); // set current time } return responseData; } catch (final UnknownHostException e) { httpRequest.abort(); IOUtils.closeQuietly(inputStream); throw new RobotCrawlAccessException("Unknown host(" + e.getMessage() + "): " + url, e); } catch (final NoRouteToHostException e) { httpRequest.abort(); IOUtils.closeQuietly(inputStream); throw new RobotCrawlAccessException("No route to host(" + e.getMessage() + "): " + url, e); } catch (final ConnectException e) { httpRequest.abort(); IOUtils.closeQuietly(inputStream); throw new RobotCrawlAccessException("Connection time out(" + e.getMessage() + "): " + url, e); } catch (final SocketException e) { httpRequest.abort(); IOUtils.closeQuietly(inputStream); throw new RobotCrawlAccessException("Socket exception(" + e.getMessage() + "): " + url, e); } catch (final IOException e) { httpRequest.abort(); IOUtils.closeQuietly(inputStream); throw new RobotCrawlAccessException("I/O exception(" + e.getMessage() + "): " + url, e); } catch (final RobotSystemException e) { httpRequest.abort(); IOUtils.closeQuietly(inputStream); throw e; } catch (final Exception e) { httpRequest.abort(); IOUtils.closeQuietly(inputStream); throw new RobotSystemException("Failed to access " + url, e); } finally { EntityUtils.consumeQuietly(httpEntity); } }
From source file:org.fcrepo.test.api.TestRESTAPI.java
@Test public void testGETMethodCustomEarlyDate() throws Exception { URI url = getURI(/* w w w .j ava 2 s . com*/ "/objects/demo:14/methods/demo:12/getDocumentStyle1?asOfDateTime=1999-11-21T16:38:32.200Z"); 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()); assertEquals(SC_NOT_FOUND, status); }
From source file:org.apache.olingo.server.example.TripPinServiceTest.java
@Test public void testBadNavigationProperty() throws Exception { String editUrl = baseURL + "/People('russellwhyte')/Unknown"; HttpResponse response = httpGET(editUrl, 404); EntityUtils.consumeQuietly(response.getEntity()); }
From source file:org.eclipse.rdf4j.http.client.SPARQLProtocolSession.java
/** * Parse the response in a background thread. HTTP connections are dealt with in the * {@link BackgroundTupleResult} or (in the error-case) in this method. */// ww w. j a v a2 s .com protected BackgroundTupleResult getBackgroundTupleQueryResult(HttpUriRequest method) throws RepositoryException, QueryInterruptedException, MalformedQueryException, IOException { boolean submitted = false; // Specify which formats we support Set<QueryResultFormat> tqrFormats = TupleQueryResultParserRegistry.getInstance().getKeys(); if (tqrFormats.isEmpty()) { throw new RepositoryException("No tuple query result parsers have been registered"); } BackgroundTupleResult tRes = null; // send the tuple query HttpResponse response = sendTupleQueryViaHttp(method, tqrFormats); try { // if we get here, HTTP code is 200 String mimeType = getResponseMIMEType(response); QueryResultFormat format = TupleQueryResultFormat.matchMIMEType(mimeType, tqrFormats).orElseThrow( () -> new RepositoryException("Server responded with an unsupported file format: " + mimeType)); TupleQueryResultParser parser = QueryResultIO.createTupleParser(format, getValueFactory()); tRes = new BackgroundTupleResult(parser, response.getEntity().getContent()); execute(tRes); submitted = true; return tRes; } finally { if (!submitted) { try { if (tRes != null) { tRes.close(); } } finally { EntityUtils.consumeQuietly(response.getEntity()); } } } }
From source file:org.eclipse.rdf4j.http.client.SPARQLProtocolSession.java
/** * Parse the response in this thread using the provided {@link TupleQueryResultHandler}. All HTTP * connections are closed and released in this method *//*from ww w . j av a 2s . c om*/ protected void getTupleQueryResult(HttpUriRequest method, TupleQueryResultHandler handler) throws IOException, TupleQueryResultHandlerException, RepositoryException, MalformedQueryException, UnauthorizedException, QueryInterruptedException { // Specify which formats we support Set<QueryResultFormat> tqrFormats = TupleQueryResultParserRegistry.getInstance().getKeys(); if (tqrFormats.isEmpty()) { throw new RepositoryException("No tuple query result parsers have been registered"); } // send the tuple query HttpResponse response = sendTupleQueryViaHttp(method, tqrFormats); try { // if we get here, HTTP code is 200 String mimeType = getResponseMIMEType(response); try { QueryResultFormat format = TupleQueryResultFormat.matchMIMEType(mimeType, tqrFormats) .orElseThrow(() -> new RepositoryException( "Server responded with an unsupported file format: " + mimeType)); TupleQueryResultParser parser = QueryResultIO.createTupleParser(format, getValueFactory()); parser.setQueryResultHandler(handler); parser.parseQueryResult(response.getEntity().getContent()); } catch (QueryResultParseException e) { throw new RepositoryException("Malformed query result from server", e); } catch (QueryResultHandlerException e) { if (e instanceof TupleQueryResultHandlerException) { throw (TupleQueryResultHandlerException) e; } else { throw new TupleQueryResultHandlerException(e); } } } finally { EntityUtils.consumeQuietly(response.getEntity()); } }
From source file:org.apache.olingo.server.example.TripPinServiceTest.java
@Test public void testUpdateReference() throws Exception { HttpResponse response = httpGET(baseURL + "/People('ronaldmundy')/Photo/$ref", 200); JsonNode node = getJSONNode(response); assertEquals("/Photos(12)", node.get("@odata.id").asText()); String msg = "{\n" + "\"@odata.id\": \"/Photos(11)\"\n" + "}"; String editUrl = baseURL + "/People('ronaldmundy')/Photo/$ref"; HttpPut putRequest = new HttpPut(editUrl); putRequest.setEntity(new StringEntity(msg, ContentType.APPLICATION_JSON)); putRequest.setHeader("Content-Type", "application/json;odata.metadata=minimal"); response = httpSend(putRequest, 204); EntityUtils.consumeQuietly(response.getEntity()); response = httpGET(baseURL + "/People('ronaldmundy')/Photo/$ref", 200); node = getJSONNode(response);/*w w w. j ava2s . c o m*/ assertEquals("/Photos(11)", node.get("@odata.id").asText()); }
From source file:org.apache.olingo.server.example.TripPinServiceTest.java
@Test public void testAddDelete2ReferenceCollection() throws Exception { // add/*from w w w .j a v a 2 s . c om*/ String msg = "{\n" + "\"@odata.id\": \"/People('russellwhyte')\"\n" + "}"; String editUrl = baseURL + "/People('vincentcalabrese')/Friends/$ref"; HttpPost postRequest = new HttpPost(editUrl); postRequest.setEntity(new StringEntity(msg, ContentType.APPLICATION_JSON)); postRequest.addHeader("Content-Type", "application/json;odata.metadata=minimal"); HttpResponse response = httpSend(postRequest, 204); EntityUtils.consumeQuietly(response.getEntity()); // get response = httpGET(editUrl, 200); JsonNode node = getJSONNode(response); assertEquals("/People('russellwhyte')", ((ArrayNode) node.get("value")).get(2).get("@odata.id").asText()); //delete HttpDelete deleteRequest = new HttpDelete(editUrl + "?$id=" + baseURL + "/People('russellwhyte')"); deleteRequest.addHeader("Content-Type", "application/json;odata.metadata=minimal"); response = httpSend(deleteRequest, 204); EntityUtils.consumeQuietly(response.getEntity()); // get response = httpGET(editUrl, 200); node = getJSONNode(response); assertNull("/People('russellwhyte')", ((ArrayNode) node.get("value")).get(2)); }
From source file:org.openrdf.http.client.SparqlSession.java
/** * Parse the response in a background thread. HTTP connections are dealt with * in the {@link BackgroundGraphResult} or (in the error-case) in this * method.//from w ww.j av a2s .com */ protected BackgroundGraphResult getRDFBackground(HttpUriRequest method, boolean requireContext) throws IOException, RDFHandlerException, RepositoryException, MalformedQueryException, UnauthorizedException, QueryInterruptedException { boolean submitted = false; // Specify which formats we support using Accept headers Set<RDFFormat> rdfFormats = RDFParserRegistry.getInstance().getKeys(); if (rdfFormats.isEmpty()) { throw new RepositoryException("No tuple RDF parsers have been registered"); } // send the tuple query HttpResponse response = sendGraphQueryViaHttp(method, requireContext, rdfFormats); try { // if we get here, HTTP code is 200 String mimeType = getResponseMIMEType(response); try { RDFFormat format = RDFFormat.matchMIMEType(mimeType, rdfFormats); RDFParser parser = Rio.createParser(format, getValueFactory()); parser.setParserConfig(getParserConfig()); parser.setParseErrorListener(new ParseErrorLogger()); Charset charset = null; // SES-1793 : Do not attempt to check for a charset if the format is // defined not to have a charset // This prevents errors caused by people erroneously attaching a // charset to a binary formatted document HttpEntity entity = response.getEntity(); if (format.hasCharset() && entity != null && entity.getContentType() != null) { // TODO copied from SPARQLGraphQuery repository, is this // required? try { charset = ContentType.parse(entity.getContentType().getValue()).getCharset(); } catch (IllegalCharsetNameException e) { // work around for Joseki-3.2 // Content-Type: application/rdf+xml; // charset=application/rdf+xml } if (charset == null) { charset = UTF8; } } if (entity == null) { throw new RepositoryException("Server response was empty."); } String baseURI = method.getURI().toASCIIString(); BackgroundGraphResult gRes = new BackgroundGraphResult(parser, entity.getContent(), charset, baseURI); execute(gRes); submitted = true; return gRes; } catch (UnsupportedQueryResultFormatException e) { throw new RepositoryException("Server responded with an unsupported file format: " + mimeType); } } finally { if (!submitted) { EntityUtils.consumeQuietly(response.getEntity()); } } }
From source file:org.apache.olingo.server.example.TripPinServiceTest.java
@Test public void testDeleteReference() throws Exception { String editUrl = baseURL + "/People('russellwhyte')/Photo/$ref"; HttpResponse response = httpGET(editUrl, 200); EntityUtils.consumeQuietly(response.getEntity()); HttpDelete deleteRequest = new HttpDelete(editUrl); response = httpSend(deleteRequest, 204); EntityUtils.consumeQuietly(response.getEntity()); response = httpGET(editUrl, 204);//w w w . j av a 2s .c o m EntityUtils.consumeQuietly(response.getEntity()); }
From source file:org.apache.olingo.server.example.TripPinServiceTest.java
@Test public void crossJoin() throws Exception { String editUrl = baseURL + "/$crossjoin(People,Airlines)?$filter=" + "People/UserName%20eq%20Airlines/AirlineCode"; HttpResponse response = httpGET(editUrl, 200); EntityUtils.consumeQuietly(response.getEntity()); }