Java源码示例:org.jclouds.http.HttpRequest
示例1
@Override
public HttpRequest filter(final HttpRequest request) throws HttpException {
if (creds.authType() == AuthenticationType.Anonymous) {
return request;
} else {
final String authHeader = creds.authType() + " " + creds.authValue();
final HttpRequest.Builder<? extends HttpRequest.Builder<?>> builder = request.toBuilder();
builder.addHeader(HttpHeaders.AUTHORIZATION, authHeader);
// whether to add crumb header or not
final Pair<Crumb, Boolean> localCrumb = getCrumb();
if (localCrumb.getKey().value() != null) {
builder.addHeader(CRUMB_HEADER, localCrumb.getKey().value());
Optional.ofNullable(localCrumb.getKey().sessionIdCookie())
.ifPresent(sessionId -> builder.addHeader(HttpHeaders.COOKIE, sessionId));
} else {
if (localCrumb.getValue() == false) {
throw new RuntimeException("Unexpected exception being thrown: error=" + localCrumb.getKey().errors().get(0));
}
}
return builder.build();
}
}
示例2
public URI getSignedGetContentUri( String key )
{
BlobRequestSigner signer = blobStoreContext.getSigner();
if ( !requestSigningSupported( signer ) )
{
return null;
}
HttpRequest httpRequest;
try
{
httpRequest = signer.signGetBlob( config.container, key, FIVE_MINUTES_IN_SECONDS );
}
catch ( UnsupportedOperationException uoe )
{
return null;
}
return httpRequest.getEndpoint();
}
示例3
@Override
public URI getSignedGetContentUri( String key )
{
BlobRequestSigner signer = blobStoreContext.getSigner();
if ( !requestSigningSupported( signer ) )
{
return null;
}
HttpRequest httpRequest;
try
{
httpRequest = signer.signGetBlob( config.container, key, FIVE_MINUTES_IN_SECONDS );
}
catch ( UnsupportedOperationException uoe )
{
return null;
}
return httpRequest.getEndpoint();
}
示例4
private void generatePutTempURL() throws IOException {
System.out.format("Generate PUT Temp URL%n");
// Create the Payload
String data = "This object will be public for 10 minutes.";
ByteSource source = ByteSource.wrap(data.getBytes());
Payload payload = Payloads.newByteSourcePayload(source);
// Create the Blob
Blob blob = blobStore.blobBuilder(FILENAME).payload(payload).contentType("text/plain").build();
HttpRequest request = blobStoreContext.getSigner(REGION).signPutBlob(CONTAINER, blob, TEN_MINUTES);
System.out.format(" %s %s%n", request.getMethod(), request.getEndpoint());
// PUT the file using jclouds
HttpResponse response = blobStoreContext.utils().http().invoke(request);
int statusCode = response.getStatusCode();
if (statusCode >= 200 && statusCode < 299) {
System.out.format(" PUT Success (%s)%n", statusCode);
}
else {
throw new HttpResponseException(null, response);
}
}
示例5
private void generateGetTempURL() throws IOException {
System.out.format("Generate GET Temp URL%n");
HttpRequest request = blobStoreContext.getSigner(REGION).signGetBlob(CONTAINER, FILENAME, TEN_MINUTES);
System.out.format(" %s %s%n", request.getMethod(), request.getEndpoint());
// GET the file using jclouds
File file = File.createTempFile(FILENAME, ".tmp");
Payload payload = blobStoreContext.utils().http().invoke(request).getPayload();
try {
Files.asByteSink(file).writeFrom(payload.openStream());
System.out.format(" GET Success (%s)%n", file.getAbsolutePath());
} finally {
payload.release();
file.delete();
}
}
示例6
private void generateDeleteTempURL() throws IOException {
System.out.format("Generate DELETE Temp URL%n");
HttpRequest request = blobStoreContext.getSigner(REGION).signRemoveBlob(CONTAINER, FILENAME);
System.out.format(" %s %s%n", request.getMethod(), request.getEndpoint());
// DELETE the file using jclouds
HttpResponse response = blobStoreContext.utils().http().invoke(request);
int statusCode = response.getStatusCode();
if (statusCode >= 200 && statusCode < 299) {
System.out.format(" DELETE Success (%s)%n", statusCode);
}
else {
throw new HttpResponseException(null, response);
}
}
示例7
@SuppressWarnings("unchecked")
@Override
public <R extends HttpRequest> R bindToRequest(final R request, final Object properties) {
if (properties == null) {
return (R) request.toBuilder().build();
}
checkArgument(properties instanceof Map, "binder is only valid for Map");
Map<String, List<String>> props = (Map<String, List<String>>) properties;
Builder<?> builder = request.toBuilder();
for (Map.Entry<String, List<String>> prop : props.entrySet()) {
if (prop.getKey() != null) {
String potentialKey = prop.getKey().trim();
if (potentialKey.length() > 0) {
if (prop.getValue() == null) {
prop.setValue(Lists.newArrayList(""));
}
builder.addFormParam(potentialKey, prop.getValue().toArray(new String[prop.getValue().size()]));
}
}
}
return (R) builder.build();
}
示例8
@Override
public HttpRequest filter(final HttpRequest request) throws HttpException {
if (creds.authType() == AuthenticationType.Anonymous) {
return request;
} else {
final String authHeader = creds.authType() + " " + creds.authValue();
return request.toBuilder().addHeader(HttpHeaders.AUTHORIZATION, authHeader).build();
}
}
示例9
@SuppressWarnings("unchecked")
@Override
public <R extends HttpRequest> R bindToRequest(final R request, final Object hookSettings) {
checkArgument(hookSettings instanceof HookSettings, "binder is only valid for HookSettings");
final HookSettings passedHookSettings = HookSettings.class.cast(hookSettings);
final String payload = passedHookSettings.settings().toString();
return (R) request.toBuilder().payload(payload).build();
}
示例10
@Override
public HttpRequest filter(final HttpRequest request) throws HttpException {
if (creds.authType() == AuthenticationType.Anonymous) {
return request;
} else {
final String authHeader = creds.authType() + " " + creds.authValue();
return request.toBuilder().addHeader(HttpHeaders.AUTHORIZATION, authHeader).build();
}
}
示例11
@Override
public HttpRequest filter(final HttpRequest request) throws HttpException {
String requestPath = request.getEndpoint().getRawPath()
.replaceAll(SCRUB_NULL_PARAM, EMPTY_STRING)
.replaceAll(DOUBLE_FORWARD_SLASH, FORWARD_SLASH);
if (requestPath.charAt(requestPath.length() - 1) == FORWARD_SLASH_CHAR) {
requestPath = requestPath.substring(0, requestPath.length() - 1);
}
return request.toBuilder().replacePath(requestPath).build();
}
示例12
/**
* {@inheritDoc}
*/
@Override
public URI getAssetDirectLink(String id, String root, String filePath) throws IOException {
ContainerAndName can = getContainerAndName(id, root, filePath);
HttpRequest hr = context.getSigner().signGetBlob(can.container, can.name, SIGNED_URL_VALIDITY_SECONDS);
if (hr == null) {
throw new IOException("No object found to creat signed url " + id);
}
return hr.getEndpoint();
}
示例13
@SuppressWarnings("unchecked")
@Override
public <R extends HttpRequest> R bindToRequest(R request, Object input) {
checkArgument(input instanceof LaunchSpecification, "this binder is only valid for LaunchSpecifications!");
LaunchSpecification launchSpec = LaunchSpecification.class.cast(input);
return (R) request.toBuilder().replaceFormParams(Multimaps.forMap(apply(launchSpec))).build();
}
示例14
@Override
public HandlerForGeneratedRequestWithResult<SecurityGroup> setContext(HttpRequest request) {
region = AWSUtils.findRegionInArgsOrNull(GeneratedHttpRequest.class.cast(request));
if (region == null)
region = defaultRegion.get();
builder = builder();
return super.setContext(request);
}
示例15
@Override protected void assertNonPayloadHeadersEqual(HttpRequest request, String toMatch) {
Multimap<String, String> headersToCheck = LinkedHashMultimap.create();
for (String key : request.getHeaders().keySet()) {
if (key.equals("X-Amz-Date")) {
assertEquals(request.getFirstHeaderOrNull(key), "20120416T155408Z");
} else if (key.equals("Authorization")) {
assertThat(request.getFirstHeaderOrNull(AUTHORIZATION)).startsWith(
"AWS4-HMAC-SHA256 Credential=identity/20120416/"
+ "us-east-1/ec2/aws4_request, SignedHeaders=content-type;host;x-amz-date, Signature=");
} else {
headersToCheck.putAll(key, request.getHeaders().get(key));
}
}
assertEquals(sortAndConcatHeadersIntoString(headersToCheck), toMatch);
}
示例16
/**
* {@inheritDoc}
*/
@Override
public URI getAssetDirectLink(String id, String root, String filePath) throws IOException {
ContainerAndName can = getContainerAndName(id, root, filePath);
HttpRequest hr = context.getSigner().signGetBlob(can.container, can.name, SIGNED_URL_VALIDITY_SECONDS);
if (hr == null) {
throw new IOException("No object found to creat signed url " + id);
}
return hr.getEndpoint();
}
示例17
@Override
public HttpRequest filter(final HttpRequest request) throws HttpException {
final String requestPath = request.getEndpoint().getRawPath().replaceAll(SCRUB_NULL_PARAM, EMPTY_STRING);
return request.toBuilder().fromHttpRequest(request).replacePath(requestPath).build();
}
示例18
@Override
public <R extends HttpRequest> R bindToRequest(R request, Object input) {
checkArgument(checkNotNull(input, "input") instanceof Map, "This binder is only valid for Map<String,String>");
return AWSUtils.indexMapToFormValuesWithPrefix(request, "Tag", "Key", "Value", input);
}
示例19
@Override
public <R extends HttpRequest> R bindToRequest(R request, Object input) {
checkArgument(checkNotNull(input, "input") instanceof Map<?, ?>, "this binder is only valid for Map<?, Iterable<?>>");
return AWSUtils.indexMapOfIterableToFormValuesWithPrefix(request, "Filter", "Name", "Value", input);
}
示例20
@Override
public <R extends HttpRequest> R bindToRequest(R request, Object input) {
return AWSUtils.indexStringArrayToFormValuesWithPrefix(request, "SpotInstanceRequestId", input);
}
示例21
@Override
public HandlerWithResult<Set<ReservedInstancesOffering>> setContext(HttpRequest request) {
reservedInstancesOffering.setContext(request);
return super.setContext(request);
}
示例22
@Override
public HandlerWithResult<Set<Spot>> setContext(HttpRequest request) {
spotHandler.setContext(request);
return super.setContext(request);
}
示例23
@Override
public HandlerWithResult<Set<PlacementGroup>> setContext(HttpRequest request) {
bundleTaskHandler.setContext(request);
return super.setContext(request);
}
示例24
@Override
public HandlerWithResult<Set<SpotInstanceRequest>> setContext(HttpRequest request) {
spotRequestHandler.setContext(request);
return super.setContext(request);
}
示例25
@Override
public HandlerForGeneratedRequestWithResult<Set<SecurityGroup>> setContext(HttpRequest request) {
securityGroupHandler.setContext(request);
return super.setContext(request);
}
示例26
public void test() {
HttpRequest request = HttpRequest.builder().method("POST").endpoint("http://localhost").build();
request = binder.bindToRequest(request, ImmutableMap.<String, String>builder().put("one", "alpha").put("two", "beta").build());
assertEquals(request.getPayload().getRawContent(), "Tag.1.Key=one&Tag.1.Value=alpha&Tag.2.Key=two&Tag.2.Value=beta");
}
示例27
public void testEmpty() {
HttpRequest request = HttpRequest.builder().method("POST").endpoint("http://localhost").build();
request = binder.bindToRequest(request, ImmutableMap.<String, String>builder().put("empty", "").build());
assertEquals(request.getPayload().getRawContent(), "Tag.1.Key=empty&Tag.1.Value=");
}
示例28
@Test(expectedExceptions = IllegalArgumentException.class)
public void testMustBeArray() {
HttpRequest request = HttpRequest.builder().method("POST").endpoint("http://localhost").build();
binder.bindToRequest(request, new File("foo"));
}
示例29
@Test(expectedExceptions = NullPointerException.class)
public void testNullIsBad() {
HttpRequest request = HttpRequest.builder().method("GET").endpoint("http://momma").build();
binder.bindToRequest(request, null);
}
示例30
@Override
protected void checkFilters(HttpRequest request) {
assertEquals(request.getFilters().size(), 1);
assertTrue(request.getFilters().get(0) instanceof FormSignerV4);
}