public RegionToEndpointNegotiateVersion()

in openstack-glance/src/main/java/org/jclouds/openstack/glance/functions/RegionToEndpointNegotiateVersion.java [78:124]


   public RegionToEndpointNegotiateVersion(@Region Supplier<Map<String, Supplier<URI>>> regionToEndpointSupplier,
         @ApiVersion String rawApiVersionString, final HttpClient client, final Json json) {
      this.regionToEndpointSupplier = checkNotNull(regionToEndpointSupplier, "regionToEndpointSupplier");
      if (!rawApiVersionString.startsWith("v")) {
         this.apiVersion = "v" + rawApiVersionString;
      } else {
         this.apiVersion = rawApiVersionString;
      }
      this.endpointCache = CacheBuilder.newBuilder()
         .build(
            new CacheLoader<URI, URI>() {
               public URI load(URI baseEndpointUri) {
                  try {
                     List<String> baseEndpointPathParts = Splitter.on('/').omitEmptyStrings().splitToList(baseEndpointUri.getPath());
                     if (!baseEndpointPathParts.isEmpty()
                           && versionRegex.matcher(baseEndpointPathParts.get(baseEndpointPathParts.size() - 1)).matches()) {
                        // Constructs a base URI Glance endpoint by stripping the version from the received URI
                        baseEndpointUri = new URI(baseEndpointUri.getScheme(), baseEndpointUri.getUserInfo(),
                           baseEndpointUri.getHost(), baseEndpointUri.getPort(),
                           Joiner.on('/').join(baseEndpointPathParts.subList(0, baseEndpointPathParts.size() - 1)) + "/",
                           baseEndpointUri.getQuery(), baseEndpointUri.getFragment());
                     }

                     HttpRequest negotiationRequest = HttpRequest.builder()
                        .method("GET").endpoint(baseEndpointUri)
                        .addHeader(VERSION_NEGOTIATION_HEADER, "true").build();
                     InputStream response = client.invoke(negotiationRequest).getPayload().openStream();
                     VersionsJsonResponse versions = json.fromJson(Strings2.toStringAndClose(response), VersionsJsonResponse.class);
                     for (VersionsJsonResponse.Version version : versions.versions) {
                        if (apiVersion.equals(version.id)) {
                           // We only expect one element here, we'll get an exception here if that changes
                           URI versionedEndpointUri = new URI(Iterables.getOnlyElement(version.links).href);
                           return new URI(baseEndpointUri.getScheme(), versionedEndpointUri.getUserInfo(),
                              versionedEndpointUri.getHost(), versionedEndpointUri.getPort(),
                              versionedEndpointUri.getPath(), versionedEndpointUri.getQuery(),
                              versionedEndpointUri.getFragment());
                        }
                     }
                  } catch (URISyntaxException ex) {
                     throw Throwables.propagate(ex);
                  } catch (IOException ex) {
                     throw Throwables.propagate(ex);
                  }
                  throw new UnsupportedOperationException("Glance endpoint does not support API version: " + apiVersion);
              }
         });
   }