MNT-24321 Transfer Service Exception Handling (#2573)

* Mark method deserialize in ExceptionJsonSerializer as deprecated
* Remove usage of jsonErrorSerializer
This commit is contained in:
Eva Vasques
2024-04-10 09:15:25 +01:00
committed by GitHub
parent 2b4fc52203
commit c31158a113
2 changed files with 61 additions and 43 deletions

View File

@@ -53,8 +53,6 @@ import org.alfresco.service.cmr.transfer.TransferTarget;
import org.alfresco.service.cmr.transfer.TransferVersion; import org.alfresco.service.cmr.transfer.TransferVersion;
import org.alfresco.util.HttpClientHelper; import org.alfresco.util.HttpClientHelper;
import org.alfresco.util.PropertyCheck; import org.alfresco.util.PropertyCheck;
import org.alfresco.util.json.ExceptionJsonSerializer;
import org.alfresco.util.json.JsonSerializer;
import org.apache.commons.httpclient.Credentials; import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HostConfiguration; import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpClient;
@@ -73,8 +71,10 @@ import org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory;
import org.apache.commons.httpclient.protocol.Protocol; import org.apache.commons.httpclient.protocol.Protocol;
import org.apache.commons.httpclient.protocol.ProtocolSocketFactory; import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
import org.apache.commons.httpclient.protocol.SSLProtocolSocketFactory; import org.apache.commons.httpclient.protocol.SSLProtocolSocketFactory;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
/** /**
@@ -102,7 +102,6 @@ public class HttpClientTransmitterImpl implements TransferTransmitter
private Protocol httpsProtocol = new Protocol(HTTPS_SCHEME_NAME, (ProtocolSocketFactory) new SSLProtocolSocketFactory(), DEFAULT_HTTPS_PORT); private Protocol httpsProtocol = new Protocol(HTTPS_SCHEME_NAME, (ProtocolSocketFactory) new SSLProtocolSocketFactory(), DEFAULT_HTTPS_PORT);
private Map<String,Protocol> protocolMap = null; private Map<String,Protocol> protocolMap = null;
private HttpMethodFactory httpMethodFactory = null; private HttpMethodFactory httpMethodFactory = null;
private JsonSerializer<Throwable, JSONObject> jsonErrorSerializer;
private ContentService contentService; private ContentService contentService;
@@ -125,7 +124,6 @@ public class HttpClientTransmitterImpl implements TransferTransmitter
httpClient = new HttpClient(); httpClient = new HttpClient();
httpClient.setHttpConnectionManager(new MultiThreadedHttpConnectionManager()); httpClient.setHttpConnectionManager(new MultiThreadedHttpConnectionManager());
httpMethodFactory = new StandardHttpMethodFactoryImpl(); httpMethodFactory = new StandardHttpMethodFactoryImpl();
jsonErrorSerializer = new ExceptionJsonSerializer();
// Create an HTTP Proxy Host if appropriate system properties are set // Create an HTTP Proxy Host if appropriate system properties are set
httpProxyHost = HttpClientHelper.createProxyHost("http.proxyHost", "http.proxyPort", DEFAULT_HTTP_PORT); httpProxyHost = HttpClientHelper.createProxyHost("http.proxyHost", "http.proxyPort", DEFAULT_HTTP_PORT);
@@ -852,7 +850,27 @@ public class HttpClientTransmitterImpl implements TransferTransmitter
*/ */
private Throwable rehydrateError(JSONObject errorJSON) private Throwable rehydrateError(JSONObject errorJSON)
{ {
return jsonErrorSerializer.deserialize(errorJSON); if (errorJSON == null)
{
return null;
}
String errorMessage = errorJSON.optString("errorMessage", StringUtils.EMPTY);
String errorId = errorJSON.optString("alfrescoMessageId", null);
Object[] errorParams = new Object[0];
JSONArray errorParamArray = errorJSON.optJSONArray("alfrescoMessageParams");
if (errorParamArray != null)
{
int length = errorParamArray.length();
errorParams = new Object[length];
for (int i = 0; i < length; ++i)
{
errorParams[i] = errorParamArray.getString(i);
}
}
return new TransferException(errorId == null ? errorMessage : errorId, errorParams);
} }
public void setContentService(ContentService contentService) public void setContentService(ContentService contentService)
@@ -870,11 +888,6 @@ public class HttpClientTransmitterImpl implements TransferTransmitter
this.httpMethodFactory = httpMethodFactory; this.httpMethodFactory = httpMethodFactory;
} }
public void setJsonErrorSerializer(JsonSerializer<Throwable, JSONObject> jsonErrorSerializer)
{
this.jsonErrorSerializer = jsonErrorSerializer;
}
public void setNodeService(NodeService nodeService) public void setNodeService(NodeService nodeService)
{ {
this.nodeService = nodeService; this.nodeService = nodeService;

View File

@@ -41,7 +41,8 @@ import org.json.JSONObject;
public class ExceptionJsonSerializer implements JsonSerializer<Throwable, JSONObject> public class ExceptionJsonSerializer implements JsonSerializer<Throwable, JSONObject>
{ {
private final static Log log = LogFactory.getLog(ExceptionJsonSerializer.class); private final static Log log = LogFactory.getLog(ExceptionJsonSerializer.class);
@Deprecated
@Override @Override
public Throwable deserialize(JSONObject errorJSON) public Throwable deserialize(JSONObject errorJSON)
{ {
@@ -89,38 +90,42 @@ public class ExceptionJsonSerializer implements JsonSerializer<Throwable, JSONOb
catch (ClassNotFoundException e) catch (ClassNotFoundException e)
{ {
errorClass = Exception.class; errorClass = Exception.class;
} }
Constructor<?> constructor = null;
try if (Throwable.class.isAssignableFrom(errorClass))
{ {
try Constructor<?> constructor = null;
{ try
constructor = errorClass.getConstructor(String.class, Object[].class); {
createdObject = constructor.newInstance(errorId, errorParams); try
} {
catch (NoSuchMethodException e) constructor = errorClass.getConstructor(String.class, Object[].class);
{ createdObject = constructor.newInstance(errorId, errorParams);
try }
{ catch (NoSuchMethodException e)
constructor = errorClass.getConstructor(String.class); {
createdObject = constructor.newInstance(errorId == null ? errorMessage : errorId); try
} {
catch (NoSuchMethodException e1) constructor = errorClass.getConstructor(String.class);
{ createdObject = constructor.newInstance(errorId == null ? errorMessage : errorId);
try }
{ catch (NoSuchMethodException e1)
constructor = errorClass.getConstructor(); {
createdObject = constructor.newInstance(); try
} {
catch (NoSuchMethodException e2) constructor = errorClass.getConstructor();
{ createdObject = constructor.newInstance();
} }
} catch (NoSuchMethodException e2)
} {
} }
catch(Exception ex) }
{ }
//We don't need to do anything here. Code below will fix things up }
catch (Exception ex)
{
// We don't need to do anything here. Code below will fix things up
}
} }
if (createdObject == null || !Throwable.class.isAssignableFrom(createdObject.getClass())) if (createdObject == null || !Throwable.class.isAssignableFrom(createdObject.getClass()))
{ {