Issue 429: adjusted regex to accept both mosso and swift syntax

This commit is contained in:
Adrian Cole 2010-12-18 12:36:32 +01:00
parent fa736f3467
commit a4501eb9ca
3 changed files with 71 additions and 72 deletions

View File

@ -46,9 +46,9 @@ import org.jclouds.util.Utils;
public class ParseCloudFilesErrorFromHttpResponse implements HttpErrorHandler { public class ParseCloudFilesErrorFromHttpResponse implements HttpErrorHandler {
@Resource @Resource
protected Logger logger = Logger.NULL; protected Logger logger = Logger.NULL;
public static final String MOSSO_PREFIX = "^/v1[^/]*/MossoCloudFS_[^/]+/"; public static final String PREFIX = "^/v[0-9][^/]*/[a-zA-Z]+_[^/]+/";
public static final Pattern CONTAINER_PATH = Pattern.compile(MOSSO_PREFIX + "([^/]+)$"); public static final Pattern CONTAINER_PATH = Pattern.compile(PREFIX + "([^/]+)$");
public static final Pattern CONTAINER_KEY_PATH = Pattern.compile(MOSSO_PREFIX + "([^/]+)/(.*)"); public static final Pattern CONTAINER_KEY_PATH = Pattern.compile(PREFIX + "([^/]+)/(.*)");
public void handleError(HttpCommand command, HttpResponse response) { public void handleError(HttpCommand command, HttpResponse response) {
Exception exception = new HttpResponseException(command, response); Exception exception = new HttpResponseException(command, response);

View File

@ -29,118 +29,116 @@ import static org.jclouds.rackspace.reference.RackspaceHeaders.STORAGE_URL;
import java.net.URI; import java.net.URI;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.inject.Singleton; import javax.inject.Inject;
import javax.inject.Provider;
import javax.ws.rs.core.UriBuilder;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse; import org.jclouds.http.HttpResponse;
import org.jclouds.logging.Logger; import org.jclouds.logging.Logger;
import org.jclouds.rackspace.RackspaceAuthAsyncClient.AuthenticationResponse; import org.jclouds.rackspace.RackspaceAuthAsyncClient.AuthenticationResponse;
import org.jclouds.rest.InvocationContext;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.base.Objects;
/** /**
* This parses {@link AuthenticationResponse} from HTTP headers. * This parses {@link AuthenticationResponse} from HTTP headers.
* *
* @author Adrian Cole * @author Adrian Cole
*/ */
@Singleton
public class ParseAuthenticationResponseFromHeaders implements Function<HttpResponse, AuthenticationResponse> { public class ParseAuthenticationResponseFromHeaders implements Function<HttpResponse, AuthenticationResponse>,
InvocationContext {
public static final class AuthenticationResponseImpl implements AuthenticationResponse { public static final class AuthenticationResponseImpl implements AuthenticationResponse {
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((CDNManagementUrl == null) ? 0 : CDNManagementUrl.hashCode());
result = prime * result + ((authToken == null) ? 0 : authToken.hashCode());
result = prime * result + ((serverManagementUrl == null) ? 0 : serverManagementUrl.hashCode());
result = prime * result + ((storageUrl == null) ? 0 : storageUrl.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
AuthenticationResponseImpl other = (AuthenticationResponseImpl) obj;
if (CDNManagementUrl == null) {
if (other.CDNManagementUrl != null)
return false;
} else if (!CDNManagementUrl.equals(other.CDNManagementUrl))
return false;
if (authToken == null) {
if (other.authToken != null)
return false;
} else if (!authToken.equals(other.authToken))
return false;
if (serverManagementUrl == null) {
if (other.serverManagementUrl != null)
return false;
} else if (!serverManagementUrl.equals(other.serverManagementUrl))
return false;
if (storageUrl == null) {
if (other.storageUrl != null)
return false;
} else if (!storageUrl.equals(other.storageUrl))
return false;
return true;
}
private final String authToken; private final String authToken;
private final String CDNManagementUrl; private final URI CDNManagementUrl;
private final String serverManagementUrl; private final URI serverManagementUrl;
private final String storageUrl; private final URI storageUrl;
public AuthenticationResponseImpl(String authToken, String CDNManagementUrl, String serverManagementUrl, public AuthenticationResponseImpl(String authToken, URI CDNManagementUrl, URI serverManagementUrl, URI storageUrl) {
String storageUrl) {
this.authToken = authToken; this.authToken = authToken;
this.CDNManagementUrl = CDNManagementUrl; this.CDNManagementUrl = CDNManagementUrl;
this.serverManagementUrl = serverManagementUrl; this.serverManagementUrl = serverManagementUrl;
this.storageUrl = storageUrl; this.storageUrl = storageUrl;
} }
@Override
public String toString() {
return "[CDNManagementUrl=" + CDNManagementUrl + ", serverManagementUrl=" + serverManagementUrl
+ ", storageUrl=" + storageUrl + "]";
}
public String getAuthToken() { public String getAuthToken() {
return authToken; return authToken;
} }
public URI getCDNManagementUrl() { public URI getCDNManagementUrl() {
return URI.create(CDNManagementUrl); return CDNManagementUrl;
} }
public URI getServerManagementUrl() { public URI getServerManagementUrl() {
return URI.create(serverManagementUrl); return serverManagementUrl;
} }
public URI getStorageUrl() { public URI getStorageUrl() {
return URI.create(storageUrl); return storageUrl;
} }
// performance isn't a concern on a infrequent object like this, so using shortcuts;
@Override
public int hashCode() {
return Objects.hashCode(CDNManagementUrl, authToken, serverManagementUrl, storageUrl);
}
@Override
public boolean equals(Object that) {
if (that == null)
return false;
return Objects.equal(this.toString(), that.toString());
}
@Override
public String toString() {
return Objects.toStringHelper(this).add("CDNManagementUrl", CDNManagementUrl)
.add("serverManagementUrl", serverManagementUrl).add("storageUrl", storageUrl).toString();
}
} }
@Resource @Resource
protected Logger logger = Logger.NULL; protected Logger logger = Logger.NULL;
private final Provider<UriBuilder> uriBuilderProvider;
private String hostToReplace;
@Inject
public ParseAuthenticationResponseFromHeaders(Provider<UriBuilder> uriBuilderProvider) {
this.uriBuilderProvider = uriBuilderProvider;
}
/** /**
* parses the http response headers to create a new * parses the http response headers to create a new {@link AuthenticationResponse} object.
* {@link AuthenticationResponse} object.
*/ */
public AuthenticationResponse apply(HttpResponse from) { public AuthenticationResponse apply(HttpResponse from) {
releasePayload(from); releasePayload(from);
AuthenticationResponse response = new AuthenticationResponseImpl(checkNotNull(from AuthenticationResponse response = new AuthenticationResponseImpl(checkNotNull(
.getFirstHeaderOrNull(AUTH_TOKEN), AUTH_TOKEN), checkNotNull(from.getFirstHeaderOrNull(CDN_MANAGEMENT_URL), from.getFirstHeaderOrNull(AUTH_TOKEN), AUTH_TOKEN), getURI(from, CDN_MANAGEMENT_URL), getURI(from,
CDN_MANAGEMENT_URL), checkNotNull(from.getFirstHeaderOrNull(SERVER_MANAGEMENT_URL), SERVER_MANAGEMENT_URL), SERVER_MANAGEMENT_URL), getURI(from, STORAGE_URL));
checkNotNull(from.getFirstHeaderOrNull(STORAGE_URL), STORAGE_URL + " not found in headers:"
+ from.getStatusLine() + " - " + from.getHeaders()));
logger.debug("will connect to: ", response); logger.debug("will connect to: ", response);
return response; return response;
} }
protected URI getURI(HttpResponse from, String header) {
String headerValue = from.getFirstHeaderOrNull(header);
if (headerValue == null)
return null;
URI toReturn = URI.create(headerValue);
if (!"127.0.0.1".equals(toReturn.getHost()))
return toReturn;
return uriBuilderProvider.get().uri(toReturn).host(hostToReplace).build();
}
@Override
public Object setContext(HttpRequest request) {
hostToReplace = request.getEndpoint().getHost();
return this;
}
} }

View File

@ -19,6 +19,7 @@
package org.jclouds.rackspace.cloudservers; package org.jclouds.rackspace.cloudservers;
import java.net.URI;
import java.util.Date; import java.util.Date;
import org.jclouds.http.RequiresHttp; import org.jclouds.http.RequiresHttp;
@ -39,8 +40,8 @@ public class TestRackspaceAuthenticationRestClientModule extends RackspaceAuthen
@Override @Override
protected AuthenticationResponse provideAuthenticationResponse(Supplier<AuthenticationResponse> supplier) { protected AuthenticationResponse provideAuthenticationResponse(Supplier<AuthenticationResponse> supplier) {
return new AuthenticationResponseImpl("authToken", "http://CDNManagementUrl", "http://serverManagementUrl", return new AuthenticationResponseImpl("authToken", URI.create("http://CDNManagementUrl"),
"http://storageUrl"); URI.create("http://serverManagementUrl"), URI.create("http://storageUrl"));
} }
@Override @Override