Skip to content

Commit

Permalink
Remove CXF workarounds from core module
Browse files Browse the repository at this point in the history
Signed-off-by: Erdle, Tobias <tobi.erdle@gmail.com>
  • Loading branch information
erdlet committed Apr 12, 2021
1 parent e8043b8 commit 486d08c
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 83 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2019 Eclipse Krazo committers and contributors
* Copyright (c) 2018, 2021 Eclipse Krazo committers and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -47,12 +47,6 @@ public boolean configure(FeatureContext context) {
// RESTEasy seems to ignore @ConstrainedTo in some cases
if (context.getConfiguration().getRuntimeType() == RuntimeType.SERVER) {

// https://issues.apache.org/jira/browse/CXF-7501
// https://issues.apache.org/jira/browse/TOMEE-2122
if (servletContext == null) {
log.warning("The ServletContext wasn't injected into the JAX-RS Feature class");
}

Initializer.initialize(context, servletContext);
return true;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2019 Eclipse Krazo committers and contributors
* Copyright (c) 2018, 2021 Eclipse Krazo committers and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,10 @@
*/
package org.eclipse.krazo.bootstrap;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import org.eclipse.krazo.binding.convert.MvcConverterProvider;
import org.eclipse.krazo.core.ViewResponseFilter;
import org.eclipse.krazo.core.ViewableWriter;
Expand All @@ -26,13 +30,8 @@
import org.eclipse.krazo.security.CsrfExceptionMapper;
import org.eclipse.krazo.security.CsrfProtectFilter;
import org.eclipse.krazo.security.CsrfValidateFilter;
import org.eclipse.krazo.util.CdiUtils;

import jakarta.ws.rs.core.FeatureContext;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* Implementation of ConfigProvider which registers all providers of the core module.
Expand Down Expand Up @@ -61,30 +60,7 @@ public void configure(FeatureContext context) {
}

private void register(FeatureContext context, Class<?> providerClass) {

boolean isCxf = context.getClass().getName().startsWith("org.apache.cxf");

/*
* With CXF there is no CDI injection if JAX-RS providers are registered via
* context.register(Class). So we try to lookup provider instances from CDI
* and register them instead.
* See: https://issues.apache.org/jira/browse/CXF-7501
*/
if (isCxf) {
List<?> providerInstances = CdiUtils.getApplicationBeans(providerClass);
if (!providerInstances.isEmpty()) {
context.register(providerInstances.get(0));
} else {
context.register(providerClass);
}
}

// will work for all other containers
else {
context.register(providerClass);
}


context.register(providerClass);
}

}
12 changes: 5 additions & 7 deletions core/src/main/java/org/eclipse/krazo/bootstrap/Initializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,18 @@
*/
package org.eclipse.krazo.bootstrap;

import java.util.Objects;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.eclipse.krazo.core.ViewResponseFilter;
import org.eclipse.krazo.servlet.KrazoContainerInitializer;
import org.eclipse.krazo.util.ServiceLoaders;

import jakarta.servlet.ServletContext;
import jakarta.ws.rs.core.Configuration;
import jakarta.ws.rs.core.FeatureContext;
import java.util.Objects;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* Main class for triggering initialization of Eclipse Krazo
Expand Down Expand Up @@ -71,9 +72,6 @@ private static boolean isAlreadyInitialized(Configuration config) {
return config.isRegistered(ViewResponseFilter.class);
}

/**
* Note: ServletContext may be null here, because CXF doesn't inject it correctly
*/
private static boolean isMvcApplication(ServletContext servletContext) {

// we fall back to enable Krazo if detection didn't work
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import static org.eclipse.krazo.util.PathUtils.noPrefix;
import static org.eclipse.krazo.util.PathUtils.noStartingSlash;

import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URI;
import java.util.Arrays;
Expand All @@ -37,6 +36,11 @@
import java.util.logging.Logger;
import java.util.stream.Collectors;

import org.eclipse.krazo.KrazoConfig;
import org.eclipse.krazo.engine.Viewable;
import org.eclipse.krazo.event.ControllerRedirectEventImpl;
import org.eclipse.krazo.lifecycle.RequestLifecycle;

import jakarta.annotation.Priority;
import jakarta.enterprise.event.Event;
import jakarta.inject.Inject;
Expand All @@ -60,11 +64,6 @@
import jakarta.ws.rs.core.UriInfo;
import jakarta.ws.rs.core.Variant;

import org.eclipse.krazo.KrazoConfig;
import org.eclipse.krazo.engine.Viewable;
import org.eclipse.krazo.event.ControllerRedirectEventImpl;
import org.eclipse.krazo.lifecycle.RequestLifecycle;

/**
* <p>A JAX-RS response filter that fires a {@link jakarta.mvc.event.AfterControllerEvent}
* event. It also verifies the static return type of the controller method is correct,
Expand Down Expand Up @@ -155,7 +154,6 @@ public void filter(ContainerRequestContext requestContext,
// If the entity is null the status will be set to 204 by Jersey. For void methods we need to
// set the status to 200 unless no other status was set by e.g. throwing an Exception.

// Don't use equals() on the result of getStatusInfo(), because it doesn't work on CXF
if (responseContext.getStatusInfo().getStatusCode() == Response.Status.NO_CONTENT.getStatusCode()) {
responseContext.setStatusInfo(Response.Status.OK);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
*/
package org.eclipse.krazo.jaxrs;

import org.eclipse.krazo.util.CdiUtils;

import jakarta.annotation.Priority;
import jakarta.mvc.Controller;
import jakarta.servlet.http.HttpServletRequest;
Expand All @@ -30,7 +28,6 @@
import jakarta.ws.rs.core.Configuration;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.UriInfo;
import java.io.IOException;

/**
* Post-Matching ContainerRequestFilter
Expand Down Expand Up @@ -59,16 +56,12 @@ public class PostMatchingRequestFilter implements ContainerRequestFilter {
@Context
private ResourceInfo resourceInfo;

@Context
private JaxRsContextProducer contextProducer;

@Override
public void filter(ContainerRequestContext requestContext) {

/*
* Please note that we CANNOT inject JaxRsContextProducer here, because this will
* fail on TomEE/CXF/OWB because processing @Context fails for some reason.
*/
JaxRsContextProducer contextProducer = CdiUtils.getApplicationBean(JaxRsContextProducer.class)
.orElseThrow(() -> new IllegalStateException("Cannot find CDI managed JaxRsContextProducer"));

// store JAX-RS context objects so we can produce them via CDI
contextProducer.setConfiguration(configuration);
contextProducer.setRequest(request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,12 @@ public class PreMatchingRequestFilter implements ContainerRequestFilter {
@Context
private UriInfo uriInfo;

@Context
private JaxRsContextProducer contextProducer;

@Override
public void filter(ContainerRequestContext requestContext) {

/*
* Please note that we CANNOT inject JaxRsContextProducer here, because this will
* fail on TomEE/CXF/OWB because processing @Context fails for some reason.
*/
JaxRsContextProducer contextProducer = CdiUtils.getApplicationBean(JaxRsContextProducer.class)
.orElseThrow(() -> new IllegalStateException("Cannot find CDI managed JaxRsContextProducer"));

// store JAX-RS context objects so we can produce them via CDI
contextProducer.setConfiguration(configuration);
contextProducer.setRequest(request);
Expand Down
18 changes: 3 additions & 15 deletions core/src/main/java/org/eclipse/krazo/util/ServiceLoaders.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2019 Eclipse Krazo committers and contributors
* Copyright (c) 2018, 2021 Eclipse Krazo committers and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,15 +17,14 @@
*/
package org.eclipse.krazo.util;

import org.eclipse.krazo.bootstrap.ConfigProvider;

import jakarta.annotation.Priority;
import java.util.Comparator;
import java.util.List;
import java.util.ServiceLoader;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import jakarta.annotation.Priority;

/**
* Utility code for the {@link ServiceLoader} class.
*
Expand All @@ -42,15 +41,6 @@ public static <T> List<T> list(Class<T> type) {
// classloader to use for ServiceLoader lookups
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

/*
* Special workaround for TomEE . The context classloader is an instance of CxfContainerClassLoader
* and NOT the TomEEWebappClassLoader, which seems to break when redeploying apps into a running
* container for some reason.
*/
if (classLoader.getClass().getName().contains("CxfContainerClassLoader")) {
classLoader = ConfigProvider.class.getClassLoader();
}

// return the SPI implementations as a list
return StreamSupport.stream(ServiceLoader.load(type, classLoader).spliterator(), false)
.sorted(new PriorityComparator())
Expand All @@ -76,6 +66,4 @@ private int getPriority(Object o) {
}
}
}


}

0 comments on commit 486d08c

Please sign in to comment.