2
2
3
3
import io .zentity .model .Model ;
4
4
import io .zentity .model .ValidationException ;
5
+ import org .elasticsearch .ElasticsearchSecurityException ;
5
6
import org .elasticsearch .action .admin .indices .exists .indices .IndicesExistsRequestBuilder ;
6
7
import org .elasticsearch .action .admin .indices .exists .indices .IndicesExistsResponse ;
7
8
import org .elasticsearch .action .delete .DeleteRequestBuilder ;
34
35
35
36
public class ModelsAction extends BaseRestHandler {
36
37
37
- public static final String INDEX = ".zentity-models" ;
38
- public static final String INDEX_MAPPING = "{\n " +
39
- " \" doc\" : {\n " +
40
- " \" dynamic\" : \" strict\" ,\n " +
41
- " \" properties\" : {\n " +
42
- " \" attributes\" : {\n " +
43
- " \" type\" : \" object\" ,\n " +
44
- " \" enabled\" : false\n " +
45
- " },\n " +
46
- " \" resolvers\" : {\n " +
47
- " \" type\" : \" object\" ,\n " +
48
- " \" enabled\" : false\n " +
49
- " },\n " +
50
- " \" matchers\" : {\n " +
51
- " \" type\" : \" object\" ,\n " +
52
- " \" enabled\" : false\n " +
53
- " },\n " +
54
- " \" indices\" : {\n " +
55
- " \" type\" : \" object\" ,\n " +
56
- " \" enabled\" : false\n " +
57
- " }\n " +
58
- " }\n " +
59
- " }\n " +
60
- "}" ;
38
+ public static final String INDEX_NAME = ".zentity-models" ;
61
39
62
40
@ Inject
63
41
public ModelsAction (Settings settings , RestController controller ) {
@@ -69,41 +47,41 @@ public ModelsAction(Settings settings, RestController controller) {
69
47
controller .registerHandler (DELETE , "_zentity/models/{entity_type}" , this );
70
48
}
71
49
72
- public static void createIndex (NodeClient client ) {
73
- client .admin ().indices ().prepareCreate (INDEX )
74
- .setSettings (Settings .builder ()
75
- .put ("index.number_of_shards" , 1 )
76
- .put ("index.number_of_replicas" , 1 )
77
- )
78
- .addMapping ("doc" , INDEX_MAPPING , XContentType .JSON )
79
- .get ();
80
- }
81
-
82
50
/**
83
51
* Check if the .zentity-models index exists, and if it doesn't, then create it.
84
52
*
85
53
* @param client The client that will communicate with Elasticsearch.
54
+ * @throws ForbiddenException
86
55
*/
87
- public static void ensureIndex (NodeClient client ) {
88
- IndicesExistsRequestBuilder request = client .admin ().indices ().prepareExists (INDEX );
89
- IndicesExistsResponse response = request .get ();
90
- if (!response .isExists ())
91
- createIndex (client );
56
+ public static void ensureIndex (NodeClient client ) throws ForbiddenException {
57
+ try {
58
+ IndicesExistsRequestBuilder request = client .admin ().indices ().prepareExists (INDEX_NAME );
59
+ IndicesExistsResponse response = request .get ();
60
+ if (!response .isExists ())
61
+ SetupAction .createIndex (client );
62
+ } catch (ElasticsearchSecurityException se ) {
63
+ throw new ForbiddenException ("The .zentity-models index does not exist and you do not have the 'create_index' privilege. An authorized user must create the index by submitting: POST _zentity/_setup" );
64
+ }
92
65
}
93
66
94
67
/**
95
68
* Retrieve all entity models.
96
69
*
97
70
* @param client The client that will communicate with Elasticsearch.
98
71
* @return The response from Elasticsearch.
72
+ * @throws ForbiddenException
99
73
*/
100
- public static SearchResponse getEntityModels (NodeClient client ) {
101
- SearchRequestBuilder request = client .prepareSearch (INDEX );
74
+ public static SearchResponse getEntityModels (NodeClient client ) throws ForbiddenException {
75
+ SearchRequestBuilder request = client .prepareSearch (INDEX_NAME );
102
76
request .setSize (10000 );
103
77
try {
104
78
return request .get ();
105
79
} catch (IndexNotFoundException e ) {
106
- createIndex (client );
80
+ try {
81
+ SetupAction .createIndex (client );
82
+ } catch (ElasticsearchSecurityException se ) {
83
+ throw new ForbiddenException ("The .zentity-models index does not exist and you do not have the 'create_index' privilege. An authorized user must create the index by submitting: POST _zentity/_setup" );
84
+ }
107
85
return request .get ();
108
86
}
109
87
}
@@ -114,13 +92,18 @@ public static SearchResponse getEntityModels(NodeClient client) {
114
92
* @param entityType The entity type.
115
93
* @param client The client that will communicate with Elasticsearch.
116
94
* @return The response from Elasticsearch.
95
+ * @throws ForbiddenException
117
96
*/
118
- public static GetResponse getEntityModel (String entityType , NodeClient client ) {
119
- GetRequestBuilder request = client .prepareGet (INDEX , "doc" , entityType );
97
+ public static GetResponse getEntityModel (String entityType , NodeClient client ) throws ForbiddenException {
98
+ GetRequestBuilder request = client .prepareGet (INDEX_NAME , "doc" , entityType );
120
99
try {
121
100
return request .get ();
122
101
} catch (IndexNotFoundException e ) {
123
- createIndex (client );
102
+ try {
103
+ SetupAction .createIndex (client );
104
+ } catch (ElasticsearchSecurityException se ) {
105
+ throw new ForbiddenException ("The .zentity-models index does not exist and you do not have the 'create_index' privilege. An authorized user must create the index by submitting: POST _zentity/_setup" );
106
+ }
124
107
return request .get ();
125
108
}
126
109
}
@@ -132,10 +115,11 @@ public static GetResponse getEntityModel(String entityType, NodeClient client) {
132
115
* @param requestBody The request body.
133
116
* @param client The client that will communicate with Elasticsearch.
134
117
* @return The response from Elasticsearch.
118
+ * @throws ForbiddenException
135
119
*/
136
- public static IndexResponse indexEntityModel (String entityType , String requestBody , NodeClient client ) {
120
+ public static IndexResponse indexEntityModel (String entityType , String requestBody , NodeClient client ) throws ForbiddenException {
137
121
ensureIndex (client );
138
- IndexRequestBuilder request = client .prepareIndex (INDEX , "doc" , entityType );
122
+ IndexRequestBuilder request = client .prepareIndex (INDEX_NAME , "doc" , entityType );
139
123
request .setSource (requestBody , XContentType .JSON ).setCreate (true ).setRefreshPolicy ("wait_for" );
140
124
return request .get ();
141
125
}
@@ -147,10 +131,11 @@ public static IndexResponse indexEntityModel(String entityType, String requestBo
147
131
* @param requestBody The request body.
148
132
* @param client The client that will communicate with Elasticsearch.
149
133
* @return The response from Elasticsearch.
134
+ * @throws ForbiddenException
150
135
*/
151
- public static IndexResponse updateEntityModel (String entityType , String requestBody , NodeClient client ) {
136
+ public static IndexResponse updateEntityModel (String entityType , String requestBody , NodeClient client ) throws ForbiddenException {
152
137
ensureIndex (client );
153
- IndexRequestBuilder request = client .prepareIndex (INDEX , "doc" , entityType );
138
+ IndexRequestBuilder request = client .prepareIndex (INDEX_NAME , "doc" , entityType );
154
139
request .setSource (requestBody , XContentType .JSON ).setCreate (false ).setRefreshPolicy ("wait_for" );
155
140
return request .get ();
156
141
}
@@ -161,14 +146,19 @@ public static IndexResponse updateEntityModel(String entityType, String requestB
161
146
* @param entityType The entity type.
162
147
* @param client The client that will communicate with Elasticsearch.
163
148
* @return The response from Elasticsearch.
149
+ * @throws ForbiddenException
164
150
*/
165
- private static DeleteResponse deleteEntityModel (String entityType , NodeClient client ) {
166
- DeleteRequestBuilder request = client .prepareDelete (INDEX , "doc" , entityType );
151
+ private static DeleteResponse deleteEntityModel (String entityType , NodeClient client ) throws ForbiddenException {
152
+ DeleteRequestBuilder request = client .prepareDelete (INDEX_NAME , "doc" , entityType );
167
153
request .setRefreshPolicy ("wait_for" );
168
154
try {
169
155
return request .get ();
170
156
} catch (IndexNotFoundException e ) {
171
- createIndex (client );
157
+ try {
158
+ SetupAction .createIndex (client );
159
+ } catch (ElasticsearchSecurityException se ) {
160
+ throw new ForbiddenException ("The .zentity-models index does not exist and you do not have the 'create_index' privilege. An authorized user must create the index by submitting: POST _zentity/_setup" );
161
+ }
172
162
return request .get ();
173
163
}
174
164
}
@@ -257,6 +247,8 @@ protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient
257
247
258
248
} catch (ValidationException e ) {
259
249
channel .sendResponse (new BytesRestResponse (channel , RestStatus .BAD_REQUEST , e ));
250
+ } catch (ForbiddenException e ) {
251
+ channel .sendResponse (new BytesRestResponse (channel , RestStatus .FORBIDDEN , e ));
260
252
} catch (NotImplementedException e ) {
261
253
channel .sendResponse (new BytesRestResponse (channel , RestStatus .NOT_IMPLEMENTED , e ));
262
254
}
0 commit comments