| 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.savvis.vpdc.functions; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | import static org.jclouds.http.HttpUtils.releasePayload; |
| 23 | |
| 24 | import java.util.Set; |
| 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.savvis.vpdc.domain.Resource; |
| 38 | import org.jclouds.savvis.vpdc.domain.internal.VCloudSession; |
| 39 | import org.jclouds.savvis.vpdc.internal.VCloudToken; |
| 40 | import org.jclouds.savvis.vpdc.xml.OrgListHandler; |
| 41 | |
| 42 | import com.google.common.base.Function; |
| 43 | |
| 44 | /** |
| 45 | * This parses {@link VCloudSession} from HTTP headers. |
| 46 | * |
| 47 | * @author Adrian Cole |
| 48 | */ |
| 49 | @Singleton |
| 50 | public class ParseLoginResponseFromHeaders implements Function<HttpResponse, VCloudSession> { |
| 51 | static final Pattern pattern = Pattern.compile("vcloud-token=([^;]+);.*"); |
| 52 | |
| 53 | private final ParseSax.Factory factory; |
| 54 | private final Provider<OrgListHandler> orgHandlerProvider; |
| 55 | |
| 56 | @Inject |
| 57 | private ParseLoginResponseFromHeaders(Factory factory, |
| 58 | Provider<OrgListHandler> orgHandlerProvider) { |
| 59 | this.factory = factory; |
| 60 | this.orgHandlerProvider = orgHandlerProvider; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * parses the http response headers to create a new {@link VCloudSession} object. |
| 65 | */ |
| 66 | public VCloudSession apply(HttpResponse from) { |
| 67 | String cookieHeader = checkNotNull(from.getFirstHeaderOrNull(HttpHeaders.SET_COOKIE), |
| 68 | HttpHeaders.SET_COOKIE); |
| 69 | |
| 70 | final Matcher matcher = pattern.matcher(cookieHeader); |
| 71 | boolean matchFound = matcher.find(); |
| 72 | try { |
| 73 | if (matchFound) { |
| 74 | final Set<Resource> org = factory.create(orgHandlerProvider.get()).parse( |
| 75 | from.getPayload().getInput()); |
| 76 | |
| 77 | return new VCloudSession() { |
| 78 | @VCloudToken |
| 79 | public String getVCloudToken() { |
| 80 | return matcher.group(1); |
| 81 | } |
| 82 | |
| 83 | public Set<Resource> getOrgs() { |
| 84 | return org; |
| 85 | } |
| 86 | }; |
| 87 | |
| 88 | } |
| 89 | } finally { |
| 90 | releasePayload(from); |
| 91 | } |
| 92 | throw new HttpResponseException("not found ", null, from); |
| 93 | } |
| 94 | } |