diff --git a/server/common/dao/pom.xml b/server/common/dao/pom.xml index aa03992279..59c86e58f0 100644 --- a/server/common/dao/pom.xml +++ b/server/common/dao/pom.xml @@ -47,6 +47,10 @@ commons-io commons-io + + org.apache.commons + commons-lang3 + org.slf4j slf4j-api diff --git a/server/node/src/main/java/org/kaaproject/kaa/server/node/service/credentials/InternalCredentialsService.java b/server/common/dao/src/main/java/org/kaaproject/kaa/server/common/dao/CredentialsService.java similarity index 73% rename from server/node/src/main/java/org/kaaproject/kaa/server/node/service/credentials/InternalCredentialsService.java rename to server/common/dao/src/main/java/org/kaaproject/kaa/server/common/dao/CredentialsService.java index 14896a2c3d..52e8beaba6 100644 --- a/server/node/src/main/java/org/kaaproject/kaa/server/node/service/credentials/InternalCredentialsService.java +++ b/server/common/dao/src/main/java/org/kaaproject/kaa/server/common/dao/CredentialsService.java @@ -14,21 +14,21 @@ * limitations under the License. */ -package org.kaaproject.kaa.server.node.service.credentials; +package org.kaaproject.kaa.server.common.dao; import java.util.Optional; import org.kaaproject.kaa.common.dto.credentials.CredentialsDto; import org.kaaproject.kaa.common.dto.credentials.CredentialsStatus; +import org.kaaproject.kaa.server.common.dao.exception.CredentialsServiceException; /** - * The interface {@link InternalCredentialsService} is an internal sibling of - * {@link CredentialsService}. + * A service to manage security credentials. * * In general, each application has its own independent credentials service used * as a bridge to some external system. Since Kaa acts as such a system by * default, a single credentials service is enough to be used across all - * applications. Its methods require an additonal parameter, though, namely an + * applications. Its methods require an additonal parameter, though, namely the * application ID. * * @author Andrew Shvayka @@ -36,42 +36,39 @@ * * @since v0.9.0 */ -public interface InternalCredentialsService { +public interface CredentialsService { /** - * Provisions credentials information to the internal system. + * Provides credentials information to the internal system. * + * @param applicationId The application ID * @param credentials The credentials to provision * - * @return The credentials provisioned + * @return The credentials provided * * @throws CredentialsServiceException - if an unexpected exception occures. - * - * @see CredentialsService#provisionCredentials(CredentialsDto) */ - CredentialsDto provisionCredentials(String applicationId, CredentialsDto credentials) throws CredentialsServiceException; + CredentialsDto provideCredentials(String applicationId, CredentialsDto credentials) throws CredentialsServiceException; /** * Returns the credentials by ID. * + * @param applicationId The application ID * @param credentialsId The credentials ID * * @return The credentials with the given ID - * - * @see CredentialsService#lookupCredentials(String) */ - Optional lookupCredentials(String applicationId, String credentialsId); + Optional lookupCredentials(String applicationId, String credentialsId) throws CredentialsServiceException; /** * Sets the status of the given credentials to * {@link CredentialsStatus#IN_USE}. * + * @param applicationId The application ID * @param credentialsId The credentials ID * * @throws CredentialsServiceException - if the credentials are not - * {@link CredentialsStatus#AVAILABLE available}. - * - * @see CredentialsService#markCredentialsInUse(String) + * {@link CredentialsStatus#AVAILABLE}. */ void markCredentialsInUse(String applicationId, String credentialsId) throws CredentialsServiceException; @@ -79,11 +76,10 @@ public interface InternalCredentialsService { * Revokes the given credentials by setting their status to * {@link CredentialsStatus#REVOKED}. * + * @param applicationId The application ID * @param credentialsId The credentials ID * * @throws CredentialsServiceException - if an unexpected exception occures. - * - * @see CredentialsService#markCredentialsRevoked(String) */ void markCredentialsRevoked(String applicationId, String credentialsId) throws CredentialsServiceException; } diff --git a/server/node/src/main/java/org/kaaproject/kaa/server/node/service/registration/EndpointRegistrationService.java b/server/common/dao/src/main/java/org/kaaproject/kaa/server/common/dao/EndpointRegistrationService.java similarity index 94% rename from server/node/src/main/java/org/kaaproject/kaa/server/node/service/registration/EndpointRegistrationService.java rename to server/common/dao/src/main/java/org/kaaproject/kaa/server/common/dao/EndpointRegistrationService.java index 025dd07fb0..eb2c5bcef7 100644 --- a/server/node/src/main/java/org/kaaproject/kaa/server/node/service/registration/EndpointRegistrationService.java +++ b/server/common/dao/src/main/java/org/kaaproject/kaa/server/common/dao/EndpointRegistrationService.java @@ -14,11 +14,12 @@ * limitations under the License. */ -package org.kaaproject.kaa.server.node.service.registration; +package org.kaaproject.kaa.server.common.dao; import java.util.Optional; import org.kaaproject.kaa.common.dto.credentials.EndpointRegistrationDto; +import org.kaaproject.kaa.server.common.dao.exception.EndpointRegistrationServiceException; /** * A service to manage endpoint registrations. diff --git a/server/common/dao/src/main/java/org/kaaproject/kaa/server/common/dao/EndpointService.java b/server/common/dao/src/main/java/org/kaaproject/kaa/server/common/dao/EndpointService.java index eaeb3313be..984d949abf 100644 --- a/server/common/dao/src/main/java/org/kaaproject/kaa/server/common/dao/EndpointService.java +++ b/server/common/dao/src/main/java/org/kaaproject/kaa/server/common/dao/EndpointService.java @@ -296,9 +296,11 @@ public interface EndpointService { List findEndpointProfilesByExternalIdAndTenantId(String externalId, String tenantId); /** - * - * @param applicationId - * @return + * Returns the default group for the given application. + * + * @param applicationId The application ID + * + * @return The default group for the given application. */ EndpointGroupDto findDefaultGroup(String applicationId); diff --git a/server/node/src/main/java/org/kaaproject/kaa/server/node/service/credentials/CredentialsServiceException.java b/server/common/dao/src/main/java/org/kaaproject/kaa/server/common/dao/exception/CredentialsServiceException.java similarity index 89% rename from server/node/src/main/java/org/kaaproject/kaa/server/node/service/credentials/CredentialsServiceException.java rename to server/common/dao/src/main/java/org/kaaproject/kaa/server/common/dao/exception/CredentialsServiceException.java index 151fa39409..89051dd253 100644 --- a/server/node/src/main/java/org/kaaproject/kaa/server/node/service/credentials/CredentialsServiceException.java +++ b/server/common/dao/src/main/java/org/kaaproject/kaa/server/common/dao/exception/CredentialsServiceException.java @@ -14,10 +14,11 @@ * limitations under the License. */ -package org.kaaproject.kaa.server.node.service.credentials; +package org.kaaproject.kaa.server.common.dao.exception; /** - * A checked exception to be thrown by {@link CredentialsService}. + * A checked exception to be thrown by + * {@link org.kaaproject.kaa.server.common.dao.CredentialsService}. * * @author Andrew Shvayka * @author Bohdan Khablenko diff --git a/server/node/src/main/java/org/kaaproject/kaa/server/node/service/registration/EndpointRegistrationServiceException.java b/server/common/dao/src/main/java/org/kaaproject/kaa/server/common/dao/exception/EndpointRegistrationServiceException.java similarity index 91% rename from server/node/src/main/java/org/kaaproject/kaa/server/node/service/registration/EndpointRegistrationServiceException.java rename to server/common/dao/src/main/java/org/kaaproject/kaa/server/common/dao/exception/EndpointRegistrationServiceException.java index 5602a1a757..b49e7aaf9a 100644 --- a/server/node/src/main/java/org/kaaproject/kaa/server/node/service/registration/EndpointRegistrationServiceException.java +++ b/server/common/dao/src/main/java/org/kaaproject/kaa/server/common/dao/exception/EndpointRegistrationServiceException.java @@ -14,7 +14,9 @@ * limitations under the License. */ -package org.kaaproject.kaa.server.node.service.registration; +package org.kaaproject.kaa.server.common.dao.exception; + +import org.kaaproject.kaa.server.common.dao.EndpointRegistrationService; /** * A checked exception to be thrown by {@link EndpointRegistrationService}. diff --git a/server/common/dao/src/main/java/org/kaaproject/kaa/server/common/dao/impl/CredentialsDao.java b/server/common/dao/src/main/java/org/kaaproject/kaa/server/common/dao/impl/CredentialsDao.java index da10075322..3e5a8e4874 100644 --- a/server/common/dao/src/main/java/org/kaaproject/kaa/server/common/dao/impl/CredentialsDao.java +++ b/server/common/dao/src/main/java/org/kaaproject/kaa/server/common/dao/impl/CredentialsDao.java @@ -21,14 +21,53 @@ import org.kaaproject.kaa.server.common.dao.model.Credentials; import java.nio.ByteBuffer; +import java.util.Optional; +/** + * @author Andrew Shvayka + * @author Bohdan Khablenko + * + * @param A specific security credentials type + */ public interface CredentialsDao extends Dao { + /** + * Saves the given security credentials for the given application. + * + * @param applicationId The application ID + * @param credentials The security credentials to save + * + * @return The security credentials saved + */ T save(String applicationId, CredentialsDto credentials); - T find(String applicationId, String credentialsId); + /** + * Returns the security credentials with the given ID. + * + * @param applicationId The application ID to search credentials for + * @param credentialsId The security credentials ID + * + * @return The security credentials with the given ID + */ + Optional find(String applicationId, String credentialsId); - T updateStatus(String applicationId, String credentialsId, CredentialsStatus status); + /** + * Updates the status of the security credentials with the given ID. + * + * @param applicationId The application ID to update credentials for + * @param credentialsId The security credentials ID + * @param status The credentials status to set + * + * @return The security credentials with the status updated + */ + Optional updateStatus(String applicationId, String credentialsId, CredentialsStatus status); + /** + * Removes the security credentials with the given ID from the application + * specified. + * + * @param applicationId The application ID to remove credentials from + * @param credentialsId The secuity credentials ID + */ void remove(String applicationId, String credentialsId); } diff --git a/server/common/dao/src/main/java/org/kaaproject/kaa/server/common/dao/impl/EndpointProfileDao.java b/server/common/dao/src/main/java/org/kaaproject/kaa/server/common/dao/impl/EndpointProfileDao.java index 91cfce66f7..36c5bd527d 100644 --- a/server/common/dao/src/main/java/org/kaaproject/kaa/server/common/dao/impl/EndpointProfileDao.java +++ b/server/common/dao/src/main/java/org/kaaproject/kaa/server/common/dao/impl/EndpointProfileDao.java @@ -34,9 +34,11 @@ public interface EndpointProfileDao extends Dao { /** + * Saves the given endpoint profiles. * - * @param dto - * @return + * @param dto The endpoint profile to save + * + * @return The endpoint profile saved */ T save(EndpointProfileDto dto); diff --git a/server/common/dao/src/main/java/org/kaaproject/kaa/server/common/dao/impl/SqlDao.java b/server/common/dao/src/main/java/org/kaaproject/kaa/server/common/dao/impl/SqlDao.java index 685b820c45..a5721027ac 100644 --- a/server/common/dao/src/main/java/org/kaaproject/kaa/server/common/dao/impl/SqlDao.java +++ b/server/common/dao/src/main/java/org/kaaproject/kaa/server/common/dao/impl/SqlDao.java @@ -25,7 +25,7 @@ public interface SqlDao extends Dao { /** * Re-read object from database. * - * @param object + * @param object The object to refresh */ void refresh(Object object); @@ -56,7 +56,7 @@ public interface SqlDao extends Dao { /** * Build lock request with the given {@link org.hibernate.LockOptions} object * - * @param lockOptions + * @param lockOptions The lock options to use * @return the {@link org.hibernate.Session.LockRequest} object. */ Session.LockRequest lockRequest(LockOptions lockOptions); diff --git a/server/common/dao/src/main/java/org/kaaproject/kaa/server/common/dao/service/CredentialsServiceImpl.java b/server/common/dao/src/main/java/org/kaaproject/kaa/server/common/dao/service/CredentialsServiceImpl.java new file mode 100644 index 0000000000..97e737ae27 --- /dev/null +++ b/server/common/dao/src/main/java/org/kaaproject/kaa/server/common/dao/service/CredentialsServiceImpl.java @@ -0,0 +1,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 credentialsDao; + + public CredentialsServiceImpl(CredentialsDao 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 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); + } + } +} diff --git a/server/node/src/main/java/org/kaaproject/kaa/server/node/service/registration/EndpointRegistrationServiceImpl.java b/server/common/dao/src/main/java/org/kaaproject/kaa/server/common/dao/service/EndpointRegistrationServiceImpl.java similarity index 76% rename from server/node/src/main/java/org/kaaproject/kaa/server/node/service/registration/EndpointRegistrationServiceImpl.java rename to server/common/dao/src/main/java/org/kaaproject/kaa/server/common/dao/service/EndpointRegistrationServiceImpl.java index 499fc97a7d..0b1bc8accd 100644 --- a/server/node/src/main/java/org/kaaproject/kaa/server/node/service/registration/EndpointRegistrationServiceImpl.java +++ b/server/common/dao/src/main/java/org/kaaproject/kaa/server/common/dao/service/EndpointRegistrationServiceImpl.java @@ -14,18 +14,18 @@ * limitations under the License. */ -package org.kaaproject.kaa.server.node.service.registration; +package org.kaaproject.kaa.server.common.dao.service; import java.util.Optional; import org.apache.commons.lang3.Validate; import org.kaaproject.kaa.common.dto.credentials.EndpointRegistrationDto; +import org.kaaproject.kaa.server.common.dao.EndpointRegistrationService; +import org.kaaproject.kaa.server.common.dao.exception.EndpointRegistrationServiceException; import org.kaaproject.kaa.server.common.dao.impl.EndpointRegistrationDao; import org.kaaproject.kaa.server.common.dao.model.EndpointRegistration; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; /** @@ -37,22 +37,26 @@ * * @since v0.9.0 */ -@Service @Transactional public final class EndpointRegistrationServiceImpl implements EndpointRegistrationService { private static final Logger LOG = LoggerFactory.getLogger(EndpointRegistrationServiceImpl.class); - @Autowired - private EndpointRegistrationDao endpointRegistrationDao; + private final EndpointRegistrationDao endpointRegistrationDao; + + public EndpointRegistrationServiceImpl(EndpointRegistrationDao endpointRegistrationDao) { + this.endpointRegistrationDao = endpointRegistrationDao; + } @Override public EndpointRegistrationDto saveEndpointRegistration(EndpointRegistrationDto endpointRegistration) throws EndpointRegistrationServiceException { try { Validate.notNull(endpointRegistration, "Invalid endpoint registration provided!"); + String credentialsId = endpointRegistration.getCredentialsId(); + Validate.isTrue(this.findEndpointRegistrationByCredentialsId(credentialsId) == null, "The endpoint registration already exists!"); return this.endpointRegistrationDao.save(endpointRegistration).toDto(); } catch (Exception cause) { - LOG.error("An unexpected exception occured while searching for endpoint registration!", cause); + LOG.error("An unexpected exception occured while saving endpoint registration!", cause); throw new EndpointRegistrationServiceException(cause); } } @@ -61,8 +65,7 @@ public EndpointRegistrationDto saveEndpointRegistration(EndpointRegistrationDto public Optional findEndpointRegistrationByEndpointId(String endpointId) throws EndpointRegistrationServiceException { try { Validate.notBlank(endpointId, "Invalid endpoint ID provided!"); - Optional endpointRegistration = this.endpointRegistrationDao.findByEndpointId(endpointId); - return endpointRegistration.isPresent() ? Optional.of(endpointRegistration.get().toDto()) : Optional.ofNullable(null); + return this.endpointRegistrationDao.findByEndpointId(endpointId).map(EndpointRegistration::toDto); } catch (Exception cause) { LOG.error("An unexpected exception occured while searching for endpoint registration!", cause); throw new EndpointRegistrationServiceException(cause); @@ -73,8 +76,7 @@ public Optional findEndpointRegistrationByEndpointId(St public Optional findEndpointRegistrationByCredentialsId(String credentialsId) throws EndpointRegistrationServiceException { try { Validate.notBlank(credentialsId, "Invalid credentials ID provided!"); - Optional endpointRegistration = this.endpointRegistrationDao.findByCredentialsId(credentialsId); - return endpointRegistration.isPresent() ? Optional.of(endpointRegistration.get().toDto()) : Optional.ofNullable(null); + return this.endpointRegistrationDao.findByCredentialsId(credentialsId).map(EndpointRegistration::toDto); } catch (Exception cause) { LOG.error("An unexpected exception occured while searching for endpoint registration!", cause); throw new EndpointRegistrationServiceException(cause); diff --git a/server/common/dao/src/main/resources/common-dao-context.xml b/server/common/dao/src/main/resources/common-dao-context.xml index d9bcbe1e76..cbdeb42348 100644 --- a/server/common/dao/src/main/resources/common-dao-context.xml +++ b/server/common/dao/src/main/resources/common-dao-context.xml @@ -86,4 +86,12 @@ + + + + + + + + diff --git a/server/common/dto/src/main/java/org/kaaproject/kaa/common/dto/credentials/CredentialsDto.java b/server/common/dto/src/main/java/org/kaaproject/kaa/common/dto/credentials/CredentialsDto.java index 4f6c7920e2..6518be40cc 100644 --- a/server/common/dto/src/main/java/org/kaaproject/kaa/common/dto/credentials/CredentialsDto.java +++ b/server/common/dto/src/main/java/org/kaaproject/kaa/common/dto/credentials/CredentialsDto.java @@ -21,6 +21,8 @@ import org.kaaproject.kaa.common.dto.HasId; +import com.fasterxml.jackson.annotation.JsonProperty; + /** * @author Bohdan Khablenko * @author Andrew Shvayka @@ -31,15 +33,16 @@ public class CredentialsDto implements HasId, Serializable { private static final long serialVersionUID = 1000L; + @JsonProperty("credentialsId") private String id; + private byte[] credentialsBody; private CredentialsStatus status = CredentialsStatus.AVAILABLE; public CredentialsDto() { } - public CredentialsDto(String id, byte[] credentialsBody, CredentialsStatus status) { - this.id = id; + public CredentialsDto(byte[] credentialsBody, CredentialsStatus status) { this.credentialsBody = Arrays.copyOf(credentialsBody, credentialsBody.length); this.status = status; } diff --git a/server/common/nosql/mongo-dao/src/main/java/org/kaaproject/kaa/server/common/nosql/mongo/dao/CredentialsMongoDao.java b/server/common/nosql/mongo-dao/src/main/java/org/kaaproject/kaa/server/common/nosql/mongo/dao/CredentialsMongoDao.java index c1e8fcdd3e..15a4f049b2 100644 --- a/server/common/nosql/mongo-dao/src/main/java/org/kaaproject/kaa/server/common/nosql/mongo/dao/CredentialsMongoDao.java +++ b/server/common/nosql/mongo-dao/src/main/java/org/kaaproject/kaa/server/common/nosql/mongo/dao/CredentialsMongoDao.java @@ -17,6 +17,7 @@ package org.kaaproject.kaa.server.common.nosql.mongo.dao; import java.nio.ByteBuffer; +import java.util.Optional; import org.kaaproject.kaa.common.dto.credentials.CredentialsDto; import org.kaaproject.kaa.common.dto.credentials.CredentialsStatus; @@ -58,15 +59,15 @@ public MongoCredentials save(String applicationId, CredentialsDto credentials) { } @Override - public MongoCredentials find(String applicationId, String credentialsId) { + public Optional find(String applicationId, String credentialsId) { LOG.debug("Searching for credentials by application ID [{}] and credentials ID [{}]", applicationId, credentialsId); Query query = Query.query(Criteria.where(MongoModelConstants.CREDENTIALS_ID).is(credentialsId) .and(MongoModelConstants.APPLICATION_ID).is(applicationId)); - return this.findOne(query); + return Optional.ofNullable(this.findOne(query)); } @Override - public MongoCredentials updateStatus(String applicationId, String credentialsId, CredentialsStatus status) { + public Optional updateStatus(String applicationId, String credentialsId, CredentialsStatus status) { LOG.debug("Settings status [{}] for credentials [{}] in application [{}]", status.toString(), credentialsId, applicationId); updateFirst( Query.query(Criteria.where(MongoModelConstants.CREDENTIALS_ID).is(credentialsId).and(MongoModelConstants.APPLICATION_ID).is(applicationId)), diff --git a/server/common/nosql/mongo-dao/src/test/java/org/kaaproject/kaa/server/common/nosql/mongo/dao/CredentialsMongoDaoTest.java b/server/common/nosql/mongo-dao/src/test/java/org/kaaproject/kaa/server/common/nosql/mongo/dao/CredentialsMongoDaoTest.java index 7c1adfa40f..6db7db7553 100644 --- a/server/common/nosql/mongo-dao/src/test/java/org/kaaproject/kaa/server/common/nosql/mongo/dao/CredentialsMongoDaoTest.java +++ b/server/common/nosql/mongo-dao/src/test/java/org/kaaproject/kaa/server/common/nosql/mongo/dao/CredentialsMongoDaoTest.java @@ -4,6 +4,7 @@ import static org.kaaproject.kaa.common.dto.credentials.CredentialsStatus.REVOKED; import java.io.IOException; +import java.util.Optional; import org.junit.After; import org.junit.AfterClass; @@ -45,9 +46,9 @@ public void testFindCredentialsById() { Assert.assertNotNull(saved); Assert.assertNotNull(saved.getId()); - Credentials found = this.credentialsDao.find(APPLICATION_ID, saved.getId()); - Assert.assertNotNull(found); - Assert.assertEquals(saved, found.toDto()); + Optional found = this.credentialsDao.find(APPLICATION_ID, saved.getId()); + Assert.assertTrue(found.isPresent()); + Assert.assertEquals(saved, found.map(Credentials::toDto).get()); } @Test @@ -56,9 +57,9 @@ public void testUpdateStatus() { Assert.assertNotNull(credentials); Assert.assertNotNull(credentials.getId()); - Credentials updated = this.credentialsDao.updateStatus(APPLICATION_ID, credentials.getId(), REVOKED); - Assert.assertNotNull(updated); - Assert.assertEquals(REVOKED, updated.getStatus()); + Optional updated = this.credentialsDao.updateStatus(APPLICATION_ID, credentials.getId(), REVOKED); + Assert.assertTrue(updated.isPresent()); + Assert.assertEquals(REVOKED, updated.get().getStatus()); } @Test @@ -68,7 +69,7 @@ public void testRemoveCredentials() { Assert.assertNotNull(credentials.getId()); this.credentialsDao.remove(APPLICATION_ID, credentials.getId()); - Credentials removed = this.credentialsDao.find(APPLICATION_ID, credentials.getId()); - Assert.assertNull(removed); + Optional removed = this.credentialsDao.find(APPLICATION_ID, credentials.getId()); + Assert.assertFalse(removed.isPresent()); } } diff --git a/server/node/pom.xml b/server/node/pom.xml index 43dc603b31..222e3bb532 100644 --- a/server/node/pom.xml +++ b/server/node/pom.xml @@ -1153,6 +1153,12 @@ postgresql-admin + + + org.postgresql + postgresql + + diff --git a/server/node/src/main/java/org/kaaproject/kaa/server/admin/controller/KaaAdminController.java b/server/node/src/main/java/org/kaaproject/kaa/server/admin/controller/KaaAdminController.java index 38a76ba0fe..307a315b54 100644 --- a/server/node/src/main/java/org/kaaproject/kaa/server/admin/controller/KaaAdminController.java +++ b/server/node/src/main/java/org/kaaproject/kaa/server/admin/controller/KaaAdminController.java @@ -2106,53 +2106,77 @@ private byte[] getFileContent(MultipartFile file) throws KaaAdminServiceExceptio } /** - * Provides security credentials, allowing the endpoint with the given - * credentials to interact with the application specified. - * */ - @RequestMapping(value = "provisionCredentials", params = { "applicationId", "credentials" }, method = RequestMethod.POST) + * Provides security credentials, allowing an endpoint that uses them to + * interact with the specified application. + * + * @param applicationId The application ID to allow interaction with + * @param credentialsBody The security credentials to save + * + * @return The security credentials saved + * + * @throws KaaAdminServiceException - if an exception occures. + */ + @RequestMapping(value = "provideCredentials", params = { "applicationId", "credentialsBody" }, method = RequestMethod.POST) @ResponseBody - public CredentialsDto provisionCredentials ( + public CredentialsDto provideCredentials( @RequestParam String applicationId, - @RequestParam String credentials) - throws KaaAdminServiceException { - //TODO: implement - return null; + @RequestParam String credentialsBody) + throws KaaAdminServiceException { + return this.kaaAdminService.provideCredentials(applicationId, credentialsBody); } /** - * Provides registration information, allowing the endpoint with the given - * credentials to use this information during registration process. - * */ - @RequestMapping(value = "provisionRegistrationInfo", params = { "applicationId", "credentialsId", "serverSideEPProfileVersion", - "serverSideEPProfileBody" }, method = RequestMethod.POST) + * Binds credentials to the server-side endpoint profile specified. + * + * @param applicationId The application ID + * @param credentialsId The ID of the credentials to bind + * @param serverProfileVersion The server-side endpoint profile version + * @param serverProfileBody The server-side endpoint profile body + * + * @throws KaaAdminServiceException - if an exception occures. + */ + @RequestMapping(value = "provideRegistration", params = { "applicationId", "credentialsId" }, method = RequestMethod.POST) @ResponseStatus(HttpStatus.OK) - public void provisionRegistrationInfo( - @RequestParam String applicationId, + public void provideRegistration( + @RequestParam String applicationId, @RequestParam String credentialsId, - @RequestParam Integer serverSideEPProfileVersion, - @RequestParam String serverSideEPProfileBody) + @RequestParam(required = false) Integer serverProfileVersion, + @RequestParam(required = false) String serverProfileBody) throws KaaAdminServiceException { - // TODO: implement + this.kaaAdminService.provideRegistration(applicationId, credentialsId, serverProfileVersion, serverProfileBody); } /** - * Revokes security credentials in corresponding credentials storage. If endpoint is already registered using specified credentials, - * this API call launches asynchronous process that terminates all active sessions of corresponding endpoint. - * */ - @RequestMapping(value = "revokeCredentials", params = { "credentialsId" }, method = RequestMethod.POST) + * Revokes security credentials from the corresponding credentials storage. + * Also launches an asynchronous process to terminate all active sessions of + * the endpoint that uses these credentials. + * + * @param applicationId The application ID + * @param credentialsId The credentials ID + * + * @throws KaaAdminServiceException - if an exception occures. + */ + @RequestMapping(value = "revokeCredentials", params = { "applicationId", "credentialsId" }, method = RequestMethod.POST) @ResponseStatus(HttpStatus.OK) - public void revokeCredentials(String credentialsId) throws KaaAdminServiceException { - //TODO: implement + public void revokeCredentials(@RequestParam String applicationId, @RequestParam String credentialsId) throws KaaAdminServiceException { + this.kaaAdminService.revokeCredentials(applicationId, credentialsId); } /** - * Notifies Kaa cluster about revocation of security credentials. If endpoint is already registered using specified credentials, - * this API call launches asynchronous process that terminates all active sessions of corresponding endpoint. - * */ + * Notifies the Kaa cluster about security credentials revocation. If an + * endpoint is already registered with the specified credentials, this API + * call launches an asynchronous process to terminate all active sessions of + * the corresponding endpoint. + * + * @param credentialsId The credentials ID + * + * @throws KaaAdminServiceException - if an exception occures. + */ @RequestMapping(value = "notifyRevoked", params = { "credentialsId" }, method = RequestMethod.POST) @ResponseStatus(HttpStatus.OK) public void onCredentialsRevoked(String credentialsId) throws KaaAdminServiceException { - //TODO: implement + // TODO: Needs implementation! + throw new UnsupportedOperationException("This method lacks implementation!"); } /** diff --git a/server/node/src/main/java/org/kaaproject/kaa/server/admin/services/KaaAdminServiceImpl.java b/server/node/src/main/java/org/kaaproject/kaa/server/admin/services/KaaAdminServiceImpl.java index 8cd6a17ea8..ec7e2f38e8 100644 --- a/server/node/src/main/java/org/kaaproject/kaa/server/admin/services/KaaAdminServiceImpl.java +++ b/server/node/src/main/java/org/kaaproject/kaa/server/admin/services/KaaAdminServiceImpl.java @@ -36,6 +36,7 @@ import org.apache.avro.Schema; import org.apache.avro.generic.GenericRecord; import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.Validate; import org.hibernate.StaleObjectStateException; import org.kaaproject.avro.ui.converter.CtlSource; import org.kaaproject.avro.ui.converter.FormAvroConverter; @@ -80,6 +81,7 @@ import org.kaaproject.kaa.common.dto.admin.SdkProfileDto; import org.kaaproject.kaa.common.dto.admin.SdkProfileViewDto; import org.kaaproject.kaa.common.dto.admin.TenantUserDto; +import org.kaaproject.kaa.common.dto.credentials.CredentialsDto; import org.kaaproject.kaa.common.dto.ctl.CTLSchemaDto; import org.kaaproject.kaa.common.dto.ctl.CTLSchemaExportMethod; import org.kaaproject.kaa.common.dto.ctl.CTLSchemaMetaInfoDto; @@ -3586,4 +3588,51 @@ private void validateServerProfile(ServerProfileSchemaDto serverProfileSchema, S throw new IllegalArgumentException("Invalid server-side endpoint profile body provided!"); } } + + @Override + public CredentialsDto provideCredentials(String applicationId, String credentialsBody) throws KaaAdminServiceException { + this.checkAuthority(KaaAuthorityDto.TENANT_DEVELOPER, KaaAuthorityDto.TENANT_USER); + try { + this.checkApplicationId(applicationId); + return this.controlService.provideCredentials(applicationId, credentialsBody); + } catch (Exception cause) { + throw Utils.handleException(cause); + } + } + + @Override + public void revokeCredentials(String applicationId, String credentialsId) throws KaaAdminServiceException { + this.checkAuthority(KaaAuthorityDto.TENANT_DEVELOPER, KaaAuthorityDto.TENANT_USER); + try { + Validate.isTrue(this.controlService.getCredentials(applicationId, credentialsId) != null, "No credentials with the given ID found!"); + this.controlService.revokeCredentials(applicationId, credentialsId); + } catch (Exception cause) { + throw Utils.handleException(cause); + } + } + + @Override + public void provideRegistration( + String applicationId, + String credentialsId, + Integer serverProfileVersion, + String serverProfileBody) + throws KaaAdminServiceException { + this.checkAuthority(KaaAuthorityDto.TENANT_DEVELOPER, KaaAuthorityDto.TENANT_USER); + try { + this.checkApplicationId(applicationId); + Validate.isTrue(this.controlService.getCredentials(applicationId, credentialsId) != null, "No credentials with the given ID found!"); + if (serverProfileVersion != null && serverProfileBody != null) { + ServerProfileSchemaDto serverProfileSchema = this.getServerProfileSchema(applicationId, serverProfileVersion); + this.validateServerProfile(serverProfileSchema, serverProfileBody); + } else if (serverProfileVersion != null || serverProfileBody != null) { + String missingParameter = (serverProfileVersion == null ? "schema version" : "body"); + String message = MessageFormat.format("The server-side endpoint profile {0} provided is empty!", missingParameter); + throw new IllegalArgumentException(message); + } + this.controlService.provideRegistration(applicationId, credentialsId, serverProfileVersion, serverProfileBody); + } catch (Exception cause) { + throw Utils.handleException(cause); + } + } } diff --git a/server/node/src/main/java/org/kaaproject/kaa/server/admin/shared/services/KaaAdminService.java b/server/node/src/main/java/org/kaaproject/kaa/server/admin/shared/services/KaaAdminService.java index d84e83f0b1..e9d834610e 100644 --- a/server/node/src/main/java/org/kaaproject/kaa/server/admin/shared/services/KaaAdminService.java +++ b/server/node/src/main/java/org/kaaproject/kaa/server/admin/shared/services/KaaAdminService.java @@ -46,6 +46,7 @@ import org.kaaproject.kaa.common.dto.admin.SdkProfileViewDto; import org.kaaproject.kaa.common.dto.admin.TenantUserDto; import org.kaaproject.kaa.common.dto.admin.UserDto; +import org.kaaproject.kaa.common.dto.credentials.CredentialsDto; import org.kaaproject.kaa.common.dto.ctl.CTLSchemaDto; import org.kaaproject.kaa.common.dto.ctl.CTLSchemaExportMethod; import org.kaaproject.kaa.common.dto.ctl.CTLSchemaMetaInfoDto; @@ -421,4 +422,9 @@ CtlSchemaFormDto createNewCTLSchemaFormInstance(String metaInfoId, List getEndpointProfilesByUserExternalId(String endpointUserExternalId) throws KaaAdminServiceException; + CredentialsDto provideCredentials(String applicationId, String credentialsBody) throws KaaAdminServiceException; + + void revokeCredentials(String applicationId, String credentialsId) throws KaaAdminServiceException; + + void provideRegistration(String applicationId, String credentialsId, Integer serverProfileVersion, String serverProfileBody) throws KaaAdminServiceException; } diff --git a/server/node/src/main/java/org/kaaproject/kaa/server/control/service/ControlService.java b/server/node/src/main/java/org/kaaproject/kaa/server/control/service/ControlService.java index 2bd684124f..958d351c21 100644 --- a/server/node/src/main/java/org/kaaproject/kaa/server/control/service/ControlService.java +++ b/server/node/src/main/java/org/kaaproject/kaa/server/control/service/ControlService.java @@ -50,6 +50,7 @@ import org.kaaproject.kaa.common.dto.admin.RecordKey; import org.kaaproject.kaa.common.dto.admin.SdkPlatform; import org.kaaproject.kaa.common.dto.admin.SdkProfileDto; +import org.kaaproject.kaa.common.dto.credentials.CredentialsDto; import org.kaaproject.kaa.common.dto.ctl.CTLSchemaDto; import org.kaaproject.kaa.common.dto.ctl.CTLSchemaMetaInfoDto; import org.kaaproject.kaa.common.dto.event.AefMapInfoDto; @@ -62,6 +63,7 @@ import org.kaaproject.kaa.common.dto.logs.LogAppenderDto; import org.kaaproject.kaa.common.dto.logs.LogSchemaDto; import org.kaaproject.kaa.common.dto.user.UserVerifierDto; +import org.kaaproject.kaa.server.admin.shared.services.KaaAdminServiceException; import org.kaaproject.kaa.server.control.service.exception.ControlServiceException; /** @@ -1675,7 +1677,7 @@ ApplicationEventFamilyMapDto editApplicationEventFamilyMap(ApplicationEventFamil * @param endpointId * The endpoint ID * - * @throws ControlServiceException + * @throws ControlServiceException - if an exception occures. */ void removeEndpointProfileByEndpointId(String endpointId) throws ControlServiceException; @@ -1691,4 +1693,52 @@ ApplicationEventFamilyMapDto editApplicationEventFamilyMap(ApplicationEventFamil */ List getEndpointProfilesByUserExternalIdAndTenantId(String endpointUserExternalId, String tenantId) throws ControlServiceException; + /** + * Provides security credentials, allowing an endpoint that uses them to + * interact with the specified application. + * + * @param applicationId The application ID to allow interaction with + * @param credentialsBody The security credentials to save + * + * @return The security credentials saved + * + * @throws ControlServiceException - if an exception occures. + */ + CredentialsDto provideCredentials(String applicationId, String credentialsBody) throws ControlServiceException; + + /** + * Returns credentials by application ID and credentials ID. + * + * @param applicationId The application ID + * @param credentialsId The credentials ID + * + * @return The credentials found + * + * @throws ControlServiceException - if an exception occures. + */ + CredentialsDto getCredentials(String applicationId, String credentialsId) throws ControlServiceException; + + /** + * Revokes security credentials from the corresponding credentials storage. + * Also launches an asynchronous process to terminate all active sessions of + * the endpoint that uses these credentials. + * + * @param applicationId The application ID + * @param credentialsId The credentials ID + * + * @throws ControlServiceException - if an exception occures. + */ + void revokeCredentials(String applicationId, String credentialsId) throws ControlServiceException; + + /** + * Binds credentials to the server-side endpoint profile specified. + * + * @param applicationId The application ID + * @param credentialsId The ID of the credentials to bind + * @param serverProfileVersion The server-side endpoint profile version + * @param serverProfileBody The server-side endpoint profile body + * + * @throws KaaAdminServiceException - if an exception occures. + */ + void provideRegistration(String applicationId, String credentialsId, Integer serverProfileVersion, String serverProfileBody) throws ControlServiceException; } diff --git a/server/node/src/main/java/org/kaaproject/kaa/server/control/service/DefaultControlService.java b/server/node/src/main/java/org/kaaproject/kaa/server/control/service/DefaultControlService.java index 3629983aaf..01f499dfd6 100644 --- a/server/node/src/main/java/org/kaaproject/kaa/server/control/service/DefaultControlService.java +++ b/server/node/src/main/java/org/kaaproject/kaa/server/control/service/DefaultControlService.java @@ -72,6 +72,9 @@ import org.kaaproject.kaa.common.dto.admin.RecordKey.RecordFiles; import org.kaaproject.kaa.common.dto.admin.SdkPlatform; import org.kaaproject.kaa.common.dto.admin.SdkProfileDto; +import org.kaaproject.kaa.common.dto.credentials.CredentialsDto; +import org.kaaproject.kaa.common.dto.credentials.CredentialsStatus; +import org.kaaproject.kaa.common.dto.credentials.EndpointRegistrationDto; import org.kaaproject.kaa.common.dto.ctl.CTLSchemaDto; import org.kaaproject.kaa.common.dto.ctl.CTLSchemaMetaInfoDto; import org.kaaproject.kaa.common.dto.event.AefMapInfoDto; @@ -94,6 +97,7 @@ import org.kaaproject.kaa.server.common.dao.ApplicationService; import org.kaaproject.kaa.server.common.dao.CTLService; import org.kaaproject.kaa.server.common.dao.ConfigurationService; +import org.kaaproject.kaa.server.common.dao.EndpointRegistrationService; import org.kaaproject.kaa.server.common.dao.EndpointService; import org.kaaproject.kaa.server.common.dao.EventClassService; import org.kaaproject.kaa.server.common.dao.LogAppendersService; @@ -106,6 +110,8 @@ import org.kaaproject.kaa.server.common.dao.UserConfigurationService; import org.kaaproject.kaa.server.common.dao.UserService; import org.kaaproject.kaa.server.common.dao.UserVerifierService; +import org.kaaproject.kaa.server.common.dao.exception.CredentialsServiceException; +import org.kaaproject.kaa.server.common.dao.exception.EndpointRegistrationServiceException; import org.kaaproject.kaa.server.common.dao.exception.IncorrectParameterException; import org.kaaproject.kaa.server.common.dao.exception.NotFoundException; import org.kaaproject.kaa.server.common.log.shared.RecordWrapperSchemaGenerator; @@ -129,6 +135,7 @@ import org.kaaproject.kaa.server.control.service.sdk.event.EventFamilyMetadata; import org.kaaproject.kaa.server.control.service.zk.ControlZkService; import org.kaaproject.kaa.server.hash.ConsistentHashResolver; +import org.kaaproject.kaa.server.node.service.credentials.CredentialsServiceLocator; import org.kaaproject.kaa.server.node.service.thrift.OperationsServiceMsg; import org.kaaproject.kaa.server.resolve.OperationsServerResolver; import org.kaaproject.kaa.server.thrift.NeighborTemplate; @@ -238,6 +245,12 @@ public class DefaultControlService implements ControlService { @Autowired private CTLService ctlService; + @Autowired + private CredentialsServiceLocator credentialsServiceLocator; + + @Autowired + private EndpointRegistrationService endpointRegistrationService; + /** The neighbor connections size. */ @Value("#{properties[max_number_neighbor_connections]}") private int neighborConnectionsSize = DEFAULT_NEIGHBOR_CONNECTIONS_SIZE; @@ -2212,5 +2225,55 @@ public void onStop() { public void removeEndpointProfileByEndpointId(String endpointId) throws ControlServiceException { this.endpointService.removeEndpointProfileByKeyHash(endpointId.getBytes()); } - + + @Override + public CredentialsDto provideCredentials(String applicationId, String credentialsBody) throws ControlServiceException { + CredentialsDto credentials = new CredentialsDto(credentialsBody.getBytes(), CredentialsStatus.AVAILABLE); + try { + return this.credentialsServiceLocator.getCredentialsService(applicationId).provideCredentials(credentials); + } catch (CredentialsServiceException cause) { + String message = MessageFormat.format("An unexpected exception occured while saving credentials [{0}]", credentials); + LOG.error(message, cause); + throw new ControlServiceException(cause); + } + } + + @Override + public CredentialsDto getCredentials(String applicationId, String credentialsId) throws ControlServiceException { + try { + return this.credentialsServiceLocator.getCredentialsService(applicationId).lookupCredentials(credentialsId).orElse(null); + } catch (CredentialsServiceException cause) { + String message = MessageFormat.format("An unexpected exception occured while searching for credentials by ID [{0}]", credentialsId); + LOG.error(message, cause); + throw new ControlServiceException(cause); + } + } + + @Override + public void revokeCredentials(String applicationId, String credentialsId) throws ControlServiceException { + try { + this.credentialsServiceLocator.getCredentialsService(applicationId).markCredentialsRevoked(credentialsId); + } catch (CredentialsServiceException cause) { + String message = MessageFormat.format("An unexpected exception occured while revoking credentials by ID [{0}]", credentialsId); + LOG.error(message, cause); + throw new ControlServiceException(cause); + } + } + + @Override + public void provideRegistration( + String applicationId, + String credentialsId, + Integer serverProfileVersion, + String serverProfileBody) + throws ControlServiceException { + EndpointRegistrationDto endpointRegistration = new EndpointRegistrationDto(applicationId, credentialsId, null, serverProfileVersion, serverProfileBody); + try { + this.endpointRegistrationService.saveEndpointRegistration(endpointRegistration); + } catch (EndpointRegistrationServiceException cause) { + String message = MessageFormat.format("An unexpected exception occured while saving endpoint registration [{0}]", endpointRegistration); + LOG.error(message, cause); + throw new ControlServiceException(cause); + } + } } diff --git a/server/node/src/main/java/org/kaaproject/kaa/server/node/service/credentials/CredentialsService.java b/server/node/src/main/java/org/kaaproject/kaa/server/node/service/credentials/CredentialsService.java index 2c511d9451..9b7b294680 100644 --- a/server/node/src/main/java/org/kaaproject/kaa/server/node/service/credentials/CredentialsService.java +++ b/server/node/src/main/java/org/kaaproject/kaa/server/node/service/credentials/CredentialsService.java @@ -20,6 +20,7 @@ import org.kaaproject.kaa.common.dto.credentials.CredentialsDto; import org.kaaproject.kaa.common.dto.credentials.CredentialsStatus; +import org.kaaproject.kaa.server.common.dao.exception.CredentialsServiceException; /** * A service that communicates with an external system to manage credentials @@ -33,17 +34,17 @@ public interface CredentialsService { /** - * Provisions credentials information to the external system. + * Provide credentials information to the external system. * * @param credentials The credentials to provision * - * @return The credentials provisioned + * @return The credentials provided * * @throws UnsupportedOperationException - if the underlying implementation * forbids credentials provisioning. * @throws CredentialsServiceException - if an unexpected exception occures. */ - CredentialsDto provisionCredentials(CredentialsDto credentials) throws CredentialsServiceException; + CredentialsDto provideCredentials(CredentialsDto credentials) throws CredentialsServiceException; /** * Returns the credentials by ID. @@ -51,8 +52,10 @@ public interface CredentialsService { * @param credentialsId The credentials ID * * @return The credentials with the given ID + * + * @throws CredentialsServiceException - if an unexpected exception occures. */ - Optional lookupCredentials(String credentialsId); + Optional lookupCredentials(String credentialsId) throws CredentialsServiceException; /** * Sets the status of the given credentials to @@ -61,7 +64,7 @@ public interface CredentialsService { * @param credentialsId The credentials ID * * @throws CredentialsServiceException - if the credentials are not - * {@link CredentialsStatus#AVAILABLE available}. + * {@link CredentialsStatus#AVAILABLE}. */ void markCredentialsInUse(String credentialsId) throws CredentialsServiceException; diff --git a/server/node/src/main/java/org/kaaproject/kaa/server/node/service/credentials/InternalCredentialsServiceAdapter.java b/server/node/src/main/java/org/kaaproject/kaa/server/node/service/credentials/CredentialsServiceAdapter.java similarity index 71% rename from server/node/src/main/java/org/kaaproject/kaa/server/node/service/credentials/InternalCredentialsServiceAdapter.java rename to server/node/src/main/java/org/kaaproject/kaa/server/node/service/credentials/CredentialsServiceAdapter.java index cca9f27cd0..3bccb66ce3 100644 --- a/server/node/src/main/java/org/kaaproject/kaa/server/node/service/credentials/InternalCredentialsServiceAdapter.java +++ b/server/node/src/main/java/org/kaaproject/kaa/server/node/service/credentials/CredentialsServiceAdapter.java @@ -19,20 +19,20 @@ import java.util.Optional; import org.kaaproject.kaa.common.dto.credentials.CredentialsDto; +import org.kaaproject.kaa.server.common.dao.exception.CredentialsServiceException; /** - * A bridge between the {@link CredentialsService} and - * {@link InternalCredentialsService} interfaces. + * A bridge between the actual interface and the internal Kaa implementation. * * @author Andrew Shvayka * @author Bohdan Khablenko * * @since v0.9.0 */ -public final class InternalCredentialsServiceAdapter implements CredentialsService { +public final class CredentialsServiceAdapter implements CredentialsService { private final String applicationId; - private final InternalCredentialsService credentialsService; + private final org.kaaproject.kaa.server.common.dao.CredentialsService credentialsService; /** * Constructs an adapter for the given application. @@ -40,18 +40,18 @@ public final class InternalCredentialsServiceAdapter implements CredentialsServi * @param applicationId The application ID * @param credentialsService The internal credentials service used by Kaa */ - public InternalCredentialsServiceAdapter(String applicationId, InternalCredentialsService credentialsService) { + public CredentialsServiceAdapter(String applicationId, org.kaaproject.kaa.server.common.dao.CredentialsService credentialsService) { this.applicationId = applicationId; this.credentialsService = credentialsService; } @Override - public CredentialsDto provisionCredentials(CredentialsDto credentials) throws CredentialsServiceException { - return credentialsService.provisionCredentials(applicationId, credentials); + public CredentialsDto provideCredentials(CredentialsDto credentials) throws CredentialsServiceException { + return credentialsService.provideCredentials(applicationId, credentials); } @Override - public Optional lookupCredentials(String credentialsId) { + public Optional lookupCredentials(String credentialsId) throws CredentialsServiceException { return credentialsService.lookupCredentials(applicationId, credentialsId); } diff --git a/server/node/src/main/java/org/kaaproject/kaa/server/node/service/credentials/CredentialsServiceFactory.java b/server/node/src/main/java/org/kaaproject/kaa/server/node/service/credentials/CredentialsServiceLocator.java similarity index 91% rename from server/node/src/main/java/org/kaaproject/kaa/server/node/service/credentials/CredentialsServiceFactory.java rename to server/node/src/main/java/org/kaaproject/kaa/server/node/service/credentials/CredentialsServiceLocator.java index c225ed6bbb..8c5163223a 100644 --- a/server/node/src/main/java/org/kaaproject/kaa/server/node/service/credentials/CredentialsServiceFactory.java +++ b/server/node/src/main/java/org/kaaproject/kaa/server/node/service/credentials/CredentialsServiceLocator.java @@ -24,7 +24,7 @@ * * @since v0.9.0 */ -public interface CredentialsServiceFactory { +public interface CredentialsServiceLocator { /** * Returns the service that is responsible for managing credentials for the @@ -32,7 +32,7 @@ public interface CredentialsServiceFactory { * * @param applicationId The application ID * - * @return The servise that is responsible for managing credentials for the + * @return The service that is responsible for managing credentials for the * specified application. */ CredentialsService getCredentialsService(String applicationId); diff --git a/server/node/src/main/java/org/kaaproject/kaa/server/node/service/credentials/InternalCredentialsServiceFactory.java b/server/node/src/main/java/org/kaaproject/kaa/server/node/service/credentials/DefaultCredentialsServiceLocator.java similarity index 61% rename from server/node/src/main/java/org/kaaproject/kaa/server/node/service/credentials/InternalCredentialsServiceFactory.java rename to server/node/src/main/java/org/kaaproject/kaa/server/node/service/credentials/DefaultCredentialsServiceLocator.java index 968c0a90e5..1bea343e9e 100644 --- a/server/node/src/main/java/org/kaaproject/kaa/server/node/service/credentials/InternalCredentialsServiceFactory.java +++ b/server/node/src/main/java/org/kaaproject/kaa/server/node/service/credentials/DefaultCredentialsServiceLocator.java @@ -16,24 +16,25 @@ package org.kaaproject.kaa.server.node.service.credentials; -import org.springframework.beans.factory.annotation.Autowired; - /** - * The default implementation of the {@link CredentialsServiceFactory} used by - * Kaa in order to utilize {@link InternalCredentialsService}. + * The default implementation of the {@link CredentialsServiceLocator} + * interface. * * @author Andrew Shvayka * @author Bohdan Khablenko * * @since v0.9.0 */ -public final class InternalCredentialsServiceFactory implements CredentialsServiceFactory { +public final class DefaultCredentialsServiceLocator implements CredentialsServiceLocator { + + private final org.kaaproject.kaa.server.common.dao.CredentialsService credentialsService; - @Autowired - private InternalCredentialsService credentialsService; + public DefaultCredentialsServiceLocator(org.kaaproject.kaa.server.common.dao.CredentialsService credentialsService) { + this.credentialsService = credentialsService; + } @Override public CredentialsService getCredentialsService(String applicationId) { - return new InternalCredentialsServiceAdapter(applicationId, this.credentialsService); + return new CredentialsServiceAdapter(applicationId, this.credentialsService); } } diff --git a/server/node/src/main/java/org/kaaproject/kaa/server/node/service/credentials/DefaultInternalCredentialsService.java b/server/node/src/main/java/org/kaaproject/kaa/server/node/service/credentials/DefaultInternalCredentialsService.java deleted file mode 100644 index aa32ea7e33..0000000000 --- a/server/node/src/main/java/org/kaaproject/kaa/server/node/service/credentials/DefaultInternalCredentialsService.java +++ /dev/null @@ -1,56 +0,0 @@ -/** - * 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.node.service.credentials; - -import java.util.Optional; - -import org.kaaproject.kaa.common.dto.credentials.CredentialsDto; -import org.springframework.stereotype.Service; - -/** - * The default implementation of the {@link InternalCredentialsService} - * interface. - * - * @author Bohdan Khablenko - * - * @since v0.9.0 - */ -@Service -public class DefaultInternalCredentialsService implements InternalCredentialsService { - - @Override - public CredentialsDto provisionCredentials(String applicationId, CredentialsDto credentials) throws CredentialsServiceException { - // TODO Auto-generated method stub - return null; - } - - @Override - public Optional lookupCredentials(String applicationId, String credentialsId) { - // TODO Auto-generated method stub - return null; - } - - @Override - public void markCredentialsInUse(String applicationId, String credentialsId) throws CredentialsServiceException { - // TODO Auto-generated method stub - } - - @Override - public void markCredentialsRevoked(String applicationId, String credentialsId) throws CredentialsServiceException { - // TODO Auto-generated method stub - } -} diff --git a/server/node/src/main/java/org/kaaproject/kaa/server/node/service/registration/DefaultRegistrationService.java b/server/node/src/main/java/org/kaaproject/kaa/server/node/service/registration/DefaultRegistrationService.java new file mode 100644 index 0000000000..4f5365b3db --- /dev/null +++ b/server/node/src/main/java/org/kaaproject/kaa/server/node/service/registration/DefaultRegistrationService.java @@ -0,0 +1,58 @@ +/** + * 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.node.service.registration; + +import java.util.Optional; + +import org.kaaproject.kaa.common.dto.credentials.EndpointRegistrationDto; +import org.kaaproject.kaa.server.common.dao.EndpointRegistrationService; +import org.kaaproject.kaa.server.common.dao.exception.EndpointRegistrationServiceException; + +/** + * @author Andrew Shvayka + * @author Bohdan Khablenko + * + * @since v0.9.0 + */ +public final class DefaultRegistrationService implements RegistrationService { + + private final EndpointRegistrationService endpointRegistrationService; + + public DefaultRegistrationService(EndpointRegistrationService endpointRegistrationService) { + this.endpointRegistrationService = endpointRegistrationService; + } + + @Override + public EndpointRegistrationDto saveEndpointRegistration(EndpointRegistrationDto endpointRegistration) throws EndpointRegistrationServiceException { + return this.endpointRegistrationService.saveEndpointRegistration(endpointRegistration); + } + + @Override + public Optional findEndpointRegistrationByEndpointId(String endpointId) throws EndpointRegistrationServiceException { + return this.endpointRegistrationService.findEndpointRegistrationByEndpointId(endpointId); + } + + @Override + public Optional findEndpointRegistrationByCredentialsId(String credentialsId) throws EndpointRegistrationServiceException { + return this.endpointRegistrationService.findEndpointRegistrationByCredentialsId(credentialsId); + } + + @Override + public void removeEndpointRegistrationByEndpointId(String endpointId) throws EndpointRegistrationServiceException { + this.endpointRegistrationService.removeEndpointRegistrationByEndpointId(endpointId); + } +} diff --git a/server/node/src/main/java/org/kaaproject/kaa/server/node/service/registration/RegistrationService.java b/server/node/src/main/java/org/kaaproject/kaa/server/node/service/registration/RegistrationService.java new file mode 100644 index 0000000000..c9fcdfc45e --- /dev/null +++ b/server/node/src/main/java/org/kaaproject/kaa/server/node/service/registration/RegistrationService.java @@ -0,0 +1,75 @@ +/** + * 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.node.service.registration; + +import java.util.Optional; + +import org.kaaproject.kaa.common.dto.credentials.EndpointRegistrationDto; +import org.kaaproject.kaa.server.common.dao.exception.EndpointRegistrationServiceException; + +/** + * A service to manage endpoint registrations. + * + * @author Andrew Shvayka + * @author Bohdan Khablenko + * + * @since v0.9.0 + */ +public interface RegistrationService { + + /** + * Saves the given endpoint registration. + * + * @param endpointRegistration The endpoint registration to save + * + * @return The endpoint registration saved + * + * @throws EndpointRegistrationServiceException - if an exception occures. + */ + EndpointRegistrationDto saveEndpointRegistration(EndpointRegistrationDto endpointRegistration) throws EndpointRegistrationServiceException; + + /** + * Returns the endpoint registration by the endpoint ID specified. + * + * @param endpointId The endpoint ID + * + * @return The endpoint registration found + * + * @throws EndpointRegistrationServiceException - if an exception occures. + */ + Optional findEndpointRegistrationByEndpointId(String endpointId) throws EndpointRegistrationServiceException; + + /** + * Returns the endpoint registration by the credentials ID specified. + * + * @param credentialsId The credentials ID + * + * @return The endpoint registration found + * + * @throws EndpointRegistrationServiceException - if an exception occures. + */ + Optional findEndpointRegistrationByCredentialsId(String credentialsId) throws EndpointRegistrationServiceException; + + /** + * Removes the endpoint registration by the endpoint ID specified. + * + * @param endpointId The endpoint ID + * + * @throws EndpointRegistrationServiceException - if an exception occures. + */ + void removeEndpointRegistrationByEndpointId(String endpointId) throws EndpointRegistrationServiceException; +} diff --git a/server/node/src/main/resources/kaaNodeContext.xml b/server/node/src/main/resources/kaaNodeContext.xml index b883280bdf..148aa679fc 100644 --- a/server/node/src/main/resources/kaaNodeContext.xml +++ b/server/node/src/main/resources/kaaNodeContext.xml @@ -58,8 +58,15 @@ - + + + + + + + + + diff --git a/server/node/src/main/resources/postgresql-dao.properties.template b/server/node/src/main/resources/postgresql-dao.properties.template index d391da90a9..326d5cf47b 100644 --- a/server/node/src/main/resources/postgresql-dao.properties.template +++ b/server/node/src/main/resources/postgresql-dao.properties.template @@ -14,7 +14,7 @@ hibernate_hbm2ddl_auto=update jdbc_driver_className=org.postgresql.Driver # specify jdbc database user name -jdbc_username=sqladmin +jdbc_username=postgres # specify jdbc database password jdbc_password=admin