Skip to content

Commit

Permalink
attempt to fix JAX-RS TCK after TOMEE-4406
Browse files Browse the repository at this point in the history
  • Loading branch information
jungm committed Oct 3, 2024
1 parent 655b3a4 commit c99fa71
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 4 deletions.
34 changes: 34 additions & 0 deletions tck/jax-rs/jax-rs-tests/src/test/conf/web.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You 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.
-->
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee
https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd"
version="6.0">

<!-- NotFoundServlet is injected into deployments using JaxRsTckExtension,
but we use this global web.xml because it is more convenient than overwriting web.xml in said Extension -->
<servlet>
<servlet-name>default</servlet-name>
<servlet-class>org.apache.tomee.tck.jaxrs.NotFoundServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.tomee.tck.jaxrs;

import org.jboss.arquillian.container.spi.event.container.BeforeDeploy;
import org.jboss.arquillian.core.api.annotation.Observes;
import org.jboss.arquillian.core.spi.LoadableExtension;
import org.jboss.shrinkwrap.api.container.ClassContainer;

// inject NotFoundServlet into the deployment to make our global web.xml override work
public class JaxRsTckExtension implements LoadableExtension {
@Override
public void register(ExtensionBuilder builder) {
builder.observer(getClass());
}

public void observeDeployment(@Observes BeforeDeploy bd) {
if (bd.getDeployment().getArchive() instanceof ClassContainer<?> classContainer) {
classContainer.addClass(NotFoundServlet.class);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.tomee.tck.jaxrs;

import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

/**
* TCK likes to send requests to unknown URLs, which can cause a 405 in tomcats default servlet with non GET requests.
* This servlet will instead always return a 404 to make the TCK happy
*/
public class NotFoundServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.apache.tomee.tck.jaxrs.JaxRsTckExtension
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void doFilter(final ServletRequest request, final ServletResponse respons
}

// if a servlet matched it always has priority over JAX-RS endpoints, see TOMEE-4406
if (nonDefaultServletMatches(httpServletRequest)) {
if (!defaultServletMatched(httpServletRequest)) {
chain.doFilter(request, response);
return;
}
Expand Down Expand Up @@ -89,13 +89,13 @@ public void doFilter(final ServletRequest request, final ServletResponse respons
* Checks if the request matched a defined servlet mapping matches the given request
*
* @param request the HttpServletRequest to check
* @return if the servlet mapping matches and is not the tomcat DefaultServlet
* @return true if the servlet request is mapped to the tomcat default servlet
*/
private boolean nonDefaultServletMatches(final HttpServletRequest request) {
private boolean defaultServletMatched(final HttpServletRequest request) {
ServletRegistration servletRegistration = request.getServletContext().getServletRegistration(
request.getHttpServletMapping().getServletName());

return !"org.apache.catalina.servlets.DefaultServlet".equals(servletRegistration.getClassName());
return "default".equals(servletRegistration.getName());
}

@Override
Expand Down

0 comments on commit c99fa71

Please sign in to comment.