Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ public void setApplicationInfo(ApplicationInfo provider) {
Set<String> clsNameBindings = new LinkedHashSet<>(appNameBindings);
clsNameBindings.addAll(AnnotationUtils.getNameBindings(bus, cri.getServiceClass()));
cri.setNameBindings(clsNameBindings);
cri.setApplicationInfo(provider);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@
import java.util.Map;
import java.util.logging.Logger;

import jakarta.ws.rs.ApplicationPath;
import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.core.PathSegment;
import jakarta.ws.rs.core.UriBuilder;
import jakarta.ws.rs.core.UriInfo;
import org.apache.cxf.common.logging.LogUtils;
import org.apache.cxf.jaxrs.model.ClassResourceInfo;
import org.apache.cxf.jaxrs.model.MethodInvocationInfo;
import org.apache.cxf.jaxrs.model.OperationResourceInfo;
import org.apache.cxf.jaxrs.model.OperationResourceInfoStack;
Expand All @@ -41,6 +43,8 @@
import org.apache.cxf.message.Message;
import org.apache.cxf.message.MessageUtils;

import io.micrometer.common.util.StringUtils;

public class UriInfoImpl implements UriInfo {
private static final Logger LOG = LogUtils.getL7dLogger(UriInfoImpl.class);
private static final String CASE_INSENSITIVE_QUERIES = "org.apache.cxf.http.case_insensitive_queries";
Expand Down Expand Up @@ -246,20 +250,33 @@ public URI resolve(URI uri) {
@Override
public String getMatchedResourceTemplate() {
if (stack != null) {
ApplicationPath applicationPath = null;
final List<URITemplate> templates = new LinkedList<>();
for (MethodInvocationInfo invocation : stack) {
OperationResourceInfo ori = invocation.getMethodInfo();
final URITemplate classUriTemplate = ori.getClassResourceInfo().getURITemplate();
final OperationResourceInfo ori = invocation.getMethodInfo();
final ClassResourceInfo cri = ori.getClassResourceInfo();
if (cri.getApplicationInfo() != null && applicationPath == null) {
applicationPath = cri.getApplicationInfo().getApplicationPath();
}
final URITemplate classUriTemplate = cri.getURITemplate();
if (classUriTemplate != null) {
templates.add(classUriTemplate);
}
templates.add(ori.getURITemplate());
}

if (!templates.isEmpty()) {
UriBuilder builder = UriBuilder.fromPath(templates.get(0).getValue());
for (int i = 1; i < templates.size(); ++i) {
builder = builder.path(templates.get(i).getValue());
UriBuilder builder = null;
if (applicationPath != null && !StringUtils.isBlank(applicationPath.value())) {
builder = UriBuilder.fromPath(applicationPath.value());
for (int i = 0; i < templates.size(); ++i) {
builder = builder.path(templates.get(i).getValue());
}
} else {
builder = UriBuilder.fromPath(templates.get(0).getValue());
for (int i = 1; i < templates.size(); ++i) {
builder = builder.path(templates.get(i).getValue());
}
}
return builder.toTemplate();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,24 @@
import java.util.HashMap;
import java.util.Map;

import jakarta.ws.rs.ApplicationPath;
import jakarta.ws.rs.core.Application;
import org.apache.cxf.Bus;
import org.apache.cxf.jaxrs.impl.tl.ThreadLocalProxy;
import org.apache.cxf.jaxrs.utils.ResourceUtils;

public class ApplicationInfo extends ProviderInfo<Application> {
private Map<String, Object> overridingProps = Collections.emptyMap();
private final ApplicationPath applicationPath;

public ApplicationInfo(Application provider, Bus bus) {
this(provider, null, bus);
}
public ApplicationInfo(Application provider,
Map<Class<?>, ThreadLocalProxy<?>> constructorProxies,
Bus bus) {
super(provider, constructorProxies, bus, true);
this.applicationPath = ResourceUtils.locateApplicationPath(provider.getClass());
}

public Map<String, Object> getProperties() {
Expand All @@ -46,7 +51,12 @@ public Map<String, Object> getProperties() {
props.putAll(overridingProps);
return props;
}

public void setOverridingProps(Map<String, Object> overridingProps) {
this.overridingProps = overridingProps;
}

public ApplicationPath getApplicationPath() {
return applicationPath;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public class ClassResourceInfo extends BeanResourceInfo {
private Set<String> nameBindings = Collections.emptySet();
private ClassResourceInfo parent;
private Set<String> injectedSubInstances = new HashSet<>();
private ApplicationInfo applicationInfo;

public ClassResourceInfo(ClassResourceInfo cri) {
super(cri.getBus());
if (cri.isCreatedFromModel() && !InjectionUtils.isConcreteClass(cri.getServiceClass())) {
Expand Down Expand Up @@ -362,4 +364,12 @@ public void injectContexts(Object resourceObject, OperationResourceInfo ori, Mes
}
}
}

public ApplicationInfo getApplicationInfo() {
return applicationInfo;
}

public void setApplicationInfo(ApplicationInfo applicationInfo) {
this.applicationInfo = applicationInfo;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1033,7 +1033,7 @@ public static boolean isValidResourceClass(Class<?> c) {

public static ApplicationPath locateApplicationPath(Class<?> appClass) {
ApplicationPath appPath = appClass.getAnnotation(ApplicationPath.class);
if (appPath == null && appClass.getSuperclass() != Application.class) {
if (appPath == null && appClass.getSuperclass() != null && appClass.getSuperclass() != Application.class) {
return locateApplicationPath(appClass.getSuperclass());
}
return appPath;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@
import java.util.ArrayList;
import java.util.List;

import jakarta.ws.rs.ApplicationPath;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.core.Application;
import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.core.PathSegment;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.UriInfo;
import org.apache.cxf.jaxrs.model.ApplicationInfo;
import org.apache.cxf.jaxrs.model.ClassResourceInfo;
import org.apache.cxf.jaxrs.model.MethodInvocationInfo;
import org.apache.cxf.jaxrs.model.OperationResourceInfo;
Expand Down Expand Up @@ -577,17 +580,22 @@ public void testGetMatchedURIsSubResourceLocatorSubPath() throws Exception {

@Test
public void testGetMatchedResourceTemplateIncludesApplicationPathAndTemplateVariables() throws Exception {
@ApplicationPath("/app")
class App extends Application {
}

Message m = mockMessage("http://localhost:8080/app", "/foo/one/abc");
OperationResourceInfoStack oriStack = new OperationResourceInfoStack();
ClassResourceInfo cri = getCri(RootResource.class, true);
cri.setApplicationInfo(new ApplicationInfo(new App(), null));
OperationResourceInfo ori = getOri(cri, "getTemplate");

MethodInvocationInfo miInfo = new MethodInvocationInfo(ori, RootResource.class, new ArrayList<String>());
oriStack.push(miInfo);
m.put(OperationResourceInfoStack.class, oriStack);

UriInfoImpl u = new UriInfoImpl(m);
assertEquals("/foo/one/{name:[a-zA-Z][a-zA-Z_0-9]*}", u.getMatchedResourceTemplate());
assertEquals("/app/foo/one/{name:[a-zA-Z][a-zA-Z_0-9]*}", u.getMatchedResourceTemplate());
}

@Test
Expand All @@ -607,18 +615,23 @@ public void testGetMatchedResourceTemplatePreserveSpacesInTemplateVariables() th

@Test
public void testGetMatchedResourceTemplateIgnoresPathBeforeApplicationPath() throws Exception {
@ApplicationPath("/service")
class App extends Application {
}

Message m = mockMessage("http://localhost:8080/context/service", "/foo/bar");

OperationResourceInfoStack oriStack = new OperationResourceInfoStack();
ClassResourceInfo cri = getCri(RootResource.class, true);
cri.setApplicationInfo(new ApplicationInfo(new App(), null));
OperationResourceInfo ori = getOri(cri, "getSubMethod");

MethodInvocationInfo miInfo = new MethodInvocationInfo(ori, RootResource.class, new ArrayList<String>());
oriStack.push(miInfo);
m.put(OperationResourceInfoStack.class, oriStack);

UriInfoImpl u = new UriInfoImpl(m);
assertEquals("/foo/bar", u.getMatchedResourceTemplate());
assertEquals("/service/foo/bar", u.getMatchedResourceTemplate());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public void testGetMatchedResourceTemplate() throws Exception {
WebClient wc = WebClient.create("http://localhost:" + PORT
+ "/application6/the%20books2/bookstore2/book%20template/abc");
assertThat(wc.accept("*/*").get(String.class),
equalTo("/bookstore2/book%20template/{name:[a-zA-Z][a-zA-Z_0-9]*}"));
equalTo("/the%20books2/bookstore2/book%20template/{name:[a-zA-Z][a-zA-Z_0-9]*}"));
}

@SafeVarargs
Expand Down
Loading