mirror of https://github.com/apache/jclouds.git
fixed regression on azureblob
This commit is contained in:
parent
ec589e5180
commit
1e017d47e1
|
@ -19,6 +19,8 @@
|
|||
|
||||
package org.jclouds.azureblob.handlers;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Singleton;
|
||||
|
||||
|
@ -31,6 +33,10 @@ import org.jclouds.http.HttpCommand;
|
|||
import org.jclouds.http.HttpResponse;
|
||||
import org.jclouds.rest.ResourceNotFoundException;
|
||||
|
||||
import com.google.common.base.Joiner;
|
||||
import com.google.common.base.Splitter;
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
/**
|
||||
* @author Adrian Cole
|
||||
*
|
||||
|
@ -43,22 +49,28 @@ public class ParseAzureBlobErrorFromXmlContent extends ParseAzureStorageErrorFro
|
|||
super(utils);
|
||||
}
|
||||
|
||||
protected Exception refineException(HttpCommand command, HttpResponse response, Exception exception, AzureStorageError error,
|
||||
String message) {
|
||||
protected Exception refineException(HttpCommand command, HttpResponse response, Exception exception,
|
||||
AzureStorageError error, String message) {
|
||||
switch (response.getStatusCode()) {
|
||||
case 404:
|
||||
if (!command.getCurrentRequest().getMethod().equals("DELETE")) {
|
||||
exception = new ResourceNotFoundException(message, exception);
|
||||
String container = command.getCurrentRequest().getEndpoint().getHost();
|
||||
String key = command.getCurrentRequest().getEndpoint().getPath();
|
||||
if (key == null || key.equals("/"))
|
||||
exception = new ContainerNotFoundException(container, message);
|
||||
else
|
||||
exception = new KeyNotFoundException(container, key, message);
|
||||
}
|
||||
return exception;
|
||||
default:
|
||||
return super.refineException(command, response, exception, error, message);
|
||||
case 404:
|
||||
if (!command.getCurrentRequest().getMethod().equals("DELETE")) {
|
||||
exception = new ResourceNotFoundException(message, exception);
|
||||
List<String> parts = Lists.newArrayList(Splitter.on('/').split(
|
||||
command.getCurrentRequest().getEndpoint().getPath()));
|
||||
parts.remove("");
|
||||
if (parts.size() > 0) {
|
||||
String container = parts.remove(0);
|
||||
String query = command.getCurrentRequest().getEndpoint().getQuery();
|
||||
if (query != null && query.indexOf("container") != -1) {
|
||||
exception = new ContainerNotFoundException(container, message);
|
||||
} else {
|
||||
exception = new KeyNotFoundException(container, Joiner.on('/').join(parts), message);
|
||||
}
|
||||
}
|
||||
}
|
||||
return exception;
|
||||
default:
|
||||
return super.refineException(command, response, exception, error, message);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,115 @@
|
|||
/**
|
||||
*
|
||||
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
|
||||
*
|
||||
* ====================================================================
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* ====================================================================
|
||||
*/
|
||||
|
||||
package org.jclouds.azure.storage.handlers;
|
||||
|
||||
import static org.easymock.EasyMock.expect;
|
||||
import static org.easymock.EasyMock.reportMatcher;
|
||||
import static org.easymock.classextension.EasyMock.createMock;
|
||||
import static org.easymock.classextension.EasyMock.replay;
|
||||
import static org.easymock.classextension.EasyMock.verify;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import org.easymock.IArgumentMatcher;
|
||||
import org.jclouds.azure.storage.filters.SharedKeyLiteAuthentication;
|
||||
import org.jclouds.azureblob.handlers.ParseAzureBlobErrorFromXmlContent;
|
||||
import org.jclouds.blobstore.ContainerNotFoundException;
|
||||
import org.jclouds.blobstore.KeyNotFoundException;
|
||||
import org.jclouds.http.HttpCommand;
|
||||
import org.jclouds.http.HttpRequest;
|
||||
import org.jclouds.http.HttpResponse;
|
||||
import org.jclouds.http.functions.config.SaxParserModule;
|
||||
import org.jclouds.io.Payloads;
|
||||
import org.jclouds.util.Strings2;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import com.google.inject.AbstractModule;
|
||||
import com.google.inject.Guice;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Adrian Cole
|
||||
*/
|
||||
@Test(groups = { "unit" })
|
||||
public class ParseAzureBlobErrorFromXmlContentTest {
|
||||
|
||||
@Test
|
||||
public void test404OnContainerIsContainerNotFound() {
|
||||
assertCodeMakes("GET", URI
|
||||
.create("https://jclouds.blob.core.windows.net/adriancole-azureblob-413790770?restype=container"), 404,
|
||||
"Not Found", "text/html; charset=us-ascii", "<HTML><HEAD><TITLE>Not Found</TITLE>\r\n",
|
||||
ContainerNotFoundException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test404WithoutContainerIsKeyNotFound() {
|
||||
assertCodeMakes("GET", URI.create("https://jclouds.blob.core.windows.net/adriancole-blobstore0/apples"), 404,
|
||||
"Not Found", "text/html; charset=us-ascii", "<HTML><HEAD><TITLE>Not Found</TITLE>\r\n",
|
||||
KeyNotFoundException.class);
|
||||
}
|
||||
|
||||
private void assertCodeMakes(String method, URI uri, int statusCode, String message, String contentType,
|
||||
String content, Class<? extends Exception> expected) {
|
||||
|
||||
ParseAzureBlobErrorFromXmlContent function = Guice.createInjector(new SaxParserModule(), new AbstractModule() {
|
||||
|
||||
@Override
|
||||
protected void configure() {
|
||||
bind(SharedKeyLiteAuthentication.class).toInstance(createMock(SharedKeyLiteAuthentication.class));
|
||||
}
|
||||
|
||||
}).getInstance(ParseAzureBlobErrorFromXmlContent.class);
|
||||
|
||||
HttpCommand command = createMock(HttpCommand.class);
|
||||
HttpRequest request = new HttpRequest(method, uri);
|
||||
HttpResponse response = new HttpResponse(statusCode, message, Payloads.newInputStreamPayload(Strings2
|
||||
.toInputStream(content)));
|
||||
response.getPayload().getContentMetadata().setContentType(contentType);
|
||||
|
||||
expect(command.getCurrentRequest()).andReturn(request).atLeastOnce();
|
||||
command.setException(classEq(expected));
|
||||
|
||||
replay(command);
|
||||
|
||||
function.handleError(command, response);
|
||||
|
||||
verify(command);
|
||||
}
|
||||
|
||||
public static Exception classEq(final Class<? extends Exception> in) {
|
||||
reportMatcher(new IArgumentMatcher() {
|
||||
|
||||
@Override
|
||||
public void appendTo(StringBuffer buffer) {
|
||||
buffer.append("classEq(");
|
||||
buffer.append(in);
|
||||
buffer.append(")");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matches(Object arg) {
|
||||
return arg.getClass() == in;
|
||||
}
|
||||
|
||||
});
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue