Skip to content

Commit 1cc08fe

Browse files
committed
Simple request adapter mechanism
1 parent f685675 commit 1cc08fe

File tree

3 files changed

+114
-57
lines changed

3 files changed

+114
-57
lines changed

src/main/java/io/tinyauth/elasticsearch/RequestToIndices.java

Lines changed: 0 additions & 56 deletions
This file was deleted.

src/main/java/io/tinyauth/elasticsearch/TinyauthActionFilter.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,17 @@
8181
import io.tinyauth.elasticsearch.exceptions.ConnectionError;
8282
import io.tinyauth.elasticsearch.Constants;
8383
import io.tinyauth.elasticsearch.Origin;
84+
import io.tinyauth.elasticsearch.actions.*;
8485

8586
import static org.elasticsearch.common.xcontent.XContentFactory.*;
86-
import static io.tinyauth.elasticsearch.RequestToIndices.getIndices;
8787

8888

8989
@Singleton
9090
public class TinyauthActionFilter extends AbstractComponent implements ActionFilter {
9191

9292
private static final Logger logger = Loggers.getLogger(TinyauthActionFilter.class);
93+
private static final ActionIndexesAdaptor indexExtractor = new ActionIndexesAdaptor();
94+
9395
private final ThreadPool threadPool;
9496

9597
private String endpoint;
@@ -143,6 +145,8 @@ public <Request extends ActionRequest, Response extends ActionResponse> void app
143145

144146
String body = "";
145147

148+
logger.error(String.join(" ", indexExtractor.getIndices(request)));
149+
146150
try {
147151
XContentBuilder builder = jsonBuilder()
148152
.startObject()
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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

Comments
 (0)