Skip to content

Commit

Permalink
Fix more Sonar smells for GatewayProxyFactoryBean
Browse files Browse the repository at this point in the history
  • Loading branch information
artembilan committed Sep 9, 2019
1 parent 6da8112 commit 87f5409
Showing 1 changed file with 77 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -606,48 +606,19 @@ private MethodInvocationGateway createGatewayForMethod(Method method) {
if (!CollectionUtils.isEmpty(this.methodMetadataMap)) {
methodMetadata = this.methodMetadataMap.get(method.getName());
}
Map<String, Expression> headerExpressions = new HashMap<>();
Expression requestTimeout = this.defaultRequestTimeout;
Expression replyTimeout = this.defaultReplyTimeout;
Expression payloadExpression =
extractPayloadExpressionFromAnnotationOrMetadata(gatewayAnnotation, methodMetadata);
String requestChannelName = extractRequestChannelFromAnnotationOrMetadata(gatewayAnnotation, methodMetadata);
String replyChannelName = extractReplyChannelFromAnnotationOrMetadata(gatewayAnnotation, methodMetadata);
if (gatewayAnnotation != null) {
/*
* INT-2636 Unspecified annotation attributes should not
* override the default values supplied by explicit configuration.
* There is a small risk that someone has used Long.MIN_VALUE explicitly
* to indicate an indefinite timeout on a gateway method and that will
* no longer work as expected; they will need to use, say, -1 instead.
*/
if (requestTimeout == null || gatewayAnnotation.requestTimeout() != Long.MIN_VALUE) {
requestTimeout = new ValueExpression<>(gatewayAnnotation.requestTimeout());
}
if (StringUtils.hasText(gatewayAnnotation.requestTimeoutExpression())) {
requestTimeout = ExpressionUtils.longExpression(gatewayAnnotation.requestTimeoutExpression());
}
if (replyTimeout == null || gatewayAnnotation.replyTimeout() != Long.MIN_VALUE) {
replyTimeout = new ValueExpression<>(gatewayAnnotation.replyTimeout());
}
if (StringUtils.hasText(gatewayAnnotation.replyTimeoutExpression())) {
replyTimeout = ExpressionUtils.longExpression(gatewayAnnotation.replyTimeoutExpression());
}
Expression requestTimeout = extractRequestTimeoutFromAnnotationOrMetadata(gatewayAnnotation, methodMetadata);
Expression replyTimeout = extractReplyTimeoutFromAnnotationOrMetadata(gatewayAnnotation, methodMetadata);

Map<String, Expression> headerExpressions = new HashMap<>();
if (gatewayAnnotation != null) {
annotationHeaders(gatewayAnnotation, headerExpressions);
}
else if (methodMetadata != null) {
if (!CollectionUtils.isEmpty(methodMetadata.getHeaderExpressions())) {
else if (methodMetadata != null && !CollectionUtils.isEmpty(methodMetadata.getHeaderExpressions())) {
headerExpressions.putAll(methodMetadata.getHeaderExpressions());
}
String reqTimeout = methodMetadata.getRequestTimeout();
if (StringUtils.hasText(reqTimeout)) {
requestTimeout = ExpressionUtils.longExpression(reqTimeout);
}
String repTimeout = methodMetadata.getReplyTimeout();
if (StringUtils.hasText(repTimeout)) {
replyTimeout = ExpressionUtils.longExpression(repTimeout);
}
}

return doCreateMethodInvocationGateway(method, payloadExpression, headerExpressions,
Expand All @@ -664,14 +635,19 @@ private Expression extractPayloadExpressionFromAnnotationOrMetadata(@Nullable Ga
: null;

if (gatewayAnnotation != null) {
/*
* INT-2636 Unspecified annotation attributes should not
* override the default values supplied by explicit configuration.
* There is a small risk that someone has used Long.MIN_VALUE explicitly
* to indicate an indefinite timeout on a gateway method and that will
* no longer work as expected; they will need to use, say, -1 instead.
*/
if (payloadExpression == null && StringUtils.hasText(gatewayAnnotation.payloadExpression())) {
payloadExpression = PARSER.parseExpression(gatewayAnnotation.payloadExpression());
}
}
else if (methodMetadata != null) {
if (methodMetadata.getPayloadExpression() != null) {
payloadExpression = methodMetadata.getPayloadExpression();
}
else if (methodMetadata != null && methodMetadata.getPayloadExpression() != null) {
payloadExpression = methodMetadata.getPayloadExpression();
}

return payloadExpression;
Expand Down Expand Up @@ -703,6 +679,68 @@ else if (methodMetadata != null) {
return null;
}

@Nullable
private Expression extractRequestTimeoutFromAnnotationOrMetadata(@Nullable Gateway gatewayAnnotation,
@Nullable GatewayMethodMetadata methodMetadata) {

Expression requestTimeout = this.defaultRequestTimeout;

if (gatewayAnnotation != null) {
/*
* INT-2636 Unspecified annotation attributes should not
* override the default values supplied by explicit configuration.
* There is a small risk that someone has used Long.MIN_VALUE explicitly
* to indicate an indefinite timeout on a gateway method and that will
* no longer work as expected; they will need to use, say, -1 instead.
*/
if (requestTimeout == null || gatewayAnnotation.requestTimeout() != Long.MIN_VALUE) {
requestTimeout = new ValueExpression<>(gatewayAnnotation.requestTimeout());
}
if (StringUtils.hasText(gatewayAnnotation.requestTimeoutExpression())) {
requestTimeout = ExpressionUtils.longExpression(gatewayAnnotation.requestTimeoutExpression());
}

}
else if (methodMetadata != null) {
String reqTimeout = methodMetadata.getRequestTimeout();
if (StringUtils.hasText(reqTimeout)) {
requestTimeout = ExpressionUtils.longExpression(reqTimeout);
}
}
return requestTimeout;
}

@Nullable
private Expression extractReplyTimeoutFromAnnotationOrMetadata(@Nullable Gateway gatewayAnnotation,
@Nullable GatewayMethodMetadata methodMetadata) {

Expression replyTimeout = this.defaultReplyTimeout;

if (gatewayAnnotation != null) {
/*
* INT-2636 Unspecified annotation attributes should not
* override the default values supplied by explicit configuration.
* There is a small risk that someone has used Long.MIN_VALUE explicitly
* to indicate an indefinite timeout on a gateway method and that will
* no longer work as expected; they will need to use, say, -1 instead.
*/
if (replyTimeout == null || gatewayAnnotation.replyTimeout() != Long.MIN_VALUE) {
replyTimeout = new ValueExpression<>(gatewayAnnotation.replyTimeout());
}
if (StringUtils.hasText(gatewayAnnotation.replyTimeoutExpression())) {
replyTimeout = ExpressionUtils.longExpression(gatewayAnnotation.replyTimeoutExpression());
}

}
else if (methodMetadata != null) {
String repTimeout = methodMetadata.getReplyTimeout();
if (StringUtils.hasText(repTimeout)) {
replyTimeout = ExpressionUtils.longExpression(repTimeout);
}
}
return replyTimeout;
}

private void annotationHeaders(Gateway gatewayAnnotation, Map<String, Expression> headerExpressions) {
if (!ObjectUtils.isEmpty(gatewayAnnotation.headers())) {
for (GatewayHeader gatewayHeader : gatewayAnnotation.headers()) {
Expand All @@ -725,7 +763,7 @@ private void annotationHeaders(Gateway gatewayAnnotation, Map<String, Expression
private MethodInvocationGateway doCreateMethodInvocationGateway(Method method,
@Nullable Expression payloadExpression, Map<String, Expression> headerExpressions,
@Nullable String requestChannelName, @Nullable String replyChannelName,
Expression requestTimeout, Expression replyTimeout) {
@Nullable Expression requestTimeout, @Nullable Expression replyTimeout) {

GatewayMethodInboundMessageMapper messageMapper = createGatewayMessageMapper(method, headerExpressions);
MethodInvocationGateway gateway = new MethodInvocationGateway(messageMapper);
Expand Down

0 comments on commit 87f5409

Please sign in to comment.