View Javadoc

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