Project

Profile

Help

Bug #4213 » setheaderextensionDefinition.txt

code snippet - Pranay Deshpande, 2019-05-07 08:55

 
public class SetHeaderExtensionFunction extends ExtensionFunctionDefinition {

private static final long serialVersionUID = 1L;

@Override
public StructuredQName getFunctionQName() {
return new StructuredQName("", "http://sap.com/it/", "setHeader");
}

@Override
public int getMinimumNumberOfArguments() {
return 3;
}

@Override
public int getMaximumNumberOfArguments() {
return 3;
}

@Override
public SequenceType[] getArgumentTypes() {
return new SequenceType[] { SequenceType.SINGLE_ITEM, SequenceType.SINGLE_STRING, SequenceType.SINGLE_ITEM };
}

@Override
public SequenceType getResultType(SequenceType[] suppliedArgumentTypes) {
return SequenceType.EMPTY_SEQUENCE;
}

@Override
public ExtensionFunctionCall makeCallExpression() {
return new ExtensionFunctionCall() {
private static final long serialVersionUID = 1L;

@Override
public Sequence call(XPathContext xPathContext, Sequence[] arguments) throws XPathException {
// 1st argument (mandatory, index 0)
ObjectValue arg1 = (ObjectValue) arguments[0].iterate().next();
Exchange exchange;
if (arg1.getObject() instanceof Exchange) {
exchange = (Exchange) arg1.getObject();
} else {
throw new XPathException("The first parameter of " + getFunctionQName().getClarkName()
+ " must be an org.apache.camel.Exchange but is a " + arg1.getObject().getClass().getName());
}

// 2nd argument (mandatory, index 1)
StringValue arg2 = (StringValue) arguments[1].iterate().next();
String headerName = arg2.getStringValue();

// 3rd argument (mandatory, index 2)
Item arg3 = arguments[2].iterate().next();
String headerValue = arg3.getStringValue();

exchange.getOut().setHeader(headerName, headerValue);

// Don't return anything
return SequenceTool.toLazySequence(EmptyIterator.emptyIterator());
}
};
}

}
(2-2/3)