Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Refactor nexus client exception handling

Signed-off-by: Alin Dreghiciu <adreghiciu@gmail.com>
  • Loading branch information...
commit 3dab49c0671b3861021fffc0a7fce1c89861a27f 1 parent a2ea2ff
@adreghiciu adreghiciu authored cstamas committed
Showing with 919 additions and 313 deletions.
  1. +2 −2 nexus/nexus-client-core-testsuite/src/test/java/org/sonatype/nexus/client/testsuite/ArtifactMavenIT.java
  2. +7 −7 nexus/nexus-client-core-testsuite/src/test/java/org/sonatype/nexus/client/testsuite/ContentIT.java
  3. +14 −0 nexus/nexus-client-core-testsuite/src/test/java/org/sonatype/nexus/client/testsuite/RepositoriesIT.java
  4. +0 −52 nexus/nexus-client-core/src/main/java/org/sonatype/nexus/client/core/NexusErrorMessageException.java
  5. +0 −54 nexus/nexus-client-core/src/main/java/org/sonatype/nexus/client/core/NexusUnexpectedResponseException.java
  6. +8 −7 ...natype/nexus/client/core/{BadRequestException.java → exception/NexusClientErrorResponseException.java}
  7. +2 −1  ...exus-client-core/src/main/java/org/sonatype/nexus/client/core/{ → exception}/NexusClientException.java
  8. +37 −0 .../nexus-client-core/src/main/java/org/sonatype/nexus/client/core/exception/NexusClientNotFoundException.java
  9. +64 −0 .../nexus-client-core/src/main/java/org/sonatype/nexus/client/core/exception/NexusClientResponseException.java
  10. +22 −8 ...client-core/src/main/java/org/sonatype/nexus/client/internal/rest/jersey/subsystem/JerseyArtifactMaven.java
  11. +91 −38 ...nexus-client-core/src/main/java/org/sonatype/nexus/client/internal/rest/jersey/subsystem/JerseyContent.java
  12. +13 −2 ...xus-client-core/src/main/java/org/sonatype/nexus/client/internal/rest/jersey/subsystem/JerseyUtilities.java
  13. +29 −6 ...ore/src/main/java/org/sonatype/nexus/client/internal/rest/jersey/subsystem/config/JerseySegmentSupport.java
  14. +47 −15 ...rc/main/java/org/sonatype/nexus/client/internal/rest/jersey/subsystem/repository/JerseyGroupRepository.java
  15. +74 −21 ...e/src/main/java/org/sonatype/nexus/client/internal/rest/jersey/subsystem/repository/JerseyRepositories.java
  16. +91 −25 ...ore/src/main/java/org/sonatype/nexus/client/internal/rest/jersey/subsystem/repository/JerseyRepository.java
  17. +61 −10 ...client-core/src/main/java/org/sonatype/nexus/client/internal/rest/jersey/subsystem/security/JerseyRole.java
  18. +34 −3 ...lient-core/src/main/java/org/sonatype/nexus/client/internal/rest/jersey/subsystem/security/JerseyRoles.java
  19. +61 −10 ...client-core/src/main/java/org/sonatype/nexus/client/internal/rest/jersey/subsystem/security/JerseyUser.java
  20. +34 −3 ...lient-core/src/main/java/org/sonatype/nexus/client/internal/rest/jersey/subsystem/security/JerseyUsers.java
  21. +33 −0 ...-client-core/src/main/java/org/sonatype/nexus/client/rest/jersey/ContextAwareUniformInterfaceException.java
  22. +137 −21 nexus/nexus-client-core/src/main/java/org/sonatype/nexus/client/rest/jersey/JerseyNexusClient.java
  23. +8 −4 ...org/sonatype/nexus/client/{core/NotFoundException.java → rest/jersey/NexusClientHandlerException.java}
  24. +16 −1 ...t/nexus-testsuite-client/src/main/java/org/sonatype/nexus/testsuite/client/internal/JerseyRemoteLogger.java
  25. +20 −2 ...-test/nexus-testsuite-client/src/main/java/org/sonatype/nexus/testsuite/client/internal/JerseyUIDLocks.java
  26. +14 −21 ...xus-indexer-lucene-client/src/main/java/org/sonatype/nexus/mindexer/client/internal/JerseyMavenIndexer.java
View
4 ...ent-core-testsuite/src/test/java/org/sonatype/nexus/client/testsuite/ArtifactMavenIT.java
@@ -22,7 +22,7 @@
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
-import org.sonatype.nexus.client.core.NotFoundException;
+import org.sonatype.nexus.client.core.exception.NexusClientNotFoundException;
import org.sonatype.nexus.client.core.subsystem.artifact.ArtifactMaven;
import org.sonatype.nexus.client.core.subsystem.artifact.ResolveRequest;
import org.sonatype.nexus.client.core.subsystem.artifact.ResolveResponse;
@@ -69,7 +69,7 @@ public void artifactMavenResolveSuccess()
@Test
public void artifactMavenResolveFailure()
{
- thrown.expect( NotFoundException.class );
+ thrown.expect( NexusClientNotFoundException.class );
artifacts().resolve(
new ResolveRequest(
"releases", "com.sonatype.nexus.plugin", "nexus-staging-plugin", ResolveRequest.VERSION_RELEASE
View
14 ...us-client-core-testsuite/src/test/java/org/sonatype/nexus/client/testsuite/ContentIT.java
@@ -17,12 +17,12 @@
import static org.sonatype.sisu.litmus.testsupport.hamcrest.FileMatchers.matchSha1;
import java.io.File;
-import java.io.FileNotFoundException;
import java.io.IOException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
+import org.sonatype.nexus.client.core.exception.NexusClientNotFoundException;
import org.sonatype.nexus.client.core.subsystem.content.Content;
import org.sonatype.nexus.client.core.subsystem.content.Location;
@@ -61,9 +61,9 @@ public void successfulUploadAndDownloadAndDelete()
public void wrongUploadLocation()
throws IOException
{
- thrown.expect( FileNotFoundException.class );
+ thrown.expect( NexusClientNotFoundException.class );
thrown.expectMessage(
- "Inexistent Nexus path: repositories/foo/aopalliance/aopalliance/1.0/aopalliance-1.0.pom"
+ "Inexistent path: repositories/foo/aopalliance/aopalliance/1.0/aopalliance-1.0.pom"
);
content().upload( repositoryLocation( "foo", AOP_POM ), testData().resolveFile( "artifacts/" + AOP_POM ) );
}
@@ -72,9 +72,9 @@ public void wrongUploadLocation()
public void wrongDownloadLocation()
throws IOException
{
- thrown.expect( FileNotFoundException.class );
+ thrown.expect( NexusClientNotFoundException.class );
thrown.expectMessage(
- "Inexistent Nexus path: repositories/foo/aopalliance/aopalliance/1.0/aopalliance-1.0.pom"
+ "Inexistent path: repositories/foo/aopalliance/aopalliance/1.0/aopalliance-1.0.pom"
);
content().download(
repositoryLocation( "foo", AOP_POM ),
@@ -86,9 +86,9 @@ public void wrongDownloadLocation()
public void wrongDeleteLocation()
throws IOException
{
- thrown.expect( FileNotFoundException.class );
+ thrown.expect( NexusClientNotFoundException.class );
thrown.expectMessage(
- "Inexistent Nexus path: repositories/foo/aopalliance/aopalliance/1.0/aopalliance-1.0.pom"
+ "Inexistent path: repositories/foo/aopalliance/aopalliance/1.0/aopalliance-1.0.pom"
);
content().delete( repositoryLocation( "foo", AOP_POM ) );
}
View
14 ...ient-core-testsuite/src/test/java/org/sonatype/nexus/client/testsuite/RepositoriesIT.java
@@ -20,7 +20,10 @@
import java.util.Collection;
+import org.junit.Rule;
import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonatype.nexus.client.core.exception.NexusClientNotFoundException;
import org.sonatype.nexus.client.core.subsystem.repository.Repositories;
import org.sonatype.nexus.client.core.subsystem.repository.Repository;
import org.sonatype.nexus.client.core.subsystem.repository.maven.MavenGroupRepository;
@@ -32,12 +35,23 @@
extends NexusClientITSupport
{
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
public RepositoriesIT( final String nexusBundleCoordinates )
{
super( nexusBundleCoordinates );
}
@Test
+ public void getInexistentRepository()
+ {
+ thrown.expect( NexusClientNotFoundException.class );
+ thrown.expectMessage( "Repository with id 'getInexistentRepository' was not found" );
+ repositories().get( repositoryIdForTest() );
+ }
+
+ @Test
public void getHosted()
{
final Repository repository = repositories().get( "releases" );
View
52 ...-client-core/src/main/java/org/sonatype/nexus/client/core/NexusErrorMessageException.java
@@ -1,52 +0,0 @@
-/**
- * Sonatype Nexus (TM) Open Source Version
- * Copyright (c) 2007-2012 Sonatype, Inc.
- * All rights reserved. Includes the third-party code listed at http://links.sonatype.com/products/nexus/oss/attributions.
- *
- * This program and the accompanying materials are made available under the terms of the Eclipse Public License Version 1.0,
- * which accompanies this distribution and is available at http://www.eclipse.org/legal/epl-v10.html.
- *
- * Sonatype Nexus (TM) Professional Version is available from Sonatype, Inc. "Sonatype" and "Sonatype Nexus" are trademarks
- * of Sonatype, Inc. Apache Maven is a trademark of the Apache Software Foundation. M2eclipse is a trademark of the
- * Eclipse Foundation. All other trademarks are the property of their respective owners.
- */
-package org.sonatype.nexus.client.core;
-
-import java.util.Collections;
-import java.util.LinkedHashMap;
-import java.util.Map;
-
-/**
- * Thrown when Nexus responds with an Client Error status code and (optionally) with an error message.
- *
- * @since 2.1
- */
-@SuppressWarnings( "serial" )
-public class NexusErrorMessageException
- extends NexusUnexpectedResponseException
-{
-
- private final Map<String, String> errors;
-
- public NexusErrorMessageException( final int statusCode, final String statusMessage,
- final Map<String, String> errors )
- {
- super( statusCode, statusMessage, "Nexus Error Response!" );
- this.errors = initErrors( errors );
- }
-
- protected Map<String, String> initErrors( final Map<String, String> errors )
- {
- final Map<String, String> result = new LinkedHashMap<String, String>();
- if ( errors != null && !errors.isEmpty() )
- {
- result.putAll( errors );
- }
- return result;
- }
-
- public Map<String, String> getErrors()
- {
- return Collections.unmodifiableMap( errors );
- }
-}
View
54 ...t-core/src/main/java/org/sonatype/nexus/client/core/NexusUnexpectedResponseException.java
@@ -1,54 +0,0 @@
-/**
- * Sonatype Nexus (TM) Open Source Version
- * Copyright (c) 2007-2012 Sonatype, Inc.
- * All rights reserved. Includes the third-party code listed at http://links.sonatype.com/products/nexus/oss/attributions.
- *
- * This program and the accompanying materials are made available under the terms of the Eclipse Public License Version 1.0,
- * which accompanies this distribution and is available at http://www.eclipse.org/legal/epl-v10.html.
- *
- * Sonatype Nexus (TM) Professional Version is available from Sonatype, Inc. "Sonatype" and "Sonatype Nexus" are trademarks
- * of Sonatype, Inc. Apache Maven is a trademark of the Apache Software Foundation. M2eclipse is a trademark of the
- * Eclipse Foundation. All other trademarks are the property of their respective owners.
- */
-package org.sonatype.nexus.client.core;
-
-/**
- * Generic runtime exception to be thrown by Subsystems, when some unexpected error is reported by Nexus. This exception
- * here is solely for purpose of not proliferating possible runtime exceptions of underlying implementation. Best to use
- * some subclass of this exception, but this is the last resort (ie. resource does not send proper response, just HTTP
- * code and reason phrase).
- *
- * @since 2.1
- */
-@SuppressWarnings( "serial" )
-public class NexusUnexpectedResponseException
- extends NexusClientException
-{
-
- private final int statusCode;
-
- private final String statusMessage;
-
- public NexusUnexpectedResponseException( final int statusCode, final String statusMessage )
- {
- this( statusCode, statusMessage, String.format( "Unexpected response: %s %s", statusCode, statusMessage ) );
- }
-
- public NexusUnexpectedResponseException( final int statusCode, final String statusMessage, final String message )
- {
- super( message );
- this.statusCode = statusCode;
- this.statusMessage = statusMessage;
- }
-
- public int getStatusCode()
- {
- return statusCode;
- }
-
- public String getStatusMessage()
- {
- return statusMessage;
- }
-
-}
View
15 ...exus/client/core/BadRequestException.java → ...on/NexusClientErrorResponseException.java
@@ -10,12 +10,11 @@
* of Sonatype, Inc. Apache Maven is a trademark of the Apache Software Foundation. M2eclipse is a trademark of the
* Eclipse Foundation. All other trademarks are the property of their respective owners.
*/
-package org.sonatype.nexus.client.core;
-
-import static com.google.common.base.Preconditions.checkNotNull;
+package org.sonatype.nexus.client.core.exception;
import java.util.Collections;
import java.util.List;
+import javax.ws.rs.core.Response;
import org.sonatype.nexus.client.internal.msg.ErrorMessage;
import org.sonatype.nexus.client.internal.msg.ErrorResponse;
@@ -23,15 +22,17 @@
/**
* @since 2.3
*/
-public class BadRequestException
- extends NexusClientException
+public class NexusClientErrorResponseException
+ extends NexusClientResponseException
{
private final List<ErrorMessage> errors;
- public BadRequestException( final ErrorResponse response )
+ public NexusClientErrorResponseException( final String reasonPhrase,
+ final String responseBody,
+ final ErrorResponse response )
{
- super( message( checkNotNull( response ) ) );
+ super( message( response ), Response.Status.BAD_REQUEST.getStatusCode(), reasonPhrase, responseBody );
errors = Collections.unmodifiableList(
response.getErrors() == null ? Collections.<ErrorMessage>emptyList() : response.getErrors()
);
View
3  ...xus/client/core/NexusClientException.java → .../core/exception/NexusClientException.java
@@ -10,7 +10,7 @@
* of Sonatype, Inc. Apache Maven is a trademark of the Apache Software Foundation. M2eclipse is a trademark of the
* Eclipse Foundation. All other trademarks are the property of their respective owners.
*/
-package org.sonatype.nexus.client.core;
+package org.sonatype.nexus.client.core.exception;
/**
* Generic runtime exception to be thrown by Subsystems, when some error is reported by Nexus. This exception here is
@@ -37,4 +37,5 @@ public NexusClientException( Throwable cause )
{
super( cause );
}
+
}
View
37 .../src/main/java/org/sonatype/nexus/client/core/exception/NexusClientNotFoundException.java
@@ -0,0 +1,37 @@
+/**
+ * Sonatype Nexus (TM) Open Source Version
+ * Copyright (c) 2007-2012 Sonatype, Inc.
+ * All rights reserved. Includes the third-party code listed at http://links.sonatype.com/products/nexus/oss/attributions.
+ *
+ * This program and the accompanying materials are made available under the terms of the Eclipse Public License Version 1.0,
+ * which accompanies this distribution and is available at http://www.eclipse.org/legal/epl-v10.html.
+ *
+ * Sonatype Nexus (TM) Professional Version is available from Sonatype, Inc. "Sonatype" and "Sonatype Nexus" are trademarks
+ * of Sonatype, Inc. Apache Maven is a trademark of the Apache Software Foundation. M2eclipse is a trademark of the
+ * Eclipse Foundation. All other trademarks are the property of their respective owners.
+ */
+package org.sonatype.nexus.client.core.exception;
+
+import javax.ws.rs.core.Response;
+
+/**
+ * @since 2.3
+ */
+public class NexusClientNotFoundException
+ extends NexusClientResponseException
+{
+
+ public NexusClientNotFoundException( final String reasonPhrase,
+ final String responseBody )
+ {
+ super( Response.Status.NOT_FOUND.getStatusCode(), reasonPhrase, responseBody );
+ }
+
+ public NexusClientNotFoundException( final String message,
+ final String reasonPhrase,
+ final String responseBody )
+ {
+ super( message, Response.Status.NOT_FOUND.getStatusCode(), reasonPhrase, responseBody );
+ }
+
+}
View
64 .../src/main/java/org/sonatype/nexus/client/core/exception/NexusClientResponseException.java
@@ -0,0 +1,64 @@
+/**
+ * Sonatype Nexus (TM) Open Source Version
+ * Copyright (c) 2007-2012 Sonatype, Inc.
+ * All rights reserved. Includes the third-party code listed at http://links.sonatype.com/products/nexus/oss/attributions.
+ *
+ * This program and the accompanying materials are made available under the terms of the Eclipse Public License Version 1.0,
+ * which accompanies this distribution and is available at http://www.eclipse.org/legal/epl-v10.html.
+ *
+ * Sonatype Nexus (TM) Professional Version is available from Sonatype, Inc. "Sonatype" and "Sonatype Nexus" are trademarks
+ * of Sonatype, Inc. Apache Maven is a trademark of the Apache Software Foundation. M2eclipse is a trademark of the
+ * Eclipse Foundation. All other trademarks are the property of their respective owners.
+ */
+package org.sonatype.nexus.client.core.exception;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * @since 2.3
+ */
+@SuppressWarnings( "serial" )
+public class NexusClientResponseException
+ extends NexusClientException
+{
+
+ private final int responseCode;
+
+ private final String reasonPhrase;
+
+ private final String responseBody;
+
+ public NexusClientResponseException( final int responseCode,
+ final String reasonPhrase,
+ final String responseBody )
+ {
+ this( null, responseCode, reasonPhrase, responseBody );
+ }
+
+ public NexusClientResponseException( final String message,
+ final int responseCode,
+ final String reasonPhrase,
+ final String responseBody )
+ {
+ super( message == null ? String.format( "%s - %s", responseCode, reasonPhrase ) : message );
+ this.responseCode = responseCode;
+ this.reasonPhrase = checkNotNull( reasonPhrase );
+ this.responseBody = checkNotNull( responseBody );
+ }
+
+ public int getResponseCode()
+ {
+ return responseCode;
+ }
+
+ public String getReasonPhrase()
+ {
+ return reasonPhrase;
+ }
+
+ public String getResponseBody()
+ {
+ return responseBody;
+ }
+
+}
View
30 ...in/java/org/sonatype/nexus/client/internal/rest/jersey/subsystem/JerseyArtifactMaven.java
@@ -21,6 +21,8 @@
import org.sonatype.nexus.client.rest.jersey.JerseyNexusClient;
import org.sonatype.nexus.rest.model.ArtifactResolveResource;
import org.sonatype.nexus.rest.model.ArtifactResolveResourceResponse;
+import com.sun.jersey.api.client.ClientHandlerException;
+import com.sun.jersey.api.client.UniformInterfaceException;
import com.sun.jersey.core.util.MultivaluedMapImpl;
/**
@@ -58,14 +60,26 @@ public ResolveResponse resolve( final ResolveRequest req )
queryParams.add( "e", req.getExtension() );
}
- final ArtifactResolveResource data = getNexusClient().serviceResource( "artifact/maven/resolve", queryParams )
- .get( ArtifactResolveResourceResponse.class ).getData();
+ try
+ {
+ final ArtifactResolveResource data = getNexusClient()
+ .serviceResource( "artifact/maven/resolve", queryParams )
+ .get( ArtifactResolveResourceResponse.class ).getData();
- return new ResolveResponse( data.isPresentLocally(), data.getGroupId(), data.getArtifactId(),
- data.getVersion(), data.getBaseVersion(), data.getClassifier(),
- data.getExtension(), data.isSnapshot(),
- data.getSnapshotBuildNumber(), data.getSnapshotTimeStamp(), data.getFileName(),
- data.getSha1(),
- data.getRepositoryPath() );
+ return new ResolveResponse( data.isPresentLocally(), data.getGroupId(), data.getArtifactId(),
+ data.getVersion(), data.getBaseVersion(), data.getClassifier(),
+ data.getExtension(), data.isSnapshot(),
+ data.getSnapshotBuildNumber(), data.getSnapshotTimeStamp(), data.getFileName(),
+ data.getSha1(),
+ data.getRepositoryPath() );
+ }
+ catch ( UniformInterfaceException e )
+ {
+ throw getNexusClient().convert( e );
+ }
+ catch ( ClientHandlerException e )
+ {
+ throw getNexusClient().convert( e );
+ }
}
}
View
129 ...src/main/java/org/sonatype/nexus/client/internal/rest/jersey/subsystem/JerseyContent.java
@@ -15,17 +15,20 @@
import static com.google.common.base.Preconditions.checkState;
import java.io.File;
-import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
+
import javax.ws.rs.core.Response;
import org.codehaus.plexus.util.IOUtil;
import org.sonatype.nexus.client.core.spi.SubsystemSupport;
import org.sonatype.nexus.client.core.subsystem.content.Content;
import org.sonatype.nexus.client.core.subsystem.content.Location;
+import org.sonatype.nexus.client.rest.jersey.ContextAwareUniformInterfaceException;
import org.sonatype.nexus.client.rest.jersey.JerseyNexusClient;
+import com.sun.jersey.api.client.ClientHandlerException;
import com.sun.jersey.api.client.ClientResponse;
+import com.sun.jersey.api.client.UniformInterfaceException;
/**
* @since 2.1
@@ -62,21 +65,45 @@ public void download( final Location location, final File target )
);
}
- final ClientResponse response = validateResponse(
- location,
- getNexusClient().uri( CONTENT_PREFIX + location.toContentPath() ).get( ClientResponse.class )
- );
-
- FileOutputStream fos = null;
try
{
- fos = new FileOutputStream( target );
- IOUtil.copy( response.getEntityInputStream(), fos );
+ final ClientResponse response = getNexusClient()
+ .uri( CONTENT_PREFIX + location.toContentPath() )
+ .get( ClientResponse.class );
+
+ if(!ClientResponse.Status.OK.equals( response.getClientResponseStatus() ))
+ {
+ throw getNexusClient().convert(
+ new ContextAwareUniformInterfaceException( response )
+ {
+ @Override
+ public String getMessage( final int status )
+ {
+ if ( status == Response.Status.NOT_FOUND.getStatusCode() )
+ {
+ return String.format( "Inexistent path: %s", location );
+ }
+ return null;
+ }
+ }
+ );
+ }
+
+ FileOutputStream fos = null;
+ try
+ {
+ fos = new FileOutputStream( target );
+ IOUtil.copy( response.getEntityInputStream(), fos );
+ }
+ finally
+ {
+ response.close();
+ IOUtil.close( fos );
+ }
}
- finally
+ catch ( ClientHandlerException e )
{
- response.close();
- IOUtil.close( fos );
+ throw getNexusClient().convert( e );
}
}
@@ -84,40 +111,66 @@ public void download( final Location location, final File target )
public void upload( final Location location, final File target )
throws IOException
{
- validateResponse(
- location,
- getNexusClient().uri( CONTENT_PREFIX + location.toContentPath() ).put( ClientResponse.class, target )
- );
+ try
+ {
+ getNexusClient()
+ .uri( CONTENT_PREFIX + location.toContentPath() )
+ .put( target );
+ }
+ catch ( UniformInterfaceException e )
+ {
+ throw getNexusClient().convert(
+ new ContextAwareUniformInterfaceException( e.getResponse() )
+ {
+ @Override
+ public String getMessage( final int status )
+ {
+ if ( status == Response.Status.NOT_FOUND.getStatusCode() )
+ {
+ return String.format( "Inexistent path: %s", location );
+ }
+ return null;
+ }
+ }
+ );
+ }
+ catch ( ClientHandlerException e )
+ {
+ throw getNexusClient().convert( e );
+ }
}
@Override
public void delete( final Location location )
throws IOException
{
- validateResponse(
- location,
- getNexusClient().uri( CONTENT_PREFIX + location.toContentPath() ).delete( ClientResponse.class )
- );
- }
-
- private ClientResponse validateResponse( final Location location, final ClientResponse response )
- throws IOException
- {
- if ( !successful( response ) )
+ try
{
- response.close();
- if ( Response.Status.NOT_FOUND.getStatusCode() == response.getStatus() )
- {
- throw new FileNotFoundException( "Inexistent Nexus path: " + location.toString() );
- }
- throw new IOException( "Unexpected response: " + response.getClientResponseStatus() );
+ getNexusClient()
+ .uri( CONTENT_PREFIX + location.toContentPath() )
+ .delete();
+ }
+ catch ( UniformInterfaceException e )
+ {
+ throw getNexusClient().convert(
+ new ContextAwareUniformInterfaceException( e.getResponse() )
+ {
+ @Override
+ public String getMessage( final int status )
+ {
+ if ( status == Response.Status.NOT_FOUND.getStatusCode() )
+ {
+ return String.format( "Inexistent path: %s", location );
+ }
+ return null;
+ }
+ }
+ );
+ }
+ catch ( ClientHandlerException e )
+ {
+ throw getNexusClient().convert( e );
}
- return response;
- }
-
- private boolean successful( final ClientResponse response )
- {
- return Response.Status.Family.SUCCESSFUL.equals( response.getClientResponseStatus().getFamily() );
}
}
View
15 ...c/main/java/org/sonatype/nexus/client/internal/rest/jersey/subsystem/JerseyUtilities.java
@@ -17,6 +17,7 @@
import org.sonatype.nexus.client.core.spi.SubsystemSupport;
import org.sonatype.nexus.client.core.subsystem.Utilities;
import org.sonatype.nexus.client.rest.jersey.JerseyNexusClient;
+import com.sun.jersey.api.client.ClientHandlerException;
import com.sun.jersey.api.client.ClientResponse;
/**
@@ -35,7 +36,17 @@ public JerseyUtilities( final JerseyNexusClient nexusClient )
@Override
public Date getLastModified( final String uri )
{
- final ClientResponse response = getNexusClient().uri( uri ).head();
- return response.getLastModified();
+ try
+ {
+ final ClientResponse response = getNexusClient()
+ .uri( uri )
+ .head();
+
+ return response.getLastModified();
+ }
+ catch ( ClientHandlerException e )
+ {
+ throw getNexusClient().convert( e );
+ }
}
}
View
35 ...org/sonatype/nexus/client/internal/rest/jersey/subsystem/config/JerseySegmentSupport.java
@@ -22,6 +22,8 @@
import org.sonatype.nexus.rest.model.GlobalConfigurationResourceResponse;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Throwables;
+import com.sun.jersey.api.client.ClientHandlerException;
+import com.sun.jersey.api.client.UniformInterfaceException;
public abstract class JerseySegmentSupport<ME extends Segment, S>
extends SubsystemSupport<JerseyNexusClient>
@@ -92,11 +94,21 @@ ME me()
@VisibleForTesting
GlobalConfigurationResource getConfiguration()
{
- final GlobalConfigurationResourceResponse response = getNexusClient()
- .serviceResource( "global_settings/current" )
- .get( GlobalConfigurationResourceResponse.class );
-
- return response.getData();
+ try
+ {
+ return getNexusClient()
+ .serviceResource( "global_settings/current" )
+ .get( GlobalConfigurationResourceResponse.class )
+ .getData();
+ }
+ catch ( UniformInterfaceException e )
+ {
+ throw getNexusClient().convert( e );
+ }
+ catch ( ClientHandlerException e )
+ {
+ throw getNexusClient().convert( e );
+ }
}
@VisibleForTesting
@@ -105,7 +117,18 @@ void setConfiguration( final GlobalConfigurationResource configuration )
final GlobalConfigurationResourceResponse request = new GlobalConfigurationResourceResponse();
request.setData( configuration );
- getNexusClient().serviceResource( "global_settings/current" ).put( request );
+ try
+ {
+ getNexusClient().serviceResource( "global_settings/current" ).put( request );
+ }
+ catch ( UniformInterfaceException e )
+ {
+ throw getNexusClient().convert( e );
+ }
+ catch ( ClientHandlerException e )
+ {
+ throw getNexusClient().convert( e );
+ }
}
}
View
62 ...onatype/nexus/client/internal/rest/jersey/subsystem/repository/JerseyGroupRepository.java
@@ -28,6 +28,8 @@
import org.sonatype.nexus.rest.model.RepositoryGroupResourceResponse;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
+import com.sun.jersey.api.client.ClientHandlerException;
+import com.sun.jersey.api.client.UniformInterfaceException;
/**
* Jersey based {@link GroupRepository} implementation.
@@ -79,11 +81,21 @@ String uri()
@Override
RepositoryGroupResource doGet()
{
- final RepositoryGroupResourceResponse response = getNexusClient()
- .serviceResource( uri() + "/" + id() )
- .get( RepositoryGroupResourceResponse.class );
-
- return response.getData();
+ try
+ {
+ return getNexusClient()
+ .serviceResource( uri() + "/" + id() )
+ .get( RepositoryGroupResourceResponse.class )
+ .getData();
+ }
+ catch ( UniformInterfaceException e )
+ {
+ throw getNexusClient().convert( e );
+ }
+ catch ( ClientHandlerException e )
+ {
+ throw getNexusClient().convert( e );
+ }
}
@Override
@@ -92,11 +104,21 @@ RepositoryGroupResource doCreate()
final RepositoryGroupResourceResponse request = new RepositoryGroupResourceResponse();
request.setData( settings() );
- final RepositoryGroupResourceResponse response = getNexusClient()
- .serviceResource( uri() )
- .post( RepositoryGroupResourceResponse.class, request );
-
- return response.getData();
+ try
+ {
+ return getNexusClient()
+ .serviceResource( uri() )
+ .post( RepositoryGroupResourceResponse.class, request )
+ .getData();
+ }
+ catch ( UniformInterfaceException e )
+ {
+ throw getNexusClient().convert( e );
+ }
+ catch ( ClientHandlerException e )
+ {
+ throw getNexusClient().convert( e );
+ }
}
@Override
@@ -105,11 +127,21 @@ RepositoryGroupResource doUpdate()
final RepositoryGroupResourceResponse request = new RepositoryGroupResourceResponse();
request.setData( settings() );
- final RepositoryGroupResourceResponse response = getNexusClient()
- .serviceResource( uri() + "/" + id() )
- .put( RepositoryGroupResourceResponse.class, request );
-
- return response.getData();
+ try
+ {
+ return getNexusClient()
+ .serviceResource( uri() + "/" + id() )
+ .put( RepositoryGroupResourceResponse.class, request )
+ .getData();
+ }
+ catch ( UniformInterfaceException e )
+ {
+ throw getNexusClient().convert( e );
+ }
+ catch ( ClientHandlerException e )
+ {
+ throw getNexusClient().convert( e );
+ }
}
@Override
View
95 ...g/sonatype/nexus/client/internal/rest/jersey/subsystem/repository/JerseyRepositories.java
@@ -20,12 +20,15 @@
import java.util.List;
import java.util.Set;
import javax.annotation.Nullable;
+import javax.ws.rs.core.Response;
-import org.sonatype.nexus.client.core.NotFoundException;
+import org.sonatype.nexus.client.core.exception.NexusClientException;
+import org.sonatype.nexus.client.core.exception.NexusClientNotFoundException;
import org.sonatype.nexus.client.core.spi.SubsystemSupport;
import org.sonatype.nexus.client.core.spi.subsystem.repository.RepositoryFactory;
import org.sonatype.nexus.client.core.subsystem.repository.Repositories;
import org.sonatype.nexus.client.core.subsystem.repository.Repository;
+import org.sonatype.nexus.client.rest.jersey.ContextAwareUniformInterfaceException;
import org.sonatype.nexus.client.rest.jersey.JerseyNexusClient;
import org.sonatype.nexus.rest.model.RepositoryBaseResource;
import org.sonatype.nexus.rest.model.RepositoryGroupResourceResponse;
@@ -35,6 +38,8 @@
import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
import com.google.common.collect.Lists;
+import com.sun.jersey.api.client.ClientHandlerException;
+import com.sun.jersey.api.client.UniformInterfaceException;
/**
* Jersey based {@link Repositories} implementation.
@@ -66,17 +71,54 @@ public JerseyRepositories( final JerseyNexusClient nexusClient,
{
return convert(
id,
- getNexusClient().serviceResource( "repositories/" + id )
- .get( RepositoryResourceResponse.class ).getData()
+ getNexusClient()
+ .serviceResource( "repositories/" + id )
+ .get( RepositoryResourceResponse.class )
+ .getData()
);
}
- catch ( final NotFoundException e )
+ catch ( final UniformInterfaceException e )
{
- return convert(
- id,
- getNexusClient().serviceResource( "repo_groups/" + id )
- .get( RepositoryGroupResourceResponse.class ).getData()
- );
+ final NexusClientException converted = getNexusClient().convert( e );
+ if ( converted instanceof NexusClientNotFoundException )
+ {
+ try
+ {
+ return convert(
+ id,
+ getNexusClient()
+ .serviceResource( "repo_groups/" + id )
+ .get( RepositoryGroupResourceResponse.class )
+ .getData()
+ );
+ }
+ catch ( UniformInterfaceException e2 )
+ {
+ throw getNexusClient().convert(
+ new ContextAwareUniformInterfaceException( e2.getResponse() )
+ {
+ @Override
+ public String getMessage( final int status )
+ {
+ if ( status == Response.Status.NOT_FOUND.getStatusCode() )
+ {
+ return String.format( "Repository with id '%s' was not found", id );
+ }
+ return null;
+ }
+ }
+ );
+ }
+ catch ( ClientHandlerException e2 )
+ {
+ throw getNexusClient().convert( e2 );
+ }
+ }
+ throw converted;
+ }
+ catch ( ClientHandlerException e )
+ {
+ throw getNexusClient().convert( e );
}
}
@@ -98,23 +140,34 @@ public JerseyRepositories( final JerseyNexusClient nexusClient,
@Override
public Collection<Repository> get()
{
- final RepositoryListResourceResponse response = getNexusClient()
- .serviceResource( "repositories" )
- .get( RepositoryListResourceResponse.class );
-
- if ( response == null || response.getData() == null || response.getData().isEmpty() )
+ try
{
- return Collections.emptyList();
- }
+ final RepositoryListResourceResponse response = getNexusClient()
+ .serviceResource( "repositories" )
+ .get( RepositoryListResourceResponse.class );
- List<Repository> repositories = Lists.newArrayList();
+ if ( response.getData() == null || response.getData().isEmpty() )
+ {
+ return Collections.emptyList();
+ }
+
+ List<Repository> repositories = Lists.newArrayList();
+
+ for ( final RepositoryListResource resource : response.getData() )
+ {
+ repositories.add( get( resource.getId() ) );
+ }
- for ( final RepositoryListResource resource : response.getData() )
+ return repositories;
+ }
+ catch ( UniformInterfaceException e )
{
- repositories.add( get( resource.getId() ) );
+ throw getNexusClient().convert( e );
+ }
+ catch ( ClientHandlerException e )
+ {
+ throw getNexusClient().convert( e );
}
-
- return repositories;
}
@SuppressWarnings( "unchecked" )
View
116 ...org/sonatype/nexus/client/internal/rest/jersey/subsystem/repository/JerseyRepository.java
@@ -24,6 +24,8 @@
import org.sonatype.nexus.rest.model.RepositoryStatusResource;
import org.sonatype.nexus.rest.model.RepositoryStatusResourceResponse;
import com.google.common.base.Throwables;
+import com.sun.jersey.api.client.ClientHandlerException;
+import com.sun.jersey.api.client.UniformInterfaceException;
/**
* Jersey based {@link Repository} implementation.
@@ -192,11 +194,21 @@ private T me()
S doGet()
{
- final RepositoryResourceResponse response = getNexusClient()
- .serviceResource( uri() + "/" + id() )
- .get( RepositoryResourceResponse.class );
-
- return (S) response.getData();
+ try
+ {
+ return (S) getNexusClient()
+ .serviceResource( uri() + "/" + id() )
+ .get( RepositoryResourceResponse.class )
+ .getData();
+ }
+ catch ( UniformInterfaceException e )
+ {
+ throw getNexusClient().convert( e );
+ }
+ catch ( ClientHandlerException e )
+ {
+ throw getNexusClient().convert( e );
+ }
}
S doCreate()
@@ -204,11 +216,21 @@ S doCreate()
final RepositoryResourceResponse request = new RepositoryResourceResponse();
request.setData( settings() );
- final RepositoryResourceResponse response = getNexusClient()
- .serviceResource( uri() )
- .post( RepositoryResourceResponse.class, request );
-
- return (S) response.getData();
+ try
+ {
+ return (S) getNexusClient()
+ .serviceResource( uri() )
+ .post( RepositoryResourceResponse.class, request )
+ .getData();
+ }
+ catch ( UniformInterfaceException e )
+ {
+ throw getNexusClient().convert( e );
+ }
+ catch ( ClientHandlerException e )
+ {
+ throw getNexusClient().convert( e );
+ }
}
S doUpdate()
@@ -216,18 +238,39 @@ S doUpdate()
final RepositoryResourceResponse request = new RepositoryResourceResponse();
request.setData( settings() );
- final RepositoryResourceResponse response = getNexusClient()
- .serviceResource( uri() + "/" + id() )
- .put( RepositoryResourceResponse.class, request );
-
- return (S) response.getData();
+ try
+ {
+ return (S) getNexusClient()
+ .serviceResource( uri() + "/" + id() )
+ .put( RepositoryResourceResponse.class, request )
+ .getData();
+ }
+ catch ( UniformInterfaceException e )
+ {
+ throw getNexusClient().convert( e );
+ }
+ catch ( ClientHandlerException e )
+ {
+ throw getNexusClient().convert( e );
+ }
}
private void doRemove()
{
- getNexusClient()
- .serviceResource( uri() + "/" + id() )
- .delete();
+ try
+ {
+ getNexusClient()
+ .serviceResource( uri() + "/" + id() )
+ .delete();
+ }
+ catch ( UniformInterfaceException e )
+ {
+ throw getNexusClient().convert( e );
+ }
+ catch ( ClientHandlerException e )
+ {
+ throw getNexusClient().convert( e );
+ }
}
U convertStatus( final RepositoryStatusResource status )
@@ -241,19 +284,42 @@ U convertStatus( final RepositoryStatusResource status )
RepositoryStatusResource doGetStatus()
{
- return getNexusClient()
- .serviceResource( "repositories/" + id() + "/status" )
- .get( RepositoryStatusResourceResponse.class ).getData();
+ try
+ {
+ return getNexusClient()
+ .serviceResource( "repositories/" + id() + "/status" )
+ .get( RepositoryStatusResourceResponse.class )
+ .getData();
+ }
+ catch ( UniformInterfaceException e )
+ {
+ throw getNexusClient().convert( e );
+ }
+ catch ( ClientHandlerException e )
+ {
+ throw getNexusClient().convert( e );
+ }
}
RepositoryStatusResource doUpdateStatus( final RepositoryStatusResource status )
{
final RepositoryStatusResourceResponse request = new RepositoryStatusResourceResponse();
request.setData( status );
- final RepositoryStatusResourceResponse response = getNexusClient()
- .serviceResource( "repositories/" + id() + "/status" )
- .put( RepositoryStatusResourceResponse.class, request );
- return this.status = response.getData();
+ try
+ {
+ return this.status = getNexusClient()
+ .serviceResource( "repositories/" + id() + "/status" )
+ .put( RepositoryStatusResourceResponse.class, request )
+ .getData();
+ }
+ catch ( UniformInterfaceException e )
+ {
+ throw getNexusClient().convert( e );
+ }
+ catch ( ClientHandlerException e )
+ {
+ throw getNexusClient().convert( e );
+ }
}
}
View
71 ...in/java/org/sonatype/nexus/client/internal/rest/jersey/subsystem/security/JerseyRole.java
@@ -23,6 +23,8 @@
import org.sonatype.security.rest.model.RoleResource;
import org.sonatype.security.rest.model.RoleResourceRequest;
import org.sonatype.security.rest.model.RoleResourceResponse;
+import com.sun.jersey.api.client.ClientHandlerException;
+import com.sun.jersey.api.client.UniformInterfaceException;
/**
* Jersey based {@link Role} implementation.
@@ -56,9 +58,21 @@ protected RoleResource createSettings( final String id )
@Override
protected RoleResource doGet()
{
- return getNexusClient().serviceResource( path( id() ) )
- .get( RoleResourceResponse.class )
- .getData();
+ try
+ {
+ return getNexusClient()
+ .serviceResource( path( id() ) )
+ .get( RoleResourceResponse.class )
+ .getData();
+ }
+ catch ( UniformInterfaceException e )
+ {
+ throw getNexusClient().convert( e );
+ }
+ catch ( ClientHandlerException e )
+ {
+ throw getNexusClient().convert( e );
+ }
}
@Override
@@ -66,9 +80,21 @@ protected RoleResource doCreate()
{
final RoleResourceRequest request = new RoleResourceRequest();
request.setData( settings() );
- return getNexusClient().serviceResource( "roles" )
- .post( RoleResourceResponse.class, request )
- .getData();
+ try
+ {
+ return getNexusClient()
+ .serviceResource( "roles" )
+ .post( RoleResourceResponse.class, request )
+ .getData();
+ }
+ catch ( UniformInterfaceException e )
+ {
+ throw getNexusClient().convert( e );
+ }
+ catch ( ClientHandlerException e )
+ {
+ throw getNexusClient().convert( e );
+ }
}
@Override
@@ -76,15 +102,40 @@ protected RoleResource doUpdate()
{
final RoleResourceRequest request = new RoleResourceRequest();
request.setData( settings() );
- return getNexusClient().serviceResource( path( id() ) )
- .put( RoleResourceResponse.class, request )
- .getData();
+ try
+ {
+ return getNexusClient()
+ .serviceResource( path( id() ) )
+ .put( RoleResourceResponse.class, request )
+ .getData();
+ }
+ catch ( UniformInterfaceException e )
+ {
+ throw getNexusClient().convert( e );
+ }
+ catch ( ClientHandlerException e )
+ {
+ throw getNexusClient().convert( e );
+ }
}
@Override
protected void doRemove()
{
- getNexusClient().serviceResource( path( id() ) ).delete();
+ try
+ {
+ getNexusClient()
+ .serviceResource( path( id() ) )
+ .delete();
+ }
+ catch ( UniformInterfaceException e )
+ {
+ throw getNexusClient().convert( e );
+ }
+ catch ( ClientHandlerException e )
+ {
+ throw getNexusClient().convert( e );
+ }
}
@Override
View
37 ...n/java/org/sonatype/nexus/client/internal/rest/jersey/subsystem/security/JerseyRoles.java
@@ -27,6 +27,8 @@
import com.google.common.base.Function;
import com.google.common.base.Throwables;
import com.google.common.collect.Collections2;
+import com.sun.jersey.api.client.ClientHandlerException;
+import com.sun.jersey.api.client.UniformInterfaceException;
/**
* Jersey based {@link Roles} implementation.
@@ -52,14 +54,43 @@ public JerseyRole create( final String id )
@Override
public Role get( final String id )
{
- return convert( getNexusClient().serviceResource( path( id ) ).get( RoleResourceResponse.class ).getData() );
+ try
+ {
+ return convert(
+ getNexusClient()
+ .serviceResource( path( id ) )
+ .get( RoleResourceResponse.class )
+ .getData()
+ );
+ }
+ catch ( UniformInterfaceException e )
+ {
+ throw getNexusClient().convert( e );
+ }
+ catch ( ClientHandlerException e )
+ {
+ throw getNexusClient().convert( e );
+ }
}
@Override
public Collection<Role> get()
{
- final RoleListResourceResponse roles = getNexusClient().serviceResource( "roles" )
- .get( RoleListResourceResponse.class );
+ final RoleListResourceResponse roles;
+ try
+ {
+ roles = getNexusClient()
+ .serviceResource( "roles" )
+ .get( RoleListResourceResponse.class );
+ }
+ catch ( UniformInterfaceException e )
+ {
+ throw getNexusClient().convert( e );
+ }
+ catch ( ClientHandlerException e )
+ {
+ throw getNexusClient().convert( e );
+ }
return Collections2.transform( roles.getData(), new Function<RoleResource, Role>()
{
View
71 ...in/java/org/sonatype/nexus/client/internal/rest/jersey/subsystem/security/JerseyUser.java
@@ -24,6 +24,8 @@
import org.sonatype.security.rest.model.UserResource;
import org.sonatype.security.rest.model.UserResourceRequest;
import org.sonatype.security.rest.model.UserResourceResponse;
+import com.sun.jersey.api.client.ClientHandlerException;
+import com.sun.jersey.api.client.UniformInterfaceException;
/**
* Jersey based {@link User} implementation.
@@ -57,9 +59,21 @@ protected UserResource createSettings( final String id )
@Override
protected UserResource doGet()
{
- return getNexusClient().serviceResource( path( id() ) )
- .get( UserResourceResponse.class )
- .getData();
+ try
+ {
+ return getNexusClient()
+ .serviceResource( path( id() ) )
+ .get( UserResourceResponse.class )
+ .getData();
+ }
+ catch ( UniformInterfaceException e )
+ {
+ throw getNexusClient().convert( e );
+ }
+ catch ( ClientHandlerException e )
+ {
+ throw getNexusClient().convert( e );
+ }
}
@Override
@@ -67,9 +81,21 @@ protected UserResource doCreate()
{
final UserResourceRequest request = new UserResourceRequest();
request.setData( settings() );
- return getNexusClient().serviceResource( "users" )
- .post( UserResourceResponse.class, request )
- .getData();
+ try
+ {
+ return getNexusClient()
+ .serviceResource( "users" )
+ .post( UserResourceResponse.class, request )
+ .getData();
+ }
+ catch ( UniformInterfaceException e )
+ {
+ throw getNexusClient().convert( e );
+ }
+ catch ( ClientHandlerException e )
+ {
+ throw getNexusClient().convert( e );
+ }
}
@Override
@@ -77,15 +103,40 @@ protected UserResource doUpdate()
{
final UserResourceRequest request = new UserResourceRequest();
request.setData( settings() );
- return getNexusClient().serviceResource( path( id() ) )
- .put( UserResourceResponse.class, request )
- .getData();
+ try
+ {
+ return getNexusClient()
+ .serviceResource( path( id() ) )
+ .put( UserResourceResponse.class, request )
+ .getData();
+ }
+ catch ( UniformInterfaceException e )
+ {
+ throw getNexusClient().convert( e );
+ }
+ catch ( ClientHandlerException e )
+ {
+ throw getNexusClient().convert( e );
+ }
}
@Override
protected void doRemove()
{
- getNexusClient().serviceResource( path( id() ) ).delete();
+ try
+ {
+ getNexusClient()
+ .serviceResource( path( id() ) )
+ .delete();
+ }
+ catch ( UniformInterfaceException e )
+ {
+ throw getNexusClient().convert( e );
+ }
+ catch ( ClientHandlerException e )
+ {
+ throw getNexusClient().convert( e );
+ }
}
@Override
View
37 ...n/java/org/sonatype/nexus/client/internal/rest/jersey/subsystem/security/JerseyUsers.java
@@ -27,6 +27,8 @@
import com.google.common.base.Function;
import com.google.common.base.Throwables;
import com.google.common.collect.Collections2;
+import com.sun.jersey.api.client.ClientHandlerException;
+import com.sun.jersey.api.client.UniformInterfaceException;
/**
* Jersey based {@link Users} implementation.
@@ -52,14 +54,43 @@ public JerseyUser create( final String id )
@Override
public User get( final String id )
{
- return convert( getNexusClient().serviceResource( path( id ) ).get( UserResourceResponse.class ).getData() );
+ try
+ {
+ return convert(
+ getNexusClient()
+ .serviceResource( path( id ) )
+ .get( UserResourceResponse.class )
+ .getData()
+ );
+ }
+ catch ( UniformInterfaceException e )
+ {
+ throw getNexusClient().convert( e );
+ }
+ catch ( ClientHandlerException e )
+ {
+ throw getNexusClient().convert( e );
+ }
}
@Override
public Collection<User> get()
{
- final UserListResourceResponse users = getNexusClient().serviceResource( "users" )
- .get( UserListResourceResponse.class );
+ final UserListResourceResponse users;
+ try
+ {
+ users = getNexusClient()
+ .serviceResource( "users" )
+ .get( UserListResourceResponse.class );
+ }
+ catch ( UniformInterfaceException e )
+ {
+ throw getNexusClient().convert( e );
+ }
+ catch ( ClientHandlerException e )
+ {
+ throw getNexusClient().convert( e );
+ }
return Collections2.transform( users.getData(), new Function<UserResource, User>()
{
View
33 ...ain/java/org/sonatype/nexus/client/rest/jersey/ContextAwareUniformInterfaceException.java
@@ -0,0 +1,33 @@
+/**
+ * Sonatype Nexus (TM) Open Source Version
+ * Copyright (c) 2007-2012 Sonatype, Inc.
+ * All rights reserved. Includes the third-party code listed at http://links.sonatype.com/products/nexus/oss/attributions.
+ *
+ * This program and the accompanying materials are made available under the terms of the Eclipse Public License Version 1.0,
+ * which accompanies this distribution and is available at http://www.eclipse.org/legal/epl-v10.html.
+ *
+ * Sonatype Nexus (TM) Professional Version is available from Sonatype, Inc. "Sonatype" and "Sonatype Nexus" are trademarks
+ * of Sonatype, Inc. Apache Maven is a trademark of the Apache Software Foundation. M2eclipse is a trademark of the
+ * Eclipse Foundation. All other trademarks are the property of their respective owners.
+ */
+package org.sonatype.nexus.client.rest.jersey;
+
+import com.sun.jersey.api.client.ClientResponse;
+import com.sun.jersey.api.client.UniformInterfaceException;
+
+/**
+ * @since 2.3
+ */
+@SuppressWarnings( "serial" )
+public abstract class ContextAwareUniformInterfaceException
+ extends UniformInterfaceException
+{
+
+ public ContextAwareUniformInterfaceException( final ClientResponse response )
+ {
+ super( response );
+ }
+
+ public abstract String getMessage( int status );
+
+}
View
158 ...us-client-core/src/main/java/org/sonatype/nexus/client/rest/jersey/JerseyNexusClient.java
@@ -12,17 +12,20 @@
*/
package org.sonatype.nexus.client.rest.jersey;
+import java.io.IOException;
import java.util.Collection;
import java.util.LinkedHashMap;
-import java.util.Map;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
+import org.codehaus.plexus.util.IOUtil;
import org.sonatype.nexus.client.core.Condition;
-import org.sonatype.nexus.client.core.NexusErrorMessageException;
import org.sonatype.nexus.client.core.NexusStatus;
+import org.sonatype.nexus.client.core.exception.NexusClientErrorResponseException;
+import org.sonatype.nexus.client.core.exception.NexusClientException;
+import org.sonatype.nexus.client.core.exception.NexusClientNotFoundException;
+import org.sonatype.nexus.client.core.exception.NexusClientResponseException;
import org.sonatype.nexus.client.core.spi.SubsystemFactory;
-import org.sonatype.nexus.client.internal.msg.ErrorMessage;
import org.sonatype.nexus.client.internal.msg.ErrorResponse;
import org.sonatype.nexus.client.internal.rest.AbstractXStreamNexusClient;
import org.sonatype.nexus.client.internal.util.Check;
@@ -30,6 +33,9 @@
import org.sonatype.nexus.rest.model.StatusResource;
import org.sonatype.nexus.rest.model.StatusResourceResponse;
import com.sun.jersey.api.client.Client;
+import com.sun.jersey.api.client.ClientHandlerException;
+import com.sun.jersey.api.client.ClientResponse;
+import com.sun.jersey.api.client.UniformInterfaceException;
import com.sun.jersey.api.client.WebResource;
import com.thoughtworks.xstream.XStream;
@@ -123,13 +129,27 @@ public String resolveServicePath( final String path )
@Override
public NexusStatus getStatus()
{
- final StatusResource response = serviceResource( "status" ).get( StatusResourceResponse.class ).getData();
- return new NexusStatus( response.getAppName(), response.getFormattedAppName(), response.getVersion(),
- response.getApiVersion(), response.getEditionLong(), response.getEditionShort(),
- response.getState(),
- response.getInitializedAt(), response.getStartedAt(), response.getLastConfigChange(),
- -1,
- response.getBaseUrl() );
+ try
+ {
+ final StatusResource response = serviceResource( "status" )
+ .get( StatusResourceResponse.class )
+ .getData();
+ return new NexusStatus( response.getAppName(), response.getFormattedAppName(), response.getVersion(),
+ response.getApiVersion(), response.getEditionLong(), response.getEditionShort(),
+ response.getState(),
+ response.getInitializedAt(), response.getStartedAt(),
+ response.getLastConfigChange(),
+ -1,
+ response.getBaseUrl() );
+ }
+ catch ( UniformInterfaceException e )
+ {
+ throw convert( e );
+ }
+ catch ( ClientHandlerException e )
+ {
+ throw convert( e );
+ }
}
@Override
@@ -192,21 +212,117 @@ protected void initializeSubsystems( final SubsystemFactory<?, JerseyNexusClient
}
}
- // ==
+ public NexusClientNotFoundException convertIf404( final UniformInterfaceException e )
+ {
+ final ClientResponse response = e.getResponse();
+ if ( ClientResponse.Status.NOT_FOUND.equals( response.getClientResponseStatus() ) )
+ {
+ return new NexusClientNotFoundException(
+ getMessageIfPresent( ClientResponse.Status.NOT_FOUND.getStatusCode(), e ),
+ response.getClientResponseStatus().getReasonPhrase(),
+ consume( response )
+ );
+ }
+ return null;
+ }
- /**
- * Internal method to be used by subsystem implementations to convert Jersey specific exception to
- * {@link NexusErrorMessageException}.
- */
- public NexusErrorMessageException convertErrorResponse( final int statusCode, final String reasonPhrase,
- final ErrorResponse errorResponse )
+ public NexusClientErrorResponseException convertIf400WithErrorMessage( final UniformInterfaceException e )
{
- final Map<String, String> errors = new LinkedHashMap<String, String>();
- for ( ErrorMessage errorMessage : errorResponse.getErrors() )
+ final ClientResponse response = e.getResponse();
+ if ( ClientResponse.Status.BAD_REQUEST.equals( response.getClientResponseStatus() ) )
{
- errors.put( errorMessage.getId(), errorMessage.getMsg() );
+ final String body = getResponseBody( response );
+ ErrorResponse errorResponse = null;
+ try
+ {
+ errorResponse = response.getEntity( ErrorResponse.class );
+ }
+ catch ( Exception e1 )
+ {
+ // ignore
+ }
+ if ( errorResponse != null )
+ {
+ return new NexusClientErrorResponseException(
+ response.getClientResponseStatus().getReasonPhrase(),
+ body,
+ errorResponse
+ );
+ }
+ }
+ return null;
+ }
+
+ public NexusClientException convert( final UniformInterfaceException e )
+ {
+ NexusClientException exception = convertIfKnown( e );
+ if ( exception != null )
+ {
+ return exception;
}
- return new NexusErrorMessageException( statusCode, reasonPhrase, errors );
+ return new NexusClientResponseException(
+ getMessageIfPresent( e.getResponse().getClientResponseStatus().getStatusCode(), e ),
+ e.getResponse().getClientResponseStatus().getStatusCode(),
+ e.getResponse().getClientResponseStatus().getReasonPhrase(),
+ consume( e.getResponse() )
+ );
}
+
+ public NexusClientException convertIfKnown( final UniformInterfaceException e )
+ {
+ NexusClientException exception = convertIf404( e );
+ if ( exception != null )
+ {
+ return exception;
+ }
+
+ exception = convertIf400WithErrorMessage( e );
+ if ( exception != null )
+ {
+ return exception;
+ }
+
+ return null;
+ }
+
+ public NexusClientException convert( final ClientHandlerException e )
+ {
+ throw new NexusClientHandlerException( e );
+ }
+
+ public String consume( final ClientResponse response )
+ {
+ try
+ {
+ return response.getEntity( String.class );
+ }
+ catch ( Exception e )
+ {
+ // maybe already consumed
+ return null;
+ }
+ }
+
+ private static String getResponseBody( final ClientResponse response )
+ {
+ try
+ {
+ return IOUtil.toString( response.getEntityInputStream(), "UTF-8" );
+ }
+ catch ( IOException e )
+ {
+ return null;
+ }
+ }
+
+ private String getMessageIfPresent( final int status, final UniformInterfaceException e )
+ {
+ if ( e instanceof ContextAwareUniformInterfaceException )
+ {
+ return ( (ContextAwareUniformInterfaceException) e ).getMessage( status );
+ }
+ return null;
+ }
+
}
View
12 .../nexus/client/core/NotFoundException.java → ...t/jersey/NexusClientHandlerException.java
@@ -10,18 +10,22 @@
* of Sonatype, Inc. Apache Maven is a trademark of the Apache Software Foundation. M2eclipse is a trademark of the
* Eclipse Foundation. All other trademarks are the property of their respective owners.
*/
-package org.sonatype.nexus.client.core;
+package org.sonatype.nexus.client.rest.jersey;
+
+import org.sonatype.nexus.client.core.exception.NexusClientException;
+import com.sun.jersey.api.client.ClientHandlerException;
/**
* @since 2.3
*/
-public class NotFoundException
+@SuppressWarnings( "serial" )
+public class NexusClientHandlerException
extends NexusClientException
{
- public NotFoundException()
+ public NexusClientHandlerException( final ClientHandlerException cause )
{
- super( "" );
+ super( cause );
}
}
View
17 ...client/src/main/java/org/sonatype/nexus/testsuite/client/internal/JerseyRemoteLogger.java
@@ -20,7 +20,9 @@
import org.slf4j.helpers.MessageFormatter;
import org.sonatype.nexus.client.core.spi.SubsystemSupport;
import org.sonatype.nexus.client.rest.jersey.JerseyNexusClient;
+import com.sun.jersey.api.client.ClientHandlerException;
import com.sun.jersey.api.client.ClientResponse;
+import com.sun.jersey.api.client.UniformInterfaceException;
import com.sun.jersey.core.util.MultivaluedMapImpl;
/**
@@ -67,7 +69,20 @@ private void log( final String level, final String message, final Throwable t )
queryParams.add( "exceptionMessage", exceptionMessage );
}
}
- getNexusClient().serviceResource( "loghelper", queryParams ).get( ClientResponse.class ).close();
+ try
+ {
+ getNexusClient()
+ .serviceResource( "loghelper", queryParams )
+ .get( ClientResponse.class );
+ }
+ catch ( UniformInterfaceException e )
+ {
+ throw JerseyNexusClient.convert( e );
+ }
+ catch ( ClientHandlerException e )
+ {
+ throw JerseyNexusClient.convert( e );
+ }
}
private void formatAndLog( final String level, final String format, final Object arg1, final Object arg2 )
View
22 ...ite-client/src/main/java/org/sonatype/nexus/testsuite/client/internal/JerseyUIDLocks.java
@@ -15,9 +15,12 @@
import static java.lang.String.format;
import org.sonatype.nexus.client.core.spi.SubsystemSupport;
+import org.sonatype.nexus.client.rest.jersey.JerseyExceptions;
import org.sonatype.nexus.client.rest.jersey.JerseyNexusClient;
import org.sonatype.nexus.testsuite.client.UIDLocks;
+import com.sun.jersey.api.client.ClientHandlerException;
import com.sun.jersey.api.client.ClientResponse;
+import com.sun.jersey.api.client.UniformInterfaceException;
/**
* Jersey based {@link UIDLocks} Nexus Client Subsystem implementation.
@@ -38,14 +41,29 @@ public JerseyUIDLocks( final JerseyNexusClient nexusClient )
public void lock( final String repository, final String path, final LockType lockType )
{
final String uri = format( "nexus-it-helper-plugin/uid/lock/%s/%s/%s", repository, lockType.name(), path );
- getNexusClient().serviceResource( uri ).get( ClientResponse.class ).close();
+ try
+ {
+ getNexusClient()
+ .serviceResource( uri )
+ .get( ClientResponse.class );
+ }
+ catch ( ClientHandlerException e )
+ {
+ throw JerseyExceptions.convertAndPropagate( e );
+ }
+ catch ( UniformInterfaceException e )
+ {
+ throw JerseyExceptions.convertAndPropagate( e );
+ }
}
@Override
public void unlock( final String repository, final String path )
{
final String uri = format( "nexus-it-helper-plugin/uid/lock/%s/unlock/%s", repository, path );
- getNexusClient().serviceResource( uri ).delete();
+ getNexusClient()
+ .serviceResource( uri )
+ .delete( ClientResponse.class );
}
}
View
35 ...-client/src/main/java/org/sonatype/nexus/mindexer/client/internal/JerseyMavenIndexer.java
@@ -19,7 +19,6 @@
import javax.ws.rs.core.MultivaluedMap;
import org.sonatype.nexus.client.core.NexusUnexpectedResponseException;
-import org.sonatype.nexus.client.internal.msg.ErrorResponse;
import org.sonatype.nexus.client.rest.jersey.JerseyNexusClient;
import org.sonatype.nexus.mindexer.client.SearchRequest;
import org.sonatype.nexus.mindexer.client.SearchResponse;
@@ -32,7 +31,8 @@
import org.sonatype.nexus.rest.model.NexusNGRepositoryDetail;
import org.sonatype.nexus.rest.model.SearchNGResponse;
-import com.sun.jersey.api.client.ClientResponse;
+import com.sun.jersey.api.client.ClientHandlerException;
+import com.sun.jersey.api.client.UniformInterfaceException;
import com.sun.jersey.core.util.MultivaluedMapImpl;
/**
@@ -84,29 +84,22 @@ public SearchResponse search( final SearchRequest request )
queryParams.add( "collapseresults", request.getCollapseresults().toString() );
}
- final ClientResponse clientResponse =
- getNexusClient().serviceResource( "lucene/search", queryParams ).get( ClientResponse.class );
try
{
- if ( clientResponse.getClientResponseStatus().getStatusCode() == 200 )
- {
- return toResponse( request, clientResponse.getEntity( SearchNGResponse.class ) );
- }
- else if ( clientResponse.getClientResponseStatus().getStatusCode() == 400 )
- {
- final ErrorResponse errorResponse = clientResponse.getEntity( ErrorResponse.class );
- throw getNexusClient().convertErrorResponse( clientResponse.getClientResponseStatus().getStatusCode(),
- clientResponse.getClientResponseStatus().getReasonPhrase(), errorResponse );
- }
- else
- {
- throw new NexusUnexpectedResponseException( clientResponse.getClientResponseStatus().getStatusCode(),
- clientResponse.getClientResponseStatus().getReasonPhrase() );
- }
+ return toResponse(
+ request,
+ getNexusClient()
+ .serviceResource( "lucene/search", queryParams )
+ .get( SearchNGResponse.class )
+ );
+ }
+ catch ( UniformInterfaceException e )
+ {
+ throw JerseyNexusClient.convert( e );
}
- finally
+ catch ( ClientHandlerException e )
{
- clientResponse.close();
+ throw JerseyNexusClient.convert( e );
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.