下面列出了java.net.URI#getHost ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Adds status field to a representation of a site configuration.
* @param siteConfigJson The JSONObject representing a single site configuration.
* @param user The user making the request.
* @param resource The original request passed to the decorator.
*/
private void addStatus(HttpServletRequest req, JSONObject siteConfigJson, UserInfo user, URI resource) throws JSONException {
String id = siteConfigJson.optString(ProtocolConstants.KEY_ID);
if (id == null) {
return;
}
SiteInfo siteConfiguration = SiteInfo.getSite(user, id);
if (siteConfiguration == null)
return;
IHostedSite site = HostingActivator.getDefault().getHostingService().get(siteConfiguration, user);
JSONObject hostingStatus = new JSONObject();
if (site != null) {
try {
hostingStatus.put(SiteConfigurationConstants.KEY_HOSTING_STATUS_STATUS, "started"); //$NON-NLS-1$
// Site generates a root URL, need to add contextPath
URI uri = new URI(site.getUrl());
URI newURI = new URI(uri.getScheme(), null, uri.getHost(), uri.getPort(), req.getContextPath(), null, null);
hostingStatus.put(SiteConfigurationConstants.KEY_HOSTING_STATUS_URL, newURI.toString());
} catch (URISyntaxException e) {
LogHelper.log(e);
}
} else {
hostingStatus.put(SiteConfigurationConstants.KEY_HOSTING_STATUS_STATUS, "stopped"); //$NON-NLS-1$
}
siteConfigJson.put(SiteConfigurationConstants.KEY_HOSTING_STATUS, hostingStatus);
}
protected URI getManagementUrl(ServiceInstance instance) {
String managementPath = instance.getMetadata().get(KEY_MANAGEMENT_PATH);
if (isEmpty(managementPath)) {
managementPath = managementContextPath;
}
URI serviceUrl = getServiceUrl(instance);
String managementServerAddress = instance.getMetadata().get(KEY_MANAGEMENT_ADDRESS);
if (isEmpty(managementServerAddress)) {
managementServerAddress = serviceUrl.getHost();
}
String managementPort = instance.getMetadata().get(KEY_MANAGEMENT_PORT);
if (isEmpty(managementPort)) {
managementPort = String.valueOf(serviceUrl.getPort());
}
return UriComponentsBuilder.fromUri(serviceUrl)
.host(managementServerAddress)
.port(managementPort)
.path("/")
.path(managementPath)
.build()
.toUri();
}
@VisibleForTesting
static InetSocketAddress parseEntry(String type, String fn, String line) {
try {
URI uri = new URI("dummy", line, null, null, null);
int port = uri.getPort() == -1 ? 0 : uri.getPort();
InetSocketAddress addr = new InetSocketAddress(uri.getHost(), port);
if (addr.isUnresolved()) {
LOG.warn(String.format("Failed to resolve address `%s` in `%s`. " +
"Ignoring in the %s list.", line, fn, type));
return null;
}
return addr;
} catch (URISyntaxException e) {
LOG.warn(String.format("Failed to parse `%s` in `%s`. " + "Ignoring in " +
"the %s list.", line, fn, type));
}
return null;
}
@Override
public void initialize(URI uri, Configuration conf) throws IOException {
super.initialize(uri, conf);
setConf(conf);
String host = uri.getHost();
if (host == null) {
throw new IOException("Incomplete HDFS URI, no host: "+ uri);
}
homeDirPrefix = conf.get(
DFSConfigKeys.DFS_USER_HOME_DIR_PREFIX_KEY,
DFSConfigKeys.DFS_USER_HOME_DIR_PREFIX_DEFAULT);
this.dfs = new DFSClient(uri, conf, statistics);
this.uri = URI.create(uri.getScheme()+"://"+uri.getAuthority());
this.workingDir = getHomeDirectory();
}
/**
* Identify the host of an HTTP request. This method uses the URI of the request if possible, otherwise it attempts to find the host
* in the request headers.
*
* @param httpRequest HTTP request to parse the host from
* @return the host the request is connecting to, or null if no host can be found
*/
public static String getHostFromRequest(HttpRequest httpRequest) {
// try to use the URI from the request first, if the URI starts with http:// or https://. checking for http/https avoids confusing
// java's URI class when the request is for a malformed URL like '//some-resource'.
String host = null;
if (startsWithHttpOrHttps(httpRequest.getUri())) {
try {
URI uri = new URI(httpRequest.getUri());
host = uri.getHost();
} catch (URISyntaxException e) {
}
}
// if there was no host in the URI, attempt to grab the host from the Host header
if (host == null || host.isEmpty()) {
host = parseHostHeader(httpRequest, false);
}
return host;
}
/**
* Returns a {@link CloudStorageAccount} object that represents the development storage credentials, using the
* specified proxy URI. Secondary endpoints are enabled by default.
*
* @param proxyUri
* A <code>java.net.URI</code> object that represents the proxy endpoint to use. Specifying
* <code>null</code> will use the default <code>http://127.0.0.1</code>.
*
* @return A {@link CloudStorageAccount} object for the development storage credentials.
*
* @throws URISyntaxException
* If the resource URI is invalid.
*/
public static CloudStorageAccount getDevelopmentStorageAccount(final URI proxyUri) throws URISyntaxException {
String scheme;
String host;
if (proxyUri == null) {
scheme = "http";
host = "127.0.0.1";
}
else {
scheme = proxyUri.getScheme();
host = proxyUri.getHost();
}
StorageCredentials credentials = new StorageCredentialsAccountAndKey(DEVSTORE_ACCOUNT_NAME,
DEVSTORE_ACCOUNT_KEY);
URI blobPrimaryEndpoint = new URI(String.format(DEVELOPMENT_STORAGE_PRIMARY_ENDPOINT_FORMAT, scheme, host,
"10000", DEVSTORE_ACCOUNT_NAME));
URI queuePrimaryEndpoint = new URI(String.format(DEVELOPMENT_STORAGE_PRIMARY_ENDPOINT_FORMAT, scheme, host,
"10001", DEVSTORE_ACCOUNT_NAME));
URI tablePrimaryEndpoint = new URI(String.format(DEVELOPMENT_STORAGE_PRIMARY_ENDPOINT_FORMAT, scheme, host,
"10002", DEVSTORE_ACCOUNT_NAME));
URI blobSecondaryEndpoint = new URI(String.format(DEVELOPMENT_STORAGE_SECONDARY_ENDPOINT_FORMAT, scheme, host,
"10000", DEVSTORE_ACCOUNT_NAME));
URI queueSecondaryEndpoint = new URI(String.format(DEVELOPMENT_STORAGE_SECONDARY_ENDPOINT_FORMAT, scheme, host,
"10001", DEVSTORE_ACCOUNT_NAME));
URI tableSecondaryEndpoint = new URI(String.format(DEVELOPMENT_STORAGE_SECONDARY_ENDPOINT_FORMAT, scheme, host,
"10002", DEVSTORE_ACCOUNT_NAME));
CloudStorageAccount account = new CloudStorageAccount(credentials, new StorageUri(blobPrimaryEndpoint,
blobSecondaryEndpoint), new StorageUri(queuePrimaryEndpoint, queueSecondaryEndpoint), new StorageUri(
tablePrimaryEndpoint, tableSecondaryEndpoint), null /* fileStorageUri */);
account.isDevStoreAccount = true;
return account;
}
private static boolean isLinkHostOnline(Log log, URI uri) {
var host = uri.getHost();
try {
InetAddress.getByName(host);
return true;
} catch (@SuppressWarnings("unused") IOException e) {
log.info(host, t -> "link: could not reach host " + host);
return false;
}
}
/**
* Set the namenode address that will be used by clients to access this
* namenode or name service. This needs to be called before the config
* is overriden.
*/
public void setClientNamenodeAddress(Configuration conf) {
String nnAddr = conf.get(FS_DEFAULT_NAME_KEY);
if (nnAddr == null) {
// default fs is not set.
clientNamenodeAddress = null;
return;
}
LOG.info("{} is {}", FS_DEFAULT_NAME_KEY, nnAddr);
URI nnUri = URI.create(nnAddr);
String nnHost = nnUri.getHost();
if (nnHost == null) {
clientNamenodeAddress = null;
return;
}
if (DFSUtil.getNameServiceIds(conf).contains(nnHost)) {
// host name is logical
clientNamenodeAddress = nnHost;
} else if (nnUri.getPort() > 0) {
// physical address with a valid port
clientNamenodeAddress = nnUri.getAuthority();
} else {
// the port is missing or 0. Figure out real bind address later.
clientNamenodeAddress = null;
return;
}
LOG.info("Clients are to use {} to access"
+ " this namenode/service.", clientNamenodeAddress );
}
/**
* Get the URI for the file system based on the given URI. The path, query
* part of the given URI is stripped out and default file system port is used
* to form the URI.
*
* @param uri FileSystem URI.
* @param authorityNeeded if true authority cannot be null in the URI. If
* false authority must be null.
* @param defaultPort default port to use if port is not specified in the URI.
*
* @return URI of the file system
*
* @throws URISyntaxException <code>uri</code> has syntax error
*/
private URI getUri(URI uri, String supportedScheme,
boolean authorityNeeded, int defaultPort) throws URISyntaxException {
checkScheme(uri, supportedScheme);
// A file system implementation that requires authority must always
// specify default port
if (defaultPort < 0 && authorityNeeded) {
throw new HadoopIllegalArgumentException(
"FileSystem implementation error - default port " + defaultPort
+ " is not valid");
}
String authority = uri.getAuthority();
if (authority == null) {
if (authorityNeeded) {
throw new HadoopIllegalArgumentException("Uri without authority: " + uri);
} else {
return new URI(supportedScheme + ":///");
}
}
// authority is non null - AuthorityNeeded may be true or false.
int port = uri.getPort();
port = (port == -1 ? defaultPort : port);
if (port == -1) { // no port supplied and default port is not specified
return new URI(supportedScheme, authority, "/", null);
}
return new URI(supportedScheme + "://" + uri.getHost() + ":" + port);
}
private static void doRun ( URI u, final Object payloadObject, String username, String password ) {
ConnectionProvider instance = null;
ConnectionProviderContextImpl context = null;
ConnectionHandler ch = null;
Channel c = null;
VersionedConnection vc = null;
try {
Logger logger = LogManager.getLogManager().getLogger("");
logger.addHandler(new ConsoleLogHandler());
logger.setLevel(Level.INFO);
OptionMap options = OptionMap.builder().set(Options.SSL_ENABLED, u.getScheme().equals("https")).getMap();
context = new ConnectionProviderContextImpl(options, "endpoint");
instance = new HttpUpgradeConnectionProviderFactory().createInstance(context, options);
String host = u.getHost();
int port = u.getPort() > 0 ? u.getPort() : 9990;
SocketAddress destination = new InetSocketAddress(host, port);
ConnectionHandlerFactory chf = getConnection(destination, username, password, context, instance, options);
ch = chf.createInstance(new ConnectionHandlerContextImpl(context));
c = getChannel(context, ch, options);
System.err.println("Connected");
vc = makeVersionedConnection(c);
MBeanServerConnection mbc = vc.getMBeanServerConnection(null);
doExploit(payloadObject, mbc);
System.err.println("DONE");
}
catch ( Throwable e ) {
e.printStackTrace(System.err);
}
finally {
cleanup(instance, context, ch, c, vc);
}
}
public static String getDockerHost() throws Exception {
String dockerHost = System.getenv("DOCKER_HOST");
if (dockerHost == null) {
InetAddress localHost = InetAddress.getLocalHost();
return localHost.getHostAddress();
}
URI uri = new URI(dockerHost);
return uri.getHost();
}
private UriBuilder makeURIAbsolute(URI uri) {
UriBuilder uriBuilder = UriBuilder.fromUri(uri);
if (!uri.isAbsolute() && uri.getHost() == null) {
uriBuilder
.scheme(apiEndpoint.getScheme())
.host(apiEndpoint.getHost())
.port(apiEndpoint.getPort())
.replacePath(apiEndpoint.getPath() + uri.toString());
}
return uriBuilder;
}
@Override
public ClientHttpResponse intercept(HttpRequest httpRequest, byte[] bytes,
ClientHttpRequestExecution clientHttpRequestExecution) throws IOException {
URI asUri = httpRequest.getURI();
String httpMethod = httpRequest.getMethod().toString();
String serviceName = asUri.getHost();
String url = asUri.getPath();
logger.info("http with serviceName:{}, menthod:{}, url:{}", serviceName, httpMethod, url);
if (circuitBreakerCore.checkRulesExist(httpMethod, serviceName, url)) {
try {
Method wrappedMethod = RestTemplateCircuitBreakerInterceptor.class.getMethod(
"doExecute", ClientHttpRequestExecution.class, HttpRequest.class, byte[].class);
Object[] args = {clientHttpRequestExecution, httpRequest, bytes};
ClientHttpResponse response = (ClientHttpResponse) circuitBreakerCore.process(httpMethod,
serviceName, url, wrappedMethod, this, args);
// todo 熔断返回null
return response;
} catch (Exception e) {
logger.error(e.getMessage(), e);
if (e instanceof CircuitBreakerOpenException) {
throw new RuntimeException(e.getMessage());
} else if (e instanceof IOException) {
throw new IOException(e.getMessage());
} else {
throw new RuntimeException(e.getMessage());
}
}
} else {
return clientHttpRequestExecution.execute(httpRequest, bytes);
}
}
@VisibleForTesting
public LocalFetcher(TajoConf conf, URI uri, String tableName) throws IOException {
super(conf, uri);
this.maxUrlLength = conf.getIntVar(ConfVars.PULLSERVER_FETCH_URL_MAX_LENGTH);
this.tableName = tableName;
this.localFileSystem = new LocalFileSystem();
this.localDirAllocator = new LocalDirAllocator(ConfVars.WORKER_TEMPORAL_DIR.varname);
this.pullServerService = null;
String scheme = uri.getScheme() == null ? "http" : uri.getScheme();
this.host = uri.getHost() == null ? "localhost" : uri.getHost();
this.port = uri.getPort();
if (port == -1) {
if (scheme.equalsIgnoreCase("http")) {
this.port = 80;
} else if (scheme.equalsIgnoreCase("https")) {
this.port = 443;
}
}
bootstrap = new Bootstrap()
.group(
NettyUtils.getSharedEventLoopGroup(NettyUtils.GROUP.FETCHER,
conf.getIntVar(ConfVars.SHUFFLE_RPC_CLIENT_WORKER_THREAD_NUM)))
.channel(NioSocketChannel.class)
.option(ChannelOption.ALLOCATOR, NettyUtils.ALLOCATOR)
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS,
conf.getIntVar(ConfVars.SHUFFLE_FETCHER_CONNECT_TIMEOUT) * 1000)
.option(ChannelOption.SO_RCVBUF, 1048576) // set 1M
.option(ChannelOption.TCP_NODELAY, true);
}
@Override
protected void writeLatestFile(URI fileUri, byte[] contents)
throws IOException {
String bucket = fileUri.getHost();
String key = getKeyFromPath(fileUri);
uploadContents(bucket, key, contents);
}
private String lockVersionControl(final URI originalUri, final String groupId) throws URISyntaxException {
final URI createRequestUri = new URI(originalUri.getScheme(), originalUri.getUserInfo(), originalUri.getHost(),
originalUri.getPort(), "/nifi-api/versions/active-requests", null, originalUri.getFragment());
final NodeResponse clusterResponse;
try {
// create an active request entity to indicate the group id
final CreateActiveRequestEntity activeRequestEntity = new CreateActiveRequestEntity();
activeRequestEntity.setProcessGroupId(groupId);
final Map<String, String> headers = new HashMap<>();
headers.put("content-type", MediaType.APPLICATION_JSON);
if (getReplicationTarget() == ReplicationTarget.CLUSTER_NODES) {
clusterResponse = getRequestReplicator().replicate(HttpMethod.POST, createRequestUri, activeRequestEntity, headers).awaitMergedResponse();
} else {
clusterResponse = getRequestReplicator().forwardToCoordinator(
getClusterCoordinatorNode(), HttpMethod.POST, createRequestUri, activeRequestEntity, headers).awaitMergedResponse();
}
} catch (final InterruptedException ie) {
Thread.currentThread().interrupt();
throw new RuntimeException("Interrupted while updating Version Control Information for Process Group with ID " + groupId + ".", ie);
}
if (clusterResponse.getStatus() != Status.OK.getStatusCode()) {
final String errorResponse = getResponseEntity(clusterResponse, String.class);
throw new IllegalStateException(
"Failed to create a Version Control Request across all nodes in the cluster. Received response code " + clusterResponse.getStatus() + " with content: " + errorResponse);
}
final String requestId = getResponseEntity(clusterResponse, String.class);
return requestId;
}
/**
* Splits the specified 'remoteUri' into:
* - the actual remote repository URI
* - the path in the remote repository
* - the branch name.
*
* <p>e.g. git+ssh://foo.com/bar.git/some-path#master is split into:
* - remoteRepoUri: git+ssh://foo.com/bar.git
* - remotePath: /some-path
* - remoteBranch: master
*
* <p>e.g. dogma://foo.com/bar/qux.dogma is split into:
* - remoteRepoUri: dogma://foo.com/bar/qux.dogma
* - remotePath: / (default)
* - remoteBranch: {@code defaultBranch}
*/
static String[] split(URI remoteUri, String suffix, @Nullable String defaultBranch) {
final String host = remoteUri.getHost();
if (host == null && !remoteUri.getScheme().endsWith("+file")) {
throw new IllegalArgumentException("no host in remoteUri: " + remoteUri);
}
final String path = remoteUri.getRawPath();
if (path == null) {
throw new IllegalArgumentException("no path in remoteUri: " + remoteUri);
}
final Matcher matcher = Pattern.compile("^(.*?\\." + suffix + ")(?:$|/)").matcher(path);
if (!matcher.find()) {
throw new IllegalArgumentException("no '." + suffix + "' in remoteUri path: " + remoteUri);
}
final String newRemoteUri;
final int port = remoteUri.getPort();
if (host != null) {
if (port > 0) {
newRemoteUri = remoteUri.getScheme() + "://" + host + ':' + port +
matcher.group(1);
} else {
newRemoteUri = remoteUri.getScheme() + "://" + host + matcher.group(1);
}
} else {
newRemoteUri = remoteUri.getScheme() + "://" + matcher.group(1);
}
final String remotePath;
try {
String decoded = URLDecoder.decode(path.substring(matcher.group(1).length()), "UTF-8");
decoded = normalizePath(decoded);
remotePath = decoded;
} catch (UnsupportedEncodingException e) {
throw new Error(e);
}
final String fragment = remoteUri.getFragment();
final String remoteBranch = fragment != null ? fragment : defaultBranch;
return new String[] { newRemoteUri, remotePath, remoteBranch };
}
/**
* Test that the stored cookie is applied to requests for different URLs. Most of the other
* tests just check a single URL.
*
* @throws Exception
*/
@TestTemplate
public void cookieAppliedToDifferentURL() throws Exception {
mockWebServer.enqueue(OK_COOKIE);
mockWebServer.enqueue(new MockResponse().setBody("first"));
mockWebServer.enqueue(new MockResponse().setBody("second"));
CloudantClient c = CloudantClientHelper.newMockWebServerClientBuilder(mockWebServer)
.username("a")
.password("b")
.build();
URI baseURI = c.getBaseUri();
URL first = new URL(baseURI.getScheme(), baseURI.getHost(), baseURI.getPort(), "/testdb");
String response = c.executeRequest(Http.GET(first)).responseAsString();
assertEquals("first", response, "The correct response body should be present");
// There should be a request for a cookie followed by a the real request
assertEquals(2, mockWebServer.getRequestCount(), "There should be 2 requests");
assertEquals("/_session", MockWebServerResources.takeRequestWithTimeout(mockWebServer)
.getPath(), "The first request should have been for a cookie");
RecordedRequest request = MockWebServerResources.takeRequestWithTimeout(mockWebServer);
assertEquals("/testdb",
request.getPath(), "The second request should have been for /testdb");
assertNotNull(request.getHeader("Cookie"), "There should be a cookie on the request");
// Now make a request to another URL
URL second = new URL(baseURI.getScheme(), baseURI.getHost(), baseURI.getPort(),
"/_all_dbs");
response = c.executeRequest(Http.GET(second)).responseAsString();
assertEquals("second", response, "The correct response body should be present");
// There should now be an additional request
assertEquals(3, mockWebServer.getRequestCount(), "There should be 3 requests");
request = MockWebServerResources.takeRequestWithTimeout(mockWebServer);
assertEquals("/_all_dbs", request.getPath(), "The second request should have been for " +
"/_all_dbs");
String cookieHeader = request.getHeader("Cookie");
assertNotNull(cookieHeader, "There should be a cookie on the request");
assertTrue(
request.getHeader("Cookie").contains(EXPECTED_OK_COOKIE), "The cookie header " +
cookieHeader + " should contain the expected value.");
}
public static String getDomainName(String url) throws URISyntaxException {
URI uri = new URI(url);
return uri.getHost();
}
private List<String> filterNonDirectChilds(URI baseURI, List<URI> inputURIs) throws MalformedURLException {
final int baseURIPort = baseURI.getPort();
final String baseURIHost = baseURI.getHost();
final String baseURIScheme = baseURI.getScheme();
List<String> uris = new ArrayList<String>();
final String prefixPath = baseURI.getPath();
for (URI parsedURI : inputURIs) {
if (parsedURI.getHost() != null && !parsedURI.getHost().equals(baseURIHost)) {
continue;
}
if (parsedURI.getScheme() != null && !parsedURI.getScheme().equals(baseURIScheme)) {
continue;
}
if (parsedURI.getPort() != baseURIPort) {
continue;
}
if (parsedURI.getPath() != null && !parsedURI.getPath().startsWith(prefixPath)) {
continue;
}
String childPathPart = parsedURI.getPath().substring(prefixPath.length(), parsedURI.getPath().length());
if (childPathPart.startsWith("../")) {
continue;
}
if (childPathPart.equals("") || childPathPart.split("/").length > 1) {
continue;
}
String path = parsedURI.getPath();
int pos = path.lastIndexOf('/');
if (pos < 0) {
uris.add(path);
} else if (pos == path.length() - 1) {
int start = path.lastIndexOf('/', pos - 1);
if (start < 0) {
uris.add(path.substring(0, pos));
} else {
uris.add(path.substring(start + 1, pos));
}
} else {
uris.add(path.substring(pos + 1));
}
}
return uris;
}