mirror of https://github.com/apache/jclouds.git
Replaced IllegalStateException with KeyAlreadyExistsException and fixed '\n' typo
This commit is contained in:
parent
03dc864115
commit
0d8848b397
|
@ -45,7 +45,6 @@ import org.jclouds.atmos.functions.ParseSystemMetadataFromHeaders;
|
|||
import org.jclouds.atmos.functions.ParseUserMetadataFromHeaders;
|
||||
import org.jclouds.atmos.functions.ReturnEndpointIfAlreadyExists;
|
||||
import org.jclouds.atmos.functions.ReturnTrueIfGroupACLIsOtherRead;
|
||||
import org.jclouds.atmos.functions.ThrowIllegalStateExceptionOn400;
|
||||
import org.jclouds.atmos.options.ListOptions;
|
||||
import org.jclouds.atmos.options.PutOptions;
|
||||
import org.jclouds.blobstore.functions.ThrowContainerNotFoundOn404;
|
||||
|
@ -59,6 +58,7 @@ import org.jclouds.rest.annotations.RequestFilters;
|
|||
import org.jclouds.rest.annotations.ResponseParser;
|
||||
import org.jclouds.rest.annotations.SkipEncoding;
|
||||
import org.jclouds.rest.functions.ReturnFalseOnNotFoundOr404;
|
||||
|
||||
import org.jclouds.rest.functions.ReturnNullOnNotFoundOr404;
|
||||
import org.jclouds.rest.functions.ReturnVoidOnNotFoundOr404;
|
||||
|
||||
|
@ -117,7 +117,6 @@ public interface AtmosAsyncClient {
|
|||
*/
|
||||
@POST
|
||||
@Path("/{parent}/{name}")
|
||||
@ExceptionParser(ThrowIllegalStateExceptionOn400.class)
|
||||
@Consumes(MediaType.WILDCARD)
|
||||
ListenableFuture<URI> createFile(
|
||||
@PathParam("parent") String parent,
|
||||
|
|
|
@ -1,71 +0,0 @@
|
|||
/**
|
||||
* Licensed to jclouds, Inc. (jclouds) under one or more
|
||||
* contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. jclouds licenses this file
|
||||
* to you 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.atmos.functions;
|
||||
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.base.Throwables;
|
||||
import com.google.inject.Inject;
|
||||
import org.jclouds.atmos.domain.AtmosError;
|
||||
import org.jclouds.atmos.util.AtmosUtils;
|
||||
import org.jclouds.http.HttpCommand;
|
||||
import org.jclouds.http.HttpResponse;
|
||||
import org.jclouds.http.HttpResponseException;
|
||||
import org.jclouds.http.HttpUtils;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
|
||||
/**
|
||||
* @author Andrei Savu
|
||||
* @see Error codes section at <a href="https://www.synaptic.att.com/assets/us/en/home/Atmos_Programmers_Guide_1.3.4A.pdf" />
|
||||
*/
|
||||
public class ThrowIllegalStateExceptionOn400 implements Function<Exception, Object> {
|
||||
|
||||
private final AtmosUtils utils;
|
||||
|
||||
@Inject
|
||||
public ThrowIllegalStateExceptionOn400(AtmosUtils utils) {
|
||||
this.utils = checkNotNull(utils, "utils is null");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object apply(Exception from) {
|
||||
if (from instanceof HttpResponseException) {
|
||||
HttpResponseException exception = (HttpResponseException) from;
|
||||
if (exception.getResponse().getStatusCode() == 400) {
|
||||
AtmosError error = parseErrorFromResponse(exception);
|
||||
|
||||
if (error.getCode() == 1016) {
|
||||
throw new IllegalStateException("The resource you are trying to create\n" +
|
||||
"already exists.", from);
|
||||
}
|
||||
}
|
||||
}
|
||||
throw Throwables.propagate(from);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
protected AtmosError parseErrorFromResponse(HttpResponseException responseException) {
|
||||
HttpResponse response = responseException.getResponse();
|
||||
HttpCommand command = responseException.getCommand();
|
||||
|
||||
byte[] content = HttpUtils.closeClientButKeepContentStream(response);
|
||||
return utils.parseAtmosErrorFromContent(command, response, new String(content));
|
||||
}
|
||||
}
|
|
@ -31,6 +31,7 @@ import org.jclouds.atmos.domain.AtmosObject;
|
|||
import org.jclouds.atmos.filters.SignRequest;
|
||||
import org.jclouds.atmos.options.PutOptions;
|
||||
import org.jclouds.atmos.xml.ErrorHandler;
|
||||
import org.jclouds.blobstore.KeyAlreadyExistsException;
|
||||
import org.jclouds.blobstore.domain.Blob;
|
||||
import org.jclouds.crypto.Crypto;
|
||||
import org.jclouds.http.HttpCommand;
|
||||
|
@ -76,7 +77,7 @@ public class AtmosUtils {
|
|||
try {
|
||||
sync.createFile(container, object, options);
|
||||
|
||||
} catch(IllegalStateException e) {
|
||||
} catch(KeyAlreadyExistsException e) {
|
||||
deleteAndEnsureGone(sync, path);
|
||||
sync.createFile(container, object, options);
|
||||
}
|
||||
|
|
|
@ -35,7 +35,6 @@ import org.jclouds.atmos.functions.ParseObjectFromHeadersAndHttpContent;
|
|||
import org.jclouds.atmos.functions.ParseSystemMetadataFromHeaders;
|
||||
import org.jclouds.atmos.functions.ReturnEndpointIfAlreadyExists;
|
||||
import org.jclouds.atmos.functions.ReturnTrueIfGroupACLIsOtherRead;
|
||||
import org.jclouds.atmos.functions.ThrowIllegalStateExceptionOn400;
|
||||
import org.jclouds.atmos.options.ListOptions;
|
||||
import org.jclouds.atmos.options.PutOptions;
|
||||
import org.jclouds.blobstore.binders.BindBlobToMultipartFormTest;
|
||||
|
@ -173,7 +172,7 @@ public class AtmosAsyncClientTest extends BaseAsyncClientTest<AtmosAsyncClient>
|
|||
|
||||
assertResponseParserClassEquals(method, request, ParseURIFromListOrLocationHeaderIf20x.class);
|
||||
assertSaxResponseParserClassEquals(method, null);
|
||||
assertExceptionParserClassEquals(method, ThrowIllegalStateExceptionOn400.class);
|
||||
assertExceptionParserClassEquals(method, null);
|
||||
|
||||
checkFilters(request);
|
||||
}
|
||||
|
@ -191,7 +190,7 @@ public class AtmosAsyncClientTest extends BaseAsyncClientTest<AtmosAsyncClient>
|
|||
|
||||
assertResponseParserClassEquals(method, request, ParseURIFromListOrLocationHeaderIf20x.class);
|
||||
assertSaxResponseParserClassEquals(method, null);
|
||||
assertExceptionParserClassEquals(method, ThrowIllegalStateExceptionOn400.class);
|
||||
assertExceptionParserClassEquals(method, null);
|
||||
|
||||
checkFilters(request);
|
||||
}
|
||||
|
|
|
@ -1,54 +0,0 @@
|
|||
/**
|
||||
* Licensed to jclouds, Inc. (jclouds) under one or more
|
||||
* contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. jclouds licenses this file
|
||||
* to you 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.atmos.functions;
|
||||
|
||||
import org.jclouds.atmos.domain.AtmosError;
|
||||
import org.jclouds.atmos.util.AtmosUtils;
|
||||
import org.jclouds.http.HttpResponse;
|
||||
import org.jclouds.http.HttpResponseException;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
/**
|
||||
* @author Andrei Savu
|
||||
*/
|
||||
public class ThrowIllegalStateExceptionOn400Test {
|
||||
|
||||
@Test(expectedExceptions = IllegalStateException.class)
|
||||
public void testResourceAlreadyExists() {
|
||||
new ThrowIllegalStateExceptionOn400(new AtmosUtils()) {
|
||||
@Override
|
||||
protected AtmosError parseErrorFromResponse(HttpResponseException ignore) {
|
||||
return new AtmosError(1016, "Resource already exists");
|
||||
}
|
||||
}.apply(new HttpResponseException("Resource already exists", null,
|
||||
HttpResponse.builder().statusCode(400).build(), (Throwable) null));
|
||||
|
||||
}
|
||||
|
||||
@Test(expectedExceptions = RuntimeException.class)
|
||||
public void testNotFoundPropagates() {
|
||||
new ThrowIllegalStateExceptionOn400(new AtmosUtils()).apply(new RuntimeException());
|
||||
}
|
||||
|
||||
@Test(expectedExceptions = NullPointerException.class)
|
||||
public void testNullIsBad() {
|
||||
new ThrowIllegalStateExceptionOn400(new AtmosUtils()).apply(null);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue