2 * ============LICENSE_START=======================================================
3 * Copyright (C) 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;
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;
33 * This class carries out session handling for Apex REST editor sessions.
35 public class RestSessionHandler {
36 // Get a reference to the logger
37 private static final XLogger LOGGER = XLoggerFactory.getXLogger(RestSessionHandler.class);
39 // The next session will have this number, stating at 0
40 private AtomicInteger nextSessionId = new AtomicInteger();
42 // All REST editor sessions being handled by this handler
43 private final Map<Integer, RestSession> sessionMap = new TreeMap<>();
46 * Create a new session.
48 * @param result the result of session creation
49 * @return the new session object
51 public RestSession createSession(ApexApiResult result) {
52 LOGGER.entry("creating session");
54 // Create the session with the next session ID
55 final int newSessionId = nextSessionId.getAndIncrement();
56 sessionMap.put(newSessionId, new RestSession(newSessionId));
58 result.addMessage(Integer.toString(newSessionId));
60 LOGGER.exit("created session with ID: " + newSessionId);
61 return sessionMap.get(newSessionId);
65 * Get a session for the given session ID.
67 * @param sessionId the session ID of the session we require
68 * @param result the result of the session get
71 public RestSession getSession(final int sessionId, ApexApiResult result) {
72 LOGGER.entry("finding session: " + sessionId);
74 // Check for valid session IDs
76 result.setResult(Result.FAILED);
77 result.addMessage("Session ID \"" + sessionId + "\" is negative");
78 LOGGER.exit(result.getMessage());
82 // Check if session exits
83 if (!sessionMap.containsKey(sessionId)) {
84 result.setResult(Result.FAILED);
85 result.addMessage("A session with session ID \"" + sessionId + "\" does not exist");
86 LOGGER.exit(result.getMessage());
90 RestSession session = sessionMap.get(sessionId);
92 // Check if session is valid
93 if (session == null) {
94 result.setResult(Result.FAILED);
95 result.addMessage("The session with session ID \"" + sessionId + "\" is corrupt");
96 LOGGER.exit(result.getMessage());
100 // Return the session
101 LOGGER.exit("session found: " + sessionId);
106 * This is a test method to set a corrupt session ID in the session map
108 * @param corruptSessionId the ID of the corrupt session
110 protected void setCorruptSession(int corruptSessionId) {
111 sessionMap.put(corruptSessionId, null);