| 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.vcloud.functions; |
| 20 | import static com.google.common.base.Preconditions.checkNotNull; |
| 21 | import static org.jclouds.http.HttpUtils.releasePayload; |
| 22 | |
| 23 | import java.util.Map; |
| 24 | import java.util.NoSuchElementException; |
| 25 | import java.util.regex.Matcher; |
| 26 | import java.util.regex.Pattern; |
| 27 | |
| 28 | import javax.inject.Inject; |
| 29 | import javax.inject.Provider; |
| 30 | import javax.inject.Singleton; |
| 31 | import javax.ws.rs.core.HttpHeaders; |
| 32 | |
| 33 | import org.jclouds.http.HttpResponse; |
| 34 | import org.jclouds.http.HttpResponseException; |
| 35 | import org.jclouds.http.functions.ParseSax; |
| 36 | import org.jclouds.http.functions.ParseSax.Factory; |
| 37 | import org.jclouds.vcloud.VCloudToken; |
| 38 | import org.jclouds.vcloud.domain.ReferenceType; |
| 39 | import org.jclouds.vcloud.domain.VCloudSession; |
| 40 | import org.jclouds.vcloud.endpoints.Org; |
| 41 | import org.jclouds.vcloud.xml.OrgListHandler; |
| 42 | |
| 43 | import com.google.common.base.Function; |
| 44 | import com.google.common.base.Predicates; |
| 45 | import com.google.common.collect.Iterables; |
| 46 | |
| 47 | /** |
| 48 | * This parses {@link VCloudSession} from HTTP headers. |
| 49 | * |
| 50 | * @author Adrian Cole |
| 51 | */ |
| 52 | @Singleton |
| 53 | public class ParseLoginResponseFromHeaders implements Function<HttpResponse, VCloudSession> { |
| 54 | static final Pattern pattern = Pattern.compile("(vcloud-token)=?([^;]+)(;.*)?"); |
| 55 | |
| 56 | private final ParseSax.Factory factory; |
| 57 | private final Provider<OrgListHandler> orgHandlerProvider; |
| 58 | |
| 59 | @Inject |
| 60 | private ParseLoginResponseFromHeaders(Factory factory, Provider<OrgListHandler> orgHandlerProvider) { |
| 61 | this.factory = factory; |
| 62 | this.orgHandlerProvider = orgHandlerProvider; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * parses the http response headers to create a new {@link VCloudSession} object. |
| 67 | */ |
| 68 | public VCloudSession apply(HttpResponse from) { |
| 69 | try { |
| 70 | final String token = parseTokenFromHeaders(from); |
| 71 | final Map<String, ReferenceType> org = factory.create(orgHandlerProvider.get()).parse( |
| 72 | checkNotNull(from.getPayload().getInput(), "no payload in http response to login request %s", from)); |
| 73 | |
| 74 | return new VCloudSession() { |
| 75 | @VCloudToken |
| 76 | public String getVCloudToken() { |
| 77 | return token; |
| 78 | } |
| 79 | |
| 80 | @Org |
| 81 | public Map<String, ReferenceType> getOrgs() { |
| 82 | return org; |
| 83 | } |
| 84 | }; |
| 85 | } finally { |
| 86 | releasePayload(from); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | public String parseTokenFromHeaders(HttpResponse from) { |
| 91 | String cookieHeader = from.getFirstHeaderOrNull("x-vcloud-authorization"); |
| 92 | if (cookieHeader != null) { |
| 93 | Matcher matcher = pattern.matcher(cookieHeader); |
| 94 | return matcher.find() ? matcher.group(2) : cookieHeader; |
| 95 | } else { |
| 96 | try { |
| 97 | cookieHeader = Iterables.find(from.getHeaders().get(HttpHeaders.SET_COOKIE), Predicates.contains(pattern)); |
| 98 | Matcher matcher = pattern.matcher(cookieHeader); |
| 99 | matcher.find(); |
| 100 | return matcher.group(2); |
| 101 | } catch (NoSuchElementException e) { |
| 102 | throw new HttpResponseException(String.format("Header %s or %s must be present", "x-vcloud-authorization", |
| 103 | HttpHeaders.SET_COOKIE), null, from); |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | } |