| 1 | /** |
| 2 | * |
| 3 | * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com> |
| 4 | * |
| 5 | * ==================================================================== |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * 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, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | * ==================================================================== |
| 18 | */ |
| 19 | package org.jclouds.blobstore.binders; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkArgument; |
| 22 | import static com.google.common.base.Preconditions.checkNotNull; |
| 23 | import static org.jclouds.blobstore.reference.BlobStoreConstants.PROPERTY_USER_METADATA_PREFIX; |
| 24 | |
| 25 | import java.util.Map; |
| 26 | |
| 27 | import javax.inject.Inject; |
| 28 | import javax.inject.Named; |
| 29 | import javax.inject.Singleton; |
| 30 | |
| 31 | import org.jclouds.http.HttpRequest; |
| 32 | import org.jclouds.http.utils.ModifyRequest; |
| 33 | import org.jclouds.rest.Binder; |
| 34 | import org.jclouds.util.Maps2; |
| 35 | |
| 36 | import com.google.common.base.Function; |
| 37 | import com.google.common.collect.Multimaps; |
| 38 | |
| 39 | /** |
| 40 | * |
| 41 | * @author Adrian Cole |
| 42 | */ |
| 43 | @Singleton |
| 44 | public class BindMapToHeadersWithPrefix implements Binder { |
| 45 | private final Function<String, String> FN; |
| 46 | |
| 47 | @Inject |
| 48 | public BindMapToHeadersWithPrefix(@Named(PROPERTY_USER_METADATA_PREFIX) final String metadataPrefix) { |
| 49 | checkNotNull(metadataPrefix, PROPERTY_USER_METADATA_PREFIX); |
| 50 | FN = new Function<String, String>() { |
| 51 | |
| 52 | @Override |
| 53 | public String apply(String arg0) { |
| 54 | String inLowercase = arg0.toLowerCase(); |
| 55 | return (inLowercase.startsWith(metadataPrefix)) ? inLowercase : metadataPrefix + inLowercase; |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public String toString() { |
| 60 | return "prefix: " + metadataPrefix; |
| 61 | } |
| 62 | |
| 63 | }; |
| 64 | |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public <R extends HttpRequest> R bindToRequest(R request, Object input) { |
| 69 | checkArgument(checkNotNull(input, "input") instanceof Map<?,?>, "this binder is only valid for Maps!"); |
| 70 | checkNotNull(request, "request"); |
| 71 | |
| 72 | @SuppressWarnings("unchecked") |
| 73 | Map<String, String> userMetadata = Maps2.transformKeys((Map<String, String>) input, FN); |
| 74 | return ModifyRequest.putHeaders(request, Multimaps.forMap(userMetadata)); |
| 75 | } |
| 76 | |
| 77 | } |