Skip to content

Commit

Permalink
KAA-876: Bug Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ashvayka committed Apr 14, 2016
1 parent 3f3fe1c commit aa12bd7
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 5 deletions.
Expand Up @@ -90,6 +90,7 @@
import org.springframework.http.client.ClientHttpRequest;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.util.Base64Utils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RequestCallback;
Expand Down Expand Up @@ -1062,10 +1063,10 @@ public List<EndpointProfileDto> getEndpointProfilesByUserExternalId(String endpo
return response.getBody();
}

public CredentialsDto provideCredentials (String applicationId, String credentialsBody) {
public CredentialsDto provideCredentials (String applicationId, byte[] credentialsBody) {
MultiValueMap<String, Object> parameters = new LinkedMultiValueMap<>();
parameters.add("applicationId", applicationId);
parameters.add("credentialsBody", credentialsBody);
parameters.add("credentialsBody", Base64Utils.encodeToString(credentialsBody));
return this.restTemplate.postForObject(restTemplate.getUrl() + "provideCredentials", parameters, CredentialsDto.class);
}

Expand Down
Expand Up @@ -55,7 +55,8 @@ public EndpointRegistrationDto saveEndpointRegistration(EndpointRegistrationDto
try {
Validate.notNull(endpointRegistration, "Invalid endpoint registration provided!");
String credentialsId = endpointRegistration.getCredentialsId();
Validate.isTrue(this.findEndpointRegistrationByCredentialsId(credentialsId) == null, "The endpoint registration already exists!");
Optional<EndpointRegistrationDto> oldRegistration = findEndpointRegistrationByCredentialsId(credentialsId);
Validate.isTrue(!oldRegistration.isPresent(), "The endpoint registration already exists!");
return this.endpointRegistrationDao.save(endpointRegistration).toDto();
} catch (Exception cause) {
LOG.error("An unexpected exception occured while saving endpoint registration!", cause);
Expand Down
Expand Up @@ -22,6 +22,7 @@
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.common.hash.SHA1HashUtils;
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;
Expand All @@ -30,6 +31,7 @@
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Base64Utils;

/**
* The default implementation of the {@link CredentialsService} interface.
Expand All @@ -55,6 +57,8 @@ public CredentialsDto provideCredentials(String applicationId, CredentialsDto cr
Validate.notBlank(applicationId, "Invalid application ID provided!");
Validate.notNull(credentials, "Invalid credentials provided!");
try {
byte[] credentialsBody = credentials.getCredentialsBody();
credentials.setId(Base64Utils.encodeToUrlSafeString(SHA1HashUtils.hashToBytes(credentialsBody)));
return this.credentialsDao.save(applicationId, credentials).toDto();
} catch (Exception cause) {
String message = MessageFormat.format("[{0}] An unexpected exception occured while saving credentials!", applicationId);
Expand Down
Expand Up @@ -93,7 +93,7 @@ public Optional<CassandraEndpointRegistration> findByEndpointId(String endpointI
public Optional<CassandraEndpointRegistration> findByCredentialsId(String credentialsId) {
LOG.debug("Searching for endpoint registration by credentials ID [{}]", credentialsId);
String endpointId = this.byCredentialsID.getEndpointIdByCredentialsId(credentialsId);
Clause clause = QueryBuilder.eq(CassandraModelConstants.EP_REGISTRATION_ENDPOINT_ID_PROPERTY, endpointId);
Clause clause = QueryBuilder.eq(CassandraModelConstants.EP_REGISTRATION_CREDENTIALS_ID_PROPERTY, endpointId);
Statement statement = QueryBuilder.select().from(this.getColumnFamilyName()).where(clause);
return Optional.ofNullable(this.findOneByStatement(statement));
}
Expand Down
Expand Up @@ -150,6 +150,7 @@
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.Base64Utils;

/**
* The Class DefaultControlService.
Expand Down Expand Up @@ -2243,7 +2244,7 @@ public void removeEndpointProfile(EndpointProfileDto endpointProfile) throws Con

@Override
public CredentialsDto provideCredentials(String applicationId, String credentialsBody) throws ControlServiceException {
CredentialsDto credentials = new CredentialsDto(credentialsBody.getBytes(), CredentialsStatus.AVAILABLE);
CredentialsDto credentials = new CredentialsDto(Base64Utils.decodeFromString(credentialsBody), CredentialsStatus.AVAILABLE);
try {
return this.credentialsServiceLocator.getCredentialsService(applicationId).provideCredentials(credentials);
} catch (CredentialsServiceException cause) {
Expand Down

0 comments on commit aa12bd7

Please sign in to comment.