Skip to content

Commit

Permalink
Merge pull request quarkusio#1266 from stuartwdouglas/1055
Browse files Browse the repository at this point in the history
Merge web.xml and annotations
  • Loading branch information
stuartwdouglas authored Mar 6, 2019
2 parents f9673b4 + 6b6822c commit f96a251
Show file tree
Hide file tree
Showing 6 changed files with 253 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -447,8 +447,16 @@ private void processAnnotations(IndexView index, WebMetaData metaData) {
// @WebServlet
final Collection<AnnotationInstance> webServletAnnotations = index.getAnnotations(WEB_SERVLET);
if (webServletAnnotations != null && webServletAnnotations.size() > 0) {
ServletsMetaData servlets = new ServletsMetaData();
List<ServletMappingMetaData> servletMappings = new ArrayList<ServletMappingMetaData>();
ServletsMetaData servlets = metaData.getServlets();
if (servlets == null) {
servlets = new ServletsMetaData();
metaData.setServlets(servlets);
}
List<ServletMappingMetaData> servletMappings = metaData.getServletMappings();
if (servletMappings == null) {
servletMappings = new ArrayList<>();
metaData.setServletMappings(servletMappings);
}
for (final AnnotationInstance annotation : webServletAnnotations) {
ServletMetaData servlet = new ServletMetaData();
AnnotationTarget target = annotation.target();
Expand Down Expand Up @@ -524,14 +532,20 @@ private void processAnnotations(IndexView index, WebMetaData metaData) {
}
servlets.add(servlet);
}
metaData.setServlets(servlets);
metaData.setServletMappings(servletMappings);
}
// @WebFilter
final Collection<AnnotationInstance> webFilterAnnotations = index.getAnnotations(WEB_FILTER);
if (webFilterAnnotations != null && webFilterAnnotations.size() > 0) {
FiltersMetaData filters = new FiltersMetaData();
List<FilterMappingMetaData> filterMappings = new ArrayList<FilterMappingMetaData>();
FiltersMetaData filters = metaData.getFilters();
if (filters == null) {
filters = new FiltersMetaData();
metaData.setFilters(filters);
}
List<FilterMappingMetaData> filterMappings = metaData.getFilterMappings();
if (filterMappings == null) {
filterMappings = new ArrayList<>();
metaData.setFilterMappings(filterMappings);
}
for (final AnnotationInstance annotation : webFilterAnnotations) {
FilterMetaData filter = new FilterMetaData();
AnnotationTarget target = annotation.target();
Expand Down Expand Up @@ -625,13 +639,15 @@ private void processAnnotations(IndexView index, WebMetaData metaData) {
filterMappings.add(filterMapping);
}
}
metaData.setFilters(filters);
metaData.setFilterMappings(filterMappings);
}
// @WebListener
final Collection<AnnotationInstance> webListenerAnnotations = index.getAnnotations(WEB_LISTENER);
if (webListenerAnnotations != null && webListenerAnnotations.size() > 0) {
List<ListenerMetaData> listeners = new ArrayList<ListenerMetaData>();
List<ListenerMetaData> listeners = metaData.getListeners();
if (listeners == null) {
listeners = new ArrayList<>();
metaData.setListeners(listeners);
}
for (final AnnotationInstance annotation : webListenerAnnotations) {
ListenerMetaData listener = new ListenerMetaData();
AnnotationTarget target = annotation.target();
Expand All @@ -646,7 +662,6 @@ private void processAnnotations(IndexView index, WebMetaData metaData) {
}
listeners.add(listener);
}
metaData.setListeners(listeners);
}
// @RunAs
final Collection<AnnotationInstance> runAsAnnotations = index.getAnnotations(RUN_AS);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2018 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.quarkus.undertow.test;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpFilter;

@WebFilter(urlPatterns = "/*")
public class AnnotatedFilter extends HttpFilter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
Enumeration<String> attributeNames = request.getServletContext().getAttributeNames();
while (attributeNames.hasMoreElements()) {
response.getWriter().println(attributeNames.nextElement());
}
response.getWriter().println("annotated filter");
chain.doFilter(request, response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2018 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.quarkus.undertow.test;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

@WebListener
public class AnnotatedListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
sce.getServletContext().setAttribute("annotated listener", true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright 2018 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.quarkus.undertow.test;

import static org.hamcrest.Matchers.containsString;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.StringAsset;
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;
import io.restassured.RestAssured;

public class AnnotationWithWebXmlTestCase {

static final String WEB_XML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"\n" +
"<web-app version=\"3.0\"\n" +
" xmlns=\"http://java.sun.com/xml/ns/javaee\"\n" +
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
" xsi:schemaLocation=\"http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd\"\n"
+
" metadata-complete=\"false\">\n" +
" <servlet>\n" +
" <servlet-name>mapped</servlet-name>\n" +
" <servlet-class>" + WebXmlServlet.class.getName() + "</servlet-class>\n" +
" </servlet>\n" +
"\n" +
" <servlet-mapping>\n" +
" <servlet-name>mapped</servlet-name>\n" +
" <url-pattern>/mapped</url-pattern>\n" +
" </servlet-mapping>" +
"\n" +
" <filter> \n" +
" <filter-name>mapped-filter</filter-name>\n" +
" <filter-class>" + WebXmlFilter.class.getName() + "</filter-class> \n" +
" </filter> \n" +
" <filter-mapping> \n" +
" <filter-name>mapped-filter</filter-name>\n" +
" <url-pattern>/*</url-pattern> \n" +
" </filter-mapping> " +
"\n" +
" <listener>\n" +
" <listener-class>" + WebXmlListener.class.getName() + "</listener-class>\n" +
" </listener>" +
"</web-app>";

@RegisterExtension
static QuarkusUnitTest runner = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addClasses(WebXmlServlet.class, TestServlet.class,
WebXmlFilter.class, AnnotatedFilter.class,
WebXmlListener.class, AnnotatedListener.class)
.addAsManifestResource(new StringAsset(WEB_XML), "web.xml"));

@Test
public void testWebXmlServlet() {
RestAssured.when().get("/mapped").then()
.statusCode(200)
.body(containsString("web xml listener"))
.body(containsString("annotated listener"))
.body(containsString("web xml filter"))
.body(containsString("annotated filter"))
.body(containsString("web xml servlet"));
}

@Test
public void testAnnotatedServlet() {
RestAssured.when().get("/test").then()
.statusCode(200)
.body(containsString("web xml listener"))
.body(containsString("annotated listener"))
.body(containsString("web xml filter"))
.body(containsString("annotated filter"))
.body(containsString("test servlet"));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2018 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.quarkus.undertow.test;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpFilter;

public class WebXmlFilter extends HttpFilter {

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
response.getWriter().println("web xml filter");
chain.doFilter(request, response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2018 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.quarkus.undertow.test;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

public class WebXmlListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
sce.getServletContext().setAttribute("web xml listener", true);
}
}

0 comments on commit f96a251

Please sign in to comment.