2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020-2022 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;
25 import java.util.TreeMap;
26 import java.util.concurrent.atomic.AtomicInteger;
27 import org.onap.policy.apex.model.modelapi.ApexApiResult;
28 import org.onap.policy.apex.model.modelapi.ApexApiResult.Result;
29 import org.slf4j.ext.XLogger;
30 import org.slf4j.ext.XLoggerFactory;
31 import org.springframework.stereotype.Service;
34 * This class carries out session handling for Apex REST editor sessions.
37 public class RestSessionHandler {
38 // Get a reference to the logger
39 private static final XLogger LOGGER = XLoggerFactory.getXLogger(RestSessionHandler.class);
41 // The next session will have this number, stating at 0
42 private AtomicInteger nextSessionId = new AtomicInteger();
44 // All REST editor sessions being handled by this handler
45 private final Map<Integer, RestSession> sessionMap = new TreeMap<>();
48 * Create a new session.
50 * @param result the result of session creation
51 * @return the new session object
53 public RestSession createSession(ApexApiResult result) {
54 LOGGER.entry("creating session");
56 // Create the session with the next session ID
57 final int newSessionId = nextSessionId.getAndIncrement();
58 sessionMap.put(newSessionId, new RestSession(newSessionId));
60 result.addMessage(Integer.toString(newSessionId));
62 LOGGER.exit("created session with ID: " + newSessionId);
63 return sessionMap.get(newSessionId);
67 * Get a session for the given session ID.
69 * @param sessionId the session ID of the session we require
70 * @param result the result of the session get
73 public RestSession getSession(final int sessionId, ApexApiResult result) {
74 LOGGER.entry("finding session: " + sessionId);
76 // Check for valid session IDs
78 result.setResult(Result.FAILED);
79 result.addMessage("Session ID \"" + sessionId + "\" is negative");
80 LOGGER.exit(result.getMessage());
84 // Check if session exits
85 if (!sessionMap.containsKey(sessionId)) {
86 result.setResult(Result.FAILED);
87 result.addMessage("A session with session ID \"" + sessionId + "\" does not exist");
88 LOGGER.exit(result.getMessage());
92 RestSession session = sessionMap.get(sessionId);
94 // Check if session is valid
95 if (session == null) {
96 result.setResult(Result.FAILED);
97 result.addMessage("The session with session ID \"" + sessionId + "\" is corrupt");
98 LOGGER.exit(result.getMessage());
102 // Return the session
103 LOGGER.exit("session found: " + sessionId);
108 * This is a test method to set a corrupt session ID in the session map
110 * @param corruptSessionId the ID of the corrupt session
112 protected void setCorruptSession(int corruptSessionId) {
113 sessionMap.put(corruptSessionId, null);