下面列出了org.apache.http.client.utils.URIBuilder#setScheme ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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();
}
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());
}
}
private URI createUri(ApiRequest request) {
URIBuilder ub = new URIBuilder();
ub.setScheme(HTTPS_SCHEME);
ub.setHost(apiHost);
ub.setPath(createPath(request));
if (null != request.getPageNo()) {
ub.setParameter(PARAM_PAGE_NO, request.getPageNo().toString());
}
if (RequestMethod.GET == request.getMethod()) {
if (null == request.getPerPage()) {
ub.setParameter(PARAM_PER_PAGE, String.valueOf(DEFAULT_PAGE_SIZE)); // As per DO
// documentation
} else {
ub.setParameter(PARAM_PER_PAGE, request.getPerPage().toString());
}
}
if (null != request.getQueryParams()) {
for (Map.Entry<String, String> entry : request.getQueryParams().entrySet()) {
ub.setParameter(entry.getKey(), entry.getValue());
}
}
URI uri = null;
try {
uri = ub.build();
} catch (URISyntaxException use) {
log.error(use.getMessage(), use);
}
return uri;
}
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;
}
@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;
}
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
URIBuilder builder = new URIBuilder(exchange.getRequestURL());
builder.setScheme("https");
exchange.setStatusCode(StatusCodes.MOVED_PERMANENTLY);
exchange.getResponseHeaders().add(Headers.LOCATION, builder.toString());
exchange.endExchange();
}
/**
* 创建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();
}
@NotNull
public static URIBuilder getUriBuilder(@NotNull final String region) {
final URIBuilder uriBuilder = new URIBuilder();
uriBuilder.setHost(IAAS + "." + region + "." + OCI_HOST);
uriBuilder.setScheme(HTTPS);
return uriBuilder;
}
/**
* 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();
}
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 + "'");
}
}
@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 String getBaseUrl() {
URIBuilder uri = new URIBuilder();
uri.setScheme(schema);
uri.setHost(hostname);
uri.setPort(port);
return RequestHelper.appendPath(uri.toString(), webapp);
}
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() {
final URIBuilder uriBuilder = new URIBuilder();
uriBuilder.setHost(TERRAFORM_HOST);
uriBuilder.setScheme(HTTPS);
return uriBuilder;
}
@NotNull
public static URIBuilder getUriBuilder() {
final URIBuilder uriBuilder = new URIBuilder();
uriBuilder.setHost(GRAPH_HOST);
uriBuilder.setScheme(HTTPS);
return uriBuilder;
}
@NotNull
public static URIBuilder getUriBuilder(String url) {
final URIBuilder uriBuilder = new URIBuilder();
uriBuilder.setHost(url);
uriBuilder.setScheme(HTTPS);
return uriBuilder;
}
@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;
}
/**
* 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;
}
private MySqlJdbcUrl() {
builder = new URIBuilder();
builder.setScheme("mysql");
}