From 73025bf7e5d3d70ab4c4b895685e84d31e2023ed Mon Sep 17 00:00:00 2001 From: Brian Remmington Date: Fri, 17 Sep 2010 15:31:30 +0000 Subject: [PATCH] ALF-4842 (Transfer Service): - Fixed issue in rehydrating exception received from target repo. Added appropriate test case. git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@22618 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../transfer/HttpClientTransmitterImpl.java | 29 ++++++++++--------- .../HttpClientTransmitterImplTest.java | 28 ++++++++++++++++++ 2 files changed, 43 insertions(+), 14 deletions(-) diff --git a/source/java/org/alfresco/repo/transfer/HttpClientTransmitterImpl.java b/source/java/org/alfresco/repo/transfer/HttpClientTransmitterImpl.java index a410dafbec..c50a7337eb 100644 --- a/source/java/org/alfresco/repo/transfer/HttpClientTransmitterImpl.java +++ b/source/java/org/alfresco/repo/transfer/HttpClientTransmitterImpl.java @@ -60,7 +60,6 @@ import org.apache.commons.httpclient.protocol.ProtocolSocketFactory; import org.apache.commons.httpclient.protocol.SSLProtocolSocketFactory; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -171,24 +170,26 @@ public class HttpClientTransmitterImpl implements TransferTransmitter { if (response != 200) { - String errorId = null; - String[] errorParams = null; - try { + Throwable error = null; + try + { log.error("Received \"unsuccessful\" response code from target server: " + response); String errorPayload = method.getResponseBodyAsString(); JSONObject errorObj = new JSONObject(errorPayload); - errorId = errorObj.getString("alfrescoErrorId"); - JSONArray errorParamArray = errorObj.getJSONArray("alfrescoErrorParams"); - int length = errorParamArray.length(); - errorParams = new String[length]; - for (int i = 0; i < length; ++i) - { - errorParams[i] = errorParamArray.getString(i); - } - } catch (Exception ex) { + error = rehydrateError(errorObj); + } + catch (Exception ex) + { throw new TransferException(MSG_UNSUCCESSFUL_RESPONSE, new Object[] {methodName, response}); } - throw new TransferException(errorId, errorParams); + if ((error != null) && TransferException.class.isAssignableFrom(error.getClass())) + { + throw (TransferException)error; + } + else + { + throw new TransferException(MSG_UNSUCCESSFUL_RESPONSE, error); + } } } diff --git a/source/java/org/alfresco/repo/transfer/HttpClientTransmitterImplTest.java b/source/java/org/alfresco/repo/transfer/HttpClientTransmitterImplTest.java index 1c42df586c..28744be433 100644 --- a/source/java/org/alfresco/repo/transfer/HttpClientTransmitterImplTest.java +++ b/source/java/org/alfresco/repo/transfer/HttpClientTransmitterImplTest.java @@ -238,6 +238,34 @@ public class HttpClientTransmitterImplTest extends TestCase assertTrue(Arrays.deepEquals(expectedException.getMsgParams(), receivedException.getMsgParams())); } + public void testBeginFailure() throws Exception + { + final ExceptionJsonSerializer errorSerializer = new ExceptionJsonSerializer(); + final TransferException expectedException = new TransferException("my message id", new Object[] {"param1", "param2"}); + when(mockedHttpClient.executeMethod(any(HostConfiguration.class), any(HttpMethod.class), + any(HttpState.class))).thenReturn(500); + doAnswer(new Answer() { + @Override + public String answer(InvocationOnMock invocation) throws Throwable + { + JSONObject errorObject = errorSerializer.serialize(expectedException); + return errorObject.toString(); + } + }).when(mockedHttpMethodFactory.latestPostMethod).getResponseBodyAsString(); + + try + { + transmitter.begin(target); + fail(); + } + catch(TransferException ex) + { + assertEquals(expectedException.getClass(), ex.getClass()); + assertEquals(expectedException.getMsgId(), ex.getMsgId()); + assertTrue(Arrays.deepEquals(expectedException.getMsgParams(), ex.getMsgParams())); + } + } + private static class CustomSocketFactory implements SecureProtocolSocketFactory {