Skip to content

Commit

Permalink
KAA-876: Implement the REST API
Browse files Browse the repository at this point in the history
  • Loading branch information
Bohdan Khablenko committed Mar 30, 2016
1 parent 3c0d7c6 commit 46d90f8
Show file tree
Hide file tree
Showing 30 changed files with 614 additions and 168 deletions.
4 changes: 4 additions & 0 deletions server/common/dao/pom.xml
Expand Up @@ -47,6 +47,10 @@
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand Down
Expand Up @@ -14,76 +14,72 @@
* 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
* @author Bohdan Khablenko
*
* @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<CredentialsDto> lookupCredentials(String applicationId, String credentialsId);
Optional<CredentialsDto> 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;

/**
* 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;
}
Expand Up @@ -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.
Expand Down
Expand Up @@ -296,9 +296,11 @@ public interface EndpointService {
List<EndpointProfileDto> 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);

Expand Down
Expand Up @@ -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
Expand Down
Expand Up @@ -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}.
Expand Down
Expand Up @@ -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 <T> A specific security credentials type
*/
public interface CredentialsDao<T extends Credentials> extends Dao<T, ByteBuffer> {

/**
* 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<T> 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<T> 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);
}
Expand Up @@ -34,9 +34,11 @@
public interface EndpointProfileDao<T extends EndpointProfile> extends Dao<T, ByteBuffer> {

/**
* Saves the given endpoint profiles.
*
* @param dto
* @return
* @param dto The endpoint profile to save
*
* @return The endpoint profile saved
*/
T save(EndpointProfileDto dto);

Expand Down
Expand Up @@ -25,7 +25,7 @@ public interface SqlDao<T> extends Dao<T, String> {
/**
* Re-read object from database.
*
* @param object
* @param object The object to refresh
*/
void refresh(Object object);

Expand Down Expand Up @@ -56,7 +56,7 @@ public interface SqlDao<T> extends Dao<T, String> {
/**
* 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);
Expand Down
@@ -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<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);
}
}
}

0 comments on commit 46d90f8

Please sign in to comment.