List of usage examples for org.apache.http.client.utils URIBuilder setFragment
public URIBuilder setFragment(final String fragment)
From source file:es.upv.grycap.coreutils.fiber.net.UrlBuilder.java
/** * Creates a new URL relative to the base URL provided in the constructor of this class. The new relative URL * includes the path, query parameters and the internal reference of the {@link UrlBuilder#baseUrl base URL} * provided with this class. An additional fragment, as well as additional query parameters can be optionally * added to the new URL. In addition to the parameters passed to the method as an argument, the supplied * fragment can also include parameters that will be added to the created URL. The created URL is normalized * and unencoded before returning it to the caller. The current implementation has the following limitations: * <ul>//from w w w .j a v a 2s.c o m * <li>Arrays are not supported: <tt>q=foo&q=bar</tt> will produce an error.</li> * <li>Internal references are only supported in the base URL. Any additional reference provided with the * fragment will be silently ignored: the fragment <tt>/rd#ref</tt> will be appended to the base URL as * <tt>/rd</tt>, ignoring the internal reference.</li> * </ul> * @param fragment - optional URL fragment (may include parameters, but not references) that will be added * to the base URL * @param params - optional query parameters that will be added to the base URL * @return A relative URL created from the base URL provided in the constructor of this class and adding the * fragment and parameters passed as arguments to this method. */ public String buildRelativeUrl(final @Nullable String fragment, final @Nullable Map<String, String> params) { String url = null; final Optional<String> fragment2 = ofNullable(trimToNull(fragment)); try { final Optional<URL> fragmentUrl = ofNullable( fragment2.isPresent() ? new URL("http://example.com/" + fragment2.get()) : null); final URIBuilder uriBuilder = new URIBuilder(); // add path uriBuilder.setPath(new StringBuilder(ofNullable(trimToNull(baseUrl.getPath())).orElse("/")) .append(fragmentUrl.isPresent() ? "/" + stripEnd(fragmentUrl.get().getPath(), "/") : "") .toString().replaceAll("[/]{2,}", "/")); // add query parameters if (isNotBlank(baseUrl.getQuery())) { uriBuilder.setParameters(URLEncodedUtils.parse(baseUrl.getQuery(), defaultCharset())); } if (fragmentUrl.isPresent() && isNotBlank(fragmentUrl.get().getQuery())) { URLEncodedUtils.parse(fragmentUrl.get().getQuery(), defaultCharset()).stream().forEach(p -> { uriBuilder.addParameter(p.getName(), p.getValue()); }); } ofNullable(params).orElse(emptyMap()).entrySet().stream().forEach(p -> { uriBuilder.addParameter(p.getKey(), p.getValue()); }); // add internal reference uriBuilder.setFragment(baseUrl.getRef()); // build relative URL url = uriBuilder.build().normalize().toString(); } catch (MalformedURLException | URISyntaxException e) { throw new IllegalStateException( new StringBuilder("Failed to create relative URL from provided parameters: fragment=") .append(fragment2.orElse("null")).append(", params=") .append(params != null ? params.toString() : "null").toString(), e); } return url; }
From source file:com.github.brandtg.pantopod.crawler.CrawlingEventHandler.java
private URI getNextUri(URI url, String href, String chroot) throws Exception { // if (href.contains("..")) { ////from w w w. j a va 2 s. c o m // throw new IllegalArgumentException("Relative URI not allowed: " + href); // } URI hrefUri = URI.create(href.trim().replaceAll(" ", "+")); URIBuilder builder = new URIBuilder(); builder.setScheme(hrefUri.getScheme() == null ? url.getScheme() : hrefUri.getScheme()); builder.setHost(hrefUri.getHost() == null ? url.getHost() : hrefUri.getHost()); builder.setPort(hrefUri.getPort() == -1 ? url.getPort() : hrefUri.getPort()); if (hrefUri.getPath() != null) { StringBuilder path = new StringBuilder(); if (hrefUri.getHost() == null && chroot != null) { path.append(chroot); } // Ensure no two slashes if (hrefUri.getPath() != null && hrefUri.getPath().length() > 0 && hrefUri.getPath().charAt(0) == '/' && path.length() > 0 && path.charAt(path.length() - 1) == '/') { path.setLength(path.length() - 1); } path.append(hrefUri.getPath()); builder.setPath(chroot == null ? "" : chroot + hrefUri.getPath()); } if (hrefUri.getQuery() != null) { builder.setCustomQuery(hrefUri.getQuery()); } if (hrefUri.getFragment() != null) { builder.setFragment(hrefUri.getFragment()); } return builder.build(); }
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("&"); }// www . ja v a 2 s . c om } 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:org.dataconservancy.packaging.tool.impl.DcsBoPackageOntologyServiceImpl.java
/** * Change the current artifact to DataFile. Add a DataItem to contain it in the proper spot in the PackageTree * @param packageDesc the PackageDescription * @param tree the PackageTree/*ww w. j av a 2 s . c om*/ * @param packageArtifact the current artifact * @param contentRoot the root directory for the package content */ protected void makeDataItemFileCombo(PackageDescription packageDesc, PackageTree tree, PackageArtifact packageArtifact, File contentRoot) { String currentArtifactId = packageArtifact.getId(); if (ontology == null) { throw new RuntimeException("Ontology and hierarchical relationship names all have to be specified, " + "before service could be used."); } if (tree == null || currentArtifactId == null) { throw new IllegalArgumentException("Package tree and currentArifactId cannot be null"); } //get current fileNode PackageNode fileNode = tree.getNodesMap().get(currentArtifactId); if (fileNode == null) { throw new IllegalArgumentException("Provided artifact id " + currentArtifactId + " does not identify any" + " package artifact in the package."); } //obtain the containing Collection node as a starting place PackageNode containingCollectionNode = tree.getNodesMap().get(fileNode.getParentNode().getValue().getId()); //unhook fileNode from tree/containing collection node //set appropriate node linkage fileNode.setParentNode(null); fileNode.setChildrenNodes(); containingCollectionNode.getChildrenNodes().remove(fileNode); //remove relevant relationships fileNode.getValue().removeRelationship(DcsBoPackageOntology.IS_METADATA_FOR, containingCollectionNode.getValue().getId()); containingCollectionNode.getValue().removeRelationship(DcsBoPackageOntology.HAS_METADATA, fileNode.getValue().getId()); //remove file node with the old id from nodes map tree.getNodesMap().remove(fileNode.getValue().getId()); //Create a new URIBuilder to help manipulate file uris to be used for new DI and DF ids and ArtifactReferences URIBuilder fileURIBuilder; URI artifactFileURI = packageArtifact.getArtifactRef().getResolvedAbsoluteRefPath(contentRoot).toUri(); fileURIBuilder = new URIBuilder(artifactFileURI); //Set up package artifact for the new DI PackageArtifact diArtifact = new PackageArtifact(); //set new DI's id and artifact ref //TODO: do we want to use a different ID? String fragment = Integer.toString(rand.nextInt(Integer.MAX_VALUE)); diArtifact.setId((fileURIBuilder.setFragment(fragment).toString())); diArtifact.setArtifactRef(packageArtifact.getArtifactRef().getRefString()); diArtifact.getArtifactRef().setFragment(fragment); diArtifact.setIgnored(containingCollectionNode.getValue().isIgnored()); //set package artifact type diArtifact.setType(DcsBoPackageOntology.DATAITEM); //loop through the available properties in the file artifact for (String propertyName : fileNode.getValue().getPropertyNames()) { // if the property is applicable to the DI then copy it to the DI. if (getProperties(DcsBoPackageOntology.DATAITEM).containsKey(propertyName)) { if (isPropertyComplex(propertyName)) { diArtifact.setPropertyValueGroups(propertyName, new HashSet<>(fileNode.getValue().getPropertyValueGroups(propertyName))); } else { diArtifact.setSimplePropertyValues(propertyName, new HashSet<>(fileNode.getValue().getSimplePropertyValues(propertyName))); } } } //set DI artifact's name Iterator<String> nameIter = fileNode.getValue().getSimplePropertyValues(DcsBoPackageOntology.FILE_NAME) .iterator(); if (nameIter.hasNext()) { diArtifact.addSimplePropertyValue(DcsBoPackageOntology.NAME, synthesizedArtifactName + " for " + nameIter.next()); } else { diArtifact.addSimplePropertyValue(DcsBoPackageOntology.NAME, synthesizedArtifactName); } //copy over the fileNode's relationship, ***AFTER*** fileNode's hierarchical relationship has been removed. diArtifact.setRelationships(new ArrayList<>(fileNode.getValue().getRelationships())); //set hierarchical relationships diArtifact.getRelationships().add(new PackageRelationship(DcsBoPackageOntology.IS_MEMBER_OF, true, containingCollectionNode.getValue().getId())); //Add diArtifact to package description packageDesc.getPackageArtifacts().add(diArtifact); //Set up Package node for the new DI, give it the value PackageNode diNode = new PackageNode(diArtifact); //Hook the DI node onto the tree and add it to the Nodes map containingCollectionNode.getChildrenNodes().add(diNode); diNode.setParentNode(containingCollectionNode); diNode.setChildrenNodes(fileNode); //make DI node the parent of the fileNode tree.getNodesMap().put(diArtifact.getId(), diNode); //file node will keep id //Change type to Data File fileNode.getValue().setType(DcsBoPackageOntology.DATAFILE); //clear out fileNode's relationships as they have been moved to the containing DI fileNode.getValue().setRelationships(); //Update the artifact's relationship to make it a member of the Di artifact fileNode.getValue().setRelationships( new PackageRelationship(DcsBoPackageOntology.IS_MEMBER_OF, true, diArtifact.getId())); //hook childNode to the tree as child of the DI node fileNode.setParentNode(diNode); //add new mapping: new id to file node on nodes maps tree.getNodesMap().put(fileNode.getValue().getId(), fileNode); }
From source file:org.opennms.netmgt.collectd.HttpCollector.java
private static URI buildUri(final HttpCollectionSet collectionSet) throws URISyntaxException { HashMap<String, String> substitutions = new HashMap<String, String>(); substitutions.put("ipaddr", InetAddressUtils.str(collectionSet.getAgent().getAddress())); substitutions.put("nodeid", Integer.toString(collectionSet.getAgent().getNodeId())); final URIBuilder ub = new URIBuilder(); ub.setScheme(collectionSet.getUriDef().getUrl().getScheme()); ub.setHost(substituteKeywords(substitutions, collectionSet.getUriDef().getUrl().getHost(), "getHost")); ub.setPort(collectionSet.getPort()); ub.setPath(substituteKeywords(substitutions, collectionSet.getUriDef().getUrl().getPath(), "getURL")); final String query = substituteKeywords(substitutions, collectionSet.getUriDef().getUrl().getQuery(), "getQuery"); final List<NameValuePair> params = URLEncodedUtils.parse(query, Charset.forName("UTF-8")); ub.setParameters(params);/* w w w . j a v a 2 s . co m*/ ub.setFragment( substituteKeywords(substitutions, collectionSet.getUriDef().getUrl().getFragment(), "getFragment")); return ub.build(); }