Skip to content

Commit

Permalink
Address some Sonar issues (#3737)
Browse files Browse the repository at this point in the history
* Simplify AuthDynamicFeature with a helper method

Sonar called this method out as being more complex than it would like. Move
some logic to a helper method.

* Avoid accessing static fields through a subclass

Sonar was unhappy about access to static fields not being directly via the
defining parent class.

* Stop treating log placeholders as regular expressions in SyslogAppenderFactory#build

Swap out replaceAll() for replace(). The log format placeholders are not
regular expressions.

* Factor out duplicated string construction in AssetsBundle

Factor out this duplicated string into a variable, eliminating a Sonar warning
about not using the logging formatter to do it.
  • Loading branch information
rhowe committed Mar 17, 2021
1 parent ff41c97 commit 7a5e899
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,9 @@ public AssetsBundle(String resourcePath, String uriPath, String indexFile, Strin

@Override
public void run(Configuration configuration, Environment environment) {
LOGGER.info("Registering AssetBundle with name: {} for path {}", assetsName, uriPath + '*');
environment.servlets().addServlet(assetsName, createServlet()).addMapping(uriPath + '*');
final String pathPattern = uriPath + '*';
LOGGER.info("Registering AssetBundle with name: {} for path {}", assetsName, pathPattern);
environment.servlets().addServlet(assetsName, createServlet()).addMapping(pathPattern);
}

public String getResourcePath() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,14 @@ public void configure(ResourceInfo resourceInfo, FeatureContext context) {

// First, check for any @Auth annotations on the method.
for (int i = 0; i < parameterAnnotations.length; i++) {
for (final Annotation annotation : parameterAnnotations[i]) {
if (annotation instanceof Auth) {
// Optional auth requires that a concrete AuthFilter be provided.
if (parameterTypes[i].equals(Optional.class) && authFilter != null) {
context.register(new WebApplicationExceptionCatchingFilter(authFilter));
return;
} else {
registerAuthFilter(context);
return;
}
if (containsAuthAnnotation(parameterAnnotations[i])) {
// Optional auth requires that a concrete AuthFilter be provided.
if (parameterTypes[i].equals(Optional.class) && authFilter != null) {
context.register(new WebApplicationExceptionCatchingFilter(authFilter));
} else {
registerAuthFilter(context);
}
return;
}
}

Expand All @@ -83,6 +80,15 @@ public void configure(ResourceInfo resourceInfo, FeatureContext context) {
}
}

private boolean containsAuthAnnotation(final Annotation[] annotations) {
for (final Annotation annotation : annotations) {
if (annotation instanceof Auth) {
return true;
}
}
return false;
}

private void registerAuthFilter(FeatureContext context) {
if (authFilter != null) {
context.register(authFilter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@
import javax.servlet.http.HttpSession;
import java.util.function.Function;

import static org.glassfish.jersey.model.Parameter.Source.UNKNOWN;

@Singleton
public class SessionFactoryProvider extends AbstractValueParamProvider {

private final InjectionManager im;

@Inject
public SessionFactoryProvider(final Provider<MultivaluedParameterExtractorProvider> extractorProvider, InjectionManager im) {
super(extractorProvider, Parameter.Source.UNKNOWN);
super(extractorProvider, UNKNOWN);
this.im = im;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
import java.util.Set;
import java.util.stream.Collectors;

import static org.glassfish.jersey.model.Parameter.Source.BEAN_PARAM;
import static org.glassfish.jersey.model.Parameter.Source.UNKNOWN;

public class ConstraintMessage {

private static final Cache<Pair<Path, ? extends ConstraintDescriptor<?>>, String> PREFIX_CACHE =
Expand Down Expand Up @@ -96,7 +99,7 @@ public static Optional<String> isRequestEntity(ConstraintViolation<?> violation,

if (parent.getKind() == ElementKind.PARAMETER) {
final Parameter param = parameters.get(parent.as(Path.ParameterNode.class).getParameterIndex());
if (param.getSource().equals(Parameter.Source.UNKNOWN)) {
if (param.getSource().equals(UNKNOWN)) {
final String path = propertyPath.stream()
.skip(2L)
.map(Path.Node::toString)
Expand Down Expand Up @@ -128,7 +131,7 @@ private static Optional<String> getMemberName(ConstraintViolation<?> violation,
final Parameter param = parameters.get(parent.as(Path.ParameterNode.class).getParameterIndex());

// Extract the failing *Param annotation inside the Bean Param
if (param.getSource().equals(Parameter.Source.BEAN_PARAM)) {
if (param.getSource().equals(BEAN_PARAM)) {
final Field field = FieldUtils.getField(param.getRawType(), member.getName(), true);
return JerseyParameterNameProvider.getParameterNameFromAnnotations(field.getDeclaredAnnotations());
}
Expand Down Expand Up @@ -184,7 +187,7 @@ public static <T extends ConstraintViolation<?>> int determineStatus(Set<T> viol
// Now determine if the parameter is the request entity
final int index = node.as(Path.ParameterNode.class).getParameterIndex();
final Parameter parameter = invocable.getParameters().get(index);
return parameter.getSource().equals(Parameter.Source.UNKNOWN) ? 422 : 400;
return parameter.getSource().equals(UNKNOWN) ? 422 : 400;
default:
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ public Appender<ILoggingEvent> build(LoggerContext context, String applicationNa
appender.setContext(context);
if (logFormat != null && !logFormat.isEmpty()) {
appender.setSuffixPattern(logFormat
.replaceAll(LOG_TOKEN_PID, pid)
.replaceAll(LOG_TOKEN_NAME, Matcher.quoteReplacement(applicationName)));
.replace(LOG_TOKEN_PID, pid)
.replace(LOG_TOKEN_NAME, Matcher.quoteReplacement(applicationName)));
}
appender.setSyslogHost(host);
appender.setPort(port);
Expand Down

0 comments on commit 7a5e899

Please sign in to comment.