|
| 1 | +/* |
| 2 | + * Copyright 2017 tinyauth.io |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + */ |
| 17 | + |
| 18 | +package io.tinyauth.elasticsearch.actions; |
| 19 | + |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Set; |
| 23 | +import java.util.HashSet; |
| 24 | +import java.util.Collections; |
| 25 | +import java.util.Comparator; |
| 26 | +import java.util.ArrayList; |
| 27 | +import java.lang.reflect.Method; |
| 28 | +import java.lang.reflect.InvocationTargetException; |
| 29 | + |
| 30 | +import org.apache.logging.log4j.Logger; |
| 31 | + |
| 32 | +import org.elasticsearch.action.ActionRequest; |
| 33 | +import org.elasticsearch.action.CompositeIndicesRequest; |
| 34 | +import org.elasticsearch.action.DocWriteRequest; |
| 35 | +import org.elasticsearch.action.IndicesRequest; |
| 36 | +import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest; |
| 37 | +import org.elasticsearch.action.bulk.BulkRequest; |
| 38 | +import org.elasticsearch.action.bulk.BulkShardRequest; |
| 39 | +import org.elasticsearch.action.delete.DeleteRequest; |
| 40 | +import org.elasticsearch.action.get.MultiGetRequest; |
| 41 | +import org.elasticsearch.action.index.IndexRequest; |
| 42 | +import org.elasticsearch.action.search.MultiSearchRequest; |
| 43 | +import org.elasticsearch.action.search.SearchRequest; |
| 44 | +import org.elasticsearch.action.admin.indices.stats.IndicesStatsRequest; |
| 45 | +import org.elasticsearch.action.admin.cluster.node.stats.TransportNodesStatsAction; |
| 46 | +import org.elasticsearch.action.get.GetRequest; |
| 47 | +import org.elasticsearch.common.logging.Loggers; |
| 48 | + |
| 49 | + |
| 50 | +public class ActionIndexesAdaptor { |
| 51 | + private static final Logger logger = Loggers.getLogger(ActionIndexesAdaptor.class); |
| 52 | + private ArrayList<Method> methods; |
| 53 | + |
| 54 | + public ActionIndexesAdaptor() { |
| 55 | + methods = new ArrayList<Method>(); |
| 56 | + |
| 57 | + for (Method m: this.getClass().getMethods()) { |
| 58 | + if (m.getName() != "extractIndices") |
| 59 | + continue; |
| 60 | + |
| 61 | + if (!m.getGenericReturnType().toString().equals("java.util.Set<java.lang.String>")) |
| 62 | + continue; |
| 63 | + |
| 64 | + logger.error(m); |
| 65 | + methods.add(m); |
| 66 | + } |
| 67 | + // Collections.sort(methods, methodComparator); |
| 68 | + } |
| 69 | + |
| 70 | + public Set<String> extractIndices(SearchRequest req) { |
| 71 | + logger.error("SearchRequest"); |
| 72 | + Set<String> idxs = new HashSet<String>(); |
| 73 | + Collections.addAll(idxs, req.indices()); |
| 74 | + return idxs; |
| 75 | + } |
| 76 | + |
| 77 | + public Set<String> extractIndices(ActionRequest req) { |
| 78 | + logger.error("ActionRequest"); |
| 79 | + return new HashSet<String>(); |
| 80 | + } |
| 81 | + |
| 82 | + public Set<String> getIndices(ActionRequest req) { |
| 83 | + for (Method m: methods) { |
| 84 | + Class<?>[] c = m.getParameterTypes(); |
| 85 | + if (c[0].isInstance(req)) { |
| 86 | + logger.error("Found adaptor for type " + c[0]); |
| 87 | + |
| 88 | + try { |
| 89 | + @SuppressWarnings("unchecked") |
| 90 | + Set<String> indices = (Set<String>) m.invoke(this, req); |
| 91 | + return indices; |
| 92 | + } catch (IllegalAccessException e) { |
| 93 | + logger.error("IllegalAccessException"); |
| 94 | + } catch (IllegalArgumentException e) { |
| 95 | + logger.error("IllegalArgumentException"); |
| 96 | + } catch (InvocationTargetException e) { |
| 97 | + logger.error("InvocationTargetException"); |
| 98 | + } catch (NullPointerException e) { |
| 99 | + logger.error("NullPointerException"); |
| 100 | + } catch (ExceptionInInitializerError e) { |
| 101 | + logger.error("NullPointerException"); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + logger.error("Unable to find adaptor for request"); |
| 107 | + return new HashSet<String>(); |
| 108 | + } |
| 109 | +} |
0 commit comments