-
Notifications
You must be signed in to change notification settings - Fork 477
/
CredentialsServiceImpl.java
98 lines (86 loc) · 4.27 KB
/
CredentialsServiceImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/**
* Copyright 2014-2016 CyberVision, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.kaaproject.kaa.server.common.dao.service;
import java.text.MessageFormat;
import java.util.Optional;
import org.apache.commons.lang3.Validate;
import org.kaaproject.kaa.common.dto.credentials.CredentialsDto;
import org.kaaproject.kaa.common.dto.credentials.CredentialsStatus;
import org.kaaproject.kaa.server.common.dao.CredentialsService;
import org.kaaproject.kaa.server.common.dao.exception.CredentialsServiceException;
import org.kaaproject.kaa.server.common.dao.impl.CredentialsDao;
import org.kaaproject.kaa.server.common.dao.model.Credentials;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;
/**
* The default implementation of the {@link CredentialsService} interface.
*
* @author Bohdan Khablenko
*
* @since v0.9.0
*/
@Transactional
public class CredentialsServiceImpl implements CredentialsService {
private static final Logger LOG = LoggerFactory.getLogger(EndpointRegistrationServiceImpl.class);
private final CredentialsDao<Credentials> credentialsDao;
public CredentialsServiceImpl(CredentialsDao<Credentials> credentialsDao) {
this.credentialsDao = credentialsDao;
}
@Override
public CredentialsDto provideCredentials(String applicationId, CredentialsDto credentials) throws CredentialsServiceException {
Validate.notBlank(applicationId, "Invalid application ID provided!");
Validate.notNull(credentials, "Invalid credentials provided!");
try {
return this.credentialsDao.save(applicationId, credentials).toDto();
} catch (Exception cause) {
String message = MessageFormat.format("[{0}] An unexpected exception occured while saving credentials!", applicationId);
LOG.error(message, cause);
throw new CredentialsServiceException(cause);
}
}
@Override
public Optional<CredentialsDto> lookupCredentials(String applicationId, String credentialsId) throws CredentialsServiceException {
Validate.notBlank(applicationId, "Invalid application ID provided!");
Validate.notBlank(credentialsId, "Invalid credentials ID provided!");
try {
return this.credentialsDao.find(applicationId, credentialsId).map(Credentials::toDto);
} catch (Exception cause) {
String message = MessageFormat.format("[{0}] An unexpected exception occured while searching for credentials [{1}]", applicationId, credentialsId);
LOG.error(message, cause);
throw new CredentialsServiceException(cause);
}
}
@Override
public void markCredentialsInUse(String applicationId, String credentialsId) throws CredentialsServiceException {
this.updateStatus(applicationId, credentialsId, CredentialsStatus.IN_USE);
}
@Override
public void markCredentialsRevoked(String applicationId, String credentialsId) throws CredentialsServiceException {
this.updateStatus(applicationId, credentialsId, CredentialsStatus.REVOKED);
}
private void updateStatus(String applicationId, String credentialsId, CredentialsStatus status) throws CredentialsServiceException {
Validate.notBlank(applicationId, "Invalid application ID provided!");
Validate.notBlank(credentialsId, "Invalid credentials ID provided!");
try {
this.credentialsDao.updateStatus(applicationId, credentialsId, status);
} catch (Exception cause) {
String message = MessageFormat.format("[{0}] An unexpected exception occured while updating credentials [{1}]", applicationId, credentialsId);
LOG.error(message, cause);
throw new CredentialsServiceException(cause);
}
}
}