EMMA Coverage Report (generated Wed Aug 10 12:30:04 EDT 2011)
[all classes][org.jclouds.http.functions]

COVERAGE SUMMARY FOR SOURCE FILE [ParseURIFromListOrLocationHeaderIf20x.java]

nameclass, %method, %block, %line, %
ParseURIFromListOrLocationHeaderIf20x.java100% (1/1)100% (3/3)82%  (133/162)87%  (27.9/32)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ParseURIFromListOrLocationHeaderIf20x100% (1/1)100% (3/3)82%  (133/162)87%  (27.9/32)
apply (HttpResponse): URI 100% (1/1)81%  (122/151)85%  (22.9/27)
ParseURIFromListOrLocationHeaderIf20x (Provider): void 100% (1/1)100% (6/6)100% (3/3)
setContext (HttpRequest): ParseURIFromListOrLocationHeaderIf20x 100% (1/1)100% (5/5)100% (2/2)

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 */
19package org.jclouds.http.functions;
20 
21import static com.google.common.base.Preconditions.checkState;
22import static org.jclouds.http.HttpUtils.releasePayload;
23 
24import java.io.IOException;
25import java.net.URI;
26 
27import javax.inject.Inject;
28import javax.inject.Provider;
29import javax.ws.rs.core.HttpHeaders;
30import javax.ws.rs.core.UriBuilder;
31 
32import org.jclouds.http.HttpException;
33import org.jclouds.http.HttpRequest;
34import org.jclouds.http.HttpResponse;
35import org.jclouds.http.HttpResponseException;
36import org.jclouds.rest.InvocationContext;
37import org.jclouds.util.Strings2;
38 
39import com.google.common.base.Function;
40 
41/**
42 * parses a single URI from a list
43 * 
44 * @author Adrian Cole
45 */
46public class ParseURIFromListOrLocationHeaderIf20x implements Function<HttpResponse, URI>,
47      InvocationContext<ParseURIFromListOrLocationHeaderIf20x> {
48   private final Provider<UriBuilder> uriBuilderProvider;
49 
50   @Inject
51   ParseURIFromListOrLocationHeaderIf20x(Provider<UriBuilder> uriBuilderProvider) {
52      this.uriBuilderProvider = uriBuilderProvider;
53   }
54 
55   private HttpRequest request;
56 
57   public URI apply(HttpResponse from) {
58      if (from.getStatusCode() > 206)
59         throw new HttpException(String.format("Unhandled status code  - %1$s", from));
60      if ("text/uri-list".equals(from.getFirstHeaderOrNull(HttpHeaders.CONTENT_TYPE))) {
61         try {
62            if (from.getPayload().getInput() == null)
63               throw new HttpResponseException("no content", null, from);
64            String toParse = Strings2.toStringAndClose(from.getPayload().getInput());
65            return URI.create(toParse.trim());
66         } catch (IOException e) {
67            throw new HttpResponseException("couldn't parse uri from content", null, from, e);
68         } finally {
69            releasePayload(from);
70         }
71      } else {
72         releasePayload(from);
73         String location = from.getFirstHeaderOrNull(HttpHeaders.LOCATION);
74         if (location == null)
75            location = from.getFirstHeaderOrNull("location");
76         if (location != null) {
77            URI locationUri = URI.create(location);
78            if (locationUri.getHost() != null)
79               return locationUri;
80            checkState(request != null, "request should have been initialized");
81            if (!location.startsWith("/"))
82               location = "/" + location;
83            UriBuilder builder = uriBuilderProvider.get().uri(URI.create("http://localhost" + location));
84            builder.host(request.getEndpoint().getHost());
85            builder.port(request.getEndpoint().getPort());
86            builder.scheme(request.getEndpoint().getScheme());
87            return builder.build();
88         } else {
89            throw new HttpResponseException("no uri in headers or content", null, from);
90         }
91 
92      }
93   }
94 
95   @Override
96   public ParseURIFromListOrLocationHeaderIf20x setContext(HttpRequest request) {
97      this.request = request;
98      return this;
99   }
100}

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