Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WFLY-14307] Warning if wrong annotation is added to a Servlet #16542

Merged
merged 1 commit into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
package org.wildfly.extension.undertow.deployment;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -35,6 +36,7 @@
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.annotation.WebListener;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;

import org.jboss.annotation.javaee.Descriptions;
import org.jboss.annotation.javaee.DisplayNames;
Expand Down Expand Up @@ -101,6 +103,14 @@ public class WarAnnotationDeploymentProcessor implements DeploymentUnitProcessor
private static final DotName declareRoles = DotName.createSimple(DeclareRoles.class.getName());
private static final DotName multipartConfig = DotName.createSimple(MultipartConfig.class.getName());
private static final DotName servletSecurity = DotName.createSimple(ServletSecurity.class.getName());
private static final DotName servletDotName = DotName.createSimple(HttpServlet.class.getName());
// This list defines some annotations that won't get processed if they are added to a servlet according to Servlet Specification
// but sometimes users may misuse them, users will get a warning log in such scenarios.
// https://issues.redhat.com/browse/WFLY-14307
private static final List<DotName> badAnnotationsInServlet = new ArrayList<>();
static {
badAnnotationsInServlet.add(DotName.createSimple("org.jboss.ejb3.annotation.RunAsPrincipal"));
}

/**
* Process web annotations.
Expand Down Expand Up @@ -530,6 +540,22 @@ protected WebMetaData processAnnotations(Index index)
annotationMD.setServletSecurity(servletSecurity);
}
}
// bad annotations
for (DotName badAnnotation: badAnnotationsInServlet) {
final List<AnnotationInstance> badAnnotationInstance = index.getAnnotations(badAnnotation);
if (badAnnotationInstance != null && !badAnnotationInstance.isEmpty()) {
Collection<ClassInfo> servlets = index.getAllKnownSubclasses(servletDotName);
for (AnnotationInstance annotation: badAnnotationInstance) {
AnnotationTarget target = annotation.target();
if (target instanceof ClassInfo) {
ClassInfo classInfo = (ClassInfo) target;
if (servlets.contains(classInfo)) {
UndertowLogger.ROOT_LOGGER.badAnnotationOnServlet(badAnnotation.toString(), classInfo.toString());
}
}
}
}
}
return metaData;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -442,4 +442,8 @@ public interface UndertowLogger extends BasicLogger {
@Message(id = 110, value = "The use of security realms at runtime is unsupported.")
OperationFailedException runtimeSecurityRealmUnsupported();

@LogMessage(level = WARN)
@Message(id = 111, value = "The annotation: '%s' will have no effect on Servlet: '%s'")
void badAnnotationOnServlet(String annotation, String servlet);

}