View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.monitoring.reporting.web.handler.internal;
18  
19  import org.apache.commons.monitoring.configuration.Configuration;
20  import org.apache.commons.monitoring.reporting.web.handler.api.Regex;
21  import org.apache.commons.monitoring.reporting.web.handler.api.TemplateHelper;
22  
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpServletResponse;
25  import java.lang.reflect.Method;
26  import java.util.HashMap;
27  import java.util.Map;
28  import java.util.regex.Pattern;
29  
30  public class EndpointInfo {
31      private final Map<Pattern, Invoker> invokers = new HashMap<Pattern, Invoker>();
32  
33      public Map<Pattern, Invoker> getInvokers() {
34          return invokers;
35      }
36  
37      public static EndpointInfo build(final Class<?> endpointClass, final String id, final String rootMapping) {
38          final EndpointInfo info = new EndpointInfo();
39          final Object instance = Configuration.newInstance(endpointClass);
40          for (final Method method : endpointClass.getMethods()) {
41              if (method.getDeclaringClass() == Object.class) {
42                  continue;
43              }
44  
45              final Regex regex = method.getAnnotation(Regex.class);
46              if (regex != null) {
47                  final Pattern pattern = Pattern.compile(rootMapping + regex.value());
48                  final Invoker invoker = new Invoker(instance, method, id);
49                  int partIdx = 1; // regex index, it starts from 1
50                  for (final Class<?> clazz : method.getParameterTypes()) {
51                      if (HttpServletRequest.class.equals(clazz)) {
52                          invoker.addRequestParameter();
53                      } else if (HttpServletResponse.class.equals(clazz)) {
54                          invoker.addResponseParameter();
55                      } else if (TemplateHelper.class.equals(clazz)) {
56                          invoker.addTemplateHelper(id);
57                      } else {
58                          invoker.addSegmentParameter(clazz, partIdx);
59                          partIdx++;
60                      }
61                  }
62                  info.invokers.put(pattern, invoker);
63              }
64          }
65          return info;
66      }
67  }