List of usage examples for org.apache.http.client.methods HttpHead HttpHead
public HttpHead(final String uri)
From source file:com.hdsfed.cometapi.HCPClient.java
public String HttpGetHCPHeader(URL url, String header, Boolean neverfail) throws Exception { if (!getExecute()) return ""; HttpResponse httpResponse;//w w w. j a v a 2s . c o m HttpClient mHttpClient = HCPUtils.initHttpClient(); HttpHead httpRequest = new HttpHead(url.toString()); httpRequest.setHeader(HTTP_AUTH_HEADER, getAuthToken()); httpResponse = mHttpClient.execute(httpRequest); setStatusCode(HttpHCPCatchError(httpRequest, httpResponse, neverfail)); if (header == "") return ""; if (!Headers.containsKey(header)) return ""; return Headers.get(header); }
From source file:es.upm.oeg.tools.quality.ldsniffer.eval.Evaluation.java
public void calculateMetrics() { Model model = ModelFactory.createDefaultModel(); //TODO try to limit the size of the file being loaded try {// w w w . j av a2 s .c o m model.read(url); } catch (RiotException e) { model = RDFDataMgr.loadModel(url + ".rdf"); } catch (Exception e) { throw new BadRequestException( String.format("Unable to read the content from the uri - %s \n(%s)", url, e.getMessage()), e); } totalTriples = QueryUtils.getCountAsC(model, TOTAL_TRIPLES); iriSubjects = QueryUtils.getIriList(model, DISTINCT_IRI_SUBJECTS); iriPredicates = QueryUtils.getIriList(model, DISTINCT_IRI_PREDICATES); iriObjects = QueryUtils.getIriList(model, DISTINCT_IRI_OBJECTS); iriSet.addAll(iriSubjects); iriSet.addAll(iriPredicates); iriSet.addAll(iriObjects); final ExecutorService executor = Executors.newFixedThreadPool(5); iriSet.forEach(iri -> { executor.submit(() -> { HttpResponse cachedResponse = Executor.getCachedResponse(iri); if (cachedResponse == null) { Date date = new Date(); CloseableHttpClient httpclient = HttpClients.createDefault(); HttpHead head = new HttpHead(iri); String method = "HEAD"; try (CloseableHttpResponse response = httpclient.execute(head);) { StatusLine statusLine = response.getStatusLine(); int statusCode = statusLine.getStatusCode(); if (statusCode == HttpStatus.SC_METHOD_NOT_ALLOWED || statusCode == HttpStatus.SC_INTERNAL_SERVER_ERROR || statusCode == HttpStatus.SC_NOT_IMPLEMENTED) { HttpGet get = new HttpGet(iri); method = "GET"; try (CloseableHttpResponse getResponse = httpclient.execute(get);) { statusLine = getResponse.getStatusLine(); statusCode = statusLine.getStatusCode(); } } responseMap.put(iri, new HttpResponse(iri, method, statusCode, statusLine.getReasonPhrase(), date, false)); if (statusCode >= 200 && statusCode < 300) { derefIriCount.getAndIncrement(); } Executor.putCachedResponse(iri, new HttpResponse(iri, method, statusCode, statusLine.getReasonPhrase(), date, true)); } catch (ConnectException e) { logger.error("Connection timed out ...", e); responseMap.put(iri, new HttpResponse(iri, method, -1, e.getMessage(), date, false)); } catch (IOException e) { logger.error("IO error occurred ..", e); responseMap.put(iri, new HttpResponse(iri, method, -1, e.getMessage(), date, false)); } } else { responseMap.put(iri, cachedResponse); } }); }); executor.shutdown(); try { executor.awaitTermination(LDSnifferApp.getEvaluationTimeout(), TimeUnit.MINUTES); } catch (InterruptedException e) { throw new ServerError("Interrupted ..."); } iriSubjects.forEach(subject -> incrementDerefCount(subject, derefIriSubjectCount)); iriPredicates.forEach(predicate -> incrementDerefCount(predicate, derefIriPredicateCount)); iriObjects.forEach(object -> incrementDerefCount(object, derefIriObjectCount)); }
From source file:org.apache.shindig.gadgets.http.BasicHttpFetcher.java
public HttpResponse fetch(org.apache.shindig.gadgets.http.HttpRequest request) throws GadgetException { HttpUriRequest httpMethod = null;//from w ww .j av a 2 s. c o m Preconditions.checkNotNull(request); final String methodType = request.getMethod(); final org.apache.http.HttpResponse response; final long started = System.currentTimeMillis(); // Break the request Uri to its components: Uri uri = request.getUri(); if (StringUtils.isEmpty(uri.getAuthority())) { throw new GadgetException(GadgetException.Code.INVALID_USER_DATA, "Missing domain name for request: " + uri, HttpServletResponse.SC_BAD_REQUEST); } if (StringUtils.isEmpty(uri.getScheme())) { throw new GadgetException(GadgetException.Code.INVALID_USER_DATA, "Missing schema for request: " + uri, HttpServletResponse.SC_BAD_REQUEST); } String[] hostparts = StringUtils.splitPreserveAllTokens(uri.getAuthority(), ':'); int port = -1; // default port if (hostparts.length > 2) { throw new GadgetException(GadgetException.Code.INVALID_USER_DATA, "Bad host name in request: " + uri.getAuthority(), HttpServletResponse.SC_BAD_REQUEST); } if (hostparts.length == 2) { try { port = Integer.parseInt(hostparts[1]); } catch (NumberFormatException e) { throw new GadgetException(GadgetException.Code.INVALID_USER_DATA, "Bad port number in request: " + uri.getAuthority(), HttpServletResponse.SC_BAD_REQUEST); } } String requestUri = uri.getPath(); // Treat path as / if set as null. if (uri.getPath() == null) { requestUri = "/"; } if (uri.getQuery() != null) { requestUri += '?' + uri.getQuery(); } // Get the http host to connect to. HttpHost host = new HttpHost(hostparts[0], port, uri.getScheme()); try { if ("POST".equals(methodType) || "PUT".equals(methodType)) { HttpEntityEnclosingRequestBase enclosingMethod = ("POST".equals(methodType)) ? new HttpPost(requestUri) : new HttpPut(requestUri); if (request.getPostBodyLength() > 0) { enclosingMethod .setEntity(new InputStreamEntity(request.getPostBody(), request.getPostBodyLength())); } httpMethod = enclosingMethod; } else if ("GET".equals(methodType)) { httpMethod = new HttpGet(requestUri); } else if ("HEAD".equals(methodType)) { httpMethod = new HttpHead(requestUri); } else if ("DELETE".equals(methodType)) { httpMethod = new HttpDelete(requestUri); } for (Map.Entry<String, List<String>> entry : request.getHeaders().entrySet()) { httpMethod.addHeader(entry.getKey(), StringUtils.join(entry.getValue(), ',')); } // Disable following redirects. if (!request.getFollowRedirects()) { httpMethod.getParams().setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, false); } // HttpClient doesn't handle all cases when breaking url (specifically '_' in domain) // So lets pass it the url parsed: response = FETCHER.execute(host, httpMethod); if (response == null) { throw new IOException("Unknown problem with request"); } long now = System.currentTimeMillis(); if (now - started > slowResponseWarning) { slowResponseWarning(request, started, now); } return makeResponse(response); } catch (Exception e) { long now = System.currentTimeMillis(); // Find timeout exceptions, respond accordingly if (TIMEOUT_EXCEPTIONS.contains(e.getClass())) { LOG.info("Timeout for " + request.getUri() + " Exception: " + e.getClass().getName() + " - " + e.getMessage() + " - " + (now - started) + "ms"); return HttpResponse.timeout(); } LOG.log(Level.INFO, "Got Exception fetching " + request.getUri() + " - " + (now - started) + "ms", e); // Separate shindig error from external error throw new GadgetException(GadgetException.Code.INTERNAL_SERVER_ERROR, e, HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } finally { // cleanup any outstanding resources.. if (httpMethod != null) try { httpMethod.abort(); } catch (UnsupportedOperationException e) { // ignore } } }
From source file:org.wymiwyg.wrhapi.test.BaseTests.java
/** * @throws Exception//w w w . jav a2 s .co m */ @Test public void testStatusCode() throws Exception { final int statusCode = 302; WebServer webServer = createServer().startNewWebServer(new Handler() { public void handle(Request request, Response response) throws HandlerException { log.info("handling testStatusCode"); response.setResponseStatus(ResponseStatus.getInstanceByCode(statusCode)); response.setHeader(HeaderName.SERVER, "Ad-Hoc testing server"); response.setHeader(HeaderName.LOCATION, "http://example.org/"); } }, serverBinding); try { URI serverURL = new URI("http://" + serverBinding.getInetAddress().getHostAddress() + ":" + serverBinding.getPort() + "/"); HttpHead method = new HttpHead(serverURL); DefaultHttpClient client = new DefaultHttpClient(); client.setHttpRequestRetryHandler(null); client.setRedirectHandler(nullRedirectHandler); HttpResponse response = client.execute(method); // for the handler to be invoked, something of the response has to // be asked log.info(response); log.info(response.getStatusLine()); assertEquals(statusCode, response.getStatusLine().getStatusCode()); } catch (MalformedURLException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } finally { webServer.stop(); } }
From source file:com.olleh.ucloudbiz.ucloudstorage.FilesClientExt.java
/** * <p>/*from www. j a v a 2 s .c o m*/ * FilesObjectMetaData manifest . * </p> * * @param containerName * container * @param objName * * * @return FilesObjectMetaDataExt * * @see <A HREF="../../../../com/rackspacecloud/client/cloudfiles/FilesClient.html#getObjectMetaData(java.lang.String, java.lang.String)"><CODE>FilesClient.getObjectMetaData(String, String)</CODE></A>, * <A HREF="../../../../com/rackspacecloud/client/cloudfiles/FilesObjectMetaData.html"><CODE>FilesObjectMetaData</CODE></A>, * <A HREF="../../../../com/kt/client/ucloudstorage/FilesObjectMetaDataExt.html"><CODE>FilesObjectMetaDataExt</CODE></A> */ public FilesObjectMetaDataExt getObjectMetaDataExt(String containerName, String objName) throws IOException, FilesNotFoundException, HttpException, FilesAuthorizationException, FilesInvalidNameException { FilesObjectMetaDataExt metaData; if (this.isLoggedin()) { if (isValidContainerName(containerName) && isValidObjectName(objName)) { HttpHead method = new HttpHead( storageURL + "/" + sanitizeForURI(containerName) + "/" + sanitizeForURI(objName)); try { method.getParams().setIntParameter("http.socket.timeout", connectionTimeOut); method.setHeader(FilesConstants.X_AUTH_TOKEN, authToken); FilesResponseExt response = new FilesResponseExt(client.execute(method)); if (response.getStatusCode() == HttpStatus.SC_UNAUTHORIZED) { method.abort(); login(); method.getParams().setIntParameter("http.socket.timeout", connectionTimeOut); method.setHeader(FilesConstants.X_AUTH_TOKEN, authToken); response = new FilesResponseExt(client.execute(method)); } if (response.getStatusCode() == HttpStatus.SC_NO_CONTENT || response.getStatusCode() == HttpStatus.SC_OK) { logger.debug("Object metadata retreived : " + objName); String mimeType = response.getContentType(); String lastModified = response.getLastModified(); String eTag = response.getETag(); String contentLength = response.getContentLength(); String contentType = response.getContentType(); String objectManifest = response.getObjectManifest(); metaData = new FilesObjectMetaDataExt(mimeType, contentLength, eTag, lastModified, contentType, objectManifest); Header[] headers = response.getResponseHeaders(); HashMap<String, String> headerMap = new HashMap<String, String>(); for (Header h : headers) { if (h.getName().startsWith(FilesConstants.X_OBJECT_META)) { headerMap.put(h.getName().substring(FilesConstants.X_OBJECT_META.length()), unencodeURI(h.getValue())); } } if (headerMap.size() > 0) metaData.setMetaData(headerMap); return metaData; } else if (response.getStatusCode() == HttpStatus.SC_NOT_FOUND) { throw new FilesNotFoundException( "Container: " + containerName + " did not have object " + objName, response.getResponseHeaders(), response.getStatusLine()); } else { throw new FilesException("Unexpected Return Code from Server", response.getResponseHeaders(), response.getStatusLine()); } } finally { method.abort(); } } else { if (!isValidObjectName(objName)) { throw new FilesInvalidNameException(objName); } else { throw new FilesInvalidNameException(containerName); } } } else { throw new FilesAuthorizationException("You must be logged in", null, null); } }
From source file:de.jetwick.snacktory.HtmlFetcher.java
protected CloseableHttpResponse createUrlConnection(String urlAsStr, int timeout, boolean includeSomeGooseOptions, boolean isHead) throws MalformedURLException, IOException { CloseableHttpClient httpclient = HttpClients.createDefault(); HttpRequestBase request = null;//from w w w. j a va 2s .co m if (isHead) { request = new HttpHead(urlAsStr); } else { request = new HttpGet(urlAsStr); } RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(timeout) .setConnectTimeout(timeout).setSocketTimeout(timeout).setCookieSpec(CookieSpecs.STANDARD).build(); request.setHeader("User-Agent", userAgent); request.setHeader("Accept", accept); if (includeSomeGooseOptions) { request.setHeader("Accept-Language", language); request.setHeader("content-charset", charset); request.setHeader("Referer", referrer); // avoid the cache for testing purposes only? request.setHeader("Cache-Control", cacheControl); } // suggest respond to be gzipped or deflated (which is just another compression) // http://stackoverflow.com/q/3932117 request.setHeader("Accept-Encoding", "gzip, deflate"); request.setConfig(requestConfig); return httpclient.execute(request); }
From source file:com.arangodb.http.HttpManager.java
/** * Executes the request//from www.j av a 2 s . c o m * * @param requestEntity * the request * @return the response of the request * @throws ArangoException */ private HttpResponseEntity executeInternal(String baseUrl, HttpRequestEntity requestEntity) throws ArangoException, SocketException { String url = buildUrl(baseUrl, requestEntity); if (logger.isDebugEnabled()) { if (requestEntity.type == RequestType.POST || requestEntity.type == RequestType.PUT || requestEntity.type == RequestType.PATCH) { logger.debug("[REQ]http-{}: url={}, headers={}, body={}", new Object[] { requestEntity.type, url, requestEntity.headers, requestEntity.bodyText }); } else { logger.debug("[REQ]http-{}: url={}, headers={}", new Object[] { requestEntity.type, url, requestEntity.headers }); } } HttpRequestBase request = null; switch (requestEntity.type) { case GET: request = new HttpGet(url); break; case POST: HttpPost post = new HttpPost(url); configureBodyParams(requestEntity, post); request = post; break; case PUT: HttpPut put = new HttpPut(url); configureBodyParams(requestEntity, put); request = put; break; case PATCH: HttpPatch patch = new HttpPatch(url); configureBodyParams(requestEntity, patch); request = patch; break; case HEAD: request = new HttpHead(url); break; case DELETE: request = new HttpDelete(url); break; } // common-header String userAgent = "Mozilla/5.0 (compatible; ArangoDB-JavaDriver/1.1; +http://mt.orz.at/)"; request.setHeader("User-Agent", userAgent); // optinal-headers if (requestEntity.headers != null) { for (Entry<String, Object> keyValue : requestEntity.headers.entrySet()) { request.setHeader(keyValue.getKey(), keyValue.getValue().toString()); } } // Basic Auth Credentials credentials = null; if (requestEntity.username != null && requestEntity.password != null) { credentials = new UsernamePasswordCredentials(requestEntity.username, requestEntity.password); } else if (configure.getUser() != null && configure.getPassword() != null) { credentials = new UsernamePasswordCredentials(configure.getUser(), configure.getPassword()); } if (credentials != null) { BasicScheme basicScheme = new BasicScheme(); try { request.addHeader(basicScheme.authenticate(credentials, request, null)); } catch (AuthenticationException e) { throw new ArangoException(e); } } if (this.getHttpMode().equals(HttpMode.ASYNC)) { request.addHeader("x-arango-async", "store"); } else if (this.getHttpMode().equals(HttpMode.FIREANDFORGET)) { request.addHeader("x-arango-async", "true"); } // CURL/httpie Logger if (configure.isEnableCURLLogger()) { CURLLogger.log(url, requestEntity, userAgent, credentials); } HttpResponse response = null; if (preDefinedResponse != null) { return preDefinedResponse; } try { response = client.execute(request); if (response == null) { return null; } HttpResponseEntity responseEntity = new HttpResponseEntity(); // http status StatusLine status = response.getStatusLine(); responseEntity.statusCode = status.getStatusCode(); responseEntity.statusPhrase = status.getReasonPhrase(); logger.debug("[RES]http-{}: statusCode={}", requestEntity.type, responseEntity.statusCode); // ?? // // TODO etag??? Header etagHeader = response.getLastHeader("etag"); if (etagHeader != null) { responseEntity.etag = Long.parseLong(etagHeader.getValue().replace("\"", "")); } // Map??? responseEntity.headers = new TreeMap<String, String>(); for (Header header : response.getAllHeaders()) { responseEntity.headers.put(header.getName(), header.getValue()); } // ??? HttpEntity entity = response.getEntity(); if (entity != null) { Header contentType = entity.getContentType(); if (contentType != null) { responseEntity.contentType = contentType.getValue(); if (responseEntity.isDumpResponse()) { responseEntity.stream = entity.getContent(); logger.debug("[RES]http-{}: stream, {}", requestEntity.type, contentType.getValue()); } } // Close stream in this method. if (responseEntity.stream == null) { responseEntity.text = IOUtils.toString(entity.getContent()); logger.debug("[RES]http-{}: text={}", requestEntity.type, responseEntity.text); } } if (this.getHttpMode().equals(HttpMode.ASYNC)) { Map<String, String> map = responseEntity.getHeaders(); this.addJob(map.get("X-Arango-Async-Id"), this.getCurrentObject()); } else if (this.getHttpMode().equals(HttpMode.FIREANDFORGET)) { return null; } return responseEntity; } catch (SocketException ex) { throw ex; } catch (ClientProtocolException e) { throw new ArangoException(e); } catch (IOException e) { throw new ArangoException(e); } }
From source file:org.janusgraph.diskstorage.es.ElasticSearchIndexTest.java
private boolean indexExists(String name) throws IOException { final CloseableHttpResponse response = httpClient.execute(host, new HttpHead(name)); final boolean exists = response.getStatusLine().getStatusCode() == 200; IOUtils.closeQuietly(response);/*from ww w . j a v a 2 s. com*/ return exists; }