2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.gui.editors.apex.rest.handling;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import javax.ws.rs.Consumes;
27 import javax.ws.rs.DELETE;
28 import javax.ws.rs.GET;
29 import javax.ws.rs.POST;
30 import javax.ws.rs.PUT;
31 import javax.ws.rs.Path;
32 import javax.ws.rs.PathParam;
33 import javax.ws.rs.Produces;
34 import javax.ws.rs.QueryParam;
35 import javax.ws.rs.core.MediaType;
36 import org.glassfish.jersey.media.multipart.FormDataParam;
37 import org.onap.policy.apex.model.modelapi.ApexApiResult;
38 import org.onap.policy.apex.model.modelapi.ApexApiResult.Result;
39 import org.onap.policy.common.parameters.ParameterService;
40 import org.onap.policy.common.utils.coder.StandardCoder;
41 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
42 import org.onap.policy.common.utils.resources.TextFileUtils;
43 import org.onap.policy.gui.editors.apex.rest.UploadPluginConfigParameters;
44 import org.onap.policy.gui.editors.apex.rest.handling.converter.tosca.ApexConfigProcessor;
45 import org.onap.policy.gui.editors.apex.rest.handling.converter.tosca.PolicyToscaConverter;
46 import org.onap.policy.gui.editors.apex.rest.handling.converter.tosca.ToscaTemplateProcessor;
47 import org.onap.policy.gui.editors.apex.rest.handling.plugin.upload.UploadPluginClient;
48 import org.slf4j.ext.XLogger;
49 import org.slf4j.ext.XLoggerFactory;
52 * The class represents the root resource exposed at the base URL<br> The url to access this resource would be in the
53 * form {@code <baseURL>/rest/<session>/....} <br> For example: a PUT request to the following URL
54 * {@code http://localhost:8080/apex/rest/109/ContextSchema/Update}, with a JSON string payload containing the new
55 * {@code Schema} in the body, can be explained as: <ul> <li>The server or servlet is running at the base URL
56 * {@code http://localhost:8080/apex} <li>This resource {@code ApexRestEditorResource} is used because the path
57 * {@code rest/109} matches the {@code Path} filter specification for this Resource ({@code @Path("rest/{session}")}),
58 * where the {@code int} path parameter {@code session} is assigned the {@code int} value {@code 109} <li>The path
59 * {@code ContextSchema/Update} redirects this call to the method {@link #updateContextSchema(String)}, which should be
60 * a {@link javax.ws.rs.PUT}, with a single String in the body/payload which gets mapped to the single String parameter
61 * for the method. <li>So, in summary, the REST request updates a {@code ContextSchema} as specified in the payload for
62 * {@code session} number {@code 109} </ul>
64 * <b>Note:</b> An allocated {@code Session} identifier must be included in (almost) all requests. Models for different
65 * {@code Session} identifiers are completely isolated from one another.
67 * <b>Note:</b> To create a new {@code Session}, and have a new session ID allocated use {@link javax.ws.rs.GET} request
68 * to {@code <baseURL>/rest/-1/Session/Create} (for example: {@code http://localhost:8080/apex/rest/-1/Session/Create} )
71 @Path("editor/{session}")
72 @Produces({MediaType.APPLICATION_JSON})
73 @Consumes({MediaType.APPLICATION_JSON})
74 public class ApexEditorRestResource implements RestCommandHandler {
76 // Get a reference to the logger
77 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexEditorRestResource.class);
78 // Location of the periodi event template
80 private static final String PERIODIC_EVENT_TEMPLATE = "src/main/resources/templates/PeriodicEventTemplate.json";
81 // Recurring string constants
83 private static final String NAME = "name";
84 private static final String VERSION = "version";
85 private static final String REST_COMMAND_NOT_RECOGNISED = "REST command not recognised";
86 private static final String OK = ": OK";
87 private static final String NOT_OK = ": Not OK";
88 private static final String SESSION_CREATE = "Session/Create";
89 private static final String SESSION_CREATE_NOT_OK = "Session/Create: Not OK";
90 // The session handler for sessions on the Apex editor
92 private static final RestSessionHandler SESSION_HANDLER = new RestSessionHandler();
93 // Handlers for the various parts of an Apex model
96 private static final ModelHandler MODEL_HANDLER = new ModelHandler();
97 private static final KeyInfoHandler KEY_INFO_HANDLER = new KeyInfoHandler();
98 private static final ContextSchemaHandler CONTEXT_SCHEMA_HANDLER = new ContextSchemaHandler();
99 private static final ContextAlbumHandler CONTEXT_ALBUM_HANDLER = new ContextAlbumHandler();
100 private static final EventHandler EVENT_HANDLER = new EventHandler();
101 private static final TaskHandler TASK_HANDLER = new TaskHandler();
102 private static final PolicyHandler POLICY_HANDLER = new PolicyHandler();
104 private final PolicyUploadHandler policyUploadHandler;
106 // The ID of this session. This gets injected from the URL.
108 @PathParam("session")
109 private int sessionId = -1;
112 * Creates the ApexEditorRestResource instance.
114 public ApexEditorRestResource() {
115 final StandardCoder standardCoder = new StandardCoder();
116 policyUploadHandler = new PolicyUploadHandler(
117 new UploadPluginClient(), new PolicyToscaConverter(standardCoder, new YamlJsonTranslator()),
118 new ToscaTemplateProcessor(standardCoder), new ApexConfigProcessor(standardCoder),
119 ParameterService.get(UploadPluginConfigParameters.GROUP_NAME)
124 * Creates a new session. Always call this method with sessionID -1, whereby a new sessionID will be allocated. If
125 * successful the new sessionID will be available in the first message in the result.
127 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
128 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}. This includes the session id
132 @Path("Session/Create")
133 public ApexApiResult createSession() {
134 if (sessionId != -1) {
135 return new ApexApiResult(Result.FAILED, "Session ID must be set to -1 to create sessions: " + sessionId);
138 ApexApiResult result = new ApexApiResult();
139 SESSION_HANDLER.createSession(result);
144 * Load the model from a JSON string for this session.
146 * @param jsonString the JSON string to be parsed. The returned value(s) will be similar to {@code AxPolicyModel},
147 * with merged {@code AxKeyInfo} for the root object.
148 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
149 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
153 public ApexApiResult loadFromString(final String jsonString) {
154 return processRestCommand(RestCommandType.MODEL, RestCommand.LOAD, jsonString);
158 * Analyse the model and return analysis results. If successful the analysis results will be available in the
159 * messages in the result.
161 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
162 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
165 @Path("Model/Analyse")
166 public ApexApiResult analyse() {
167 return processRestCommand(RestCommandType.MODEL, RestCommand.ANALYSE);
171 * Validate the model and return validation results. If successful the validation results will be available in the
172 * messages in the result.
174 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
175 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
178 @Path("Model/Validate")
179 public ApexApiResult validate() {
180 return processRestCommand(RestCommandType.MODEL, RestCommand.VALIDATE);
184 * Creates the new model model for this session.
186 * @param jsonString the JSON string to be parsed containing the new model. See {@code BeanModel}
187 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
188 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
191 @Path("Model/Create")
192 public ApexApiResult createModel(final String jsonString) {
193 return processRestCommand(RestCommandType.MODEL, RestCommand.CREATE, jsonString);
197 * Update the model for this session.
199 * @param jsonString the JSON string to be parsed containing the updated model. See {@code BeanModel}
200 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
201 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
204 @Path("Model/Update")
205 public ApexApiResult updateModel(final String jsonString) {
206 return processRestCommand(RestCommandType.MODEL, RestCommand.UPDATE, jsonString);
210 * Gets the key for the model for this session. If successful the model key will be available in the first message
211 * in the result. See {@code AxKey}
213 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
214 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
217 @Path("Model/GetKey")
218 public ApexApiResult getModelKey() {
219 return processRestCommand(RestCommandType.MODEL, RestCommand.GET_KEY);
223 * Retrieve the model for this session. If successful the model will be available in the first message in the
224 * result. The returned value will be similar to a {@code AxPolicyModel}, with merged {@code AxKeyInfo} for the root
227 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
228 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
232 public ApexApiResult listModel() {
233 return processRestCommand(RestCommandType.MODEL, RestCommand.LIST);
237 * Download the model for this session as a String.
239 * @return the model represented as a JSON string. See {@code AxPolicyModel}
242 @Path("Model/Download")
243 public String downloadModel() {
244 ApexApiResult result = processRestCommand(RestCommandType.MODEL, RestCommand.DOWNLOAD);
245 if (result != null && result.isOk()) {
246 return result.getMessage();
253 * Uploads a Policy Model to a configured endpoint converting it to tosca based on the given apex config and tosca
256 * @param toscaTemplateFileStream the tosca template file input stream
257 * @param apexConfigFileStream the apex config file input stream
258 * @return an ApexAPIResult that contains the operation status and success/error messages
261 @Path("Model/Upload")
262 @Consumes({MediaType.MULTIPART_FORM_DATA})
263 public ApexApiResult uploadModel(@FormDataParam("tosca-template-file") InputStream toscaTemplateFileStream,
264 @FormDataParam("apex-config-file") InputStream apexConfigFileStream,
265 @FormDataParam("userId") String userId) {
266 final ApexApiResult result = new ApexApiResult();
267 final RestSession session = SESSION_HANDLER.getSession(sessionId, result);
268 if (session == null) {
271 return policyUploadHandler.doUpload(session.getApexModel(), toscaTemplateFileStream,
272 apexConfigFileStream, userId);
276 * Delete the model for this session.
278 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
279 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
282 @Path("Model/Delete")
283 public ApexApiResult deleteModel() {
284 return processRestCommand(RestCommandType.MODEL, RestCommand.DELETE);
288 * List key information with the given key names/versions. If successful the result(s) will be available in the
289 * result messages. See {@code AxKeyInfo}
291 * @param name the name to search for. If null or empty, then all names will be queried
292 * @param version the version to search for. If null then all versions will be searched for.
293 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
294 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
297 @Path("KeyInformation/Get")
298 public ApexApiResult listKeyInformation(@QueryParam(NAME) final String name,
299 @QueryParam(VERSION) final String version) {
300 return processRestCommand(RestCommandType.KEY_INFO, RestCommand.LIST, name, version);
304 * Creates a context schema with the information in the JSON string passed.
306 * @param jsonString the JSON string to be parsed. See {@code BeanContextSchema}
307 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
308 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
311 @Path("ContextSchema/Create")
312 public ApexApiResult createContextSchema(final String jsonString) {
313 return processRestCommand(RestCommandType.CONTEXT_SCHEMA, RestCommand.CREATE, jsonString);
317 * Update a context schema with the information in the JSON string passed.
319 * @param jsonString the JSON string to be parsed. See {@code BeanContextSchema}
320 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
321 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
324 @Path("ContextSchema/Update")
325 public ApexApiResult updateContextSchema(final String jsonString) {
326 return processRestCommand(RestCommandType.CONTEXT_SCHEMA, RestCommand.UPDATE, jsonString);
330 * List context schemas with the given key names/versions. If successful the result(s) will be available in the
331 * result messages. The returned value(s) will be similar to {@code AxContextSchema}, with merged {@code AxKeyInfo}
332 * for the root object.
334 * @param name the name to search for. If null or empty, then all names will be queried
335 * @param version the version to search for. If null then all versions will be searched for.
336 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
337 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
340 @Path("ContextSchema/Get")
341 public ApexApiResult listContextSchemas(@QueryParam(NAME) final String name,
342 @QueryParam(VERSION) final String version) {
343 return processRestCommand(RestCommandType.CONTEXT_SCHEMA, RestCommand.LIST, name, version);
347 * Delete context schemas with the given key names/versions.
349 * @param name the name to search for. If null or empty, then all names will be queried
350 * @param version the version to search for. If null then all versions will be searched for.
351 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
352 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
355 @Path("ContextSchema/Delete")
356 public ApexApiResult deleteContextSchema(@QueryParam(NAME) final String name,
357 @QueryParam(VERSION) final String version) {
358 return processRestCommand(RestCommandType.CONTEXT_SCHEMA, RestCommand.DELETE, name, version);
362 * Validate context schemas with the given key names/versions. The result(s) will be available in the result
365 * @param name the name to search for. If null or empty, then all names will be queried
366 * @param version the version to search for. If null then all versions will be searched for.
367 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
368 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
371 @Path("Validate/ContextSchema")
372 public ApexApiResult validateContextSchemas(@QueryParam(NAME) final String name,
373 @QueryParam(VERSION) final String version) {
374 return processRestCommand(RestCommandType.CONTEXT_SCHEMA, RestCommand.VALIDATE, name, version);
378 * Creates a context album with the information in the JSON string passed.
380 * @param jsonString the JSON string to be parsed. See {@code BeanContextAlbum}
381 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
382 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
385 @Path("ContextAlbum/Create")
386 public ApexApiResult createContextAlbum(final String jsonString) {
387 return processRestCommand(RestCommandType.CONTEXT_ALBUM, RestCommand.CREATE, jsonString);
391 * Update a context album with the information in the JSON string passed.
393 * @param jsonString the JSON string to be parsed. See {@code BeanContextAlbum}
394 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
395 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
398 @Path("ContextAlbum/Update")
399 public ApexApiResult updateContextAlbum(final String jsonString) {
400 return processRestCommand(RestCommandType.CONTEXT_ALBUM, RestCommand.UPDATE, jsonString);
404 * List context albums with the given key names/versions. If successful the result(s) will be available in the
405 * result messages. The returned value(s) will be similar to {@code AxContextAlbum}, with merged {@code AxKeyInfo}
406 * for the root object.
408 * @param name the name to search for. If null or empty, then all names will be queried
409 * @param version the version to search for. If null then all versions will be searched for.
410 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
411 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
414 @Path("ContextAlbum/Get")
415 public ApexApiResult listContextAlbums(@QueryParam(NAME) final String name,
416 @QueryParam(VERSION) final String version) {
417 return processRestCommand(RestCommandType.CONTEXT_ALBUM, RestCommand.LIST, name, version);
421 * Delete context albums with the given key names/versions.
423 * @param name the name to search for. If null or empty, then all names will be queried
424 * @param version the version to search for. If null then all versions will be searched for.
425 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
426 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
429 @Path("ContextAlbum/Delete")
430 public ApexApiResult deleteContextAlbum(@QueryParam(NAME) final String name,
431 @QueryParam(VERSION) final String version) {
432 return processRestCommand(RestCommandType.CONTEXT_ALBUM, RestCommand.DELETE, name, version);
436 * Validate context albums with the given key names/versions. The result(s) will be available in the result
439 * @param name the name to search for. If null or empty, then all names will be queried
440 * @param version the version to search for. If null then all versions will be searched for.
441 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
442 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
445 @Path("Validate/ContextAlbum")
446 public ApexApiResult validateContextAlbums(@QueryParam(NAME) final String name,
447 @QueryParam(VERSION) final String version) {
448 return processRestCommand(RestCommandType.CONTEXT_ALBUM, RestCommand.VALIDATE, name, version);
452 * Creates an event with the information in the JSON string passed.
454 * @param jsonString the JSON string to be parsed. See {@code BeanEvent}
455 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
456 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
459 @Path("Event/Create")
460 public ApexApiResult createEvent(final String jsonString) {
461 return processRestCommand(RestCommandType.EVENT, RestCommand.CREATE, jsonString);
465 * Update an event with the information in the JSON string passed.
467 * @param jsonString the JSON string to be parsed. See {@code BeanEvent}
468 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
469 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
472 @Path("Event/Update")
473 public ApexApiResult updateEvent(final String jsonString) {
474 return processRestCommand(RestCommandType.EVENT, RestCommand.UPDATE, jsonString);
478 * List events with the given key names/versions. If successful the result(s) will be available in the result
479 * messages. The returned value(s) will be similar to {@code AxEvent}, with merged {@code AxKeyInfo} for the root
482 * @param name the name to search for. If null or empty, then all names will be queried
483 * @param version the version to search for. If null then all versions will be searched for.
484 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
485 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
489 public ApexApiResult listEvent(@QueryParam(NAME) final String name, @QueryParam(VERSION) final String version) {
490 return processRestCommand(RestCommandType.EVENT, RestCommand.LIST, name, version);
494 * Delete events with the given key names/versions.
496 * @param name the name to search for. If null or empty, then all names will be queried
497 * @param version the version to search for. If null then all versions will be searched for.
498 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
499 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
502 @Path("Event/Delete")
503 public ApexApiResult deleteEvent(@QueryParam(NAME) final String name, @QueryParam(VERSION) final String version) {
504 return processRestCommand(RestCommandType.EVENT, RestCommand.DELETE, name, version);
508 * Validate events with the given key names/versions. The result(s) will be available in the result messages.
510 * @param name the name to search for. If null or empty, then all names will be queried
511 * @param version the version to search for. If null then all versions will be searched for.
512 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
513 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
516 @Path("Validate/Event")
517 public ApexApiResult validateEvent(@QueryParam(NAME) final String name, @QueryParam(VERSION) final String version) {
518 return processRestCommand(RestCommandType.EVENT, RestCommand.VALIDATE, name, version);
522 * Creates a task with the information in the JSON string passed.
524 * @param jsonString the JSON string to be parsed. See {@code BeanTask}
525 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
526 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
530 public ApexApiResult createTask(final String jsonString) {
531 return processRestCommand(RestCommandType.TASK, RestCommand.CREATE, jsonString);
535 * Update a task with the information in the JSON string passed.
537 * @param jsonString the JSON string to be parsed. See {@code BeanTask}
538 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
539 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
543 public ApexApiResult updateTask(final String jsonString) {
544 return processRestCommand(RestCommandType.TASK, RestCommand.UPDATE, jsonString);
548 * List tasks with the given key names/versions. If successful the result(s) will be available in the result
549 * messages. The returned value(s) will be similar to {@code AxTask}, with merged {@code AxKeyInfo} for the root
552 * @param name the name to search for. If null or empty, then all names will be queried
553 * @param version the version to search for. If null then all versions will be searched for.
554 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
555 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
559 public ApexApiResult listTask(@QueryParam(NAME) final String name, @QueryParam(VERSION) final String version) {
560 return processRestCommand(RestCommandType.TASK, RestCommand.LIST, name, version);
564 * Delete tasks with the given key names/versions.
566 * @param name the name to search for. If null or empty, then all names will be queried
567 * @param version the version to search for. If null then all versions will be searched for.
568 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
569 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
573 public ApexApiResult deleteTask(@QueryParam(NAME) final String name, @QueryParam(VERSION) final String version) {
574 return processRestCommand(RestCommandType.TASK, RestCommand.DELETE, name, version);
578 * Validate tasks with the given key names/versions. The result(s) will be available in the result messages.
580 * @param name the name to search for. If null or empty, then all names will be queried
581 * @param version the version to search for. If null then all versions will be searched for.
582 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
583 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
586 @Path("Validate/Task")
587 public ApexApiResult validateTask(@QueryParam(NAME) final String name, @QueryParam(VERSION) final String version) {
588 return processRestCommand(RestCommandType.TASK, RestCommand.VALIDATE, name, version);
592 * Creates a policy with the information in the JSON string passed.
594 * @param jsonString the JSON string to be parsed See {@code BeanPolicy}
595 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
596 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
599 @Path("Policy/Create")
600 public ApexApiResult createPolicy(final String jsonString) {
601 return processRestCommand(RestCommandType.POLICY, RestCommand.CREATE, jsonString);
605 * Update a policy with the information in the JSON string passed.
607 * @param firstStatePeriodic indicates if periodic event should be created and added to model
608 * @param jsonString the JSON string to be parsed. See {@code BeanPolicy}
609 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
610 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
613 @Path("Policy/Update")
614 public ApexApiResult updatePolicy(@QueryParam("firstStatePeriodic") final boolean firstStatePeriodic,
615 final String jsonString) {
617 ApexApiResult result = processRestCommand(RestCommandType.POLICY, RestCommand.UPDATE, jsonString);
618 if (result != null && result.isOk() && firstStatePeriodic) {
619 result = createPeriodicEvent();
625 * List policies with the given key names/versions. If successful the result(s) will be available in the result
626 * messages. The returned value(s) will be similar to {@code AxPolicy}, with merged {@code AxKeyInfo} for the root
629 * @param name the name to search for. If null or empty, then all names will be queried
630 * @param version the version to search for. If null then all versions will be searched for.
631 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
632 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
636 public ApexApiResult listPolicy(@QueryParam(NAME) final String name, @QueryParam(VERSION) final String version) {
637 return processRestCommand(RestCommandType.POLICY, RestCommand.LIST, name, version);
641 * Delete policies with the given key names/versions.
643 * @param name the name to search for. If null or empty, then all names will be queried
644 * @param version the version to search for. If null then all versions will be searched for.
645 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
646 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
649 @Path("Policy/Delete")
650 public ApexApiResult deletePolicy(@QueryParam(NAME) final String name, @QueryParam(VERSION) final String version) {
651 return processRestCommand(RestCommandType.POLICY, RestCommand.DELETE, name, version);
655 * This method routes REST commands that take no parameters to their caller.
657 * @param commandType the type of REST command to process
658 * @param command the REST command to process
659 * @return the result of the REST command
661 private ApexApiResult processRestCommand(final RestCommandType commandType, final RestCommand command) {
662 LOGGER.entry(commandType);
664 ApexApiResult result = new ApexApiResult();
665 RestSession session = SESSION_HANDLER.getSession(sessionId, result);
666 if (session == null) {
669 result = executeRestCommand(session, commandType, command);
670 LOGGER.exit(SESSION_CREATE + (result != null && result.isOk() ? OK : NOT_OK));
672 } catch (final Exception e) {
674 LOGGER.exit(SESSION_CREATE_NOT_OK);
680 * This method routes REST commands that take a JSON string to their caller.
682 * @param commandType the type of REST command to process
683 * @param command the REST command to process
684 * @param jsonString the JSON string received in the REST request
685 * @return the result of the REST command
687 private ApexApiResult processRestCommand(final RestCommandType commandType, final RestCommand command,
688 final String jsonString) {
689 LOGGER.entry(commandType, jsonString);
691 ApexApiResult result = new ApexApiResult();
692 RestSession session = SESSION_HANDLER.getSession(sessionId, result);
693 if (session == null) {
696 result = executeRestCommand(session, commandType, command, jsonString);
697 LOGGER.exit(SESSION_CREATE + (result != null && result.isOk() ? OK : NOT_OK));
699 } catch (final Exception e) {
701 LOGGER.exit(SESSION_CREATE_NOT_OK);
707 * This method routes REST commands that take a name and version to their caller.
709 * @param commandType the type of REST command to process
710 * @param command the REST command to process
711 * @param name the name received in the REST request
712 * @param version the name received in the REST request
713 * @return the result of the REST command
715 private ApexApiResult processRestCommand(final RestCommandType commandType, final RestCommand command,
716 final String name, final String version) {
717 LOGGER.entry(commandType, name, version);
719 ApexApiResult result = new ApexApiResult();
720 RestSession session = SESSION_HANDLER.getSession(sessionId, result);
721 if (session == null) {
724 result = executeRestCommand(session, commandType, command, name, version);
725 LOGGER.exit(SESSION_CREATE + (result != null && result.isOk() ? OK : NOT_OK));
727 } catch (final Exception e) {
729 LOGGER.exit(SESSION_CREATE_NOT_OK);
735 * This method invokes callers to run REST commands that take no parameters.
737 * @param session the Apex editor session
738 * @param commandType the type of REST command to process
739 * @param command the REST command to process
740 * @return the result of the REST command
743 public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType,
744 final RestCommand command) {
745 switch (commandType) {
747 return MODEL_HANDLER.executeRestCommand(session, commandType, command);
749 return KEY_INFO_HANDLER.executeRestCommand(session, commandType, command);
751 return CONTEXT_SCHEMA_HANDLER.executeRestCommand(session, commandType, command);
753 return CONTEXT_ALBUM_HANDLER.executeRestCommand(session, commandType, command);
755 return EVENT_HANDLER.executeRestCommand(session, commandType, command);
757 return TASK_HANDLER.executeRestCommand(session, commandType, command);
759 return POLICY_HANDLER.executeRestCommand(session, commandType, command);
761 return new ApexApiResult(Result.FAILED, REST_COMMAND_NOT_RECOGNISED);
766 * This method invokes callers to run REST commands that take a JSON string.
768 * @param session the Apex editor session
769 * @param commandType the type of REST command to process
770 * @param command the REST command to process
771 * @param jsonString the JSON string received in the REST request
772 * @return the result of the REST command
775 public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType,
776 final RestCommand command, final String jsonString) {
777 switch (commandType) {
779 return MODEL_HANDLER.executeRestCommand(session, commandType, command, jsonString);
781 return KEY_INFO_HANDLER.executeRestCommand(session, commandType, command, jsonString);
783 return CONTEXT_SCHEMA_HANDLER.executeRestCommand(session, commandType, command, jsonString);
785 return CONTEXT_ALBUM_HANDLER.executeRestCommand(session, commandType, command, jsonString);
787 return EVENT_HANDLER.executeRestCommand(session, commandType, command, jsonString);
789 return TASK_HANDLER.executeRestCommand(session, commandType, command, jsonString);
791 return POLICY_HANDLER.executeRestCommand(session, commandType, command, jsonString);
793 return new ApexApiResult(Result.FAILED, REST_COMMAND_NOT_RECOGNISED);
798 * This method invokes callers to run REST commands that take a name and version.
800 * @param session the Apex editor session
801 * @param commandType the type of REST command to process
802 * @param command the REST command to process
803 * @param name the name received in the REST request
804 * @param version the name received in the REST request
805 * @return the result of the REST command
808 public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType,
809 final RestCommand command, final String name, final String version) {
810 switch (commandType) {
812 return MODEL_HANDLER.executeRestCommand(session, commandType, command, name, version);
814 return KEY_INFO_HANDLER.executeRestCommand(session, commandType, command, name, version);
816 return CONTEXT_SCHEMA_HANDLER.executeRestCommand(session, commandType, command, name, version);
818 return CONTEXT_ALBUM_HANDLER.executeRestCommand(session, commandType, command, name, version);
820 return EVENT_HANDLER.executeRestCommand(session, commandType, command, name, version);
822 return TASK_HANDLER.executeRestCommand(session, commandType, command, name, version);
824 return POLICY_HANDLER.executeRestCommand(session, commandType, command, name, version);
826 return new ApexApiResult(Result.FAILED, REST_COMMAND_NOT_RECOGNISED);
831 * Create a periodic event from the periodic event template.
833 private ApexApiResult createPeriodicEvent() {
834 String periodicEventJsonString;
836 periodicEventJsonString = TextFileUtils.getTextFileAsString(PERIODIC_EVENT_TEMPLATE);
837 } catch (IOException ioException) {
838 String message = "read of periodic event tempalte from " + PERIODIC_EVENT_TEMPLATE + "failed: "
839 + ioException.getMessage();
840 LOGGER.debug(message, ioException);
841 return new ApexApiResult(Result.FAILED, message);
844 return processRestCommand(RestCommandType.EVENT, RestCommand.CREATE, periodicEventJsonString);
848 * This method is used only for testing and is used to cause an exception on calls from unit test to test exception
851 protected static int createCorruptSession() {
852 final ApexEditorRestResource apexEditorRestResource = new ApexEditorRestResource();
853 final ApexApiResult result = apexEditorRestResource.createSession();
854 final int corruptSessionId = Integer.parseInt(result.getMessages().get(0));
856 SESSION_HANDLER.setCorruptSession(corruptSessionId);
858 return corruptSessionId;