Merge pull request #64 in ITERATE/cyberduck from feature/windows/9074 to master
[cyberduck.git] / source / ch / cyberduck / core / openstack / SwiftRegionService.java
blob5518e0254f3ccdebe15ea1f5ccbefbca155d2fcf
1 package ch.cyberduck.core.openstack;
3 /*
4 * Copyright (c) 2002-2013 David Kocher. All rights reserved.
5 * http://cyberduck.ch/
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * Bug fixes, suggestions and comments should be sent to feedback@cyberduck.ch
20 import ch.cyberduck.core.Path;
21 import ch.cyberduck.core.PathContainerService;
22 import ch.cyberduck.core.exception.BackgroundException;
23 import ch.cyberduck.core.exception.InteroperabilityException;
24 import ch.cyberduck.core.features.Location;
26 import org.apache.commons.lang3.StringUtils;
27 import org.apache.log4j.Logger;
29 import ch.iterate.openstack.swift.model.Region;
31 /**
32 * @version $Id$
34 public class SwiftRegionService {
35 private static final Logger log = Logger.getLogger(SwiftRegionService.class);
37 private SwiftSession session;
39 private PathContainerService containerService
40 = new SwiftPathContainerService();
42 private SwiftLocationFeature location;
44 public SwiftRegionService(final SwiftSession session) {
45 this(session, new SwiftLocationFeature(session));
48 public SwiftRegionService(final SwiftSession session, final SwiftLocationFeature location) {
49 this.session = session;
50 this.location = location;
53 public Region lookup(final Path file) throws BackgroundException {
54 final Path container = containerService.getContainer(file);
55 if(Location.unknown.equals(new SwiftLocationFeature.SwiftRegion(container.attributes().getRegion()))) {
56 return this.lookup(location.getLocation(container));
58 return this.lookup(new SwiftLocationFeature.SwiftRegion(file.attributes().getRegion()));
61 public Region lookup(final Location.Name location) throws InteroperabilityException {
62 if(!session.isConnected()) {
63 log.warn("Cannot determine region if not connected");
64 return new Region(location.getIdentifier(), null, null);
66 for(Region region : session.getClient().getRegions()) {
67 if(StringUtils.isBlank(region.getRegionId())) {
68 continue;
70 if(region.getRegionId().equals(location.getIdentifier())) {
71 return region;
74 log.warn(String.format("Unknown region %s in authentication context", location));
75 if(session.getClient().getRegions().isEmpty()) {
76 throw new InteroperabilityException("No region found in authentication context");
78 for(Region region : session.getClient().getRegions()) {
79 if(region.isDefault()) {
80 log.warn(String.format("Fallback to default region %s", region.getRegionId()));
81 return region;
84 final Region region = session.getClient().getRegions().iterator().next();
85 log.warn(String.format("Fallback to first region %s", region.getRegionId()));
86 if(null == region.getStorageUrl()) {
87 throw new InteroperabilityException(String.format("No storage endpoint found for region %s", region.getRegionId()));
89 if(null == region.getCDNManagementUrl()) {
90 log.warn(String.format("No CDN management endpoint found for region %s", region.getRegionId()));
92 return region;