Skip to content

Commit

Permalink
Merge pull request quarkusio#5316 from jaikiran/qk-5293
Browse files Browse the repository at this point in the history
Fix NPE when processing a @WebServlet which has additional annotations other than @ServletSecurity
  • Loading branch information
gwenneg authored Nov 8, 2019
2 parents 9d892c0 + 643a583 commit f671ae4
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,9 @@ public ServletDeploymentManagerBuildItem build(List<ServletBuildItem> servlets,
// Map the @ServletSecurity annotations
if (webMetaData.getAnnotations() != null) {
for (AnnotationMetaData amd : webMetaData.getAnnotations()) {
if (amd.getClassName().equals(servlet.getServletClass())) {
final ServletSecurityMetaData ssmd = amd.getServletSecurity();
if (ssmd != null && amd.getClassName().equals(servlet.getServletClass())) {
// Process the @ServletSecurity into metadata
ServletSecurityMetaData ssmd = amd.getServletSecurity();
ServletSecurityInfo securityInfo = new ServletSecurityInfo();
securityInfo.setEmptyRoleSemantic(
ssmd.getEmptyRoleSemantic() == EmptyRoleSemanticType.DENY ? DENY : PERMIT);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.quarkus.undertow.test;

import java.io.IOException;

import javax.annotation.security.RunAs;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
*
*/
@WebServlet(urlPatterns = AnnotatedServlet.SERVLET_ENDPOINT)
// we aren't really interested in testing the RunAs feature, instead this is there to reproduce
// a NPE https://github.com/quarkusio/quarkus/issues/5293
@RunAs("dummy")
public class AnnotatedServlet extends HttpServlet {

public static final String SERVLET_ENDPOINT = "/plainAnnotatedServlet";

public static final String OK_RESPONSE = "Success";

@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().write(OK_RESPONSE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.quarkus.undertow.test;

import static io.restassured.RestAssured.when;
import static org.hamcrest.Matchers.is;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;

/**
*
*/
public class AnnotatedServletTestCase {
@RegisterExtension
static QuarkusUnitTest runner = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addClasses(AnnotatedServlet.class));

/**
* Tests the issue noted in https://github.com/quarkusio/quarkus/issues/5293
* where {@code UndertowBuildStep} would throw an NPE when the annotated servlet
* had additional annotation(s) but not the {@code @ServletSecurity} annotation
*
* @throws Exception
*/
@Test
public void testNPE() throws Exception {
when().get(AnnotatedServlet.SERVLET_ENDPOINT).then()
.statusCode(200)
.body(is(AnnotatedServlet.OK_RESPONSE));
}
}

0 comments on commit f671ae4

Please sign in to comment.