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

COVERAGE SUMMARY FOR SOURCE FILE [ParseAuthenticationResponseFromHeaders.java]

nameclass, %method, %block, %line, %
ParseAuthenticationResponseFromHeaders.java100% (1/1)60%  (3/5)19%  (20/104)33%  (7/21)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ParseAuthenticationResponseFromHeaders100% (1/1)60%  (3/5)19%  (20/104)33%  (7/21)
apply (HttpResponse): OpenStackAuthAsyncClient$AuthenticationResponse 0%   (0/1)0%   (0/57)0%   (0/8)
getURI (String): URI 0%   (0/1)0%   (0/27)0%   (0/6)
ParseAuthenticationResponseFromHeaders (Provider): void 100% (1/1)100% (9/9)100% (4/4)
setContext (HttpRequest): ParseAuthenticationResponseFromHeaders 100% (1/1)100% (6/6)100% (1/1)
setHostToReplace (String): ParseAuthenticationResponseFromHeaders 100% (1/1)100% (5/5)100% (2/2)

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.openstack.functions;
20 
21import static com.google.common.base.Preconditions.checkNotNull;
22import static org.jclouds.http.HttpUtils.releasePayload;
23import static org.jclouds.openstack.reference.AuthHeaders.AUTH_TOKEN;
24import static org.jclouds.openstack.reference.AuthHeaders.URL_SUFFIX;
25 
26import java.net.URI;
27import java.util.Map.Entry;
28 
29import javax.annotation.Resource;
30import javax.inject.Inject;
31import javax.inject.Provider;
32import javax.ws.rs.core.UriBuilder;
33 
34import org.jclouds.http.HttpRequest;
35import org.jclouds.http.HttpResponse;
36import org.jclouds.logging.Logger;
37import org.jclouds.openstack.OpenStackAuthAsyncClient.AuthenticationResponse;
38import org.jclouds.rest.InvocationContext;
39 
40import com.google.common.annotations.VisibleForTesting;
41import com.google.common.base.Function;
42import com.google.common.collect.ImmutableMap;
43import com.google.common.collect.ImmutableMap.Builder;
44 
45/**
46 * This parses {@link AuthenticationResponse} from HTTP headers.
47 * 
48 * @author Adrian Cole
49 */
50public class ParseAuthenticationResponseFromHeaders implements Function<HttpResponse, AuthenticationResponse>,
51         InvocationContext<ParseAuthenticationResponseFromHeaders> {
52 
53   @Resource
54   protected Logger logger = Logger.NULL;
55 
56   private final Provider<UriBuilder> uriBuilderProvider;
57   private String hostToReplace;
58 
59   @Inject
60   public ParseAuthenticationResponseFromHeaders(Provider<UriBuilder> uriBuilderProvider) {
61      this.uriBuilderProvider = uriBuilderProvider;
62   }
63 
64   /**
65    * parses the http response headers to create a new {@link AuthenticationResponse} object.
66    */
67   public AuthenticationResponse apply(HttpResponse from) {
68      releasePayload(from);
69      Builder<String, URI> builder = ImmutableMap.<String, URI> builder();
70      for (Entry<String, String> entry : from.getHeaders().entries()) {
71         if (entry.getKey().endsWith(URL_SUFFIX))
72            builder.put(entry.getKey(), getURI(entry.getValue()));
73      }
74      AuthenticationResponse response = new AuthenticationResponse(checkNotNull(from.getFirstHeaderOrNull(AUTH_TOKEN),
75               AUTH_TOKEN), builder.build());
76      logger.debug("will connect to: ", response);
77      return response;
78   }
79 
80   // TODO: find the swift configuration or bug related to returning localhost
81   protected URI getURI(String headerValue) {
82      if (headerValue == null)
83         return null;
84      URI toReturn = URI.create(headerValue);
85      if (!"127.0.0.1".equals(toReturn.getHost()))
86         return toReturn;
87      return uriBuilderProvider.get().uri(toReturn).host(hostToReplace).build();
88   }
89 
90   @Override
91   public ParseAuthenticationResponseFromHeaders setContext(HttpRequest request) {
92      return setHostToReplace(request.getEndpoint().getHost());
93   }
94 
95   @VisibleForTesting
96   ParseAuthenticationResponseFromHeaders setHostToReplace(String hostToReplace) {
97      this.hostToReplace = hostToReplace;
98      return this;
99   }
100}

[all classes][org.jclouds.openstack.functions]
EMMA 2.0.5312 (C) Vladimir Roubtsov