1d23410b899d8f7248439bebf2cfdee273755920
[sdc.git] /
1 /*
2  *  Copyright © 2016-2017 European Support Limited
3  *
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
7  *
8  *       http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17
18 package org.openecomp.sdc.common.session.impl;
19
20 import lombok.AllArgsConstructor;
21 import lombok.Getter;
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;
27
28 public class AsdcSessionContextProvider implements SessionContextProvider {
29
30     private static final ThreadLocal<String> threadUserId = new ThreadLocal<>();
31     private static final ThreadLocal<String> threadTenant = new ThreadLocal<>();
32
33     @Override
34     public void create(String userId, String tenant) {
35         threadUserId.set(userId);
36         threadTenant.set(tenant);
37     }
38
39     @Override
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());
44         }
45
46         if (threadTenant.get() == null) {
47             throw new CoreException(new ErrorCode.ErrorCodeBuilder().withMessage("Tenant was not set "
48                     + "for this thread").build());
49         }
50
51         return new AsdcSessionContext(new User(threadUserId.get()), threadTenant.get());
52     }
53
54     @Override
55     public void close() {
56         threadUserId.remove();
57         threadTenant.remove();
58     }
59
60     @Getter
61     @AllArgsConstructor
62     private static class AsdcSessionContext implements SessionContext {
63
64         private final User user;
65         private final String tenant;
66     }
67 }