下面列出了org.apache.http.client.utils.URIBuilder#setUserInfo ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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;
}
@JsonIgnore
public String getMetricsUrl() {
URIBuilder builder =
new URIBuilder()
.setScheme(getScheme())
.setPort(getPort())
.setHost("localhost")
.setPath("spectator/metrics");
if (getBasicAuthEnabled() != null && getBasicAuthEnabled()) {
builder.setUserInfo(getUsername(), getPassword());
}
try {
return builder.build().toString();
} catch (URISyntaxException e) {
throw new HalException(
Problem.Severity.FATAL, "Could not build metrics endpoint. This is probably a bug.", e);
}
}
@Test
public void upload1 () throws URISyntaxException, IOException
{
final ChannelTester tester = getTester ();
final File file = getAbsolutePath ( CommonResources.BUNDLE_1_RESOURCE );
final URIBuilder b = new URIBuilder ( resolve ( "/api/v2/upload/channel/%s/%s", tester.getId (), file.getName () ) );
b.setUserInfo ( "deploy", this.deployKey );
b.addParameter ( "foo:bar", "baz" );
try ( final CloseableHttpResponse response = upload ( b, file ) )
{
Assert.assertEquals ( 200, response.getStatusLine ().getStatusCode () );
}
final Set<String> arts = tester.getAllArtifactIds ();
Assert.assertEquals ( 1, arts.size () );
}
/**
* 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());
}
}
private String makeCredentialsPrefix ( final String sitePrefix, final String name, final String password )
{
try
{
final URIBuilder builder = new URIBuilder ( sitePrefix );
builder.setUserInfo ( name, password );
return builder.build ().toString ();
}
catch ( final URISyntaxException e )
{
return sitePrefix;
}
}
@Test
public void upload1Plain () throws URISyntaxException, IOException
{
final ChannelTester tester = ChannelTester.create ( getWebContext (), "uploadapi2a" );
tester.assignDeployGroup ( "m1" );
final String deployKey = tester.getDeployKeys ().iterator ().next ();
final File file = getAbsolutePath ( CommonResources.BUNDLE_1_RESOURCE );
final URIBuilder b = new URIBuilder ( resolve ( "/api/v3/upload/plain/channel/%s/%s", tester.getId (), file.getName () ) );
b.setUserInfo ( "deploy", deployKey );
b.addParameter ( "foo:bar", "baz" );
System.out.println ( "Request: " + b.build () );
try ( final CloseableHttpResponse response = upload ( b, file ) )
{
final String result = CharStreams.toString ( new InputStreamReader ( response.getEntity ().getContent (), StandardCharsets.UTF_8 ) );
System.out.println ( "Result: " + response.getStatusLine () );
System.out.println ( result );
Assert.assertEquals ( 200, response.getStatusLine ().getStatusCode () );
}
final Set<String> arts = tester.getAllArtifactIds ();
Assert.assertEquals ( 1, arts.size () );
}
@Test
public void upload2Archive () throws URISyntaxException, IOException
{
final ChannelTester tester = ChannelTester.create ( getWebContext (), "uploadapi2b" );
tester.assignDeployGroup ( "m1" );
final String deployKey = tester.getDeployKeys ().iterator ().next ();
final File file1 = getAbsolutePath ( CommonResources.BUNDLE_1_RESOURCE );
final File file2 = getAbsolutePath ( CommonResources.BUNDLE_2_RESOURCE );
final Path tmp = Paths.get ( "upload-1.zip" );
try ( TransferArchiveWriter writer = new TransferArchiveWriter ( Files.newOutputStream ( tmp ) ) )
{
final Map<MetaKey, String> properties = new HashMap<> ();
properties.put ( new MetaKey ( "foo", "bar" ), "baz" );
properties.put ( new MetaKey ( "foo", "bar2" ), "baz2" );
writer.createEntry ( file1.getName (), properties, ContentProvider.file ( file1 ) );
writer.createEntry ( file2.getName (), properties, ContentProvider.file ( file2 ) );
}
final URIBuilder b = new URIBuilder ( resolve ( "/api/v3/upload/archive/channel/%s", tester.getId () ) );
b.setUserInfo ( "deploy", deployKey );
System.out.println ( "Request: " + b.build () );
try ( final CloseableHttpResponse response = upload ( b, tmp.toFile () ) )
{
final String result = CharStreams.toString ( new InputStreamReader ( response.getEntity ().getContent (), StandardCharsets.UTF_8 ) );
System.out.println ( "Result: " + response.getStatusLine () );
System.out.println ( result );
Assert.assertEquals ( 200, response.getStatusLine ().getStatusCode () );
}
final Set<String> arts = tester.getAllArtifactIds ();
Assert.assertEquals ( 2, arts.size () );
}
private String buildRemoteRepo() {
URI remoteRepo = deployConfig.getNexusUrl();
if (remoteRepo != null && deployConfig.isHttpAuthentication()) {
URIBuilder builder = new URIBuilder(remoteRepo);
builder.setUserInfo(deployConfig.getHttpAuthUser() + ":" + deployConfig.getHttpAuthPassword());
return builder.toString();
}
return deployConfig.getNexusUrl().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 + "'");
}
}
/**
* 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;
}