EMMA Coverage Report (generated Mon Oct 17 05:41:20 EDT 2011)
[all classes][org.jclouds.s3.binders]

COVERAGE SUMMARY FOR SOURCE FILE [BindAsHostPrefixIfConfigured.java]

nameclass, %method, %block, %line, %
BindAsHostPrefixIfConfigured.java100% (1/1)100% (2/2)98%  (106/108)99%  (18.9/19)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class BindAsHostPrefixIfConfigured100% (1/1)100% (2/2)98%  (106/108)99%  (18.9/19)
bindToRequest (HttpRequest, Object): HttpRequest 100% (1/1)98%  (91/93)99%  (12.9/13)
BindAsHostPrefixIfConfigured (BindAsHostPrefix, boolean, String, Provider): void 100% (1/1)100% (15/15)100% (6/6)

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 */
19package org.jclouds.s3.binders;
20 
21import static org.jclouds.s3.reference.S3Constants.PROPERTY_S3_SERVICE_PATH;
22import static org.jclouds.s3.reference.S3Constants.PROPERTY_S3_VIRTUAL_HOST_BUCKETS;
23 
24import javax.inject.Inject;
25import javax.inject.Named;
26import javax.inject.Provider;
27import javax.inject.Singleton;
28import javax.ws.rs.core.HttpHeaders;
29import javax.ws.rs.core.UriBuilder;
30 
31import org.jclouds.http.HttpRequest;
32import org.jclouds.http.utils.ModifyRequest;
33import org.jclouds.rest.Binder;
34import org.jclouds.rest.annotations.SkipEncoding;
35import org.jclouds.rest.binders.BindAsHostPrefix;
36import org.jclouds.s3.S3AsyncClient;
37import org.jclouds.util.Strings2;
38 
39import com.google.common.collect.ImmutableMap;
40 
41/**
42 * 
43 * @author Adrian Cole
44 */
45@Singleton
46public 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}

[all classes][org.jclouds.s3.binders]
EMMA 2.0.5312 (C) Vladimir Roubtsov