2 * Copyright © 2016-2017 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 package org.openecomp.sdc.common.session.impl;
20 import lombok.AllArgsConstructor;
22 import org.openecomp.sdc.common.errors.CoreException;
23 import org.openecomp.sdc.common.errors.ErrorCode;
24 import org.openecomp.sdc.common.session.SessionContext;
25 import org.openecomp.sdc.common.session.SessionContextProvider;
26 import org.openecomp.sdc.common.session.User;
28 public class AsdcSessionContextProvider implements SessionContextProvider {
30 private static final ThreadLocal<String> threadUserId = new ThreadLocal<>();
31 private static final ThreadLocal<String> threadTenant = new ThreadLocal<>();
34 public void create(String userId, String tenant) {
35 threadUserId.set(userId);
36 threadTenant.set(tenant);
40 public SessionContext get() {
41 if (threadUserId.get() == null) {
42 throw new CoreException(new ErrorCode.ErrorCodeBuilder().withMessage("UserId was not set "
43 + "for this thread").build());
46 if (threadTenant.get() == null) {
47 throw new CoreException(new ErrorCode.ErrorCodeBuilder().withMessage("Tenant was not set "
48 + "for this thread").build());
51 return new AsdcSessionContext(new User(threadUserId.get()), threadTenant.get());
56 threadUserId.remove();
57 threadTenant.remove();
62 private static class AsdcSessionContext implements SessionContext {
64 private final User user;
65 private final String tenant;