001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.monitoring.reporting.web.handler.internal;
018    
019    import org.apache.commons.monitoring.configuration.Configuration;
020    import org.apache.commons.monitoring.reporting.web.handler.api.Regex;
021    import org.apache.commons.monitoring.reporting.web.handler.api.TemplateHelper;
022    
023    import javax.servlet.http.HttpServletRequest;
024    import javax.servlet.http.HttpServletResponse;
025    import java.lang.reflect.Method;
026    import java.util.HashMap;
027    import java.util.Map;
028    import java.util.regex.Pattern;
029    
030    public class EndpointInfo {
031        private final Map<Pattern, Invoker> invokers = new HashMap<Pattern, Invoker>();
032    
033        public Map<Pattern, Invoker> getInvokers() {
034            return invokers;
035        }
036    
037        public static EndpointInfo build(final Class<?> endpointClass, final String id, final String rootMapping) {
038            final EndpointInfo info = new EndpointInfo();
039            final Object instance = Configuration.newInstance(endpointClass);
040            for (final Method method : endpointClass.getMethods()) {
041                if (method.getDeclaringClass() == Object.class) {
042                    continue;
043                }
044    
045                final Regex regex = method.getAnnotation(Regex.class);
046                if (regex != null) {
047                    final Pattern pattern = Pattern.compile(rootMapping + regex.value());
048                    final Invoker invoker = new Invoker(instance, method, id);
049                    int partIdx = 1; // regex index, it starts from 1
050                    for (final Class<?> clazz : method.getParameterTypes()) {
051                        if (HttpServletRequest.class.equals(clazz)) {
052                            invoker.addRequestParameter();
053                        } else if (HttpServletResponse.class.equals(clazz)) {
054                            invoker.addResponseParameter();
055                        } else if (TemplateHelper.class.equals(clazz)) {
056                            invoker.addTemplateHelper(id);
057                        } else {
058                            invoker.addSegmentParameter(clazz, partIdx);
059                            partIdx++;
060                        }
061                    }
062                    info.invokers.put(pattern, invoker);
063                }
064            }
065            return info;
066        }
067    }