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.util.HttpClientHelper;
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.HostConfiguration;
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.ProtocolSocketFactory;
import org.apache.commons.httpclient.protocol.SSLProtocolSocketFactory;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONArray;
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 Map<String,Protocol> protocolMap = null;
private HttpMethodFactory httpMethodFactory = null;
private JsonSerializer<Throwable, JSONObject> jsonErrorSerializer;
private ContentService contentService;
@@ -125,7 +124,6 @@ public class HttpClientTransmitterImpl implements TransferTransmitter
httpClient = new HttpClient();
httpClient.setHttpConnectionManager(new MultiThreadedHttpConnectionManager());
httpMethodFactory = new StandardHttpMethodFactoryImpl();
jsonErrorSerializer = new ExceptionJsonSerializer();
// Create an HTTP Proxy Host if appropriate system properties are set
httpProxyHost = HttpClientHelper.createProxyHost("http.proxyHost", "http.proxyPort", DEFAULT_HTTP_PORT);
@@ -852,7 +850,27 @@ public class HttpClientTransmitterImpl implements TransferTransmitter
*/
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)
@@ -870,11 +888,6 @@ public class HttpClientTransmitterImpl implements TransferTransmitter
this.httpMethodFactory = httpMethodFactory;
}
public void setJsonErrorSerializer(JsonSerializer<Throwable, JSONObject> jsonErrorSerializer)
{
this.jsonErrorSerializer = jsonErrorSerializer;
}
public void setNodeService(NodeService nodeService)
{
this.nodeService = nodeService;

View File

@@ -41,7 +41,8 @@ import org.json.JSONObject;
public class ExceptionJsonSerializer implements JsonSerializer<Throwable, JSONObject>
{
private final static Log log = LogFactory.getLog(ExceptionJsonSerializer.class);
@Deprecated
@Override
public Throwable deserialize(JSONObject errorJSON)
{
@@ -89,38 +90,42 @@ public class ExceptionJsonSerializer implements JsonSerializer<Throwable, JSONOb
catch (ClassNotFoundException e)
{
errorClass = Exception.class;
}
Constructor<?> constructor = null;
try
{
try
{
constructor = errorClass.getConstructor(String.class, Object[].class);
createdObject = constructor.newInstance(errorId, errorParams);
}
catch (NoSuchMethodException e)
{
try
{
constructor = errorClass.getConstructor(String.class);
createdObject = constructor.newInstance(errorId == null ? errorMessage : errorId);
}
catch (NoSuchMethodException e1)
{
try
{
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
}
if (Throwable.class.isAssignableFrom(errorClass))
{
Constructor<?> constructor = null;
try
{
try
{
constructor = errorClass.getConstructor(String.class, Object[].class);
createdObject = constructor.newInstance(errorId, errorParams);
}
catch (NoSuchMethodException e)
{
try
{
constructor = errorClass.getConstructor(String.class);
createdObject = constructor.newInstance(errorId == null ? errorMessage : errorId);
}
catch (NoSuchMethodException e1)
{
try
{
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
}
}
if (createdObject == null || !Throwable.class.isAssignableFrom(createdObject.getClass()))
{