List of usage examples for org.apache.http.client.utils URIBuilder setPath
public URIBuilder setPath(final String path)
From source file:com.arpnetworking.configuration.jackson.JsonNodePaginatedUriSource.java
private JsonNodePaginatedUriSource(final Builder builder) { super(builder); _uri = builder._uri;// w w w. j a v a 2 s . c o m _dataKeys = builder._dataKeys.toArray(new String[builder._dataKeys.size()]); _nextPageKeys = builder._nextPageKeys.toArray(new String[builder._nextPageKeys.size()]); final JsonNodeMergingSource.Builder mergingSourceBuilder = new JsonNodeMergingSource.Builder(); try { final URIBuilder uriBuilder = new URIBuilder(_uri); URI currentUri = uriBuilder.build(); while (currentUri != null) { LOGGER.debug().setMessage("Creating JsonNodeUriSource for page").addData("uri", currentUri).log(); // Create a URI source for the page final JsonNodeUriSource uriSource = new JsonNodeUriSource.Builder().setUri(currentUri).build(); mergingSourceBuilder.addSource(uriSource); // Extract the link for the next page final Optional<JsonNode> nextPageNode = uriSource.getValue(_nextPageKeys); if (nextPageNode.isPresent() && !nextPageNode.get().isNull()) { final String nextPagePath = nextPageNode.get().asText(); final URI nextPageUri = URI .create(nextPagePath.startsWith("/") ? nextPagePath : "/" + nextPagePath); final URIBuilder nextPageUriBuilder = new URIBuilder(nextPageUri); currentUri = uriBuilder.setPath(nextPageUri.getPath()) .setParameters(nextPageUriBuilder.getQueryParams()).build(); } else { currentUri = null; } } } catch (final URISyntaxException e) { throw Throwables.propagate(e); } _mergingSource = mergingSourceBuilder.build(); }
From source file:com.pennassurancesoftware.tutum.client.TutumClient.java
private URI createUri(ApiRequest request) { URIBuilder ub = new URIBuilder(); ub.setScheme(Constants.HTTPS_SCHEME); ub.setHost(apiHost);//from w w w . j ava 2 s . c o m ub.setPath(createPath(request)); for (String paramName : request.getQueryParams().keySet()) { final List<String> values = request.getQueryParams().get(paramName); for (String value : values) { ub.setParameter(paramName, value); } } URI uri = null; try { uri = ub.build(); } catch (URISyntaxException use) { LOG.error(use.getMessage(), use); } return uri; }
From source file:nl.nn.adapterframework.http.HttpSenderBase.java
protected URIBuilder getURI(String url) throws URISyntaxException { URIBuilder uri = new URIBuilder(url); if (uri.getPath() == null) { uri.setPath("/"); }//w w w. j a v a 2 s. co m log.info(getLogPrefix() + "created uri: scheme=[" + uri.getScheme() + "] host=[" + uri.getHost() + "] path=[" + uri.getPath() + "]"); return uri; }
From source file:com.buffalokiwi.api.API.java
/** * Convert a string representing a URI into a URI object. * This combines url with the host, port and scheme from the defined host * in the constructor./*w w w. j a va 2 s. co m*/ * @param uri The URI (path/fragment/querystring) * @return The URI object representing the full address * @throws APIException If there is a problem building the URI */ private URI stringToURI(final String url) throws APIException { try { final URIBuilder b = new URIBuilder(url.replace(" ", "%20")); //..Overwrite the host if necessary if (lockHost && client.getHost().getHost() != null && !client.getHost().getHost().isEmpty() //..Kind of stupid, but I didn't think ahead on this one. && !url.startsWith("http://") && !url.startsWith("https://")) { b.setHost(client.getHost().getHost()).setPort(client.getHost().getPort()) .setScheme(client.getHost().getScheme()); if (client.getHost().getPath() != null && !client.getHost().getPath().isEmpty()) { b.setPath(client.getHost().getPath() + b.getPath()); } } //...done final URI out = b.build(); APILog.debug(LOG, "Query:", out.toString()); return out; } catch (Exception e) { throw new APIException("Could not build url: " + url, e); } }
From source file:ching.icecreaming.action.ResourceDescriptors.java
@Action(value = "resource-descriptors", results = { @Result(name = "success", location = "json.jsp") }) public String execute() throws Exception { int int1 = 401, int0 = 0; String string1 = null;// w w w . j a va 2 s .co m URL url1 = null; URI uri1 = null; HttpGet httpGet1 = null; HttpResponse httpResponse1 = null; HttpEntity httpEntity1 = null; HttpPost httpPost1 = null; HttpHost httpHost1 = null; DefaultHttpClient httpClient1 = null; File file1 = null; InputStream inputStream1 = null; OutputStream outputStream1 = null; BeanComparator beanComparator1 = null; List<Map<String, Object>> list1 = null, list2 = null; Map<String, Object> map1 = null; Object object1 = null; int toIndex = rows * page; int fromIndex = toIndex - rows; GridModel1 jsonObject1 = null; Gson gson = null; Long long1 = -1L; java.util.Date date1 = null; URIBuilder uriBuilder1 = null; Node node1 = null; Element element1 = null; NodeList nodeList1 = null; Document document1 = null; DocumentBuilder documentBuilder1 = null; DocumentBuilderFactory documentBuilderFactory1 = null; org.joda.time.DateTime dateTime1 = null, dateTime2 = null; try { if (StringUtils.isNotEmpty(sid) && StringUtils.isNotEmpty(uid) && StringUtils.isNotEmpty(pid)) { sid = new String(Base64.decodeBase64(sid.getBytes())); uid = new String(Base64.decodeBase64(uid.getBytes())); pid = new String(Base64.decodeBase64(pid.getBytes())); httpClient1 = new DefaultHttpClient(); url1 = new URL(sid); uriBuilder1 = new URIBuilder(sid); uriBuilder1.setParameter("j_username", uid); uriBuilder1.setParameter("j_password", pid); uriBuilder1.setPath(url1.getPath() + "/rest/resources" + urlString); uriBuilder1.setUserInfo(uid, pid); uri1 = uriBuilder1.build(); httpGet1 = new HttpGet(uri1); httpResponse1 = httpClient1.execute(httpGet1); int1 = httpResponse1.getStatusLine().getStatusCode(); if (int1 == HttpStatus.SC_OK) { httpEntity1 = httpResponse1.getEntity(); inputStream1 = httpResponse1.getEntity().getContent(); if (inputStream1 != null) { documentBuilderFactory1 = DocumentBuilderFactory.newInstance(); documentBuilder1 = documentBuilderFactory1.newDocumentBuilder(); document1 = documentBuilder1.parse(inputStream1); document1.getDocumentElement().normalize(); nodeList1 = document1.getElementsByTagName("resourceDescriptor"); int1 = nodeList1.getLength(); list1 = new ArrayList<Map<String, Object>>(); for (int0 = 0; int0 < int1; int0++) { node1 = nodeList1.item(int0); if (node1.getNodeType() == Node.ELEMENT_NODE) { element1 = (Element) node1; map1 = new HashMap<String, Object>(); map1.put("wsType", element1.getAttribute("wsType")); map1.put("uriString", element1.getAttribute("uriString")); string1 = getTagValue("label", element1); map1.put("label1", StringUtils.defaultString(string1)); string1 = getTagValue("description", element1); map1.put("description", StringUtils.defaultString(string1)); string1 = getTagValue("creationDate", element1); long1 = (string1 == null) ? -1L : NumberUtils.toLong(StringUtils.trim(string1), -1L); if (long1 > 0) { if (StringUtils.isNotBlank(timeZone1)) { dateTime1 = new org.joda.time.DateTime(long1); dateTime2 = dateTime1.withZone(DateTimeZone.forID(timeZone1)); date1 = dateTime2.toLocalDateTime().toDate(); } else { date1 = new java.util.Date(long1); } } else { date1 = null; } map1.put("creationDate", date1); map1.put("type1", getText(element1.getAttribute("wsType"))); list1.add(map1); } } } EntityUtils.consume(httpEntity1); } } } catch (UnsupportedEncodingException | URISyntaxException | ParserConfigurationException | SAXException exception1) { exception1.printStackTrace(); return ERROR; } catch (org.apache.http.conn.HttpHostConnectException | java.net.NoRouteToHostException | java.net.MalformedURLException | java.net.UnknownHostException exception1) { exception1.printStackTrace(); return ERROR; } catch (IOException exception1) { httpGet1.abort(); exception1.printStackTrace(); return ERROR; } finally { if (httpClient1 != null) httpClient1.getConnectionManager().shutdown(); if (list1 != null) { records = list1.size(); if (list1.size() > 0) { if (StringUtils.isNotEmpty(sidx)) { if (StringUtils.equals(sord, "desc")) { beanComparator1 = new BeanComparator(sidx, new ReverseComparator(new ComparableComparator())); } else { beanComparator1 = new BeanComparator(sidx); } Collections.sort(list1, beanComparator1); } if (StringUtils.isNotBlank(searchField) && StringUtils.isNotEmpty(searchOper)) { Iterator iterator1 = list1.iterator(); while (iterator1.hasNext()) { map1 = (Map<String, Object>) iterator1.next(); for (Map.Entry<String, Object> entry1 : map1.entrySet()) { if (StringUtils.equals(entry1.getKey(), searchField)) { object1 = entry1.getValue(); if (searchFilter(searchField, searchOper, searchString, object1)) iterator1.remove(); break; } } } records = list1.size(); } if (toIndex > records) toIndex = records; if (fromIndex > toIndex) { fromIndex = toIndex - rows; if (fromIndex < 0) fromIndex = 0; } if (list1.size() > 0 && fromIndex >= 0 && toIndex <= list1.size() && fromIndex <= toIndex) list2 = list1.subList(fromIndex, toIndex); } total = (int) Math.ceil((double) records / (double) rows); if (page > total) page = total; jsonObject1 = new GridModel1(rows, page, sord, sidx, searchField, searchString, searchOper, total, records, id, list2); gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss.SSS").create(); jsonData = gson.toJson(jsonObject1); } IOUtils.closeQuietly(inputStream1); } return SUCCESS; }
From source file:com.cisco.oss.foundation.http.apache.ApacheHttpClient.java
private URI buildUri(HttpRequest request, Joiner joiner) { URI requestUri = request.getUri(); Map<String, Collection<String>> queryParams = request.getQueryParams(); if (queryParams != null && !queryParams.isEmpty()) { URIBuilder uriBuilder = new URIBuilder(); StringBuilder queryStringBuilder = new StringBuilder(); boolean hasQuery = !queryParams.isEmpty(); for (Map.Entry<String, Collection<String>> stringCollectionEntry : queryParams.entrySet()) { String key = stringCollectionEntry.getKey(); Collection<String> queryParamsValueList = stringCollectionEntry.getValue(); if (request.isQueryParamsParseAsMultiValue()) { for (String queryParamsValue : queryParamsValueList) { uriBuilder.addParameter(key, queryParamsValue); queryStringBuilder.append(key).append("=").append(queryParamsValue).append("&"); }//from w ww .j av a 2 s.co m } else { String value = joiner.join(queryParamsValueList); uriBuilder.addParameter(key, value); queryStringBuilder.append(key).append("=").append(value).append("&"); } } uriBuilder.setFragment(requestUri.getFragment()); uriBuilder.setHost(requestUri.getHost()); uriBuilder.setPath(requestUri.getPath()); uriBuilder.setPort(requestUri.getPort()); uriBuilder.setScheme(requestUri.getScheme()); uriBuilder.setUserInfo(requestUri.getUserInfo()); try { if (!autoEncodeUri) { String urlPath = ""; if (requestUri.getRawPath() != null && requestUri.getRawPath().startsWith("/")) { urlPath = requestUri.getRawPath(); } else { urlPath = "/" + requestUri.getRawPath(); } if (hasQuery) { String query = queryStringBuilder.substring(0, queryStringBuilder.length() - 1); requestUri = new URI(requestUri.getScheme() + "://" + requestUri.getHost() + ":" + requestUri.getPort() + urlPath + "?" + query); } else { requestUri = new URI(requestUri.getScheme() + "://" + requestUri.getHost() + ":" + requestUri.getPort() + urlPath); } } else { requestUri = uriBuilder.build(); } } catch (URISyntaxException e) { LOGGER.warn("could not update uri: {}", requestUri); } } return requestUri; }
From source file:fr.gael.dhus.olingo.v1.Processor.java
private URI makeLink(boolean remove_last_segment) throws ODataException { try {// www. j a va2s .co m URIBuilder ub = new URIBuilder(ServiceFactory.EXTERNAL_URL); StringBuilder sb = new StringBuilder(); String prefix = ub.getPath(); String path = getContext().getPathInfo().getRequestUri().getPath(); if (path == null || path.isEmpty() || prefix != null && !prefix.isEmpty() && !path.startsWith(ub.getPath())) { sb.append(prefix); if (path != null) { if (prefix.endsWith("/") && path.startsWith("/")) { sb.deleteCharAt(sb.length() - 1); } if (!prefix.endsWith("/") && !path.startsWith("/")) { sb.append('/'); } } } sb.append(path); if (remove_last_segment) { // Removes the last segment. int lio = sb.lastIndexOf("/"); while (lio != -1 && lio == sb.length() - 1) { sb.deleteCharAt(lio); lio = sb.lastIndexOf("/"); } if (lio != -1) { sb.delete(lio + 1, sb.length()); } // Removes the `$links` segment. lio = sb.lastIndexOf("$links/"); if (lio != -1) { sb.delete(lio, lio + 7); } } else if (!sb.toString().endsWith("/") && !sb.toString().endsWith("\\")) { sb.append("/"); } ub.setPath(sb.toString()); return ub.build(); } catch (NullPointerException | URISyntaxException e) { throw new ODataException(e); } }