下面列出了org.apache.http.client.utils.URIBuilder#setPort ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* verifies user provided URI.
*
* @param uri - UriString
* @return URI
*/
protected URI parseURI(String uri)
throws OzoneClientException {
if ((uri == null) || uri.isEmpty()) {
throw new OzoneClientException(
"Ozone URI is needed to execute this command.");
}
URIBuilder uriBuilder = new URIBuilder(stringToUri(uri));
if (uriBuilder.getPort() == 0) {
uriBuilder.setPort(DEFAULT_OZONE_PORT);
}
try {
return uriBuilder.build();
} catch (URISyntaxException e) {
throw new OzoneClientException("Invalid URI: " + ozoneURI, e);
}
}
public HttpSinkProvider() {
Configuration config;
try {
config = conf.getMetricsConf();
} catch (Exception e) {
throw new ExceptionInInitializerError("Unable to read configuration for sink.");
}
String protocol = config.get("timeline.metrics.service.external.http.sink.protocol", "http");
String host = config.get("timeline.metrics.service.external.http.sink.host", "localhost");
String port = config.get("timeline.metrics.service.external.http.sink.port", "6189");
if (protocol.contains("https")) {
loadTruststore(
config.getTrimmed("timeline.metrics.service.external.http.sink.truststore.path"),
config.getTrimmed("timeline.metrics.service.external.http.sink.truststore.type"),
config.getTrimmed("timeline.metrics.service.external.http.sink.truststore.password")
);
}
URIBuilder uriBuilder = new URIBuilder();
uriBuilder.setScheme(protocol);
uriBuilder.setHost(host);
uriBuilder.setPort(Integer.parseInt(port));
connectUrl = uriBuilder.toString();
}
/**
* Get the configured target URI as specified by its component parts.
* <p>
* <b>NOTE</b>: The target URI is assembled from following components:
* {@link SeleniumSettings#TARGET_SCHEME scheme}, {@link SeleniumSettings#TARGET_CREDS credentials},
* {@link SeleniumSettings#TARGET_HOST host}, {@link SeleniumSettings#TARGET_PORT port}, and
* {@link SeleniumSettings#TARGET_PATH base path}
*
* @return assembled target URI
*/
public URI getTargetUri() {
if (targetUri == null) {
URIBuilder builder = new URIBuilder().setPath(getString(SeleniumSettings.TARGET_PATH.key()) + "/")
.setScheme(getString(SeleniumSettings.TARGET_SCHEME.key()))
.setHost(getString(SeleniumSettings.TARGET_HOST.key()));
String creds = getString(SeleniumSettings.TARGET_CREDS.key());
if (creds != null) {
builder.setUserInfo(creds);
}
String port = getString(SeleniumSettings.TARGET_PORT.key());
if (port != null) {
builder.setPort(Integer.parseInt(port));
}
try {
targetUri = builder.build().normalize();
} catch (URISyntaxException eaten) { //NOSONAR
LOGGER.error("Specified target URI '{}' could not be parsed: {}", builder, eaten.getMessage());
}
}
return targetUri;
}
public URIBuilder transform(URI initial) {
URIBuilder builder = new URIBuilder(initial);
Entry mapping = mappings.get(initial.getHost());
if (mapping == null) {
throw new InternalServerError("No DNS client override for " + initial.getHost());
}
try {
URL target = new URL(mapping.getValue());
builder.setScheme(target.getProtocol());
builder.setHost(target.getHost());
if (target.getPort() != -1) {
builder.setPort(target.getPort());
}
return builder;
} catch (MalformedURLException e) {
log.warn("Skipping DNS overwrite entry {} due to invalid value [{}]: {}", mapping.getName(), mapping.getValue(), e.getMessage());
throw new ConfigurationException("Invalid DNS overwrite entry in homeserver client: " + mapping.getName(), e.getMessage());
}
}
/**
* Remove selected fields from a URI producing a new URI
*
* @param uri the uri to convert
* @param excludes the parts to exclude
* @return The new URI instance
*/
static URI uriExclude(final URI uri, URIPart... excludes) {
URIBuilder urib = new URIBuilder();
EnumSet<URIPart> set = EnumSet.of(excludes[0], excludes);
for (URIPart part : URIPart.values()) {
if (set.contains(part))
continue;
switch (part) {
case SCHEME:
urib.setScheme(uri.getScheme());
break;
case USERINFO:
urib.setUserInfo(uri.getRawUserInfo());
break;
case HOST:
urib.setHost(uri.getHost());
break;
case PORT:
urib.setPort(uri.getPort());
break;
case PATH:
urib.setPath(uri.getPath());
break;
case QUERY:
urib.setCustomQuery(uri.getQuery());
break;
case FRAGMENT:
urib.setFragment(uri.getFragment());
break;
}
}
try {
return urib.build();
} catch (URISyntaxException e) {
throw new IllegalArgumentException(e.getMessage());
}
}
protected URIBuilder buildURI() throws TextParseException {
final String host = serviceHost();
final int port = servicePort();
URIBuilder builder = new URIBuilder();
builder.setScheme(serviceScheme());
if (useSrvDns()) {
List<LookupResult> results = dnsSrvResolver.resolve(host);
if (results != null && !results.isEmpty()) {
LookupResult result = results.get(0);
builder.setHost(result.host());
builder.setPort(result.port());
// Consul sends the hostname back in its own namespace. Although the A record is included in the
// "ADDITIONAL SECTION", Spotify SRV lookup doesn't translate, so we need to do the lookup manually.
if (result.host().endsWith(".consul.")) {
Record[] newResults = new Lookup(result.host(), Type.A, DClass.IN).run();
if (newResults != null && newResults.length > 0) {
builder.setHost(newResults[0].rdataToString());
} else {
throw new IllegalArgumentException("Unable to resolve service host " + host + " -> " + result.host() + " -> ???");
}
}
} else {
throw new IllegalArgumentException("Unable to resolve service host: " + host);
}
} else {
builder.setHost(host);
builder.setPort(port);
}
return builder;
}
private static String format(URIBuilder builder, SchemeMeta schemeMeta) throws URISyntaxException {
if (schemeMeta.ssl) {
builder.addParameter("sslEnabled", "true");
}
if (schemeMeta.protocol != null) {
builder.addParameter("protocol", schemeMeta.protocol);
}
if (builder.getPort() == -1) {
builder.setPort(schemeMeta.defaultPort);
}
return builder.setScheme("rest").build().toString();
}
/**
* 创建URI
*
* @return
*/
public URI buildURIByConfig() throws URISyntaxException {
URIBuilder builder = new URIBuilder();
builder.setHost(config.getHost());
builder.setPort(config.getPort());
builder.setPath(config.getPath());
builder.setScheme(config.getProtocol());
builder.setCharset(Charset.forName(config.getCharset()));
return builder.build();
}
/**
* 创建URI
*
* @return
*/
public URI buildURIByConfig() throws URISyntaxException {
URIBuilder builder = new URIBuilder();
builder.setHost(config.getHost());
builder.setPort(config.getPort());
builder.setPath(config.getPath());
builder.setScheme(config.getProtocol());
builder.setCharset(Charset.forName(config.getCharset()));
return builder.build();
}
/**
* Given a request UUID, construct a URIBuilder for the common parts
* of any Ingest Service request
*
* @param requestId the UUID with which we want to label this request
* @return a URI builder we can use to finish build the URI
*/
private URIBuilder makeBaseURI(UUID requestId)
{
//We can't make a request with no id
if (requestId == null)
{
LOGGER.error("RequestId is null!");
throw new IllegalArgumentException();
}
//construct the builder object
URIBuilder builder = new URIBuilder();
//set the scheme
builder.setScheme(scheme);
//set the host name
builder.setHost(host);
//set the port name
builder.setPort(port);
//set the request id
builder.setParameter(REQUEST_ID, requestId.toString());
return builder;
}
public static String buildBaseUrl(String baseScheme, String baseHost, int basePort, String basePath) {
URIBuilder builder = new URIBuilder();
builder.setScheme(baseScheme);
builder.setHost(baseHost);
if (!DEFAULT_PORTS.containsKey(baseScheme.toLowerCase()) || DEFAULT_PORTS.get(baseScheme) != basePort) {
builder.setPort(basePort);
}
builder.setPath(basePath);
return builder.toString();
}
@Override
public URI buildUri(String baseUri, String endpoint, Map<String, String> queryParams) throws URISyntaxException {
URI base = super.buildUri(baseUri, endpoint, queryParams);
URIBuilder builder = new URIBuilder(base);
builder.setScheme(protocol);
builder.setPort(port);
return builder.build();
}
public void start() throws Exception {
Objects.nonNull(config);
assert(config.getScheme().startsWith("http"));
URIBuilder uriBuilder = new URIBuilder();
uriBuilder.setScheme(config.getScheme());
uriBuilder.setHost(config.getHosts().get(0));
uriBuilder.setPort(config.getPort().intValue());
baseURI = uriBuilder.build();
client = HttpClients.createDefault();
}
@NotNull
public static URIBuilder getUriBuilder(NutanixCommonInputs nutanixCommonInputs) {
final URIBuilder uriBuilder = new URIBuilder();
uriBuilder.setHost(nutanixCommonInputs.getHostname());
uriBuilder.setPort(Integer.parseInt(nutanixCommonInputs.getPort()));
uriBuilder.setScheme(HTTPS);
return uriBuilder;
}
@NotNull
private static URIBuilder getUriBuilder(@NotNull String protocol, @NotNull String dcaHost, @NotNull String dcaPort) {
final URIBuilder uriBuilder = new URIBuilder();
uriBuilder.setHost(dcaHost);
uriBuilder.setPort(Integer.valueOf(dcaPort));
uriBuilder.setScheme(protocol);
return uriBuilder;
}
public String getBaseUrl() {
URIBuilder uri = new URIBuilder();
uri.setScheme(schema);
uri.setHost(hostname);
uri.setPort(port);
return RequestHelper.appendPath(uri.toString(), webapp);
}
protected URI replacePath(String absPath) {
URIBuilder ub = new URIBuilder();
ub.setScheme(uri.getScheme());
ub.setUserInfo(uri.getUserInfo());
ub.setHost(uri.getHost());
ub.setPort(uri.getPort());
ub.setPath(absPath);
try {
return ub.build();
} catch (URISyntaxException e) {
throw new RuntimeException("Error building URI from: '" + uri + "' and '" + absPath + "'");
}
}
/**
* Get the URL defined by the specified {@link PageUrl} annotation.
* <p>
* <b>NOTES</b>: <ul>
* <li>If the {@code pageUrl} argument is {@code null} or the {@code value} element of the specified
* {@link PageUrl} annotation is unspecified, this method returns {@code null}.
* <li>If {@code scheme} of the specified {@code pageUrl} argument is unspecified or set to {@code http/https},
* the specified {@code targetUri} is overlaid by the elements of the {@link PageUrl} annotation to
* produce the fully-qualified <b>HTTP</b> target page URL.<ul>
* <li>If the {@code value} element specifies an absolute path, this path is returned as-is.</li>
* <li>If the {@code value} element specifies a relative path, this is appended to the path specified by
* {@code targetUri} to resolve the page URL.</li>
* <li>If the {@code scheme} element is specified, its value overrides the scheme of {@code targetUri}.
* If the value of the {@code scheme} element is empty, the scheme of {@code targetUri} is set to
* {@code null}.</li>
* <li>If the {@code userInfo} element is specified, its value overrides the userInfo of {@code targetUrl}.
* If the value of the {@code userInfo} element is empty, the userInfo of {@code targetUri} is set to
* {@code null}.</li>
* <li>If the {@code host} element is specified, its value overrides the host of {@code targetUrl}. If the
* value of the {@code host} element is empty, the host of {@code targetUri} is set to {@code null}.
* </li>
* <li>If the {@code port} element is specified, its value overrides the port of {@code targetUri}. If the
* value of the {@code port} element is empty, the port of {@code targetUri} is set to <b>-1</b>.</li>
* </ul></li>
* <li>For <b>HTTP</b> URLs that require query parameters, these parameters must be included in the
* {@code value} element of the specified {@link PageUrl} annotation. The {@code params} element of the
* annotation is only used for pattern-based landing page verification.</li>
* <li>If {@code scheme} of the specified {@code pageUrl} is set to {@code file}, the value of the
* {@code targetUri} argument is ignored. The only element of the {@link PageUrl} annotation that
* is used to produce the fully-qualified <b>FILE</b> target page URL is {@code value}. The value of the
* {@code value} element specifies the relative path of a file within your project's resources, which is
* resolved via {@link ClassLoader#getResource}.</li>
* </ul>
*
* @param pageUrl page URL annotation
* @param targetUri target URI
* @return defined page URL as a string (may be 'null')
*/
@SuppressWarnings({"squid:S3776", "squid:MethodCyclomaticComplexity"})
public static String getPageUrl(final PageUrl pageUrl, final URI targetUri) {
if (pageUrl == null || PLACEHOLDER.equals(pageUrl.value())) {
return null;
}
String result = null;
String scheme = pageUrl.scheme();
String path = pageUrl.value();
if ("file".equals(scheme)) {
result = Thread.currentThread().getContextClassLoader().getResource(path).toString();
} else {
String userInfo = pageUrl.userInfo();
String host = pageUrl.host();
String port = pageUrl.port();
URIBuilder builder = new URIBuilder(targetUri);
if (!path.isEmpty()) {
URI pathUri = URI.create(path);
if (pathUri.isAbsolute()) {
return pathUri.toString();
} else {
builder.setPath(URI.create(LOOPBACK + builder.getPath() + "/").resolve("./" + path).getPath());
}
}
if (!PLACEHOLDER.equals(scheme)) {
builder.setScheme(scheme.isEmpty() ? null : scheme);
}
if (!PLACEHOLDER.equals(userInfo)) {
builder.setUserInfo(userInfo.isEmpty() ? null : userInfo);
}
if (!PLACEHOLDER.equals(host)) {
builder.setHost(host.isEmpty() ? null : host);
}
if (!PLACEHOLDER.equals(port)) {
builder.setPort(port.isEmpty() ? -1 : Integer.parseInt(port));
}
result = builder.toString();
}
return result;
}