use of org.apache.cxf.headers.Header in project camel by apache.
the class CxfMessageHeadersRelayTest method buildOutOfBandHeaderList.
protected static List<Header> buildOutOfBandHeaderList(boolean invalid) throws JAXBException {
OutofBandHeader ob = new OutofBandHeader();
ob.setName("testOobHeader");
ob.setValue("testOobHeaderValue");
ob.setHdrAttribute(invalid ? "dontProcess" : "testHdrAttribute");
SoapHeader hdr = new SoapHeader(new QName(Constants.TEST_HDR_NS, Constants.TEST_HDR_REQUEST_ELEM), ob, new JAXBDataBinding(ob.getClass()));
hdr.setMustUnderstand(invalid);
List<Header> headers = new ArrayList<Header>();
headers.add(hdr);
return headers;
}
use of org.apache.cxf.headers.Header in project camel by apache.
the class CxfMessageHeadersRelayTest method doTestInOutOutOfBandHeaderCamelTemplate.
public void doTestInOutOutOfBandHeaderCamelTemplate(String producerUri) throws Exception {
// START SNIPPET: sending
Exchange senderExchange = new DefaultExchange(context, ExchangePattern.InOut);
final List<Object> params = new ArrayList<Object>();
Me me = new Me();
me.setFirstName("john");
me.setLastName("Doh");
params.add(me);
senderExchange.getIn().setBody(params);
senderExchange.getIn().setHeader(CxfConstants.OPERATION_NAME, "inoutOutOfBandHeader");
List<Header> inHeaders = buildOutOfBandHeaderList(false);
Map<String, Object> requestContext = new HashMap<String, Object>();
requestContext.put(Header.HEADER_LIST, inHeaders);
senderExchange.getIn().setHeader(Client.REQUEST_CONTEXT, requestContext);
Exchange exchange = template.send(producerUri, senderExchange);
org.apache.camel.Message out = exchange.getOut();
MessageContentsList result = (MessageContentsList) out.getBody();
assertTrue("Expected the out of band header to propagate but it didn't", result.get(0) != null && ((Me) result.get(0)).getFirstName().equals("pass"));
Map<String, Object> responseContext = CastUtils.cast((Map<?, ?>) out.getHeader(Client.RESPONSE_CONTEXT));
assertNotNull(responseContext);
validateReturnedOutOfBandHeader(responseContext);
}
use of org.apache.cxf.headers.Header in project camel by apache.
the class DefaultCxfBinding method propagateHeadersFromCamelToCxf.
@SuppressWarnings("unchecked")
protected void propagateHeadersFromCamelToCxf(Exchange camelExchange, Map<String, Object> camelHeaders, org.apache.cxf.message.Exchange cxfExchange, Map<String, Object> cxfContext) {
// get cxf transport headers (if any) from camel exchange
// use a treemap to keep ordering and ignore key case
Map<String, List<String>> transportHeaders = new TreeMap<String, List<String>>(String.CASE_INSENSITIVE_ORDER);
if (camelExchange != null) {
Map<String, List<String>> h = CastUtils.cast((Map<?, ?>) camelExchange.getProperty(Message.PROTOCOL_HEADERS));
if (h != null) {
transportHeaders.putAll(h);
}
}
Map<String, List<String>> headers = CastUtils.cast((Map<?, ?>) camelHeaders.get(Message.PROTOCOL_HEADERS));
if (headers != null) {
transportHeaders.putAll(headers);
}
DataFormat dataFormat = camelExchange.getProperty(CxfConstants.DATA_FORMAT_PROPERTY, DataFormat.class);
for (Map.Entry<String, Object> entry : camelHeaders.entrySet()) {
// put response code in request context so it will be copied to CXF message's property
if (Message.RESPONSE_CODE.equals(entry.getKey()) || Exchange.HTTP_RESPONSE_CODE.equals(entry.getKey())) {
LOG.debug("Propagate to CXF header: {} value: {}", Message.RESPONSE_CODE, entry.getValue());
cxfContext.put(Message.RESPONSE_CODE, entry.getValue());
continue;
}
// We need to copy the content-type if the dataformat is RAW
if (Message.CONTENT_TYPE.equalsIgnoreCase(entry.getKey()) && dataFormat.equals(DataFormat.RAW)) {
LOG.debug("Propagate to CXF header: {} value: {}", Message.CONTENT_TYPE, entry.getValue());
cxfContext.put(Message.CONTENT_TYPE, entry.getValue().toString());
continue;
}
// need to filter the User-Agent ignore the case, as CXF just check the header with "User-Agent"
if (entry.getKey().equalsIgnoreCase("User-Agent")) {
List<String> listValue = new ArrayList<String>();
listValue.add(entry.getValue().toString());
transportHeaders.put("User-Agent", listValue);
}
// this header should be filtered, continue to the next header
if (headerFilterStrategy.applyFilterToCamelHeaders(entry.getKey(), entry.getValue(), camelExchange)) {
continue;
}
LOG.debug("Propagate to CXF header: {} value: {}", entry.getKey(), entry.getValue());
// put SOAP/protocol header list in exchange
if (Header.HEADER_LIST.equals(entry.getKey())) {
List<Header> headerList = (List<Header>) entry.getValue();
for (Header header : headerList) {
header.setDirection(Header.Direction.DIRECTION_OUT);
LOG.trace("Propagate SOAP/protocol header: {} : {}", header.getName(), header.getObject());
}
//cxfExchange.put(Header.HEADER_LIST, headerList);
cxfContext.put(entry.getKey(), headerList);
continue;
}
if (ObjectHelper.isNotEmpty(entry.getValue())) {
// things that are not filtered and not specifically copied will be put in transport headers
if (entry.getValue() instanceof List) {
transportHeaders.put(entry.getKey(), (List<String>) entry.getValue());
} else {
List<String> listValue = new ArrayList<String>();
listValue.add(entry.getValue().toString());
transportHeaders.put(entry.getKey(), listValue);
}
}
}
if (transportHeaders.size() > 0) {
cxfContext.put(Message.PROTOCOL_HEADERS, transportHeaders);
} else {
// no propagated transport headers does really mean no headers, not the ones
// from the previous request or response propagated with the invocation context
cxfContext.remove(Message.PROTOCOL_HEADERS);
}
cxfContext.put(SoapBindingConstants.SOAP_ACTION, camelHeaders.get(SoapBindingConstants.SOAP_ACTION));
}
use of org.apache.cxf.headers.Header in project cxf by apache.
the class OOBHdrServiceImpl method sendReturnOOBHeader.
private void sendReturnOOBHeader() {
if (context != null) {
MessageContext ctx = context.getMessageContext();
if (ctx != null) {
try {
// Create out-of-band header object.
OutofBandHeader ob = new OutofBandHeader();
ob.setName("testOobReturnHeaderName");
ob.setValue("testOobReturnHeaderValue");
ob.setHdrAttribute("testReturnHdrAttribute");
// Add Out-of-band header object to HeaderHolder.
JAXBElement<OutofBandHeader> job = new JAXBElement<OutofBandHeader>(new QName(OOBHeaderTest.TEST_HDR_NS, OOBHeaderTest.TEST_HDR_RESPONSE_ELEM), OutofBandHeader.class, null, ob);
Header hdr = new Header(new QName(OOBHeaderTest.TEST_HDR_NS, OOBHeaderTest.TEST_HDR_RESPONSE_ELEM), job, new JAXBDataBinding(ob.getClass()));
List<Header> hdrList = CastUtils.cast((List<?>) ctx.get(Header.HEADER_LIST));
hdrList.add(hdr);
// Add headerHolder to requestContext.
// ctx.put(Header.HEADER_LIST, hdrList);
// System.out.println("Completed adding list to context");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
use of org.apache.cxf.headers.Header in project cxf by apache.
the class OOBHeaderTest method checkReturnedOOBHeader.
private void checkReturnedOOBHeader(PutLastTradedPricePortType portType) {
InvocationHandler handler = Proxy.getInvocationHandler(portType);
BindingProvider bp = null;
if (handler instanceof BindingProvider) {
bp = (BindingProvider) handler;
Map<String, Object> responseContext = bp.getResponseContext();
OutofBandHeader hdrToTest = null;
List<?> oobHdr = (List<?>) responseContext.get(Header.HEADER_LIST);
if (oobHdr == null) {
fail("Should have got List of out-of-band headers ..");
}
assertTrue("HeaderHolder list expected to conain 1 object received " + oobHdr.size(), oobHdr.size() == 1);
if (oobHdr != null) {
Iterator<?> iter = oobHdr.iterator();
while (iter.hasNext()) {
Object hdr = iter.next();
if (hdr instanceof Header) {
Header hdr1 = (Header) hdr;
if (hdr1.getObject() instanceof Node) {
// System.out.println("Node conains : " + hdr1.getObject().toString());
try {
JAXBElement<?> job = (JAXBElement<?>) JAXBContext.newInstance(ObjectFactory.class).createUnmarshaller().unmarshal((Node) hdr1.getObject());
hdrToTest = (OutofBandHeader) job.getValue();
// System.out.println("oob-hdr contains : \nname = "
// + hdrToTest.getName()
// + " \nvalue = " + hdrToTest.getValue()
// + " \natribute = " + hdrToTest.getHdrAttribute());
} catch (JAXBException ex) {
//
ex.printStackTrace();
}
}
}
}
}
assertNotNull("out-of-band header should not be null", hdrToTest);
assertTrue("Expected out-of-band Header name testOobReturnHeaderName recevied :" + hdrToTest.getName(), "testOobReturnHeaderName".equals(hdrToTest.getName()));
assertTrue("Expected out-of-band Header value testOobReturnHeaderValue recevied :" + hdrToTest.getValue(), "testOobReturnHeaderValue".equals(hdrToTest.getValue()));
assertTrue("Expected out-of-band Header attribute testReturnHdrAttribute recevied :" + hdrToTest.getHdrAttribute(), "testReturnHdrAttribute".equals(hdrToTest.getHdrAttribute()));
}
}
Aggregations