List of usage examples for org.apache.http.client.utils URIBuilder setHost
public URIBuilder setHost(final String host)
From source file:lh.api.showcase.server.api.lh.ApiRequestFactoryLhAbstract.java
@Override public URI getRequestUri(ApiDataArea area, NameValuePair resourceNameKey, List<NameValuePair> subResourceNameKey, List<NameValuePair> optionKeyValue) throws URISyntaxException { Preconditions.checkNotNull(area);//from ww w . j a v a2 s.c o m Preconditions.checkNotNull(resourceNameKey); Preconditions.checkArgument(StringUtils.isNotEmpty(resourceNameKey.getName())); URIBuilder urib = new URIBuilder(); urib.setScheme(getScheme()); urib.setHost(getHost()); StringBuilder sb = new StringBuilder(); // build path sb.append("/"); sb.append(getVersion()); sb.append("/"); // area sb.append(area.toString().toLowerCase()); sb.append("/"); // resource sb.append(resourceNameKey.getName()); if (StringUtils.isNotEmpty(resourceNameKey.getValue())) { sb.append("/"); sb.append(resourceNameKey.getValue()); } // sub resources if (subResourceNameKey != null) { for (NameValuePair vp : subResourceNameKey) { if (StringUtils.isEmpty(vp.getName())) { continue; } sb.append("/"); sb.append(vp.getName()); if (StringUtils.isNotEmpty(vp.getValue())) { sb.append("/"); sb.append(vp.getValue()); } } } urib.setPath(sb.toString()); // parameters if (optionKeyValue != null && !optionKeyValue.isEmpty()) { urib.setParameters(optionKeyValue); } return urib.build(); }
From source file:org.berlin.crawl.net.RobotsConnector.java
public String connect(final String scheme, final String host) throws Exception { final URIBuilder builder = new URIBuilder(); builder.setScheme(scheme);/*from w ww. jav a 2s . co m*/ builder.setHost(host); builder.setPath("/robots.txt"); return this.connect(builder); }
From source file:net.datacrow.onlinesearch.bol.BolClient.java
/** * Searches.//from www . j a v a 2 s. com * * @param term The search term (required). * @param categoryID The category id and refinements, separated by spaces (optional). */ public String search(String term, String categoryID) throws IOException, URISyntaxException { List<NameValuePair> queryParams = new ArrayList<NameValuePair>(); queryParams.add(new BasicNameValuePair("term", term)); if (categoryID != null) queryParams.add(new BasicNameValuePair("categoryId", categoryID)); queryParams.add(new BasicNameValuePair("nrProducts", "10")); queryParams.add(new BasicNameValuePair("includeProducts", "TRUE")); queryParams.add(new BasicNameValuePair("includeCategories", "TRUE")); queryParams.add(new BasicNameValuePair("includeRefinements", "FALSE")); URIBuilder builder = new URIBuilder(); builder.setScheme("https"); builder.setHost("openapi.bol.com"); builder.setPath("/openapi/services/rest/catalog/v3/searchresults/"); builder.setQuery(URLEncodedUtils.format(queryParams, "UTF-8")); HttpGet httpGet = new HttpGet(builder.build()); HttpOAuthHelper au = new HttpOAuthHelper("application/xml"); au.handleRequest(httpGet, accessKeyId, secretAccessKey, null, queryParams); HttpResponse httpResponse = httpClient.execute(httpGet); String xml = getXML(httpResponse); httpClient.getConnectionManager().shutdown(); return xml; }
From source file:org.berlin.crawl.bean.BotSeed.java
public URIBuilder toBuilder() { final URIBuilder b = new URIBuilder(); b.setPath(path);//from ww w .jav a2 s.com b.setHost(host); b.setScheme(scheme); return b; }
From source file:org.cloudcrawler.domain.crawler.robotstxt.RobotsTxtService.java
/** * This method is used to evaluate if the provided uri is * allowed to be crawled against the robots.txt of the website. * * @param uri/* w w w . j av a 2 s .co m*/ * @return * @throws Exception */ public boolean isAllowedUri(URI uri) throws Exception { URIBuilder uriBuilder = new URIBuilder(); uriBuilder.setScheme(uri.getScheme()); uriBuilder.setHost(uri.getHost()); uriBuilder.setUserInfo(uri.getUserInfo()); uriBuilder.setPath("/robots.txt"); URI robotsTxtUri = uriBuilder.build(); BaseRobotRules rules = (BaseRobotRules) cache.get(robotsTxtUri.toString()); if (rules == null) { HttpResponse response = httpService.get(robotsTxtUri); try { // HACK! DANGER! Some sites will redirect the request to the top-level domain // page, without returning a 404. So look for a response which has a redirect, // and the fetched content is not plain text, and assume it's one of these... // which is the same as not having a robotstxt.txt file. String contentType = response.getEntity().getContentType().getValue(); boolean isPlainText = (contentType != null) && (contentType.startsWith("text/plain")); if (response.getStatusLine().getStatusCode() == 404 || !isPlainText) { rules = robotsTxtParser.failedFetch(HttpStatus.SC_GONE); } else { StringWriter writer = new StringWriter(); IOUtils.copy(response.getEntity().getContent(), writer); rules = robotsTxtParser.parseContent(uri.toString(), writer.toString().getBytes(), response.getEntity().getContentType().getValue(), httpService.getUserAgent()); } } catch (Exception e) { EntityUtils.consume(response.getEntity()); throw e; } EntityUtils.consume(response.getEntity()); cache.set(robotsTxtUri.toString(), 60 * 60 * 24, rules); } return rules.isAllowed(uri.toString()); }
From source file:com.github.jjYBdx4IL.utils.fma.FMAClient.java
public FMATrack getTrack(int trackId) throws IOException { if (!config.isInitialized()) { throw new RuntimeException("no api_key configured"); }/*www . j a v a 2 s . c o m*/ URIBuilder b = new URIBuilder(); b.setScheme("https"); b.setHost("freemusicarchive.org"); b.setPath("/api/get/tracks.json"); b.addParameter("api_key", config.fmaApiKey); b.addParameter("track_id", Integer.toString(trackId)); LOG.debug("retrieving " + b.toString()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); HttpGet httpGet = new HttpGet(b.toString()); HttpResponse response = httpclient.execute(httpGet); if (response.getStatusLine().getStatusCode() != 200) { throw new IOException( "url returned status code " + response.getStatusLine().getStatusCode() + ": " + b.toString()); } try (InputStream is = response.getEntity().getContent()) { IOUtils.copy(is, baos); } byte[] data = baos.toByteArray(); Gson gson = new Gson(); FMATrackResult _result = gson.fromJson(new String(data), FMATrackResult.class); return _result.dataset.get(0); }
From source file:com.jkoolcloud.tnt4j.streams.inputs.HttpStreamTest.java
private URI makeURI() throws URISyntaxException { URIBuilder uriBuilder = new URIBuilder("http://localhost"); // NON-NLS uriBuilder.setHost("localhost"); // NON-NLS uriBuilder.setPort(TEST_PORT);/*from w w w.ja v a 2 s .c om*/ URI url = uriBuilder.build(); return url; }
From source file:com.blackducksoftware.tools.scmconnector.integrations.mercurial.MercurialConnector.java
/** * Constructs a valid HTTP url if a user *and* password is provided. * * @throws Exception/*from w w w .j a va 2s . c o m*/ */ private void buildURL() throws Exception { if (StringUtils.isNotEmpty(user) && StringUtils.isNotEmpty(password)) { URL url = new URL(repositoryURL); String protocol = url.getProtocol(); int port = url.getPort(); String host = url.getHost(); String path = url.getPath(); URIBuilder builder = new URIBuilder(); builder.setScheme(protocol); builder.setHost(host); builder.setPort(port); builder.setPath(path); builder.setUserInfo(user, password); repositoryURL = builder.toString(); // log.info("Using path: " + repositoryURL); // Reveals password } }
From source file:org.apache.ambari.view.weather.CityResourceProvider.java
private Map<String, Object> getWeatherProperty(String city, String units) throws IOException { URIBuilder uriBuilder = new URIBuilder(); uriBuilder.setScheme("http"); uriBuilder.setHost("api.openweathermap.org"); uriBuilder.setPath("/data/2.5/weather"); uriBuilder.setParameter("q", city); uriBuilder.setParameter("units", units); String url = uriBuilder.toString(); InputStream in = readFrom(url); try {/*from ww w.ja va2 s. co m*/ Type mapType = new TypeToken<Map<String, Object>>() { }.getType(); Map<String, Object> results = new Gson().fromJson(IOUtils.toString(in, "UTF-8"), mapType); ArrayList list = (ArrayList) results.get("weather"); if (list != null) { Map weather = (Map) list.get(0); results.put("weather", weather); results.put("icon_src", "http://openweathermap.org/img/w/" + weather.get("icon")); } return results; } finally { in.close(); } }
From source file:com.fredhopper.core.connector.index.upload.impl.RestPublishingStrategy.java
protected URI createUri(final String scheme, final String host, final Integer port, final String servername, final String path, final List<NameValuePair> params) { Preconditions.checkArgument(StringUtils.isNotBlank(scheme)); Preconditions.checkArgument(StringUtils.isNotBlank(host)); Preconditions.checkArgument(port != null); Preconditions.checkArgument(StringUtils.isNotBlank(servername)); Preconditions.checkArgument(StringUtils.isNotBlank(path)); final URIBuilder uriBuilder = new URIBuilder(); uriBuilder.setScheme(scheme);/*from w w w .j a va 2 s. c o m*/ uriBuilder.setHost(host); uriBuilder.setPort(port.intValue()); uriBuilder.setPath("/".concat(servername).concat(path)); if (CollectionUtils.isNotEmpty(params)) { uriBuilder.setParameters(params); } try { return uriBuilder.build(); } catch (final URISyntaxException ex) { throw new IllegalArgumentException(ex); } }