mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-12 16:35:45 +01:00
improve exception handling
This commit is contained in:
@@ -33,6 +33,11 @@
|
||||
|
||||
package sonia.scm.client;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import com.sun.jersey.api.client.Client;
|
||||
@@ -47,6 +52,67 @@ import com.sun.jersey.api.client.filter.LoggingFilter;
|
||||
public class ClientUtil
|
||||
{
|
||||
|
||||
/** the logger for ClientUtil */
|
||||
private static final Logger logger =
|
||||
LoggerFactory.getLogger(ClientUtil.class);
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param exception
|
||||
* @param response
|
||||
*/
|
||||
public static void appendContent(ScmClientException exception,
|
||||
ClientResponse response)
|
||||
{
|
||||
try
|
||||
{
|
||||
exception.setContent(response.getEntity(String.class));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.warn("could not read content", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param response
|
||||
* @param expectedStatusCode
|
||||
*/
|
||||
public static void checkResponse(ClientResponse response,
|
||||
int expectedStatusCode)
|
||||
{
|
||||
int sc = response.getStatus();
|
||||
|
||||
if (sc != expectedStatusCode)
|
||||
{
|
||||
sendException(response, sc);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param response
|
||||
*
|
||||
*/
|
||||
public static void checkResponse(ClientResponse response)
|
||||
{
|
||||
int sc = response.getStatus();
|
||||
|
||||
if (sc >= 300)
|
||||
{
|
||||
sendException(response, sc);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
@@ -97,4 +163,57 @@ public class ClientUtil
|
||||
|
||||
return resource;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param response
|
||||
* @param sc
|
||||
*/
|
||||
public static void sendException(ClientResponse response, int sc)
|
||||
{
|
||||
ScmClientException exception = null;
|
||||
|
||||
switch (sc)
|
||||
{
|
||||
case ScmClientException.SC_UNAUTHORIZED :
|
||||
exception = new ScmUnauthorizedException();
|
||||
|
||||
break;
|
||||
|
||||
case ScmClientException.SC_FORBIDDEN :
|
||||
exception = new ScmForbiddenException();
|
||||
|
||||
break;
|
||||
|
||||
case ScmClientException.SC_NOTFOUND :
|
||||
exception = new ScmNotFoundException();
|
||||
|
||||
break;
|
||||
|
||||
default :
|
||||
exception = new ScmClientException(sc);
|
||||
appendContent(exception, response);
|
||||
}
|
||||
|
||||
throw exception;
|
||||
}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param response
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static boolean isSuccessfull(ClientResponse response)
|
||||
{
|
||||
int status = response.getStatus();
|
||||
|
||||
return (status > 200) && (status < 300);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,12 +95,10 @@ public class JerseyClientProvider implements ScmClientProvider
|
||||
*
|
||||
* @return
|
||||
*
|
||||
* @throws ScmClientException
|
||||
*/
|
||||
@Override
|
||||
public JerseyClientSession createSession(String url, String username,
|
||||
String password)
|
||||
throws ScmClientException
|
||||
{
|
||||
AssertUtil.assertIsNotEmpty(url);
|
||||
|
||||
@@ -153,24 +151,7 @@ public class JerseyClientProvider implements ScmClientProvider
|
||||
response = resource.get(ClientResponse.class);
|
||||
}
|
||||
|
||||
if (response.getStatus() != 200)
|
||||
{
|
||||
String msg =
|
||||
"server returned ".concat(String.valueOf(response.getStatus()));
|
||||
|
||||
if (logger.isWarnEnabled())
|
||||
{
|
||||
logger.warn(msg);
|
||||
}
|
||||
|
||||
if (logger.isTraceEnabled())
|
||||
{
|
||||
logger.trace("server returned content: {}",
|
||||
response.getEntity(String.class));
|
||||
}
|
||||
|
||||
throw new ScmClientException(msg);
|
||||
}
|
||||
ClientUtil.checkResponse(response);
|
||||
|
||||
ScmState state = response.getEntity(ScmState.class);
|
||||
|
||||
|
||||
@@ -83,10 +83,9 @@ public class JerseyClientSession implements ScmClientSession
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
@Override
|
||||
public void close() throws IOException
|
||||
public void close()
|
||||
{
|
||||
if (logger.isInfoEnabled())
|
||||
{
|
||||
|
||||
@@ -88,26 +88,19 @@ public class JerseyRepositoryClientHandler implements RepositoryClientHandler
|
||||
try
|
||||
{
|
||||
response = resource.post(ClientResponse.class, repository);
|
||||
ClientUtil.checkResponse(response, 201);
|
||||
|
||||
if (response.getStatus() == 201)
|
||||
{
|
||||
String url = response.getHeaders().get("Location").get(0);
|
||||
String url = response.getHeaders().get("Location").get(0);
|
||||
|
||||
AssertUtil.assertIsNotEmpty(url);
|
||||
AssertUtil.assertIsNotEmpty(url);
|
||||
|
||||
Repository newRepository = getRepository(url);
|
||||
Repository newRepository = getRepository(url);
|
||||
|
||||
AssertUtil.assertIsNotNull(newRepository);
|
||||
newRepository.copyProperties(repository);
|
||||
AssertUtil.assertIsNotNull(newRepository);
|
||||
newRepository.copyProperties(repository);
|
||||
|
||||
// copyProperties does not copy the repository id
|
||||
repository.setId(newRepository.getId());
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
// todo errorhandling
|
||||
}
|
||||
// copyProperties does not copy the repository id
|
||||
repository.setId(newRepository.getId());
|
||||
}
|
||||
finally
|
||||
{
|
||||
@@ -132,12 +125,7 @@ public class JerseyRepositoryClientHandler implements RepositoryClientHandler
|
||||
try
|
||||
{
|
||||
response = resource.delete(ClientResponse.class);
|
||||
|
||||
if (response.getStatus() != 204)
|
||||
{
|
||||
|
||||
// todo errorhandling
|
||||
}
|
||||
ClientUtil.checkResponse(response, 204);
|
||||
}
|
||||
finally
|
||||
{
|
||||
@@ -179,12 +167,7 @@ public class JerseyRepositoryClientHandler implements RepositoryClientHandler
|
||||
try
|
||||
{
|
||||
response = resource.post(ClientResponse.class, repository);
|
||||
|
||||
if (response.getStatus() != 204)
|
||||
{
|
||||
|
||||
// todo errorhandling
|
||||
}
|
||||
ClientUtil.checkResponse(response, 204);
|
||||
}
|
||||
finally
|
||||
{
|
||||
@@ -224,17 +207,9 @@ public class JerseyRepositoryClientHandler implements RepositoryClientHandler
|
||||
try
|
||||
{
|
||||
response = resource.get(ClientResponse.class);
|
||||
|
||||
if (response.getStatus() == 200)
|
||||
{
|
||||
repositories = response.getEntity(new GenericType<List<Repository>>() {}
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
// todo errorhandling
|
||||
}
|
||||
ClientUtil.checkResponse(response, 200);
|
||||
repositories = response.getEntity(new GenericType<List<Repository>>() {}
|
||||
);
|
||||
}
|
||||
finally
|
||||
{
|
||||
@@ -274,14 +249,12 @@ public class JerseyRepositoryClientHandler implements RepositoryClientHandler
|
||||
{
|
||||
response = resource.get(ClientResponse.class);
|
||||
|
||||
if (response.getStatus() == 200)
|
||||
{
|
||||
repository = response.getEntity(Repository.class);
|
||||
}
|
||||
else
|
||||
{
|
||||
int sc = response.getStatus();
|
||||
|
||||
// todo errorhandling
|
||||
if (sc != ScmClientException.SC_NOTFOUND)
|
||||
{
|
||||
ClientUtil.checkResponse(response, 200);
|
||||
repository = response.getEntity(Repository.class);
|
||||
}
|
||||
}
|
||||
finally
|
||||
|
||||
@@ -39,6 +39,7 @@ import org.junit.Test;
|
||||
|
||||
import sonia.scm.client.JerseyClientSession;
|
||||
import sonia.scm.client.ScmClientException;
|
||||
import sonia.scm.client.ScmUnauthorizedException;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
@@ -60,12 +61,9 @@ public class JerseyClientProviderITCase
|
||||
*
|
||||
*
|
||||
*
|
||||
* @throws IOException
|
||||
* @throws ScmClientException
|
||||
*/
|
||||
@Test(expected = ScmClientException.class)
|
||||
@Test(expected = ScmUnauthorizedException.class)
|
||||
public void createSessionAnonymousFailedTest()
|
||||
throws ScmClientException, IOException
|
||||
{
|
||||
createAnonymousSession().close();
|
||||
}
|
||||
@@ -75,12 +73,9 @@ public class JerseyClientProviderITCase
|
||||
*
|
||||
*
|
||||
*
|
||||
* @throws IOException
|
||||
* @throws ScmClientException
|
||||
*/
|
||||
@Test
|
||||
public void createSessionAnonymousTest()
|
||||
throws ScmClientException, IOException
|
||||
{
|
||||
|
||||
// enable anonymous access
|
||||
@@ -98,11 +93,9 @@ public class JerseyClientProviderITCase
|
||||
*
|
||||
*
|
||||
*
|
||||
* @throws IOException
|
||||
* @throws ScmClientException
|
||||
*/
|
||||
@Test
|
||||
public void createSessionTest() throws ScmClientException, IOException
|
||||
public void createSessionTest()
|
||||
{
|
||||
JerseyClientSession session = createAdminSession();
|
||||
|
||||
@@ -121,7 +114,7 @@ public class JerseyClientProviderITCase
|
||||
* @throws IOException
|
||||
* @throws ScmClientException
|
||||
*/
|
||||
@Test(expected = ScmClientException.class)
|
||||
@Test(expected = ScmUnauthorizedException.class)
|
||||
public void createSessionWithUnkownUserTest()
|
||||
throws ScmClientException, IOException
|
||||
{
|
||||
@@ -136,7 +129,7 @@ public class JerseyClientProviderITCase
|
||||
* @throws IOException
|
||||
* @throws ScmClientException
|
||||
*/
|
||||
@Test(expected = ScmClientException.class)
|
||||
@Test(expected = ScmUnauthorizedException.class)
|
||||
public void createSessionWithWrongPasswordTest()
|
||||
throws ScmClientException, IOException
|
||||
{
|
||||
|
||||
@@ -40,7 +40,8 @@ import org.junit.Test;
|
||||
|
||||
import sonia.scm.client.JerseyClientSession;
|
||||
import sonia.scm.client.RepositoryClientHandler;
|
||||
import sonia.scm.client.ScmClientException;
|
||||
import sonia.scm.client.ScmForbiddenException;
|
||||
import sonia.scm.client.ScmUnauthorizedException;
|
||||
import sonia.scm.repository.Repository;
|
||||
import sonia.scm.repository.RepositoryTestData;
|
||||
import sonia.scm.util.Util;
|
||||
@@ -51,8 +52,6 @@ import static sonia.scm.client.it.TestUtil.*;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -66,12 +65,9 @@ public class JerseyClientRepositoryClientHandlerITCase
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @throws IOException
|
||||
* @throws ScmClientException
|
||||
*/
|
||||
@AfterClass
|
||||
public static void removeTestRepositories()
|
||||
throws ScmClientException, IOException
|
||||
{
|
||||
JerseyClientSession session = createAdminSession();
|
||||
RepositoryClientHandler handler = session.getRepositoryHandler();
|
||||
@@ -86,17 +82,16 @@ public class JerseyClientRepositoryClientHandlerITCase
|
||||
}
|
||||
|
||||
session.close();
|
||||
setAnonymousAccess(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @throws IOException
|
||||
* @throws ScmClientException
|
||||
*/
|
||||
@Test
|
||||
public void testCreate() throws ScmClientException, IOException
|
||||
public void testCreate()
|
||||
{
|
||||
JerseyClientSession session = createAdminSession();
|
||||
Repository hog = RepositoryTestData.createHeartOfGold(REPOSITORY_TYPE);
|
||||
@@ -118,28 +113,9 @@ public class JerseyClientRepositoryClientHandlerITCase
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @throws IOException
|
||||
* @throws ScmClientException
|
||||
*/
|
||||
@Test(expected = ScmClientException.class)
|
||||
public void testCreateAnonymous() throws ScmClientException, IOException
|
||||
{
|
||||
JerseyClientSession session = createAnonymousSession();
|
||||
Repository p42 = RepositoryTestData.create42Puzzle(REPOSITORY_TYPE);
|
||||
|
||||
session.getRepositoryHandler().create(p42);
|
||||
session.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @throws IOException
|
||||
* @throws ScmClientException
|
||||
*/
|
||||
@Test
|
||||
public void testDelete() throws ScmClientException, IOException
|
||||
public void testDelete()
|
||||
{
|
||||
JerseyClientSession session = createAdminSession();
|
||||
Repository hvpt =
|
||||
@@ -158,4 +134,47 @@ public class JerseyClientRepositoryClientHandlerITCase
|
||||
|
||||
assertNull(r);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
*/
|
||||
@Test(expected = ScmUnauthorizedException.class)
|
||||
public void testDisabledCreateAnonymous()
|
||||
{
|
||||
JerseyClientSession session = createAnonymousSession();
|
||||
Repository p42 = RepositoryTestData.create42Puzzle(REPOSITORY_TYPE);
|
||||
|
||||
session.getRepositoryHandler().create(p42);
|
||||
session.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
*/
|
||||
@Test
|
||||
public void testEnabledCreateAnonymous()
|
||||
{
|
||||
setAnonymousAccess(true);
|
||||
|
||||
JerseyClientSession session = createAnonymousSession();
|
||||
Repository p42 = RepositoryTestData.create42Puzzle(REPOSITORY_TYPE);
|
||||
boolean forbidden = false;
|
||||
|
||||
try
|
||||
{
|
||||
session.getRepositoryHandler().create(p42);
|
||||
}
|
||||
catch (ScmForbiddenException ex)
|
||||
{
|
||||
forbidden = true;
|
||||
}
|
||||
|
||||
assertTrue(forbidden);
|
||||
session.close();
|
||||
setAnonymousAccess(false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,7 +38,6 @@ package sonia.scm.client.it;
|
||||
import sonia.scm.client.ClientUtil;
|
||||
import sonia.scm.client.JerseyClientProvider;
|
||||
import sonia.scm.client.JerseyClientSession;
|
||||
import sonia.scm.client.ScmClientException;
|
||||
import sonia.scm.client.ScmUrlProvider;
|
||||
import sonia.scm.config.ScmConfiguration;
|
||||
|
||||
@@ -47,8 +46,6 @@ import sonia.scm.config.ScmConfiguration;
|
||||
import com.sun.jersey.api.client.Client;
|
||||
import com.sun.jersey.api.client.WebResource;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
@@ -79,10 +76,8 @@ public class TestUtil
|
||||
*
|
||||
* @return
|
||||
*
|
||||
* @throws ScmClientException
|
||||
*/
|
||||
public static JerseyClientSession createAdminSession()
|
||||
throws ScmClientException
|
||||
{
|
||||
return createSession(ADMIN_USERNAME, ADMIN_PASSWORD);
|
||||
}
|
||||
@@ -93,10 +88,8 @@ public class TestUtil
|
||||
*
|
||||
* @return
|
||||
*
|
||||
* @throws ScmClientException
|
||||
*/
|
||||
public static JerseyClientSession createAnonymousSession()
|
||||
throws ScmClientException
|
||||
{
|
||||
return createSession(null, null);
|
||||
}
|
||||
@@ -110,11 +103,9 @@ public class TestUtil
|
||||
*
|
||||
* @return
|
||||
*
|
||||
* @throws ScmClientException
|
||||
*/
|
||||
public static JerseyClientSession createSession(String username,
|
||||
String password)
|
||||
throws ScmClientException
|
||||
{
|
||||
JerseyClientProvider provider = new JerseyClientProvider(REQUEST_LOGGING);
|
||||
|
||||
@@ -129,11 +120,8 @@ public class TestUtil
|
||||
*
|
||||
* @param access
|
||||
*
|
||||
* @throws IOException
|
||||
* @throws ScmClientException
|
||||
*/
|
||||
public static void setAnonymousAccess(boolean access)
|
||||
throws ScmClientException, IOException
|
||||
{
|
||||
JerseyClientSession adminSession = createAdminSession();
|
||||
ScmUrlProvider up = adminSession.getUrlProvider();
|
||||
|
||||
Reference in New Issue
Block a user