improve exception handling

This commit is contained in:
Sebastian Sdorra
2011-05-12 19:36:10 +02:00
parent 8c2a383b2b
commit 883b9f1a3c
15 changed files with 486 additions and 146 deletions

View File

@@ -1,10 +1,10 @@
/**
* Copyright (c) 2010, Sebastian Sdorra
* All rights reserved.
*
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
@@ -13,7 +13,7 @@
* 3. Neither the name of SCM-Manager; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
@@ -24,27 +24,34 @@
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*
* http://bitbucket.org/sdorra/scm-manager
*
*
*/
package sonia.scm.client;
//~--- JDK imports ------------------------------------------------------------
import java.util.List;
/**
*
* @author Sebastian Sdorra
*
* @param <T>
*/
public interface ClientHandler<T>
{
/**
* Method description
*
*
* @param repository
*
* @param item
*/
public void create(T item);
@@ -60,7 +67,8 @@ public interface ClientHandler<T>
* Method description
*
*
* @param repository
*
* @param item
*/
public void delete(T item);
@@ -68,20 +76,13 @@ public interface ClientHandler<T>
* Method description
*
*
* @param repository
*
* @param item
*/
public void modify(T item);
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @return
*/
public List<T> getAll();
/**
* Method description
*
@@ -91,4 +92,12 @@ public interface ClientHandler<T>
* @return
*/
public T get(String id);
/**
* Method description
*
*
* @return
*/
public List<T> getAll();
}

View File

@@ -98,9 +98,8 @@ public class ScmClient
*
* @return
*
* @throws ScmClientException
*/
private static ScmClientProvider getProvider() throws ScmClientException
private static ScmClientProvider getProvider()
{
if (provider == null)
{

View File

@@ -37,14 +37,36 @@ package sonia.scm.client;
*
* @author Sebastian Sdorra
*/
public class ScmClientException extends Exception
public class ScmClientException extends RuntimeException
{
/** Field description */
public static final int SC_FORBIDDEN = 403;
/** Field description */
public static final int SC_NOTFOUND = 404;
/** Field description */
public static final int SC_UNAUTHORIZED = 401;
/** Field description */
public static final int SC_UNKNOWN = -1;
/** Field description */
private static final long serialVersionUID = -2302107106896701393L;
//~--- constructors ---------------------------------------------------------
/**
* Constructs ...
*
*
* @param statusCode
*/
public ScmClientException() {}
public ScmClientException(int statusCode)
{
this.statusCode = statusCode;
}
/**
* Constructs ...
@@ -68,6 +90,20 @@ public class ScmClientException extends Exception
super(cause);
}
/**
* Constructs ...
*
*
*
* @param statusCode
* @param message
*/
public ScmClientException(int statusCode, String message)
{
super(message);
this.statusCode = statusCode;
}
/**
* Constructs ...
*
@@ -79,4 +115,60 @@ public class ScmClientException extends Exception
{
super(message, cause);
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @return
*/
public String getContent()
{
return content;
}
/**
* Method description
*
*
* @return
*/
public int getStatusCode()
{
return statusCode;
}
//~--- set methods ----------------------------------------------------------
/**
* Method description
*
*
* @param content
*/
public void setContent(String content)
{
this.content = content;
}
/**
* Method description
*
*
* @param statusCode
*/
public void setStatusCode(int statusCode)
{
this.statusCode = statusCode;
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private String content;
/** Field description */
private int statusCode = SC_UNKNOWN;
}

View File

@@ -50,9 +50,7 @@ public interface ScmClientProvider
*
* @return
*
* @throws ScmClientException
*/
public ScmClientSession createSession(String url, String username,
String password)
throws ScmClientException;
String password);
}

View File

@@ -50,6 +50,15 @@ import java.io.Closeable;
public interface ScmClientSession extends Closeable
{
/**
* Method description
*
*/
@Override
public void close();
//~--- get methods ----------------------------------------------------------
/**
* Method description
*

View File

@@ -0,0 +1,67 @@
/**
* Copyright (c) 2010, Sebastian Sdorra
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of SCM-Manager; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* http://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.client;
/**
*
* @author Sebastian Sdorra
*/
public class ScmForbiddenException extends ScmClientException
{
/** Field description */
private static final long serialVersionUID = 3937346624508458660L;
//~--- constructors ---------------------------------------------------------
/**
* Constructs ...
*
*/
public ScmForbiddenException()
{
super(SC_FORBIDDEN);
}
/**
* Constructs ...
*
*
* @param message
*/
public ScmForbiddenException(String message)
{
super(SC_FORBIDDEN, message);
}
}

View File

@@ -37,4 +37,31 @@ package sonia.scm.client;
*
* @author Sebastian Sdorra
*/
public class UnauthorizedException extends ScmClientException {}
public class ScmNotFoundException extends ScmClientException
{
/** Field description */
private static final long serialVersionUID = -7015126639998723954L;
//~--- constructors ---------------------------------------------------------
/**
* Constructs ...
*
*/
public ScmNotFoundException()
{
super(SC_NOTFOUND);
}
/**
* Constructs ...
*
*
* @param message
*/
public ScmNotFoundException(String message)
{
super(SC_NOTFOUND, message);
}
}

View File

@@ -0,0 +1,67 @@
/**
* Copyright (c) 2010, Sebastian Sdorra
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of SCM-Manager; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* http://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.client;
/**
*
* @author Sebastian Sdorra
*/
public class ScmUnauthorizedException extends ScmClientException
{
/** Field description */
private static final long serialVersionUID = 4398907424134588809L;
//~--- constructors ---------------------------------------------------------
/**
* Constructs ...
*
*/
public ScmUnauthorizedException()
{
super(SC_UNAUTHORIZED);
}
/**
* Constructs ...
*
*
* @param message
*/
public ScmUnauthorizedException(String message)
{
super(SC_UNAUTHORIZED, message);
}
}

View File

@@ -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);
}
}

View File

@@ -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);

View File

@@ -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())
{

View File

@@ -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

View File

@@ -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
{

View File

@@ -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);
}
}

View File

@@ -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();