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,7 +104,12 @@ public static void propagateCamelHeadersToCxfHeaders(

LOG.trace("Propagate Camel header: {}={} as {}", entry.getKey(), entry.getValue(), cxfHeaderName);

requestHeaders.put(cxfHeaderName, Arrays.asList(entry.getValue().toString()));
Object values = entry.getValue();
if (values instanceof List<?>) {
requestHeaders.put(cxfHeaderName, CastUtils.cast((List<?>) values, String.class));
} else {
requestHeaders.put(cxfHeaderName, Arrays.asList(values.toString()));
}
});
}

Expand Down Expand Up @@ -191,8 +196,8 @@ public static void propagateCxfHeadersToCamelHeaders(

LOG.trace("Populate external header: {}={} as {}", entry.getKey(), entry.getValue(), camelHeaderName);
if (!camelHeaderName.startsWith(":")) {
///* Ignore HTTP/2 pseudo headers such as :status */
camelHeaders.put(camelHeaderName, entry.getValue().get(0));
List<Object> values = entry.getValue();
camelHeaders.put(camelHeaderName, values.size() == 1 ? values.get(0) : values);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,10 @@ public static <T> T convertTo(
Response response = (Response) value;
Object entity = response.getEntity();

if (entity == null) {
return (T) MISS_VALUE;
}

TypeConverter tc = registry.lookup(type, entity.getClass());
if (tc != null) {
return tc.convertTo(type, exchange, entity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ private org.apache.camel.Exchange prepareExchange(
Exchange cxfExchange, Method method,
Object[] paramArray, Object response) {
ExchangePattern ep = ExchangePattern.InOut;
if (method.getReturnType() == Void.class) {
if (method.getReturnType() == Void.TYPE || method.getReturnType() == Void.class) {
ep = ExchangePattern.InOnly;
}
final org.apache.camel.Exchange camelExchange = endpoint.createExchange(ep);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,19 @@ protected Map<String, String> parseResponseHeaders(Object response, Exchange cam

for (Map.Entry<String, List<Object>> entry : resp.getMetadata().entrySet()) {
LOG.trace("Parse external header {}={}", entry.getKey(), entry.getValue());
answer.put(entry.getKey(), entry.getValue().get(0).toString());
List<Object> values = entry.getValue();
if (values.size() == 1) {
answer.put(entry.getKey(), values.get(0).toString());
} else {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < values.size(); i++) {
if (i > 0) {
sb.append(", ");
}
sb.append(values.get(i));
}
answer.put(entry.getKey(), sb.toString());
}
}
}

Expand Down Expand Up @@ -701,6 +713,7 @@ public void completed(Response response) {
// handle cookies
saveCookies(exchange, client, cxfRsEndpoint.getCookieHandler());
if (!exchange.getPattern().isOutCapable()) {
response.close();
return;
}

Expand Down Expand Up @@ -806,6 +819,9 @@ public void completed(Object body) {
return;
}
if (!exchange.getPattern().isOutCapable()) {
if (response != null) {
response.close();
}
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,11 @@ private static void populateViaResponse(org.apache.cxf.message.Exchange cxfExcha
private static void setProtocolHeaders(org.apache.cxf.message.Exchange cxfExchange, Message response) {
Map<String, Object> headers
= CastUtils.cast((Map<?, ?>) response.getHeader(CxfConstants.PROTOCOL_HEADERS));
if (!ObjectHelper.isEmpty(cxfExchange) && !ObjectHelper.isEmpty(cxfExchange.getOutMessage())) {
cxfExchange.getOutMessage().putIfAbsent(CxfConstants.PROTOCOL_HEADERS,
new TreeMap<>(String.CASE_INSENSITIVE_ORDER));
if (cxfExchange == null || cxfExchange.getOutMessage() == null) {
return;
}
cxfExchange.getOutMessage().putIfAbsent(CxfConstants.PROTOCOL_HEADERS,
new TreeMap<>(String.CASE_INSENSITIVE_ORDER));
final Map<String, List<String>> cxfHeaders = CastUtils
.cast((Map<?, ?>) cxfExchange.getOutMessage().get(CxfConstants.PROTOCOL_HEADERS));

Expand Down Expand Up @@ -310,8 +311,8 @@ protected void copyProtocolHeader(org.apache.cxf.message.Message cxfMessage, Mes
/* Ignore HTTP/2 pseudo headers such as :status */
continue;
} else {
// just put the first String element, as the complex one is filtered
camelMessage.setHeader(entry.getKey(), entry.getValue().get(0));
List<String> values = entry.getValue();
camelMessage.setHeader(entry.getKey(), values.size() == 1 ? values.get(0) : values);
}
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class SubResourceClassInvocationHandler implements InvocationHandler {
public Object invoke(Object proxy, Method method, Object[] parameters) throws Throwable {
Object result = null;
Class<?> returnType = method.getReturnType();
if (!returnType.isAssignableFrom(Void.class)) {
if (returnType != Void.TYPE && returnType != Void.class) {
// create a instance to return
if (returnType.isInterface()) {
// create a new proxy for it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ public void handleException(Map<String, Object> ctx, Throwable ex) {
ConduitSelector conduitSelector = cxfExchange.get(ConduitSelector.class);
if (conduitSelector != null) {
conduitSelector.complete(cxfExchange);
ex = cxfExchange.getOutMessage().getContent(Exception.class);
if (cxfExchange.getOutMessage() != null) {
ex = cxfExchange.getOutMessage().getContent(Exception.class);
}
if (ex == null && cxfExchange.getInMessage() != null) {
ex = cxfExchange.getInMessage().getContent(Exception.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public class CxfEndpoint extends DefaultEndpoint implements AsyncEndpoint, Heade
private static final Logger LOG = LoggerFactory.getLogger(CxfEndpoint.class);

@UriParam(label = "advanced")
protected Bus bus;
protected volatile Bus bus;
@UriParam(label = "advanced")
protected boolean defaultBus;
protected volatile boolean createBus;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -473,8 +473,14 @@ public void populateCxfHeaderFromCamelExchangeBeforeCheckError(

propagateHeadersFromCamelToCxf(camelExchange, camelHeaders, cxfExchange,
responseContext);
if (cxfExchange.getOutMessage() != null) {
cxfExchange.getOutMessage().put(CxfConstants.PROTOCOL_HEADERS, responseContext.get(CxfConstants.PROTOCOL_HEADERS));
Object protocolHeaders = responseContext.get(CxfConstants.PROTOCOL_HEADERS);
if (protocolHeaders != null) {
// Store on the CXF exchange so headers survive into the fault path
// (the out message does not exist yet at this point)
cxfExchange.put(CxfConstants.PROTOCOL_HEADERS, protocolHeaders);
if (cxfExchange.getOutMessage() != null) {
cxfExchange.getOutMessage().put(CxfConstants.PROTOCOL_HEADERS, protocolHeaders);
}
}
}

Expand Down Expand Up @@ -866,7 +872,7 @@ protected String replaceMultiPartContentType(String contentType) {
if (part.charAt(0) == '\"') {
result = part.substring(1, part.length() - 1);
} else {
result = part.substring(5);
result = part;
}
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ private void init(AbstractJAXRSFactoryBean bean) {
setBeanId(beanIdAware.getBeanId());
}

ApplicationContext applicationContext = ((SpringCamelContext) getCamelContext()).getApplicationContext();
configurer = new ConfigurerImpl(applicationContext);
if (getCamelContext() instanceof SpringCamelContext springCamelContext) {
ApplicationContext applicationContext = springCamelContext.getApplicationContext();
configurer = new ConfigurerImpl(applicationContext);
}
}

@Override
Expand All @@ -58,6 +60,14 @@ protected JAXRSServerFactoryBean newJAXRSServerFactoryBean() {
return (JAXRSServerFactoryBean) bean;
}

@Override
protected void setupJAXRSServerFactoryBean(JAXRSServerFactoryBean sfb) {
if (sfb instanceof SpringJAXRSServerFactoryBean springBean) {
springBean.setPerformInvocation(isPerformInvocation());
}
super.setupJAXRSServerFactoryBean(sfb);
}

@Override
protected JAXRSClientFactoryBean newJAXRSClientFactoryBean() {
checkBeanType(bean, JAXRSClientFactoryBean.class);
Expand All @@ -67,7 +77,9 @@ protected JAXRSClientFactoryBean newJAXRSClientFactoryBean() {
@Override
protected void setupJAXRSClientFactoryBean(JAXRSClientFactoryBean cfb, String address) {
// apply Spring bean config first so URI options can override
configurer.configureBean(beanId, cfb);
if (configurer != null) {
configurer.configureBean(beanId, cfb);
}
if (getModelRef() != null) {
cfb.setModelRef(getModelRef());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.camel.component.cxf.common.NullFaultListener;
import org.apache.camel.component.cxf.jaxrs.BeanIdAware;
Expand Down Expand Up @@ -107,6 +108,15 @@ public void setLoggingSizeLimit(int loggingSizeLimit) {
}
}

@Override
public void setProperties(Map<String, Object> properties) {
if (this.getProperties() != null && properties != null) {
this.getProperties().putAll(properties);
} else {
super.setProperties(properties);
}
}

public void setSkipFaultLogging(boolean skipFaultLogging) {
if (skipFaultLogging) {
if (this.getProperties() == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ public void setApplicationContext(ApplicationContext ctx) throws BeansException

if (bus == null) {
bus = BusWiringBeanFactoryPostProcessor.addDefaultBus(ctx);
enableSpringBusShutdownGracefully(bus);
}
}

Expand Down Expand Up @@ -312,46 +313,47 @@ private void enableSpringBusShutdownGracefully(Bus bus) {
&& applicationContext instanceof AbstractApplicationContext abstractApplicationContext) {
ApplicationListener cxfSpringBusListener = null;
for (ApplicationListener listener : abstractApplicationContext.getApplicationListeners()) {

if (listener.getClass().getName().indexOf("org.apache.cxf.bus.spring.SpringBus") >= 0) {
// match by identity to ensure we remove the listener for this specific bus
if (listener == springBus) {
cxfSpringBusListener = listener;
break;
}
}
if (cxfSpringBusListener == null) {
return;
}
ApplicationEventMulticaster aem = applicationContext
.getBean(AbstractApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME,
ApplicationEventMulticaster.class);
aem.removeApplicationListener(cxfSpringBusListener);

abstractApplicationContext.addApplicationListener((final ApplicationEvent event) -> {
new Thread() {
@Override
public void run() {
if (event instanceof ContextClosedEvent && bus.getState() == BusState.RUNNING) {

try {
boolean done = false;
ShutdownStrategy shutdownStrategy = ((DefaultCamelContext) getCamelContext())
.getShutdownStrategy();
while (!done && !shutdownStrategy.hasTimeoutOccurred()) {
int inflight = getCamelContext().getInflightRepository().size();
if (inflight != 0) {
Thread.sleep(1000);
} else {
done = true;
}
if (event instanceof ContextClosedEvent && bus.getState() == BusState.RUNNING) {
// only spawn a thread for shutdown to wait for in-flight exchanges
new Thread(() -> {
try {
boolean done = false;
ShutdownStrategy shutdownStrategy = ((DefaultCamelContext) getCamelContext())
.getShutdownStrategy();
while (!done && !shutdownStrategy.hasTimeoutOccurred()) {
int inflight = getCamelContext().getInflightRepository().size();
if (inflight != 0) {
Thread.sleep(1000);
} else {
done = true;
}
} catch (InterruptedException e) {
LOG.info("Interrupted while enabling graceful SpringBus shutdown");
Thread.currentThread().interrupt();
} catch (Exception e) {
LOG.debug("Error when enabling SpringBus shutdown gracefully", e);
}
springBus.onApplicationEvent(event);
} else {
springBus.onApplicationEvent(event);
} catch (InterruptedException e) {
LOG.info("Interrupted while enabling graceful SpringBus shutdown");
Thread.currentThread().interrupt();
} catch (Exception e) {
LOG.debug("Error when enabling SpringBus shutdown gracefully", e);
}
}
}.start();
springBus.onApplicationEvent(event);
}).start();
} else {
springBus.onApplicationEvent(event);
}
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class CamelConduit extends AbstractConduit implements Configurable {
private final EndpointInfo endpointInfo;
private String targetCamelEndpointUri;
private final Producer producer;
private volatile boolean closed;
private ProducerTemplate camelTemplate;
private final Bus bus;
private final HeaderFilterStrategy headerFilterStrategy;
Expand Down Expand Up @@ -92,9 +93,11 @@ public CamelContext getCamelContext() {
return camelContext;
}

// prepare the message for send out , not actually send out the message
@Override
public void prepare(Message message) throws IOException {
if (closed) {
throw new IOException("CamelConduit is already closed");
}
LOG.trace("CamelConduit send message");
CamelOutputStream os = new CamelOutputStream(
this.targetCamelEndpointUri,
Expand All @@ -107,8 +110,8 @@ public void prepare(Message message) throws IOException {

@Override
public void close() {
closed = true;
LOG.trace("CamelConduit closed ");
// shutdown the producer
try {
producer.stop();
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,22 @@ protected void asyncInvokeFromWorkQueue(final Exchange exchange) throws IOExcept
try {
syncInvoke(exchange);
} catch (Exception e) {
((PhaseInterceptorChain) outMessage.getInterceptorChain()).abort();
outMessage.setContent(Exception.class, e);
((PhaseInterceptorChain) outMessage.getInterceptorChain()).unwind(outMessage);
MessageObserver mo = outMessage.getInterceptorChain().getFaultObserver();
if (outMessage.getInterceptorChain() instanceof PhaseInterceptorChain chain) {
chain.abort();
chain.unwind(outMessage);
}
MessageObserver mo = outMessage.getInterceptorChain() != null
? outMessage.getInterceptorChain().getFaultObserver()
: null;
if (mo == null) {
mo = outMessage.getExchange().get(MessageObserver.class);
}
mo.onMessage(outMessage);
if (mo != null) {
mo.onMessage(outMessage);
} else {
LOG.error("No fault observer available to handle transport error", e);
}
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,7 @@ protected String getBasePath(Exchange camelExchange) {
if (answer == null) {
answer = camelExchange.getFromEndpoint().getEndpointUri();
// remove leading scheme before the http(s) transport so we build a correct base path
answer = answer.replaceFirst("^\\w+:http", "http");
answer = answer.replaceFirst("^\\w+:https", "https");
answer = answer.replaceFirst("^\\w+:(https?)", "$1");
}

return answer;
Expand Down