| 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.s3.binders; |
| 20 | |
| 21 | import static org.jclouds.s3.reference.S3Constants.PROPERTY_S3_SERVICE_PATH; |
| 22 | import static org.jclouds.s3.reference.S3Constants.PROPERTY_S3_VIRTUAL_HOST_BUCKETS; |
| 23 | |
| 24 | import javax.inject.Inject; |
| 25 | import javax.inject.Named; |
| 26 | import javax.inject.Provider; |
| 27 | import javax.inject.Singleton; |
| 28 | import javax.ws.rs.core.HttpHeaders; |
| 29 | import javax.ws.rs.core.UriBuilder; |
| 30 | |
| 31 | import org.jclouds.http.HttpRequest; |
| 32 | import org.jclouds.http.utils.ModifyRequest; |
| 33 | import org.jclouds.rest.Binder; |
| 34 | import org.jclouds.rest.annotations.SkipEncoding; |
| 35 | import org.jclouds.rest.binders.BindAsHostPrefix; |
| 36 | import org.jclouds.s3.S3AsyncClient; |
| 37 | import org.jclouds.util.Strings2; |
| 38 | |
| 39 | import com.google.common.collect.ImmutableMap; |
| 40 | |
| 41 | /** |
| 42 | * |
| 43 | * @author Adrian Cole |
| 44 | */ |
| 45 | @Singleton |
| 46 | public class BindAsHostPrefixIfConfigured implements Binder { |
| 47 | |
| 48 | protected final Provider<UriBuilder> uriBuilderProvider; |
| 49 | protected final BindAsHostPrefix bindAsHostPrefix; |
| 50 | protected final boolean isVhostStyle; |
| 51 | protected final String servicePath; |
| 52 | |
| 53 | @Inject |
| 54 | public BindAsHostPrefixIfConfigured(BindAsHostPrefix bindAsHostPrefix, |
| 55 | @Named(PROPERTY_S3_VIRTUAL_HOST_BUCKETS) boolean isVhostStyle, |
| 56 | @Named(PROPERTY_S3_SERVICE_PATH) String servicePath, Provider<UriBuilder> uriBuilderProvider) { |
| 57 | this.bindAsHostPrefix = bindAsHostPrefix; |
| 58 | this.isVhostStyle = isVhostStyle; |
| 59 | this.servicePath = servicePath; |
| 60 | this.uriBuilderProvider = uriBuilderProvider; |
| 61 | } |
| 62 | |
| 63 | @SuppressWarnings("unchecked") |
| 64 | @Override |
| 65 | public <R extends HttpRequest> R bindToRequest(R request, Object payload) { |
| 66 | if (isVhostStyle) { |
| 67 | request = bindAsHostPrefix.bindToRequest(request, payload); |
| 68 | return ModifyRequest.replaceHeader(request, HttpHeaders.HOST, request.getEndpoint().getHost()); |
| 69 | } else { |
| 70 | UriBuilder builder = uriBuilderProvider.get().uri(request.getEndpoint()); |
| 71 | StringBuilder path = new StringBuilder(Strings2.urlEncode(request.getEndpoint().getPath(), S3AsyncClient.class |
| 72 | .getAnnotation(SkipEncoding.class).value())); |
| 73 | int indexToInsert = 0; |
| 74 | if (!servicePath.equals("/")) { |
| 75 | indexToInsert = path.indexOf(servicePath); |
| 76 | indexToInsert = indexToInsert == -1 ? 0 : indexToInsert; |
| 77 | indexToInsert += servicePath.length(); |
| 78 | } |
| 79 | path.insert(indexToInsert, "/" + payload.toString()); |
| 80 | builder.replacePath(path.toString()); |
| 81 | return (R) request.toBuilder().endpoint(builder.buildFromEncodedMap(ImmutableMap.<String, Object> of())) |
| 82 | .build(); |
| 83 | } |
| 84 | } |
| 85 | } |