Project

Profile

Help

Bug #4213 » setheaderextensionDefinition.txt

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

 
1
public class SetHeaderExtensionFunction extends ExtensionFunctionDefinition {
2

    
3
    private static final long serialVersionUID = 1L;
4

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

    
10
    @Override
11
    public int getMinimumNumberOfArguments() {
12
        return 3;
13
    }
14

    
15
    @Override
16
    public int getMaximumNumberOfArguments() {
17
        return 3;
18
    }
19

    
20
    @Override
21
    public SequenceType[] getArgumentTypes() {
22
        return new SequenceType[] { SequenceType.SINGLE_ITEM, SequenceType.SINGLE_STRING, SequenceType.SINGLE_ITEM };
23
    }
24

    
25
    @Override
26
    public SequenceType getResultType(SequenceType[] suppliedArgumentTypes) {
27
        return SequenceType.EMPTY_SEQUENCE;
28
    }
29

    
30
    @Override
31
    public ExtensionFunctionCall makeCallExpression() {
32
        return new ExtensionFunctionCall() {
33
            private static final long serialVersionUID = 1L;
34

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

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

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

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

    
57
                // Don't return anything
58
                return SequenceTool.toLazySequence(EmptyIterator.emptyIterator());
59
            }
60
        };
61
    }
62

    
63
}
(2-2/3)