View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.config.parsers;
8   
9   import org.mule.api.annotations.meta.Evaluator;
10  import org.mule.api.annotations.param.InboundHeaders;
11  import org.mule.api.expression.ExpressionAnnotationParser;
12  import org.mule.expression.ExpressionConfig;
13  import org.mule.expression.MessageHeaderExpressionEvaluator;
14  import org.mule.expression.MessageHeadersExpressionEvaluator;
15  import org.mule.expression.MessageHeadersListExpressionEvaluator;
16  import org.mule.expression.transformers.ExpressionArgument;
17  
18  import java.lang.annotation.Annotation;
19  import java.util.List;
20  import java.util.Map;
21  
22  import static org.mule.expression.ExpressionConstants.OPTIONAL_ARGUMENT;
23  
24  /**
25   * Responsible for parsing the {@link org.mule.api.annotations.param.InboundHeaders} annotation.  This is an iBeans
26   * framework class and cannot be used in any other context.
27   */
28  public class InboundHeadersAnnotationParser implements ExpressionAnnotationParser
29  {
30      public ExpressionArgument parse(Annotation annotation, Class<?> parameterType)
31      {
32          Evaluator evaluator = annotation.annotationType().getAnnotation(Evaluator.class);
33  
34          if (evaluator != null)
35          {
36              String expr = ((InboundHeaders) annotation).value();
37  
38              boolean optional = false;
39              String eval = MessageHeaderExpressionEvaluator.NAME;
40              if (parameterType.isAssignableFrom(Map.class))
41              {
42                  eval = MessageHeadersExpressionEvaluator.NAME;
43              }
44              else if (parameterType.isAssignableFrom(List.class))
45              {
46                  eval = MessageHeadersListExpressionEvaluator.NAME;
47              }
48              else if (expr.endsWith(OPTIONAL_ARGUMENT))
49              {
50                  //We only set optional if we're dealing with a single header, List and Maps of headers can contain
51                  //optional names but we will always return a Map or List even if it is empty
52                  optional = true;
53              }
54              return new ExpressionArgument(null, new ExpressionConfig("INBOUND:" + expr, eval, null), optional, parameterType);
55          }
56          else
57          {
58              throw new IllegalArgumentException("The @Evaluator annotation must be set on an Expression Annotation");
59          }
60      }
61  
62      public boolean supports(Annotation annotation)
63      {
64          return annotation instanceof InboundHeaders;
65      }
66  }