| 1 | /** |
| 2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
| 3 | * contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. jclouds licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.jclouds.atmos.util; |
| 20 | |
| 21 | import java.io.ByteArrayInputStream; |
| 22 | import java.io.InputStream; |
| 23 | |
| 24 | import javax.inject.Inject; |
| 25 | import javax.inject.Provider; |
| 26 | |
| 27 | import org.jclouds.atmos.AtmosClient; |
| 28 | import org.jclouds.atmos.blobstore.functions.BlobToObject; |
| 29 | import org.jclouds.atmos.domain.AtmosError; |
| 30 | import org.jclouds.atmos.filters.SignRequest; |
| 31 | import org.jclouds.atmos.options.PutOptions; |
| 32 | import org.jclouds.atmos.xml.ErrorHandler; |
| 33 | import org.jclouds.blobstore.domain.Blob; |
| 34 | import org.jclouds.crypto.Crypto; |
| 35 | import org.jclouds.http.HttpCommand; |
| 36 | import org.jclouds.http.HttpException; |
| 37 | import org.jclouds.http.HttpResponse; |
| 38 | import org.jclouds.http.functions.ParseSax; |
| 39 | import org.jclouds.util.Assertions; |
| 40 | |
| 41 | import com.google.common.base.Supplier; |
| 42 | |
| 43 | /** |
| 44 | * Encryption, Hashing, and IO Utilities needed to sign and verify Atmos Storage requests and |
| 45 | * responses. |
| 46 | * |
| 47 | * @author Adrian Cole |
| 48 | */ |
| 49 | public class AtmosUtils { |
| 50 | |
| 51 | @Inject |
| 52 | SignRequest signer; |
| 53 | |
| 54 | @Inject |
| 55 | ParseSax.Factory factory; |
| 56 | |
| 57 | @Inject |
| 58 | Provider<ErrorHandler> errorHandlerProvider; |
| 59 | |
| 60 | public AtmosError parseAtmosErrorFromContent(HttpCommand command, HttpResponse response, InputStream content) |
| 61 | throws HttpException { |
| 62 | AtmosError error = (AtmosError) factory.create(errorHandlerProvider.get()).parse(content); |
| 63 | if (error.getCode() == 1032) { |
| 64 | error.setStringSigned(signer.createStringToSign(command.getCurrentRequest())); |
| 65 | } |
| 66 | return error; |
| 67 | |
| 68 | } |
| 69 | |
| 70 | public static String putBlob(final AtmosClient sync, Crypto crypto, BlobToObject blob2Object, String container, |
| 71 | Blob blob, PutOptions options) { |
| 72 | final String path = container + "/" + blob.getMetadata().getName(); |
| 73 | deleteAndEnsureGone(sync, path); |
| 74 | sync.createFile(container, blob2Object.apply(blob), options); |
| 75 | return path; |
| 76 | } |
| 77 | |
| 78 | public static void deleteAndEnsureGone(final AtmosClient sync, final String path) { |
| 79 | try { |
| 80 | if (!Assertions.eventuallyTrue(new Supplier<Boolean>() { |
| 81 | public Boolean get() { |
| 82 | sync.deletePath(path); |
| 83 | return !sync.pathExists(path); |
| 84 | } |
| 85 | }, 3000)) { |
| 86 | throw new IllegalStateException(path + " still exists after deleting!"); |
| 87 | } |
| 88 | } catch (InterruptedException e) { |
| 89 | new IllegalStateException(path + " interrupted during deletion!", e); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | public AtmosError parseAtmosErrorFromContent(HttpCommand command, HttpResponse response, String content) |
| 94 | throws HttpException { |
| 95 | return parseAtmosErrorFromContent(command, response, new ByteArrayInputStream(content.getBytes())); |
| 96 | } |
| 97 | |
| 98 | public static String adjustContainerIfDirOptionPresent(String container, |
| 99 | org.jclouds.blobstore.options.ListContainerOptions options) { |
| 100 | if (options != org.jclouds.blobstore.options.ListContainerOptions.NONE) { |
| 101 | // if (options.isRecursive()) { |
| 102 | // throw new UnsupportedOperationException("recursive not currently supported in emcsaas"); |
| 103 | // } |
| 104 | if (options.getDir() != null) { |
| 105 | container = container + "/" + options.getDir(); |
| 106 | } |
| 107 | } |
| 108 | return container; |
| 109 | } |
| 110 | } |