2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.client.editor.rest.handling;
24 import java.util.TreeMap;
25 import java.util.concurrent.atomic.AtomicInteger;
26 import org.onap.policy.apex.model.modelapi.ApexApiResult;
27 import org.onap.policy.apex.model.modelapi.ApexApiResult.Result;
28 import org.slf4j.ext.XLogger;
29 import org.slf4j.ext.XLoggerFactory;
32 * This class carries out session handling for Apex REST editor sessions.
34 public class RestSessionHandler {
35 // Get a reference to the logger
36 private static final XLogger LOGGER = XLoggerFactory.getXLogger(RestSessionHandler.class);
38 // The next session will have this number, stating at 0
39 private AtomicInteger nextSessionId = new AtomicInteger();
41 // All REST editor sessions being handled by this handler
42 private final Map<Integer, RestSession> sessionMap = new TreeMap<>();
45 * Create a new session.
46 * @param result the result of session creation
47 * @return the new session object
49 public RestSession createSession(ApexApiResult result) {
50 LOGGER.entry("creating session");
52 // Create the session with the next session ID
53 final int newSessionId = nextSessionId.getAndIncrement();
54 sessionMap.put(newSessionId, new RestSession(newSessionId));
56 result.addMessage(Integer.toString(newSessionId));
58 LOGGER.exit("created session with ID: " + newSessionId);
59 return sessionMap.get(newSessionId);
63 * Get a session for the given session ID.
64 * @param sessionId the session ID of the session we require
65 * @param result the result of the session get
68 public RestSession getSession(final int sessionId, ApexApiResult result) {
69 LOGGER.entry("finding session: " + sessionId);
71 // Check for valid session IDs
73 result.setResult(Result.FAILED);
74 result.addMessage("Session ID \"" + sessionId + "\" is negative");
75 LOGGER.exit(result.getMessage());
79 // Check if session exits
80 if (!sessionMap.containsKey(sessionId)) {
81 result.setResult(Result.FAILED);
82 result.addMessage("A session with session ID \"" + sessionId + "\" does not exist");
83 LOGGER.exit(result.getMessage());
87 RestSession session = sessionMap.get(sessionId);
89 // Check if session is valid
90 if (session == null) {
91 result.setResult(Result.FAILED);
92 result.addMessage("The session with session ID \"" + sessionId + "\" is corrupt");
93 LOGGER.exit(result.getMessage());
98 LOGGER.exit("session found: " + sessionId);
103 * This is a test method to set a corrupt session ID in the session map
104 * @param corruptSessionId the ID of the corrupt session
106 protected void setCorruptSession(int corruptSessionId) {
107 sessionMap.put(corruptSessionId, null);