[ 
https://issues.apache.org/jira/browse/CXF-7659?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16383658#comment-16383658
 ] 

ASF GitHub Bot commented on CXF-7659:
-------------------------------------

reta closed pull request #386: CXF-7659: Improve SSE automatic configuration. 
URL: https://github.com/apache/cxf/pull/386
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git 
a/core/src/main/java/org/apache/cxf/bus/managers/DestinationFactoryManagerImpl.java
 
b/core/src/main/java/org/apache/cxf/bus/managers/DestinationFactoryManagerImpl.java
index 57478c42f9f..0de1f538a58 100644
--- 
a/core/src/main/java/org/apache/cxf/bus/managers/DestinationFactoryManagerImpl.java
+++ 
b/core/src/main/java/org/apache/cxf/bus/managers/DestinationFactoryManagerImpl.java
@@ -19,6 +19,7 @@
 
 package org.apache.cxf.bus.managers;
 
+import java.util.Collections;
 import java.util.Map;
 import java.util.Properties;
 import java.util.ResourceBundle;
@@ -108,6 +109,7 @@ public void deregisterDestinationFactory(String namespace) {
      *
      * @param namespace the namespace.
      */
+    @Override
     public DestinationFactory getDestinationFactory(String namespace) throws 
BusException {
         DestinationFactory factory = destinationFactories.get(namespace);
         if (factory == null && !failed.contains(namespace)) {
@@ -124,11 +126,17 @@ public DestinationFactory getDestinationFactory(String 
namespace) throws BusExce
         return factory;
     }
 
+    @Override
     public DestinationFactory getDestinationFactoryForUri(String uri) {
         return new TransportFinder<DestinationFactory>(bus,
                 destinationFactories,
                 loaded,
                 DestinationFactory.class).findTransportForURI(uri);
     }
-
+    
+    @Override
+    public Set<String> getRegisteredDestinationFactoryNames() {
+        return destinationFactories == null ? Collections.emptySet() 
+                : Collections.unmodifiableSet(destinationFactories.keySet());
+    }
 }
diff --git 
a/core/src/main/java/org/apache/cxf/transport/AbstractTransportFactory.java 
b/core/src/main/java/org/apache/cxf/transport/AbstractTransportFactory.java
index ea7d407adb5..4d756a40b75 100644
--- a/core/src/main/java/org/apache/cxf/transport/AbstractTransportFactory.java
+++ b/core/src/main/java/org/apache/cxf/transport/AbstractTransportFactory.java
@@ -26,6 +26,8 @@
  * Helper methods for {@link DestinationFactory}s and {@link 
ConduitInitiator}s.
  */
 public abstract class AbstractTransportFactory {
+    public static final String PREFERRED_TRANSPORT_ID = 
"org.apache.cxf.preferred.transport.id";
+    
     private List<String> transportIds;
 
     public AbstractTransportFactory() {
diff --git 
a/core/src/main/java/org/apache/cxf/transport/DestinationFactoryManager.java 
b/core/src/main/java/org/apache/cxf/transport/DestinationFactoryManager.java
index 41c69f45ed9..433f86806e3 100644
--- a/core/src/main/java/org/apache/cxf/transport/DestinationFactoryManager.java
+++ b/core/src/main/java/org/apache/cxf/transport/DestinationFactoryManager.java
@@ -19,6 +19,8 @@
 
 package org.apache.cxf.transport;
 
+import java.util.Set;
+
 import org.apache.cxf.BusException;
 
 /**
@@ -42,6 +44,12 @@
      * <code>DestinationFactory</code>.
      */
     void deregisterDestinationFactory(String name);
+    
+    /**
+     * Returns all registered (as of the moment of the call) destination 
factories.
+     * @return all registered (as of the moment of the call) destination 
factories.
+     */
+    Set<String> getRegisteredDestinationFactoryNames();
 
     /**
      * Returns the <code>DestinationFactory</code> registered with the 
specified name,
@@ -53,5 +61,13 @@
      */
     DestinationFactory getDestinationFactory(String name) throws BusException;
 
+    /**
+     * Returns the <code>DestinationFactory</code> registered with the 
specified URI,
+     * loading the appropriate plugin if necessary.
+     *
+     * @param uri the uri to look up <code>DestinationFactory</code>
+     * @return the registered <code>DestinationFactory</code>
+     * @throws BusException
+     */
     DestinationFactory getDestinationFactoryForUri(String uri);
 }
diff --git 
a/rt/rs/sse/src/main/java/org/apache/cxf/jaxrs/sse/ext/SseTransportCustomizationExtension.java
 
b/rt/rs/sse/src/main/java/org/apache/cxf/jaxrs/sse/ext/SseTransportCustomizationExtension.java
index 84a0a9a8051..b0123d03ca3 100644
--- 
a/rt/rs/sse/src/main/java/org/apache/cxf/jaxrs/sse/ext/SseTransportCustomizationExtension.java
+++ 
b/rt/rs/sse/src/main/java/org/apache/cxf/jaxrs/sse/ext/SseTransportCustomizationExtension.java
@@ -18,10 +18,12 @@
  */
 package org.apache.cxf.jaxrs.sse.ext;
 
+import org.apache.cxf.Bus;
 import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
 import org.apache.cxf.jaxrs.ext.JAXRSServerFactoryCustomizationExtension;
 import org.apache.cxf.jaxrs.sse.SseContextProvider;
 import 
org.apache.cxf.jaxrs.sse.atmosphere.SseAtmosphereEventSinkContextProvider;
+import org.apache.cxf.transport.AbstractTransportFactory;
 import org.apache.cxf.transport.sse.SseHttpTransportFactory;
 
 public class SseTransportCustomizationExtension implements 
JAXRSServerFactoryCustomizationExtension {
@@ -30,5 +32,10 @@ public void customize(final JAXRSServerFactoryBean bean) {
         bean.setTransportId(SseHttpTransportFactory.TRANSPORT_ID);
         bean.setProvider(new SseContextProvider());
         bean.setProvider(new SseAtmosphereEventSinkContextProvider());
+        
+        final Bus bus = bean.getBus();
+        if (bus != null) {
+            bus.setProperty(AbstractTransportFactory.PREFERRED_TRANSPORT_ID, 
SseHttpTransportFactory.TRANSPORT_ID);
+        }
     }
 }
diff --git 
a/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/CXFNonSpringServlet.java
 
b/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/CXFNonSpringServlet.java
index b8b5f020b81..4a28fba87a9 100644
--- 
a/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/CXFNonSpringServlet.java
+++ 
b/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/CXFNonSpringServlet.java
@@ -21,6 +21,7 @@
 import java.io.IOException;
 import java.lang.reflect.Method;
 import java.util.Collection;
+import java.util.Set;
 
 import javax.servlet.FilterChain;
 import javax.servlet.ServletConfig;
@@ -41,6 +42,7 @@
 import org.apache.cxf.common.util.StringUtils;
 import org.apache.cxf.helpers.CastUtils;
 import org.apache.cxf.resource.ResourceManager;
+import org.apache.cxf.transport.AbstractTransportFactory;
 import org.apache.cxf.transport.DestinationFactory;
 import org.apache.cxf.transport.DestinationFactoryManager;
 import org.apache.cxf.transport.http.AbstractHTTPDestination;
@@ -53,7 +55,8 @@
 
     private static final long serialVersionUID = -2437897227486327166L;
     private static final String IGNORE_SERVLET_CONTEXT_RESOLVER = 
"ignore.servlet.context.resolver";
-
+    private static final String DEFAULT_TRANSPORT_ID = 
"http://cxf.apache.org/transports/http/configuration";;
+    
     protected Bus bus;
     private DestinationRegistry destinationRegistry;
     private boolean globalRegistry;
@@ -116,9 +119,31 @@ protected ClassLoader initClassLoader() {
     protected DestinationRegistry getDestinationRegistryFromBusOrDefault(final 
String transportId) {
         DestinationFactoryManager dfm = 
bus.getExtension(DestinationFactoryManager.class);
         try {
-            DestinationFactory df = StringUtils.isEmpty(transportId)
-                ? 
dfm.getDestinationFactory("http://cxf.apache.org/transports/http/configuration";)
-                    : dfm.getDestinationFactory(transportId);
+            String peferredTransportId = transportId;
+            
+            // Check if the preferred transport is set on a bus level (f.e., 
from any
+            // extension or customization).
+            if (StringUtils.isEmpty(peferredTransportId) && getBus() != null) {
+                peferredTransportId = 
(String)getBus().getProperty(AbstractTransportFactory.PREFERRED_TRANSPORT_ID);
+            }
+            
+            if (StringUtils.isEmpty(peferredTransportId)) {
+                final Set<String> candidates = 
dfm.getRegisteredDestinationFactoryNames();
+                
+                // If the default transport is present, fall back to it and 
don't even 
+                // consider other candidates
+                if (!candidates.contains(DEFAULT_TRANSPORT_ID)) {
+                    peferredTransportId = candidates
+                        .stream()
+                        .filter(name -> name.endsWith("/configuration"))
+                        .findAny()
+                        .orElse(DEFAULT_TRANSPORT_ID);
+                }
+            }
+            
+            DestinationFactory df = StringUtils.isEmpty(peferredTransportId)
+                ? dfm.getDestinationFactory(DEFAULT_TRANSPORT_ID)
+                    : dfm.getDestinationFactory(peferredTransportId);
             if (df instanceof HTTPTransportFactory) {
                 HTTPTransportFactory transportFactory = 
(HTTPTransportFactory)df;
                 return transportFactory.getRegistry();


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


> Improve SSE automatic configuration
> -----------------------------------
>
>                 Key: CXF-7659
>                 URL: https://issues.apache.org/jira/browse/CXF-7659
>             Project: CXF
>          Issue Type: Improvement
>            Reporter: John D. Ament
>            Assignee: Andriy Redko
>            Priority: Major
>
> The current SSE configuration is a bit hard to get together.  You need to 
> enable transport in two places, servlet and bean.  It would be ideal to 
> consolidate them.
>  
> There's some proposed approaches in 
> [https://lists.apache.org/thread.html/3cecd69d0d6720cedf3395d8464281b7ffdd8a62c285dac32571a772@%3Cdev.cxf.apache.org%3E]
>  



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to