1717
1818package io .tinyauth .elasticsearch ;
1919
20- import java .util .Arrays ;
2120import java .util .List ;
2221import java .util .Set ;
2322import java .util .HashSet ;
2726import java .lang .reflect .Method ;
2827import java .lang .reflect .InvocationTargetException ;
2928
29+ import java .util .stream .Collectors ;
30+ import java .util .stream .Stream ;
31+
3032import org .apache .logging .log4j .Logger ;
3133
3234import org .elasticsearch .action .ActionRequest ;
4951
5052public class ActionIndicesAdaptor {
5153 private static final Logger logger = Loggers .getLogger (ActionIndicesAdaptor .class );
52- private ArrayList <Method > methods ;
5354
54- public ActionIndicesAdaptor () {
55- methods = new ArrayList <Method >();
55+ private String partition ;
56+ private String service ;
57+ private String region ;
5658
57- for (Method m : this .getClass ().getMethods ()) {
58- if (m .getName () != "extractIndices" )
59- continue ;
59+ private List <Method > methods ;
6060
61- if (!m .getGenericReturnType ().toString ().equals ("java.util.Set<java.lang.String>" ))
62- continue ;
61+ public ActionIndicesAdaptor (String partition , String service , String region ) {
62+ this .partition = partition ;
63+ this .service = service ;
64+ this .region = region ;
6365
64- logger .error (m );
65- methods .add (m );
66- }
67- // Collections.sort(methods, methodComparator);
66+ this .methods = Stream .of (this .getClass ().getMethods ())
67+ .filter (m -> m .getName () == "extractIndices" )
68+ .filter (m -> m .getParameterTypes ().length == 1 )
69+ .filter (m -> ActionRequest .class .isAssignableFrom (m .getParameterTypes ()[0 ]))
70+ .filter (m -> m .getGenericReturnType ().toString ().equals ("java.util.Set<java.lang.String>" ))
71+ .sorted ((left , right ) -> {
72+ Class <?> leftType = left .getParameterTypes ()[0 ];
73+ Class <?> rightType = right .getParameterTypes ()[0 ];
74+
75+ if (leftType .isAssignableFrom (rightType ))
76+ return 1 ;
77+
78+ if (rightType .isAssignableFrom (leftType ))
79+ return -1 ;
80+
81+ return leftType .getName ().compareTo (rightType .getName ());
82+ })
83+ .collect (Collectors .toList ());
84+
85+ logger .error (this .methods );
86+ }
87+
88+ private String formatArn (String resourceType , String resource ) {
89+ return String .join (":" ,
90+ "arn" ,
91+ partition ,
92+ service ,
93+ region ,
94+ "" ,
95+ resourceType + "/" + resource
96+ );
97+ }
98+
99+ public Set <String > extractIndices (MultiGetRequest req ) {
100+ return Stream .of (req .getItems ())
101+ .flatMap (ir -> Stream .of (ir .indices ()))
102+ .map (idx -> formatArn ("index" , idx ))
103+ .collect (Collectors .toSet ());
104+ }
105+
106+ public Set <String > extractIndices (MultiSearchRequest req ) {
107+ return Stream .of (req .requests ())
108+ .flatMap (ir -> Stream .of (ir .indices ()))
109+ .map (idx -> formatArn ("index" , idx ))
110+ .collect (Collectors .toSet ());
111+ }
112+
113+ /*public Set<String> extractIndices(MultiTermVectorsRequest req) {
114+ return Stream.of(req.getItems())
115+ .flatMap(ir -> Stream.of(ir.indices()))
116+ .map(idx -> formatArn("index", idx))
117+ .collect(Collectors.toSet());
118+ }*/
119+
120+ /*public Set<String> extractIndices(BulkRequest req) {
121+ return Stream.of(req.requests())
122+ .flatMap(ir -> Stream.of(getIndices(ir)))
123+ .collect(Collectors.toSet());
124+ }*/
125+
126+ public Set <String > extractIndices (DeleteRequest req ) {
127+ return Stream .of (req .indices ()).map (idx -> formatArn ("index" , idx )).collect (Collectors .toSet ());
128+ }
129+
130+ public Set <String > extractIndices (IndexRequest req ) {
131+ return Stream .of (req .indices ()).map (idx -> formatArn ("index" , idx )).collect (Collectors .toSet ());
68132 }
69133
70134 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 ;
135+ return Stream .of (req .indices ()).map (idx -> formatArn ("index" , idx )).collect (Collectors .toSet ());
75136 }
76137
77138 public Set <String > extractIndices (ActionRequest req ) {
78- logger .error ("ActionRequest" );
79139 return new HashSet <String >();
80140 }
81141
@@ -98,12 +158,12 @@ public Set<String> getIndices(ActionRequest req) {
98158 } catch (NullPointerException e ) {
99159 logger .error ("NullPointerException" );
100160 } catch (ExceptionInInitializerError e ) {
101- logger .error ("NullPointerException " );
161+ logger .error ("ExceptionInInitializerError " );
102162 }
103163 }
104164 }
105165
106- logger .error ("Unable to find adaptor for request" );
166+ logger .error ("Unable to find adaptor for request. This is a bug! " );
107167 return new HashSet <String >();
108168 }
109169}
0 commit comments