use of org.apache.http.client.methods.HttpHead in project camel by apache.
the class CMProducer method doStart.
@Override
protected void doStart() throws Exception {
// log at debug level for singletons, for prototype scoped log at trace
// level to not spam logs
log.debug("Starting CMProducer");
final CMConfiguration configuration = getConfiguration();
if (configuration.isTestConnectionOnStartup()) {
try {
log.debug("Checking connection - {}", getEndpoint().getCMUrl());
HttpClientBuilder.create().build().execute(new HttpHead(getEndpoint().getCMUrl()));
log.debug("Connection to {}: OK", getEndpoint().getCMUrl());
} catch (final Exception e) {
throw new HostUnavailableException(String.format("Connection to %s: NOT AVAILABLE", getEndpoint().getCMUrl()), e);
}
}
// keep starting
super.doStart();
log.debug("CMProducer started");
}
use of org.apache.http.client.methods.HttpHead in project spark by perwendel.
the class SparkTestUtil method getHttpRequest.
private HttpUriRequest getHttpRequest(String requestMethod, String path, String body, boolean secureConnection, String acceptType, Map<String, String> reqHeaders) {
try {
String protocol = secureConnection ? "https" : "http";
String uri = protocol + "://localhost:" + port + path;
if (requestMethod.equals("GET")) {
HttpGet httpGet = new HttpGet(uri);
httpGet.setHeader("Accept", acceptType);
addHeaders(reqHeaders, httpGet);
return httpGet;
}
if (requestMethod.equals("POST")) {
HttpPost httpPost = new HttpPost(uri);
httpPost.setHeader("Accept", acceptType);
addHeaders(reqHeaders, httpPost);
httpPost.setEntity(new StringEntity(body));
return httpPost;
}
if (requestMethod.equals("PATCH")) {
HttpPatch httpPatch = new HttpPatch(uri);
httpPatch.setHeader("Accept", acceptType);
addHeaders(reqHeaders, httpPatch);
httpPatch.setEntity(new StringEntity(body));
return httpPatch;
}
if (requestMethod.equals("DELETE")) {
HttpDelete httpDelete = new HttpDelete(uri);
addHeaders(reqHeaders, httpDelete);
httpDelete.setHeader("Accept", acceptType);
return httpDelete;
}
if (requestMethod.equals("PUT")) {
HttpPut httpPut = new HttpPut(uri);
httpPut.setHeader("Accept", acceptType);
addHeaders(reqHeaders, httpPut);
httpPut.setEntity(new StringEntity(body));
return httpPut;
}
if (requestMethod.equals("HEAD")) {
HttpHead httpHead = new HttpHead(uri);
addHeaders(reqHeaders, httpHead);
return httpHead;
}
if (requestMethod.equals("TRACE")) {
HttpTrace httpTrace = new HttpTrace(uri);
addHeaders(reqHeaders, httpTrace);
return httpTrace;
}
if (requestMethod.equals("OPTIONS")) {
HttpOptions httpOptions = new HttpOptions(uri);
addHeaders(reqHeaders, httpOptions);
return httpOptions;
}
if (requestMethod.equals("LOCK")) {
HttpLock httpLock = new HttpLock(uri);
addHeaders(reqHeaders, httpLock);
return httpLock;
}
throw new IllegalArgumentException("Unknown method " + requestMethod);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
use of org.apache.http.client.methods.HttpHead in project lucene-solr by apache.
the class CacheHeaderTestBase method getSelectMethod.
protected HttpRequestBase getSelectMethod(String method, String... params) throws URISyntaxException {
HttpSolrClient client = (HttpSolrClient) getSolrClient();
HttpRequestBase m = null;
ArrayList<BasicNameValuePair> qparams = new ArrayList<>();
if (params.length == 0) {
qparams.add(new BasicNameValuePair("q", "solr"));
qparams.add(new BasicNameValuePair("qt", "standard"));
}
for (int i = 0; i < params.length / 2; i++) {
qparams.add(new BasicNameValuePair(params[i * 2], params[i * 2 + 1]));
}
URI uri = URI.create(client.getBaseURL() + "/select?" + URLEncodedUtils.format(qparams, StandardCharsets.UTF_8));
if ("GET".equals(method)) {
m = new HttpGet(uri);
} else if ("HEAD".equals(method)) {
m = new HttpHead(uri);
} else if ("POST".equals(method)) {
m = new HttpPost(uri);
}
return m;
}
use of org.apache.http.client.methods.HttpHead in project lucene-solr by apache.
the class HttpSolrCall method remoteQuery.
private void remoteQuery(String coreUrl, HttpServletResponse resp) throws IOException {
HttpRequestBase method = null;
HttpEntity httpEntity = null;
try {
String urlstr = coreUrl + queryParams.toQueryString();
boolean isPostOrPutRequest = "POST".equals(req.getMethod()) || "PUT".equals(req.getMethod());
if ("GET".equals(req.getMethod())) {
method = new HttpGet(urlstr);
} else if ("HEAD".equals(req.getMethod())) {
method = new HttpHead(urlstr);
} else if (isPostOrPutRequest) {
HttpEntityEnclosingRequestBase entityRequest = "POST".equals(req.getMethod()) ? new HttpPost(urlstr) : new HttpPut(urlstr);
// Prevent close of container streams
InputStream in = new CloseShieldInputStream(req.getInputStream());
HttpEntity entity = new InputStreamEntity(in, req.getContentLength());
entityRequest.setEntity(entity);
method = entityRequest;
} else if ("DELETE".equals(req.getMethod())) {
method = new HttpDelete(urlstr);
} else {
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Unexpected method type: " + req.getMethod());
}
for (Enumeration<String> e = req.getHeaderNames(); e.hasMoreElements(); ) {
String headerName = e.nextElement();
if (!"host".equalsIgnoreCase(headerName) && !"authorization".equalsIgnoreCase(headerName) && !"accept".equalsIgnoreCase(headerName)) {
method.addHeader(headerName, req.getHeader(headerName));
}
}
// These headers not supported for HttpEntityEnclosingRequests
if (method instanceof HttpEntityEnclosingRequest) {
method.removeHeaders(TRANSFER_ENCODING_HEADER);
method.removeHeaders(CONTENT_LENGTH_HEADER);
}
final HttpResponse response = solrDispatchFilter.httpClient.execute(method, HttpClientUtil.createNewHttpClientRequestContext());
int httpStatus = response.getStatusLine().getStatusCode();
httpEntity = response.getEntity();
resp.setStatus(httpStatus);
for (HeaderIterator responseHeaders = response.headerIterator(); responseHeaders.hasNext(); ) {
Header header = responseHeaders.nextHeader();
// encoding issues with Tomcat
if (header != null && !header.getName().equalsIgnoreCase(TRANSFER_ENCODING_HEADER) && !header.getName().equalsIgnoreCase(CONNECTION_HEADER)) {
resp.addHeader(header.getName(), header.getValue());
}
}
if (httpEntity != null) {
if (httpEntity.getContentEncoding() != null)
resp.setCharacterEncoding(httpEntity.getContentEncoding().getValue());
if (httpEntity.getContentType() != null)
resp.setContentType(httpEntity.getContentType().getValue());
InputStream is = httpEntity.getContent();
OutputStream os = resp.getOutputStream();
IOUtils.copyLarge(is, os);
}
} catch (IOException e) {
sendError(new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Error trying to proxy request for url: " + coreUrl, e));
} finally {
Utils.consumeFully(httpEntity);
}
}
use of org.apache.http.client.methods.HttpHead in project ddf by codice.
the class HttpSolrClientFactory method createSolrCore.
private static void createSolrCore(String url, String coreName, String configFileName, HttpClient httpClient) throws IOException, SolrServerException {
HttpSolrClient client;
if (httpClient != null) {
client = new HttpSolrClient(url, httpClient);
} else {
client = new HttpSolrClient(url);
}
HttpResponse ping = client.getHttpClient().execute(new HttpHead(url));
if (ping != null && ping.getStatusLine().getStatusCode() == 200) {
ConfigurationFileProxy configProxy = new ConfigurationFileProxy(ConfigurationStore.getInstance());
configProxy.writeSolrConfiguration(coreName);
if (!solrCoreExists(client, coreName)) {
LOGGER.debug("Creating Solr core {}", coreName);
String configFile = StringUtils.defaultIfBlank(configFileName, DEFAULT_SOLRCONFIG_XML);
String solrDir;
if (System.getProperty("solr.data.dir") != null) {
solrDir = System.getProperty("solr.data.dir");
} else {
solrDir = Paths.get(System.getProperty("karaf.home"), "data", "solr").toString();
}
String instanceDir = Paths.get(solrDir, coreName).toString();
String dataDir = Paths.get(instanceDir, "data").toString();
CoreAdminRequest.createCore(coreName, instanceDir, client, configFile, DEFAULT_SCHEMA_XML, dataDir, dataDir);
} else {
LOGGER.debug("Solr core ({}) already exists - reloading it", coreName);
CoreAdminRequest.reloadCore(coreName, client);
}
} else {
LOGGER.debug("Unable to ping Solr core {}", coreName);
throw new SolrServerException("Unable to ping Solr core");
}
}
Aggregations