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;
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.
47 * @param result the result of session creation
48 * @return the new session object
50 public RestSession createSession(ApexApiResult result) {
51 LOGGER.entry("creating session");
53 // Create the session with the next session ID
54 final int newSessionId = nextSessionId.getAndIncrement();
55 sessionMap.put(newSessionId, new RestSession(newSessionId));
57 result.addMessage(Integer.toString(newSessionId));
59 LOGGER.exit("created session with ID: " + newSessionId);
60 return sessionMap.get(newSessionId);
64 * Get a session for the given session ID.
65 * @param sessionId the session ID of the session we require
66 * @param result the result of the session get
69 public RestSession getSession(final int sessionId, ApexApiResult result) {
70 LOGGER.entry("finding session: " + sessionId);
72 // Check for valid session IDs
74 result.setResult(Result.FAILED);
75 result.addMessage("Session ID \"" + sessionId + "\" is negative");
76 LOGGER.exit(result.getMessage());
80 // Check if session exits
81 if (!sessionMap.containsKey(sessionId)) {
82 result.setResult(Result.FAILED);
83 result.addMessage("A session with session ID \"" + sessionId + "\" does not exist");
84 LOGGER.exit(result.getMessage());
88 RestSession session = sessionMap.get(sessionId);
90 // Check if session is valid
91 if (session == null) {
92 result.setResult(Result.FAILED);
93 result.addMessage("The session with session ID \"" + sessionId + "\" is corrupt");
94 LOGGER.exit(result.getMessage());
99 LOGGER.exit("session found: " + sessionId);
104 * This is a test method to set a corrupt session ID in the session map
105 * @param corruptSessionId the ID of the corrupt session
107 protected void setCorruptSession(int corruptSessionId) {
108 sessionMap.put(corruptSessionId, null);