下面列出了org.apache.http.client.utils.URIBuilder#setPath ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* @throws Exception something bad happened
*/
public final void testRedirectToLoginSetsURLBounceRequestAttribute() throws Exception {
setupPxtDelegate(true, false, 1234L);
setupGetRequestURI("/rhn/YourRhn.do");
setUpRedirectToLogin();
URIBuilder uriBounceBuilder = new URIBuilder();
uriBounceBuilder.setPath("/rhn/YourRhn.do");
uriBounceBuilder.addParameter("question", "param 1 = 'Who is the one?'");
uriBounceBuilder.addParameter("answer", "param 2 = 'Neo is the one!'");
URIBuilder uriBuilder = new URIBuilder();
uriBuilder.setPath("/rhn/manager/login");
uriBuilder.addParameter("url_bounce", uriBounceBuilder.toString());
uriBuilder.addParameter("request_method", "POST");
context().checking(new Expectations() { {
allowing(mockResponse).sendRedirect(uriBuilder.toString());
will(returnValue(null));
allowing(mockRequest).getRequestURI();
will(returnValue("/rhn/YourRhn.do"));
} });
runRedirectToLoginTest();
}
private static String buildUrl(String path, List<NameValuePair> qparams) throws RestApiClientException {
URIBuilder builder = new URIBuilder();
builder.setPath(path);
ListIterator<NameValuePair> i = qparams.listIterator();
while (i.hasNext()) {
NameValuePair keyValue = i.next();
builder.setParameter(keyValue.getName(), keyValue.getValue());
}
URI uri;
try {
uri = builder.build();
} catch (Exception e) {
throw new RestApiClientException("Failed to build url; error - " + e.getMessage(), e);
}
return new HttpGet(uri).getURI().toString();
}
@Override
public ClientResponse handle(ClientRequest cr) throws ClientHandlerException
{
URIBuilder uriBuilder = new URIBuilder(cr.getURI());
String path = uriBuilder.getPath();
uriBuilder.setPath(converter.convertCommandPath(path));
try {
cr.setURI(uriBuilder.build());
ClientResponse response = getNext().handle(cr);
String newEntity = converter.convertResponse(path, response.getEntity(String.class));
response.setEntityInputStream(new ByteArrayInputStream(newEntity.getBytes()));
return response;
} catch (Exception ex) {
throw new ClientHandlerException(ex);
}
}
@NotNull
public static String updateVMURL(NutanixUpdateVMInputs nutanixUpdateVMInputs) throws Exception {
final URIBuilder uriBuilder = getUriBuilder(nutanixUpdateVMInputs.getCommonInputs());
StringBuilder pathString = new StringBuilder()
.append(API)
.append(nutanixUpdateVMInputs.getCommonInputs().getAPIVersion())
.append(GET_VM_DETAILS_PATH)
.append(PATH_SEPARATOR)
.append(nutanixUpdateVMInputs.getVmUUID());
uriBuilder.setPath(pathString.toString());
return uriBuilder.build().toURL().toString();
}
@NotNull
public static String getVMDetailsURL(NutanixGetVMDetailsInputs nutanixGetVMDetailsInputs) throws Exception {
final URIBuilder uriBuilder = getUriBuilder(nutanixGetVMDetailsInputs.getCommonInputs());
StringBuilder pathString = new StringBuilder()
.append(API)
.append(nutanixGetVMDetailsInputs.getCommonInputs().getAPIVersion())
.append(GET_VM_DETAILS_PATH)
.append(PATH_SEPARATOR)
.append(nutanixGetVMDetailsInputs.getVMUUID());
uriBuilder.setPath(pathString.toString());
return uriBuilder.build().toURL().toString();
}
private String buildIri(String id, Map<String, Object> params) {
try {
URIBuilder builder = new URIBuilder(baseUrl);
builder.setPath(String.format("%s/%s", builder.getPath(), id));
if (params != null && !params.isEmpty()) {
for (Entry<String, Object> param : params.entrySet()) {
builder.addParameter(param.getKey(), String.valueOf(param.getValue()));
}
}
return builder.toString();
} catch (URISyntaxException e) {
throw new InvalidIRIException(String.format("An error occurred building IRI with base URL [%s] with ID [%s] and parameters [%s]", baseUrl, id, params), e);
}
}
protected <T> T executeGetMethod(String uriSuffix, Consumer<URIBuilder> uriCustomizer, Consumer<HttpGet> requestCustomizer, IOFunction<T> resultConverter,
Supplier<String> errorSupplier) throws URISyntaxException, IOException {
URIBuilder builder = buildURI();
builder.setPath(serviceURI() + uriSuffix);
uriCustomizer.accept(builder);
HttpGet getRequest = new HttpGet(builder.build());
requestCustomizer.accept(getRequest);
return execute(getRequest, resultConverter, errorSupplier);
}
@NotNull
public static String getDcaDeployUrl(@NotNull final String protocol,
@NotNull final String dcaHost,
@NotNull final String dcaPort) {
final URIBuilder uriBuilder = getUriBuilder(protocol, dcaHost, dcaPort);
uriBuilder.setPath(DCA_DEPLOYMENT_PATH);
try {
return uriBuilder.build().toURL().toString();
} catch (MalformedURLException | URISyntaxException e) {
throw new RuntimeException(e);
}
}
public static String httpGetRequest(String url, Map<String, Object> params) throws URISyntaxException {
URIBuilder ub = new URIBuilder();
ub.setPath(url);
ArrayList<NameValuePair> pairs = covertParams2NVPS(params);
ub.setParameters(pairs);
HttpGet httpGet = new HttpGet(ub.build());
return getResult(httpGet);
}
@NotNull
public static String getDcaCMCredentialUrl(@NotNull final String protocol,
@NotNull final String dcaHost,
@NotNull final String dcaPort,
@NotNull final String credentialUuid) {
final URIBuilder uriBuilder = getUriBuilder(protocol, dcaHost, dcaPort);
uriBuilder.setPath(String.format("%s/%s/%s", DCA_CM_CREDENTIAL_PATH, credentialUuid, DATA_PATH));
try {
return uriBuilder.build().toURL().toString();
} catch (MalformedURLException | URISyntaxException e) {
throw new RuntimeException(e);
}
}
private void startServer() throws Exception {
URL url = Thread.currentThread().getContextClassLoader().getResource("WEB-INF");
URIBuilder uriBuilder = new URIBuilder(url.toURI());
uriBuilder.setPath(Paths.get(url.getPath()).getParent().toString());
context = new WebAppContext(uriBuilder.toString() + "/", "/");
context.setInitParameter("org.eclipse.jetty.servlet.Default.dirAllowed", "false");
context.setExtractWAR(false);
server.setHandler(context);
server.start();
}
@NotNull
public static String getDcaResourceUrl(@NotNull final String protocol,
@NotNull final String dcaHost,
@NotNull final String dcaPort,
@NotNull final String resourceUuid) {
final URIBuilder uriBuilder = getUriBuilder(protocol, dcaHost, dcaPort);
uriBuilder.setPath(String.format("%s/%s", DCA_RESOURCE_PATH, resourceUuid));
try {
return uriBuilder.build().toURL().toString();
} catch (MalformedURLException | URISyntaxException e) {
throw new RuntimeException(e);
}
}
@NotNull
public static String AddNicURL(NutanixAddNicInputs nutanixAttachNicInputs) throws Exception {
final URIBuilder uriBuilder = getUriBuilder(nutanixAttachNicInputs.getCommonInputs());
StringBuilder pathString = new StringBuilder()
.append(API)
.append(nutanixAttachNicInputs.getCommonInputs().getAPIVersion())
.append(GET_VM_DETAILS_PATH)
.append(PATH_SEPARATOR)
.append(nutanixAttachNicInputs.getVmUUID())
.append(ADD_NIC_PATH);
uriBuilder.setPath(pathString.toString());
return uriBuilder.build().toURL().toString();
}
@NotNull
public static String createVMURL(NutanixCreateVMInputs nutanixCreateVMInputs) throws Exception {
final URIBuilder uriBuilder = getUriBuilder(nutanixCreateVMInputs.getCommonInputs());
StringBuilder pathString = new StringBuilder()
.append(API)
.append(nutanixCreateVMInputs.getCommonInputs().getAPIVersion())
.append(GET_VM_DETAILS_PATH);
uriBuilder.setPath(pathString.toString());
return uriBuilder.build().toURL().toString();
}
@NotNull
public static String getTaskDetailsURL(NutanixGetTaskDetailsInputs nutanixGetTaskDetailsInputs) throws Exception {
final URIBuilder uriBuilder = getUriBuilder(nutanixGetTaskDetailsInputs.getCommonInputs());
StringBuilder pathString = new StringBuilder()
.append(API)
.append(nutanixGetTaskDetailsInputs.getCommonInputs().getAPIVersion())
.append(GET_TASK_DETAILS_PATH)
.append(PATH_SEPARATOR)
.append(nutanixGetTaskDetailsInputs.getTaskUUID());
uriBuilder.setPath(pathString.toString());
return uriBuilder.build().toURL().toString();
}
@NotNull
public static String getApplyDetailsUrl(@NotNull final String applyId) throws Exception {
final URIBuilder uriBuilder = getUriBuilder();
StringBuilder pathString = new StringBuilder()
.append(API)
.append(API_VERSION)
.append(APPLY_DETAILS_PATH)
.append(PATH_SEPARATOR)
.append(applyId);
uriBuilder.setPath(pathString.toString());
return uriBuilder.build().toURL().toString();
}
@NotNull
private static String getOrganizationDetailsUrl(@NotNull final String organizationName) throws Exception {
final URIBuilder uriBuilder = getUriBuilder();
uriBuilder.setPath(getOrganizationDetailsPath(organizationName));
return uriBuilder.build().toURL().toString();
}
@NotNull
private static String getCurrentStateVersionUrl(@NotNull final String workspaceId) throws Exception {
final URIBuilder uriBuilder = getUriBuilder();
uriBuilder.setPath(getCurrentStateVersionPath(workspaceId));
return uriBuilder.build().toURL().toString();
}
@NotNull
private static String getInstanceDefaultCredentialsUrl(@NotNull final String region, @NotNull final String instanceId) throws Exception {
final URIBuilder uriBuilder = HttpUtils.getUriBuilder(region);
uriBuilder.setPath(getInstanceDefaultCredentialsPath(instanceId));
return uriBuilder.build().toURL().toString();
}
/**
* Gets SSO URL and proof key
*
* @return SSO URL.
* @throws SFException if Snowflake error occurs
* @throws SnowflakeSQLException if Snowflake SQL error occurs
*/
private String getSSOUrl(int port) throws SFException, SnowflakeSQLException
{
try
{
String serverUrl = loginInput.getServerUrl();
String authenticator = loginInput.getAuthenticator();
URIBuilder fedUriBuilder = new URIBuilder(serverUrl);
fedUriBuilder.setPath(SessionUtil.SF_PATH_AUTHENTICATOR_REQUEST);
URI fedUrlUri = fedUriBuilder.build();
HttpPost postRequest = this.handlers.build(fedUrlUri);
ClientAuthnDTO authnData = new ClientAuthnDTO();
Map<String, Object> data = new HashMap<>();
data.put(ClientAuthnParameter.AUTHENTICATOR.name(), authenticator);
data.put(ClientAuthnParameter.ACCOUNT_NAME.name(),
loginInput.getAccountName());
data.put(ClientAuthnParameter.LOGIN_NAME.name(),
loginInput.getUserName());
data.put(ClientAuthnParameter.BROWSER_MODE_REDIRECT_PORT.name(),
Integer.toString(port));
data.put(ClientAuthnParameter.CLIENT_APP_ID.name(), loginInput.getAppId());
data.put(ClientAuthnParameter.CLIENT_APP_VERSION.name(),
loginInput.getAppVersion());
authnData.setData(data);
String json = mapper.writeValueAsString(authnData);
// attach the login info json body to the post request
StringEntity input = new StringEntity(json, StandardCharsets.UTF_8);
input.setContentType("application/json");
postRequest.setEntity(input);
postRequest.addHeader("accept", "application/json");
String theString = HttpUtil.executeGeneralRequest(
postRequest,
loginInput.getLoginTimeout(),
loginInput.getOCSPMode());
logger.debug("authenticator-request response: {}", theString);
// general method, same as with data binding
JsonNode jsonNode = mapper.readTree(theString);
// check the success field first
if (!jsonNode.path("success").asBoolean())
{
logger.debug("response = {}", theString);
String errorCode = jsonNode.path("code").asText();
throw new SnowflakeSQLException(
SqlState.SQLCLIENT_UNABLE_TO_ESTABLISH_SQLCONNECTION,
Integer.valueOf(errorCode),
jsonNode.path("message").asText());
}
JsonNode dataNode = jsonNode.path("data");
// session token is in the data field of the returned json response
this.proofKey = dataNode.path("proofKey").asText();
return dataNode.path("ssoUrl").asText();
}
catch (IOException | URISyntaxException ex)
{
throw new SFException(ex, ErrorCode.NETWORK_ERROR, ex.getMessage());
}
}