List of usage examples for org.apache.http.client.utils URIBuilder setScheme
public URIBuilder setScheme(final String scheme)
From source file:org.nectarframework.base.service.nanohttp.NanoHttpService.java
private Response serveProxy(ProxyResolution proxyResolution, String uri, Method method, Map<String, String> headers, Map<String, List<String>> parms, String queryParameterString, Map<String, String> files) { String remoteTarget = uri.substring(proxyResolution.getPath().length() + 1); if (!remoteTarget.startsWith("/")) { remoteTarget = "/" + remoteTarget; }//from w w w . j a v a 2 s. c o m CloseableHttpClient httpclient = HttpClients.createDefault(); URIBuilder remoteUri = new URIBuilder(); remoteUri.setScheme("http"); remoteUri.setHost(proxyResolution.getHost()); remoteUri.setPort(proxyResolution.getPort()); remoteUri.setPath(proxyResolution.getRequestPath() + remoteTarget); remoteUri.setCharset(Charset.defaultCharset()); for (String k : parms.keySet()) { remoteUri.addParameter(k, parms.get(k).get(0)); } HttpGet httpget; try { httpget = new HttpGet(remoteUri.build()); } catch (URISyntaxException e) { Log.warn(e); return newFixedLengthResponse(Status.INTERNAL_ERROR, NanoHttpService.MIME_PLAINTEXT, "SERVER INTERNAL ERROR: URISyntaxException" + e.getMessage()); } CloseableHttpResponse response = null; Response resp = null; try { response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); if (entity != null) { InputStream is = entity.getContent(); long isl = entity.getContentLength(); resp = new Response(Status.lookup(response.getStatusLine().getStatusCode()), null, is, isl); } else { resp = new Response(Status.lookup(response.getStatusLine().getStatusCode()), null, null, 0); } Header[] remoteHeaders = response.getAllHeaders(); for (Header h : remoteHeaders) { resp.addHeader(h.getName(), h.getValue()); } resp.setProxyResponse(response); } catch (IOException e) { Log.warn(e); return newFixedLengthResponse(Status.INTERNAL_ERROR, NanoHttpService.MIME_PLAINTEXT, "SERVER INTERNAL ERROR: IOException" + e.getMessage()); } return resp; }
From source file:sonata.kernel.vimadaptor.wrapper.openstack.javastackclient.JavaStackCore.java
public synchronized HttpResponse getStackTemplate(String stackName, String stackId) throws IOException, URISyntaxException { HttpResponseFactory factory = new DefaultHttpResponseFactory(); HttpClient httpclient = HttpClientBuilder.create().build(); HttpGet getStackTemplate = null;//from ww w .jav a2 s . c o m HttpResponse response = null; if (isAuthenticated) { URIBuilder builder = new URIBuilder(); String path = String.format("/%s/%s/stacks/%s/%s/template", Constants.HEAT_VERSION.toString(), this.tenant_id, stackName, stackId); builder.setScheme("http").setHost(endpoint).setPort(Integer.parseInt(Constants.HEAT_PORT.toString())) .setPath(path); URI uri = builder.build(); getStackTemplate = new HttpGet(uri); getStackTemplate.addHeader(Constants.AUTHTOKEN_HEADER.toString(), this.token_id); Logger.debug("Request: " + getStackTemplate.toString()); response = httpclient.execute(getStackTemplate); int status_code = response.getStatusLine().getStatusCode(); Logger.debug("Response: " + response.toString()); return (status_code == 200) ? response : factory.newHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, status_code, "Get Template Failed with Status: " + status_code), null); } else { throw new IOException("You must Authenticate before issuing this request, please re-authenticate. "); } }
From source file:sonata.kernel.vimadaptor.wrapper.openstack.javastackclient.JavaStackCore.java
public synchronized HttpResponse showResourceData(String stackName, String stackId, String resourceName) throws IOException, URISyntaxException { HttpResponseFactory factory = new DefaultHttpResponseFactory(); HttpClient httpclient = HttpClientBuilder.create().build(); HttpGet showResourceData = null;//from w w w.j ava 2 s .com HttpResponse response = null; if (isAuthenticated) { URIBuilder builder = new URIBuilder(); String path = String.format("/%s/%s/stacks/%s/%s/resources/%s", Constants.HEAT_VERSION.toString(), this.tenant_id, stackName, stackId, resourceName); builder.setScheme("http").setHost(endpoint).setPort(Integer.parseInt(Constants.HEAT_PORT.toString())) .setPath(path); URI uri = builder.build(); showResourceData = new HttpGet(uri); showResourceData.addHeader(Constants.AUTHTOKEN_HEADER.toString(), this.token_id); response = httpclient.execute(showResourceData); int status_code = response.getStatusLine().getStatusCode(); return (status_code == 200) ? response : factory.newHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, status_code, "List Failed with Status: " + status_code), null); } else { throw new IOException("You must Authenticate before issuing this request, please re-authenticate. "); } }
From source file:org.testmp.datastore.client.DataStoreClient.java
@SuppressWarnings("unused") private URIBuilder getCustomURIBuilder() { URIBuilder builder = new URIBuilder(); builder.setScheme(dataStoreURL.getScheme()); builder.setHost(dataStoreURL.getHost()); builder.setPort(dataStoreURL.getPort()); builder.setPath(dataStoreURL.getPath()); return builder; }
From source file:org.dspace.identifier.doi.DataCiteConnector.java
private DataCiteResponse sendGetRequest(String doi, String path) throws DOIIdentifierException { URIBuilder uribuilder = new URIBuilder(); uribuilder.setScheme("https").setHost(HOST).setPath(path + doi.substring(DOI.SCHEME.length())); HttpGet httpget = null;/*from w w w . j a v a2 s .c o m*/ try { httpget = new HttpGet(uribuilder.build()); } catch (URISyntaxException e) { log.error("The URL we constructed to check a DOI " + "produced a URISyntaxException. Please check the configuration parameters!"); log.error("The URL was {}.", "https://" + HOST + DOI_PATH + "/" + doi.substring(DOI.SCHEME.length())); throw new RuntimeException("The URL we constructed to check a DOI " + "produced a URISyntaxException. Please check the configuration parameters!", e); } return sendHttpRequest(httpget, doi); }
From source file:org.dspace.identifier.doi.DataCiteConnector.java
private DataCiteResponse sendMetadataDeleteRequest(String doi) throws DOIIdentifierException { // delete mds/metadata/<doi> URIBuilder uribuilder = new URIBuilder(); uribuilder.setScheme("https").setHost(HOST).setPath(METADATA_PATH + doi.substring(DOI.SCHEME.length())); HttpDelete httpdelete = null;/* w w w . j av a 2 s. co m*/ try { httpdelete = new HttpDelete(uribuilder.build()); } catch (URISyntaxException e) { log.error("The URL we constructed to check a DOI " + "produced a URISyntaxException. Please check the configuration parameters!"); log.error("The URL was {}.", "https://" + HOST + DOI_PATH + "/" + doi.substring(DOI.SCHEME.length())); throw new RuntimeException("The URL we constructed to check a DOI " + "produced a URISyntaxException. Please check the configuration parameters!", e); } return sendHttpRequest(httpdelete, doi); }
From source file:org.dspace.identifier.doi.DataCiteConnector.java
private DataCiteResponse sendMetadataPostRequest(String doi, String metadata) throws DOIIdentifierException { // post mds/metadata/ // body must contain metadata in DataCite-XML. URIBuilder uribuilder = new URIBuilder(); uribuilder.setScheme("https").setHost(HOST).setPath(METADATA_PATH); HttpPost httppost = null;//from w w w .jav a2s .c o m try { httppost = new HttpPost(uribuilder.build()); } catch (URISyntaxException e) { log.error("The URL we constructed to check a DOI " + "produced a URISyntaxException. Please check the configuration parameters!"); log.error("The URL was {}.", "https://" + HOST + DOI_PATH + "/" + doi.substring(DOI.SCHEME.length())); throw new RuntimeException("The URL we constructed to check a DOI " + "produced a URISyntaxException. Please check the configuration parameters!", e); } // assemble request content: HttpEntity reqEntity = null; try { ContentType contentType = ContentType.create("application/xml", "UTF-8"); reqEntity = new StringEntity(metadata, contentType); httppost.setEntity(reqEntity); return sendHttpRequest(httppost, doi); } finally { // release ressources try { EntityUtils.consume(reqEntity); } catch (IOException ioe) { log.info("Caught an IOException while releasing an HTTPEntity:" + ioe.getMessage()); } } }
From source file:org.dspace.identifier.doi.DataCiteConnector.java
private DataCiteResponse sendDOIPostRequest(String doi, String url) throws DOIIdentifierException { // post mds/doi/ // body must contaion "doi=<doi>\nurl=<url>}n" URIBuilder uribuilder = new URIBuilder(); uribuilder.setScheme("https").setHost(HOST).setPath(DOI_PATH); HttpPost httppost = null;/*from w w w . j av a2 s . com*/ try { httppost = new HttpPost(uribuilder.build()); } catch (URISyntaxException e) { log.error("The URL we constructed to check a DOI " + "produced a URISyntaxException. Please check the configuration parameters!"); log.error("The URL was {}.", "https://" + HOST + DOI_PATH + "/" + doi.substring(DOI.SCHEME.length())); throw new RuntimeException("The URL we constructed to check a DOI " + "produced a URISyntaxException. Please check the configuration parameters!", e); } // assemble request content: HttpEntity reqEntity = null; try { String req = "doi=" + doi.substring(DOI.SCHEME.length()) + "\n" + "url=" + url + "\n"; ContentType contentType = ContentType.create("text/plain", "UTF-8"); reqEntity = new StringEntity(req, contentType); httppost.setEntity(reqEntity); return sendHttpRequest(httppost, doi); } finally { // release ressources try { EntityUtils.consume(reqEntity); } catch (IOException ioe) { log.info("Caught an IOException while releasing a HTTPEntity:" + ioe.getMessage()); } } }
From source file:org.apache.ambari.server.controller.ganglia.GangliaPropertyProviderTest.java
@Test public void testPopulateManyResources() throws Exception { TestStreamProvider streamProvider = new TestStreamProvider("temporal_ganglia_data_1.txt"); TestGangliaHostProvider hostProvider = new TestGangliaHostProvider(); GangliaPropertyProvider propertyProvider = new GangliaHostPropertyProvider( PropertyHelper.getGangliaPropertyIds(Resource.Type.Host), streamProvider, configuration, hostProvider, CLUSTER_NAME_PROPERTY_ID, HOST_NAME_PROPERTY_ID); Set<Resource> resources = new HashSet<Resource>(); // host//w ww .ja v a 2s . c om Resource resource = new ResourceImpl(Resource.Type.Host); resource.setProperty(HOST_NAME_PROPERTY_ID, "domU-12-31-39-0E-34-E1.compute-1.internal"); resources.add(resource); resource = new ResourceImpl(Resource.Type.Host); resource.setProperty(HOST_NAME_PROPERTY_ID, "domU-12-31-39-0E-34-E2.compute-1.internal"); resources.add(resource); resource = new ResourceImpl(Resource.Type.Host); resource.setProperty(HOST_NAME_PROPERTY_ID, "domU-12-31-39-0E-34-E3.compute-1.internal"); resources.add(resource); // only ask for one property Map<String, TemporalInfo> temporalInfoMap = new HashMap<String, TemporalInfo>(); temporalInfoMap.put(PROPERTY_ID, new TemporalInfoImpl(10L, 20L, 1L)); Request request = PropertyHelper.getReadRequest(Collections.singleton(PROPERTY_ID), temporalInfoMap); Assert.assertEquals(3, propertyProvider.populateResources(resources, request, null).size()); URIBuilder uriBuilder = new URIBuilder(); uriBuilder.setScheme((configuration.isGangliaSSL() ? "https" : "http")); uriBuilder.setHost("domU-12-31-39-0E-34-E1.compute-1.internal"); uriBuilder.setPath("/cgi-bin/rrd.py"); uriBuilder.setParameter("c", "HDPJobTracker,HDPHBaseMaster,HDPKafka,HDPResourceManager,HDPFlumeServer,HDPSlaves,HDPHistoryServer,HDPJournalNode,HDPTaskTracker,HDPHBaseRegionServer,HDPNameNode"); uriBuilder.setParameter("h", "domU-12-31-39-0E-34-E3.compute-1.internal,domU-12-31-39-0E-34-E1.compute-1.internal,domU-12-31-39-0E-34-E2.compute-1.internal"); uriBuilder.setParameter("m", "jvm.metrics.gcCount"); uriBuilder.setParameter("s", "10"); uriBuilder.setParameter("e", "20"); uriBuilder.setParameter("r", "1"); String expected = uriBuilder.toString(); Assert.assertEquals(expected, streamProvider.getLastSpec()); for (Resource res : resources) { Assert.assertEquals(2, PropertyHelper.getProperties(res).size()); Assert.assertNotNull(res.getPropertyValue(PROPERTY_ID)); } }