Skip to content

Commit

Permalink
MNT-24321 Transfer Service Exception Handling (#2573) (#2610)
Browse files Browse the repository at this point in the history
* Mark method deserialize in ExceptionJsonSerializer as deprecated
* Remove usage of jsonErrorSerializer

(cherry picked from commit c31158a)
  • Loading branch information
evasques authored Apr 26, 2024
1 parent 9a9665f commit e61991b
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@
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;
Expand All @@ -73,8 +71,10 @@
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;

/**
Expand Down Expand Up @@ -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;

Expand All @@ -125,7 +124,6 @@ public HttpClientTransmitterImpl()
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);
Expand Down Expand Up @@ -852,7 +850,27 @@ protected PostMethod getPostMethod()
*/
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)
Expand All @@ -870,11 +888,6 @@ public void setHttpMethodFactory(HttpMethodFactory httpMethodFactory)
this.httpMethodFactory = httpMethodFactory;
}

public void setJsonErrorSerializer(JsonSerializer<Throwable, JSONObject> jsonErrorSerializer)
{
this.jsonErrorSerializer = jsonErrorSerializer;
}

public void setNodeService(NodeService nodeService)
{
this.nodeService = nodeService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
public class ExceptionJsonSerializer implements JsonSerializer<Throwable, JSONObject>
{
private final static Log log = LogFactory.getLog(ExceptionJsonSerializer.class);


@Deprecated
@Override
public Throwable deserialize(JSONObject errorJSON)
{
Expand Down Expand Up @@ -89,38 +90,42 @@ public Throwable deserialize(JSONObject errorJSON)
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()))
{
Expand Down

0 comments on commit e61991b

Please sign in to comment.