22
33import io .zentity .model .Model ;
44import io .zentity .model .ValidationException ;
5+ import org .elasticsearch .ElasticsearchSecurityException ;
56import org .elasticsearch .action .admin .indices .exists .indices .IndicesExistsRequestBuilder ;
67import org .elasticsearch .action .admin .indices .exists .indices .IndicesExistsResponse ;
78import org .elasticsearch .action .delete .DeleteRequestBuilder ;
3435
3536public class ModelsAction extends BaseRestHandler {
3637
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" ;
6139
6240 @ Inject
6341 public ModelsAction (Settings settings , RestController controller ) {
@@ -69,41 +47,41 @@ public ModelsAction(Settings settings, RestController controller) {
6947 controller .registerHandler (DELETE , "_zentity/models/{entity_type}" , this );
7048 }
7149
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-
8250 /**
8351 * Check if the .zentity-models index exists, and if it doesn't, then create it.
8452 *
8553 * @param client The client that will communicate with Elasticsearch.
54+ * @throws ForbiddenException
8655 */
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+ }
9265 }
9366
9467 /**
9568 * Retrieve all entity models.
9669 *
9770 * @param client The client that will communicate with Elasticsearch.
9871 * @return The response from Elasticsearch.
72+ * @throws ForbiddenException
9973 */
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 );
10276 request .setSize (10000 );
10377 try {
10478 return request .get ();
10579 } 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+ }
10785 return request .get ();
10886 }
10987 }
@@ -114,13 +92,18 @@ public static SearchResponse getEntityModels(NodeClient client) {
11492 * @param entityType The entity type.
11593 * @param client The client that will communicate with Elasticsearch.
11694 * @return The response from Elasticsearch.
95+ * @throws ForbiddenException
11796 */
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 );
12099 try {
121100 return request .get ();
122101 } 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+ }
124107 return request .get ();
125108 }
126109 }
@@ -132,10 +115,11 @@ public static GetResponse getEntityModel(String entityType, NodeClient client) {
132115 * @param requestBody The request body.
133116 * @param client The client that will communicate with Elasticsearch.
134117 * @return The response from Elasticsearch.
118+ * @throws ForbiddenException
135119 */
136- public static IndexResponse indexEntityModel (String entityType , String requestBody , NodeClient client ) {
120+ public static IndexResponse indexEntityModel (String entityType , String requestBody , NodeClient client ) throws ForbiddenException {
137121 ensureIndex (client );
138- IndexRequestBuilder request = client .prepareIndex (INDEX , "doc" , entityType );
122+ IndexRequestBuilder request = client .prepareIndex (INDEX_NAME , "doc" , entityType );
139123 request .setSource (requestBody , XContentType .JSON ).setCreate (true ).setRefreshPolicy ("wait_for" );
140124 return request .get ();
141125 }
@@ -147,10 +131,11 @@ public static IndexResponse indexEntityModel(String entityType, String requestBo
147131 * @param requestBody The request body.
148132 * @param client The client that will communicate with Elasticsearch.
149133 * @return The response from Elasticsearch.
134+ * @throws ForbiddenException
150135 */
151- public static IndexResponse updateEntityModel (String entityType , String requestBody , NodeClient client ) {
136+ public static IndexResponse updateEntityModel (String entityType , String requestBody , NodeClient client ) throws ForbiddenException {
152137 ensureIndex (client );
153- IndexRequestBuilder request = client .prepareIndex (INDEX , "doc" , entityType );
138+ IndexRequestBuilder request = client .prepareIndex (INDEX_NAME , "doc" , entityType );
154139 request .setSource (requestBody , XContentType .JSON ).setCreate (false ).setRefreshPolicy ("wait_for" );
155140 return request .get ();
156141 }
@@ -161,14 +146,19 @@ public static IndexResponse updateEntityModel(String entityType, String requestB
161146 * @param entityType The entity type.
162147 * @param client The client that will communicate with Elasticsearch.
163148 * @return The response from Elasticsearch.
149+ * @throws ForbiddenException
164150 */
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 );
167153 request .setRefreshPolicy ("wait_for" );
168154 try {
169155 return request .get ();
170156 } 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+ }
172162 return request .get ();
173163 }
174164 }
@@ -257,6 +247,8 @@ protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient
257247
258248 } catch (ValidationException e ) {
259249 channel .sendResponse (new BytesRestResponse (channel , RestStatus .BAD_REQUEST , e ));
250+ } catch (ForbiddenException e ) {
251+ channel .sendResponse (new BytesRestResponse (channel , RestStatus .FORBIDDEN , e ));
260252 } catch (NotImplementedException e ) {
261253 channel .sendResponse (new BytesRestResponse (channel , RestStatus .NOT_IMPLEMENTED , e ));
262254 }
0 commit comments