Skip to content
Permalink
Browse files
CXF-8522: jaxrs.spec.resource.requestmatching consumesOnResourceLocat…
…orTest & other subresource test cases (#925)
  • Loading branch information
reta committed Mar 26, 2022
1 parent d03510d commit a0f912b7a54297f4e405a5c8e09df682ece07c9a
Showing 5 changed files with 113 additions and 4 deletions.
@@ -510,7 +510,7 @@ public static OperationResourceInfo findTargetMethod(
int pathMatched = 0;
int methodMatched = 0;
int consumeMatched = 0;

List<OperationResourceInfo> finalPathSubresources = null;
for (Map.Entry<ClassResourceInfo, MultivaluedMap<String, String>> rEntry : matchedResources.entrySet()) {
ClassResourceInfo resource = rEntry.getKey();
@@ -561,7 +561,12 @@ public static OperationResourceInfo findTargetMethod(
LOG.fine(matchMessageLogSupplier(ori, path, httpMethod, requestType, acceptContentTypes, added));
}
}
if (finalPathSubresources != null && pathMatched > 0

// We may get several matching candidates with different HTTP methods which match subresources
// and resources. Before excluding subresources, let us make sure we have at least one matching
// HTTP method candidate.
boolean isOptions = HttpMethod.OPTIONS.equalsIgnoreCase(httpMethod);
if (finalPathSubresources != null && (methodMatched > 0 || isOptions)
&& !MessageUtils.getContextualBoolean(message, KEEP_SUBRESOURCE_CANDIDATES, false)) {
for (OperationResourceInfo key : finalPathSubresources) {
candidateList.remove(key);
@@ -0,0 +1,40 @@
/**
* 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.cxf.systest.jaxrs;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

public class BookResourceLocator {
@GET
public Book get() {
return new Book();
}

@POST
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.TEXT_PLAIN)
public Book post() {
return get();
}
}
@@ -19,19 +19,49 @@

package org.apache.cxf.systest.jaxrs;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

import org.apache.cxf.Bus;
import org.apache.cxf.endpoint.Server;
import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
import org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider;
import org.apache.cxf.testutil.common.AbstractServerTestServerBase;


public class BookServerSub extends AbstractServerTestServerBase {
public static final String PORT = allocatePort(BookServerSub.class);

@Provider
public static class ExceptionMatcher implements ExceptionMapper<WebApplicationException> {
@Override
public Response toResponse(WebApplicationException exception) {
Response response = exception.getResponse();
int status = response == null ? Status.INTERNAL_SERVER_ERROR.getStatusCode() : response.getStatus();
if (response != null && response.getEntity() != null) {
return response;
}

switch (status) {
case 404:
case 405:
case 406:
case 415:
return Response.status(status).entity(String.valueOf(status)).build();
default:
return response;
}
}
}

@Override
protected Server createServer(Bus bus) throws Exception {
JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
sf.setStaticSubresourceResolution(true);
sf.setProvider(new ExceptionMatcher());
sf.setResourceClasses(BookStoreSubObject.class);
sf.setResourceProvider(BookStoreSubObject.class,
new SingletonResourceProvider(new BookStoreSubObject(), true));
@@ -20,15 +20,27 @@
package org.apache.cxf.systest.jaxrs;


import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;

@Path("/bookstore")
public class BookStoreSubObject {

@Path("/booksubresourceobject")
public Object getBookSubResourceObject() throws BookNotFoundFault {
return new Book();
}

@Path("consumeslocator")
public BookResourceLocator consumeslocator() {
return new BookResourceLocator();
}

@GET
@Path("{id}")
public Book book(@PathParam("id") Long id) {
return new Book("Book", id);
}
}


@@ -19,14 +19,22 @@

package org.apache.cxf.systest.jaxrs;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.cxf.jaxrs.model.AbstractResourceInfo;
import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;

import org.junit.BeforeClass;
import org.junit.Test;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

public class JAXRSClientServerSubBookTest extends AbstractBusClientServerTestBase {
@@ -44,9 +52,23 @@ public static void startServers() throws Exception {
public void testGetChapterFromBookSubObject() throws Exception {
WebClient wc =
WebClient.create("http://localhost:" + PORT + "/bookstore/booksubresourceobject/chaptersobject/sub/1");
WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(100000000L);
Chapter c = wc.accept("application/xml").get(Chapter.class);
assertNotNull(c);
}

@Test
public void testSubresourceLocatorFromBookSubObject() throws Exception {
final Client c = ClientBuilder.newClient();

// There are two matching endpoints with different HTTP methods:
// - POST for BookSubObject /consumeslocator"
// - GET for BookStore /{id}
// The test verifies that in this case for the POST method the correct subresource
// locator is picked.
final WebTarget wc = c.target("http://localhost:" + PORT + "/bookstore/consumeslocator");
try (Response r = wc.request().header("Content-Type", MediaType.APPLICATION_ATOM_XML).post(null)) {
assertThat(r.getStatus(), equalTo(Response.Status.UNSUPPORTED_MEDIA_TYPE.getStatusCode()));
assertThat(Integer.toString(r.getStatus()), equalTo(r.readEntity(String.class)));
}
}
}

0 comments on commit a0f912b

Please sign in to comment.