| 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.http.functions; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkState; |
| 22 | import static org.jclouds.http.HttpUtils.releasePayload; |
| 23 | |
| 24 | import java.io.IOException; |
| 25 | import java.net.URI; |
| 26 | |
| 27 | import javax.inject.Inject; |
| 28 | import javax.inject.Provider; |
| 29 | import javax.ws.rs.core.HttpHeaders; |
| 30 | import javax.ws.rs.core.UriBuilder; |
| 31 | |
| 32 | import org.jclouds.http.HttpException; |
| 33 | import org.jclouds.http.HttpRequest; |
| 34 | import org.jclouds.http.HttpResponse; |
| 35 | import org.jclouds.http.HttpResponseException; |
| 36 | import org.jclouds.rest.InvocationContext; |
| 37 | import org.jclouds.util.Strings2; |
| 38 | |
| 39 | import com.google.common.base.Function; |
| 40 | |
| 41 | /** |
| 42 | * parses a single URI from a list |
| 43 | * |
| 44 | * @author Adrian Cole |
| 45 | */ |
| 46 | public 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 | } |