public void handleError(HttpCommand command, HttpResponse response) { String content; try { content = response.getContent() != null ? Utils.toStringAndClose(response.getContent()) : null; if (content != null) { try { if (content.indexOf('<') >= 0) { AzureStorageError error = utils.parseAzureStorageErrorFromContent(command, response, content); command.setException(new AzureStorageResponseException(command, response, error)); } else { command.setException(new HttpResponseException(command, response, content)); } } catch (Exception he) { command.setException(new HttpResponseException(command, response, content)); Utils.rethrowIfRuntime(he); } } else { command.setException(new HttpResponseException(command, response)); } } catch (Exception e) { command.setException(new HttpResponseException(command, response)); Utils.rethrowIfRuntime(e); } }
AtmosStorageError parseErrorFromContentOrNull(HttpCommand command, HttpResponse response) { if (response.getContent() != null) { try { String content = Utils.toStringAndClose(response.getContent()); if (content != null && content.indexOf('<') >= 0) return utils.parseAtmosStorageErrorFromContent( command, response, Utils.toInputStream(content)); } catch (IOException e) { logger.warn(e, "exception reading error from response", response); } } return null; }
/** Specifies the keypair used to run instances with */ public TerremarkVCloudTemplateOptions sshKeyFingerprint(String keyPair) { checkNotNull(keyPair, "use noKeyPair option to request boot without a keypair"); checkState(!noKeyPair, "you cannot specify both options keyPair and noKeyPair"); Utils.checkNotEmpty(keyPair, "keypair must be non-empty"); this.keyPair = keyPair; return this; }
private static void verifyMetadata(String metadataValue, AtmosObject getBlob) { assertEquals(getBlob.getContentMetadata().getContentLength(), new Long(16)); assert getBlob.getContentMetadata().getContentType().startsWith("text/plain"); assertEquals(getBlob.getUserMetadata().getMetadata().get("Metadata"), metadataValue); SystemMetadata md = getBlob.getSystemMetadata(); assertEquals(md.getSize(), 16); assert md.getGroupID() != null; assertEquals(md.getHardLinkCount(), 1); assert md.getInceptionTime() != null; assert md.getLastAccessTime() != null; assert md.getLastMetadataModification() != null; assert md.getLastUserDataModification() != null; assert md.getObjectID() != null; assertEquals(md.getObjectName(), "object"); assert md.getPolicyName() != null; assertEquals(md.getType(), FileType.REGULAR); assert md.getUserID() != null; try { Utils.toStringAndClose( URI.create( "http://accesspoint.emccis.com/rest/objects/" + getBlob.getSystemMetadata().getObjectID()) .toURL() .openStream()); assert false : "shouldn't have worked, since it is private"; } catch (IOException e) { } }
private void checkSecretKeyFile(String secretKeyFile) throws FileNotFoundException { Utils.checkNotEmpty( secretKeyFile, "System property: [jclouds.test.ssh.keyfile] set to an empty string"); if (!new File(secretKeyFile).exists()) { throw new FileNotFoundException("secretKeyFile not found at: " + secretKeyFile); } }
public void testAddNodeOptions() throws SecurityException, NoSuchMethodException, IOException { Method method = TerremarkVCloudAsyncClient.class.getMethod( "addNode", int.class, String.class, String.class, int.class, Array.newInstance(AddNodeOptions.class, 0).getClass()); GeneratedHttpRequest<TerremarkVCloudAsyncClient> httpMethod = processor.createRequest( method, 12, "10.2.2.2", "name", 22, AddNodeOptions.Builder.disabled().withDescription("yahoo")); assertRequestLineEquals( httpMethod, "POST http://vcloud/extensions/internetService/12/nodeServices HTTP/1.1"); assertHeadersEqual( httpMethod, "Accept: application/vnd.tmrk.vCloud.nodeService+xml\nContent-Length: 333\nContent-Type: application/vnd.tmrk.vCloud.nodeService+xml\n"); assertPayloadEquals( httpMethod, Utils.toStringAndClose( getClass().getResourceAsStream("/terremark/CreateNodeService-options-test.xml"))); assertResponseParserClassEquals(method, httpMethod, ParseSax.class); assertSaxResponseParserClassEquals(method, NodeHandler.class); assertExceptionParserClassEquals(method, null); checkFilters(httpMethod); }
public void testAddInternetServiceToExistingIp() throws SecurityException, NoSuchMethodException, IOException { Method method = TerremarkVCloudAsyncClient.class.getMethod( "addInternetServiceToExistingIp", int.class, String.class, Protocol.class, int.class, Array.newInstance(AddInternetServiceOptions.class, 0).getClass()); GeneratedHttpRequest<TerremarkVCloudAsyncClient> httpMethod = processor.createRequest(method, 12, "name", Protocol.TCP, 22); assertRequestLineEquals( httpMethod, "POST http://vcloud/extensions/publicIp/12/internetServices HTTP/1.1"); assertHeadersEqual( httpMethod, "Accept: application/vnd.tmrk.vCloud.internetService+xml\nContent-Length: 298\nContent-Type: application/vnd.tmrk.vCloud.internetService+xml\n"); assertPayloadEquals( httpMethod, Utils.toStringAndClose( getClass().getResourceAsStream("/terremark/CreateInternetService-test2.xml"))); assertResponseParserClassEquals(method, httpMethod, ParseSax.class); assertSaxResponseParserClassEquals(method, InternetServiceHandler.class); assertExceptionParserClassEquals(method, null); checkFilters(httpMethod); }
public void testInstantiateVAppTemplate() throws SecurityException, NoSuchMethodException, IOException { Method method = TerremarkVCloudAsyncClient.class.getMethod( "instantiateVAppTemplateInVDC", String.class, String.class, String.class, Array.newInstance(InstantiateVAppTemplateOptions.class, 0).getClass()); GeneratedHttpRequest<TerremarkVCloudAsyncClient> httpMethod = processor.createRequest(method, "1", "name", 3 + ""); assertRequestLineEquals( httpMethod, "POST http://vcloud/vdc/1/action/instantiateVAppTemplate HTTP/1.1"); assertHeadersEqual( httpMethod, "Accept: application/vnd.vmware.vcloud.vApp+xml\nContent-Length: 687\nContent-Type: application/vnd.vmware.vcloud.instantiateVAppTemplateParams+xml\n"); assertPayloadEquals( httpMethod, Utils.toStringAndClose( getClass().getResourceAsStream("/terremark/InstantiateVAppTemplateParams-test.xml"))); assertResponseParserClassEquals(method, httpMethod, ParseSax.class); assertSaxResponseParserClassEquals(method, VAppHandler.class); assertExceptionParserClassEquals(method, null); checkFilters(httpMethod); }
private void calculateAndReplaceAuthHeader(HttpRequest request, String toSign) throws HttpException { String signature = signString(toSign); if (signatureWire.enabled()) signatureWire.input(Utils.toInputStream(signature)); request .getHeaders() .replaceValues(AtmosStorageHeaders.SIGNATURE, Collections.singletonList(signature)); }
public static byte[] hmac(String toEncode, byte[] key, Digest digest) { HMac hmac = new HMac(digest); byte[] resBuf = new byte[hmac.getMacSize()]; byte[] plainBytes = Utils.encodeString(toEncode); byte[] keyBytes = key; hmac.init(new KeyParameter(keyBytes)); hmac.update(plainBytes, 0, plainBytes.length); hmac.doFinal(resBuf, 0); return resBuf; }
protected void deleteConsistencyAware(final String path) throws InterruptedException, ExecutionException, TimeoutException { try { connection.deletePath(path); } catch (KeyNotFoundException ex) { } assert Utils.enventuallyTrue( new Supplier<Boolean>() { public Boolean get() { return !connection.pathExists(path); } }, INCONSISTENCY_WINDOW); }
private void appendCanonicalizedHeaders(HttpRequest request, StringBuilder toSign) { // TreeSet == Sort the headers alphabetically. Set<String> headers = new TreeSet<String>(request.getHeaders().keySet()); for (String header : headers) { if (header.startsWith("x-emc-") && !header.equals(AtmosStorageHeaders.SIGNATURE)) { // Convert all header names to lowercase. toSign.append(header.toLowerCase()).append(":"); // For headers with values that span multiple lines, convert them into one line by // replacing any // newline characters and extra embedded white spaces in the value. for (String value : request.getHeaders().get(header)) { value = Utils.replaceAll(value, TWO_SPACE_PATTERN, " "); value = Utils.replaceAll(value, NEWLINE_PATTERN, ""); toSign.append(value).append(' '); } toSign.deleteCharAt(toSign.lastIndexOf(" ")); // Concatenate all headers together, using newlines (\n) separating each header from the // next one. toSign.append("\n"); } } // There should be no terminating newline character at the end of the last header. if (toSign.charAt(toSign.length() - 1) == '\n') toSign.deleteCharAt(toSign.length() - 1); }
/** * Creates a new remote context. * * @param provider * @param account nullable, if credentials are present in the overrides * @param key nullable, if credentials are present in the overrides * @param modules Configuration you'd like to pass to the context. Ex. ImmutableSet.<Module>of(new * ExecutorServiceModule(myexecutor)) * @param overrides properties to override defaults with. * @return initialized context ready for use */ @SuppressWarnings("unchecked") public T createContext( String provider, @Nullable String account, @Nullable String key, Iterable<? extends Module> modules, Properties overrides) { checkNotNull(provider, "provider"); checkNotNull(modules, "modules"); checkNotNull(overrides, "overrides"); String propertiesBuilderKey = String.format("%s.propertiesbuilder", provider); String propertiesBuilderClassName = checkNotNull( properties.getProperty(propertiesBuilderKey), provider + " service not supported"); String contextBuilderKey = String.format("%s.contextbuilder", provider); String contextBuilderClassName = checkNotNull(properties.getProperty(contextBuilderKey), contextBuilderKey); String endpointKey = String.format("%s.endpoint", provider); String endpoint = properties.getProperty(endpointKey); try { Class<PropertiesBuilder> propertiesBuilderClass = (Class<PropertiesBuilder>) Class.forName(propertiesBuilderClassName); Class<B> contextBuilderClass = (Class<B>) Class.forName(contextBuilderClassName); PropertiesBuilder builder = propertiesBuilderClass.getConstructor(Properties.class).newInstance(overrides); if (key != null) builder.withCredentials(account, key); if (endpoint != null) builder.withEndpoint(URI.create(endpoint)); B contextBuilder = (B) contextBuilderClass .getConstructor(String.class, Properties.class) .newInstance(provider, builder.build()) .withModules(Iterables.toArray(modules, Module.class)); return build(contextBuilder); } catch (ProvisionException e) { Throwable throwable = Utils.firstRootCauseOrOriginalException(e); Throwables.propagate(throwable); assert false : "exception should have propogated " + e; return null; } catch (Exception e) { Throwables.propagate(Throwables.getRootCause(e)); assert false : "exception should have propogated " + e; return null; } }
public void testComplete() throws IOException { CreateDriveRequest input = new CreateDriveRequest.Builder() .name("Ubuntu 10.10 Server Edition Linux 64bit Preinstalled System") // .size(8589934592l) // .claimType(ClaimType.SHARED) // .readers(ImmutableSet.of("ffffffff-ffff-ffff-ffff-ffffffffffff")) // .tags(ImmutableSet.of("tag1", "tag2")) .userMetadata(ImmutableMap.of("foo", "bar", "baz", "raz")) // .encryptionCipher("aes-xts-plain") .avoid(ImmutableSet.of("avoid1")) .build(); HttpRequest request = new HttpRequest("POST", URI.create("https://host/drives/create")); FN.bindToRequest(request, input); assertEquals(request.getPayload().getContentMetadata().getContentType(), MediaType.TEXT_PLAIN); assertEquals( request.getPayload().getRawContent(), Utils.toStringAndClose( BindDriveToPlainTextStringTest.class.getResourceAsStream("/create_drive.txt"))); }
@Test( timeOut = 5 * 60 * 1000, dependsOnMethods = {"testCreateContainer", "testCreatePublicContainer"}) public void testObjectOperations() throws Exception { String data = "Here is my data"; // Test PUT with string data, ETag hash, and a piece of metadata AzureBlob object = client.newBlob(); object.getProperties().setName("object"); object.setPayload(data); Payloads.calculateMD5(object); object.getProperties().getContentMetadata().setContentType("text/plain"); object.getProperties().getMetadata().put("mykey", "metadata-value"); byte[] md5 = object.getProperties().getContentMetadata().getContentMD5(); String newEtag = client.putBlob(privateContainer, object); assertEquals( CryptoStreams.hex(md5), CryptoStreams.hex(object.getProperties().getContentMetadata().getContentMD5())); // Test HEAD of missing object assert client.getBlobProperties(privateContainer, "non-existent-object") == null; // Test HEAD of object BlobProperties metadata = client.getBlobProperties(privateContainer, object.getProperties().getName()); // TODO assertEquals(metadata.getName(), object.getProperties().getName()); // we can't check this while hacking around lack of content-md5, as GET of the first byte will // show incorrect length 1, the returned size, as opposed to the real length. This is an ok // tradeoff, as a container list will contain the correct size of the objects in an // inexpensive fashion // http://code.google.com/p/jclouds/issues/detail?id=92 // assertEquals(metadata.getSize(), data.length()); assertEquals(metadata.getContentMetadata().getContentType(), "text/plain"); // Azure doesn't return the Content-MD5 on head request.. assertEquals( CryptoStreams.hex(md5), CryptoStreams.hex(object.getProperties().getContentMetadata().getContentMD5())); assertEquals(metadata.getETag(), newEtag); assertEquals(metadata.getMetadata().entrySet().size(), 1); assertEquals(metadata.getMetadata().get("mykey"), "metadata-value"); // // Test POST to update object's metadata // Multimap<String, String> userMetadata = LinkedHashMultimap.create(); // userMetadata.put("New-Metadata-1", "value-1"); // userMetadata.put("New-Metadata-2", "value-2"); // assertTrue(client.setBlobProperties(privateContainer, object.getProperties().getName(), // userMetadata)); // Test GET of missing object assert client.getBlob(privateContainer, "non-existent-object") == null; // Test GET of object (including updated metadata) AzureBlob getBlob = client.getBlob(privateContainer, object.getProperties().getName()); assertEquals(Utils.toStringAndClose(getBlob.getPayload().getInput()), data); // TODO assertEquals(getBlob.getName(), object.getProperties().getName()); assertEquals( getBlob.getPayload().getContentMetadata().getContentLength(), new Long(data.length())); assertEquals(getBlob.getProperties().getContentMetadata().getContentType(), "text/plain"); assertEquals( CryptoStreams.hex(md5), CryptoStreams.hex(getBlob.getProperties().getContentMetadata().getContentMD5())); assertEquals(newEtag, getBlob.getProperties().getETag()); // wait until we can update metadata // assertEquals(getBlob.getProperties().getMetadata().entries().size(), 2); // assertEquals( // Iterables.getLast(getBlob.getProperties().getMetadata().get("New-Metadata-1")), // "value-1"); // assertEquals( // Iterables.getLast(getBlob.getProperties().getMetadata().get("New-Metadata-2")), // "value-2"); assertEquals(metadata.getMetadata().entrySet().size(), 1); assertEquals(metadata.getMetadata().get("mykey"), "metadata-value"); // test listing ListBlobsResponse response = client.listBlobs( privateContainer, ListBlobsOptions.Builder.prefix( object .getProperties() .getName() .substring(0, object.getProperties().getName().length() - 1)) .maxResults(1) .includeMetadata()); assertEquals(response.size(), 1); assertEquals(Iterables.getOnlyElement(response).getName(), object.getProperties().getName()); assertEquals( Iterables.getOnlyElement(response).getMetadata(), ImmutableMap.of("mykey", "metadata-value")); // Test PUT with invalid ETag (as if object's data was corrupted in transit) String correctEtag = newEtag; String incorrectEtag = "0" + correctEtag.substring(1); object.getProperties().setETag(incorrectEtag); try { client.putBlob(privateContainer, object); } catch (Throwable e) { assertEquals(e.getCause().getClass(), HttpResponseException.class); assertEquals(((HttpResponseException) e.getCause()).getResponse().getStatusCode(), 422); } ByteArrayInputStream bais = new ByteArrayInputStream(data.getBytes("UTF-8")); object = client.newBlob(); object.getProperties().setName("chunked-object"); object.setPayload(bais); object.getPayload().getContentMetadata().setContentLength(new Long(data.getBytes().length)); newEtag = client.putBlob(privateContainer, object); assertEquals( CryptoStreams.hex(md5), CryptoStreams.hex(getBlob.getProperties().getContentMetadata().getContentMD5())); // Test GET with options // Non-matching ETag try { client.getBlob( privateContainer, object.getProperties().getName(), GetOptions.Builder.ifETagDoesntMatch(newEtag)); } catch (Exception e) { assertEquals(e.getCause().getClass(), HttpResponseException.class); assertEquals(((HttpResponseException) e.getCause()).getResponse().getStatusCode(), 304); } // Matching ETag TODO this shouldn't fail!!! try { getBlob = client.getBlob( privateContainer, object.getProperties().getName(), GetOptions.Builder.ifETagMatches(newEtag)); assertEquals(getBlob.getProperties().getETag(), newEtag); } catch (HttpResponseException e) { assertEquals(e.getResponse().getStatusCode(), 412); } // Range // doesn't work per // http://social.msdn.microsoft.com/Forums/en-US/windowsazure/thread/479fa63f-51df-4b66-96b5-33ae362747b6 // getBlob = client // .getBlob(privateContainer, object.getProperties().getName(), // GetOptions.Builder.startAt(8)).get(120, // TimeUnit.SECONDS); // assertEquals(Utils.toStringAndClose((InputStream) getBlob.getData()), data.substring(8)); client.deleteBlob(privateContainer, "object"); client.deleteBlob(privateContainer, "chunked-object"); }
@Test public void testSupportedProviders() { Iterable<String> providers = Utils.getSupportedProviders(); assert Iterables.contains(providers, "slicehost") : providers; }