First implemtation of REST-API

Signed-off-by: Nonnenmacher Fabian <fabian.nonnenmacher@bosch-si.com>
This commit is contained in:
Fabian Nonnenmacher
2016-01-21 15:29:10 +01:00
committed by Nonnenmacher Fabian
parent 088df73ea9
commit f3fa085c62
11 changed files with 557 additions and 3 deletions

View File

@@ -65,6 +65,19 @@ public enum SpServerError {
*/
SP_REST_SORT_PARAM_INVALID_DIRECTION("hawkbit.server.error.rest.param.invalidDirection",
"The given sort parameter direction does not exist"),
/**
*
*/
SP_REST_CONFIG_POLLING_TIME_WRONG_FOMRATTED("hawkbit.server.error.rest.param.invalidFormat",
"The given overdue polling time or polling time parameter are not formatted correctly."),
/**
*
*/
SP_REST_CONFIG_INVALID_DS_TYPE("hawkbit.server.error.rest.param.invalidFormat",
"The given default distribution set type does not exist."),
/**
*
*/

View File

@@ -10,6 +10,7 @@ package org.eclipse.hawkbit.repository;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.time.format.DateTimeParseException;
import java.util.List;
import java.util.stream.Collectors;
@@ -20,10 +21,15 @@ import org.eclipse.hawkbit.cache.TenancyCacheManager;
import org.eclipse.hawkbit.im.authentication.SpPermission.SpringEvalExpressions;
import org.eclipse.hawkbit.report.model.SystemUsageReport;
import org.eclipse.hawkbit.repository.exception.EntityNotFoundException;
import org.eclipse.hawkbit.repository.exception.InvalidDistributionSetTypeException;
import org.eclipse.hawkbit.repository.exception.InvalidPollingTimeException;
import org.eclipse.hawkbit.repository.model.DistributionSetType;
import org.eclipse.hawkbit.repository.model.SoftwareModuleType;
import org.eclipse.hawkbit.repository.model.TenantConfiguration;
import org.eclipse.hawkbit.repository.model.TenantMetaData;
import org.eclipse.hawkbit.repository.model.helper.DurationHelper;
import org.eclipse.hawkbit.repository.specifications.DistributionSetTypeSpecification;
import org.eclipse.hawkbit.rest.resource.model.system.SystemConfigurationRequestBodyPut;
import org.eclipse.hawkbit.tenancy.TenantAware;
import org.eclipse.hawkbit.tenancy.configuration.TenantConfigurationKey;
import org.eclipse.persistence.config.PersistenceUnitProperties;
@@ -461,4 +467,29 @@ public class SystemManagement implements EnvironmentAware {
}
@Transactional
@Modifying
public void updateTenantConfiguration(SystemConfigurationRequestBodyPut systemConReq) {
DurationHelper dh = new DurationHelper();
TenantMetaData tenantMetaData = getTenantMetadata();
String ddstypeKey = systemConReq.getDefaultDistributionSetType();
if (distributionSetTypeRepository.findAll(DistributionSetTypeSpecification.byKey(ddstypeKey)).isEmpty()) {
throw new InvalidDistributionSetTypeException(
String.format("The specified default distribution set type %s doe not exist.", ddstypeKey));
}
try {
tenantMetaData.setPollingOverdueTime(dh.formattedStringToDuration(systemConReq.getPollingOverdueTime()));
tenantMetaData.setPollingTime(dh.formattedStringToDuration(systemConReq.getPollingTime()));
} catch (DateTimeParseException ex) {
throw new InvalidPollingTimeException(ex);
}
updateTenantMetadata(tenantMetaData);
}
}

View File

@@ -0,0 +1,40 @@
package org.eclipse.hawkbit.repository.exception;
import org.eclipse.hawkbit.exception.SpServerError;
import org.eclipse.hawkbit.exception.SpServerRtException;
/**
* This Exception is thrown, when the user wants to set a distribution set which
* does not exist,
*
*/
public class InvalidDistributionSetTypeException extends SpServerRtException {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* Creates a new CancelActionNotAllowed with
* {@link SpServerError#SP_ACTION_NOT_CANCELABLE} error.
*/
public InvalidDistributionSetTypeException() {
super(SpServerError.SP_REST_CONFIG_INVALID_DS_TYPE);
}
/**
* @param cause
* for the exception
*/
public InvalidDistributionSetTypeException(final Throwable cause) {
super(SpServerError.SP_REST_CONFIG_INVALID_DS_TYPE, cause);
}
/**
* @param message
* of the error
*/
public InvalidDistributionSetTypeException(final String message) {
super(message, SpServerError.SP_REST_CONFIG_INVALID_DS_TYPE);
}
}

View File

@@ -0,0 +1,35 @@
package org.eclipse.hawkbit.repository.exception;
import org.eclipse.hawkbit.exception.SpServerError;
import org.eclipse.hawkbit.exception.SpServerRtException;
public class InvalidPollingTimeException extends SpServerRtException {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* Creates a new CancelActionNotAllowed with
* {@link SpServerError#SP_ACTION_NOT_CANCELABLE} error.
*/
public InvalidPollingTimeException() {
super(SpServerError.SP_REST_CONFIG_POLLING_TIME_WRONG_FOMRATTED);
}
/**
* @param cause
* for the exception
*/
public InvalidPollingTimeException(final Throwable cause) {
super(SpServerError.SP_REST_CONFIG_POLLING_TIME_WRONG_FOMRATTED, cause);
}
/**
* @param message
* of the error
*/
public InvalidPollingTimeException(final String message) {
super(message, SpServerError.SP_REST_CONFIG_POLLING_TIME_WRONG_FOMRATTED);
}
}

View File

@@ -116,6 +116,8 @@ public final class RestConstants {
* The target URL mapping rest resource.
*/
public static final String TARGET_V1_REQUEST_MAPPING = BASE_V1_REQUEST_MAPPING + "/targets";
public static final String SYSTEM_V1_REQUEST_MAPPING = BASE_V1_REQUEST_MAPPING + "/system";
/**
* The software module URL mapping rest resource.
*/

View File

@@ -18,7 +18,7 @@ import com.fasterxml.jackson.annotation.JsonInclude.Include;
*/
@JsonInclude(Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class TenantConfigurationRest {
public class AuthenticationConfigurationRest {
private String key;
private String value;

View File

@@ -0,0 +1,105 @@
package org.eclipse.hawkbit.rest.resource.model.system;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
/**
* A json annotated rest model for System Configuration for PUT.
*/
@JsonInclude(Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class SystemConfigurationRequestBodyPut {
@JsonProperty
private String pollingTime;
@JsonProperty
private String pollingOverdueTime;
@JsonProperty
private String defaultDistributionSetType;
@JsonProperty
private Map<String, Object> authenticationConfiguration;
/**
* Gets the polling time.
*
* @return the polling time
*/
public String getPollingTime() {
return pollingTime;
}
/**
* Sets the polling time.
*
* @param pollingTime
* the new polling time
*/
public void setPollingTime(String pollingTime) {
this.pollingTime = pollingTime;
}
/**
* Gets the polling overdue time.
*
* @return the polling overdue time
*/
public String getPollingOverdueTime() {
return pollingOverdueTime;
}
/**
* Sets the polling overdue time.
*
* @param pollingOverdueTime
* the new polling overdue time
*/
public void setPollingOverdueTime(String pollingOverdueTime) {
this.pollingOverdueTime = pollingOverdueTime;
}
/**
* Gets the default distribution set type.
*
* @return the default distribution set type
*/
public String getDefaultDistributionSetType() {
return defaultDistributionSetType;
}
/**
* Sets the default distribution set type.
*
* @param defaultDistributionSetType
* the new default distribution set type
*/
public void setDefaultDistributionSetType(String defaultDistributionSetType) {
this.defaultDistributionSetType = defaultDistributionSetType;
}
/**
* Gets the authentication configuration.
*
* @return the authentication configuration
*/
public Map<String, Object> getAuthenticationConfiguration() {
return authenticationConfiguration;
}
/**
* Sets the authentication configuration.
*
* @param authenticationConfiguration
* the authentication configuration
*/
public void setAuthenticationConfiguration(Map<String, Object> authenticationConfiguration) {
this.authenticationConfiguration = authenticationConfiguration;
}
}

View File

@@ -0,0 +1,194 @@
package org.eclipse.hawkbit.rest.resource.model.system;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
/**
* A json annotated rest model for SysteConfiguration to RESTful API
* representation.
*
*/
@JsonInclude(Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class SystemConfigurationRest {
@JsonProperty
private String pollingTime;
@JsonProperty
private String pollingOverdueTime;
@JsonProperty
private String defaultDistributionSetType;
@JsonProperty
private String createdBy;
@JsonProperty
private String lastModifiedBy;
@JsonProperty
private Long createdAt;
@JsonProperty
private Long lastModifiedAt;
@JsonProperty
private Map<String, Object> authenticationConfiguration;
/**
* Gets the polling time.
*
* @return the polling time
*/
public String getPollingTime() {
return pollingTime;
}
/**
* Sets the polling time.
*
* @param pollingTime
* the new polling time
*/
public void setPollingTime(String pollingTime) {
this.pollingTime = pollingTime;
}
/**
* Gets the polling overdue time.
*
* @return the polling overdue time
*/
public String getPollingOverdueTime() {
return pollingOverdueTime;
}
/**
* Sets the polling overdue time.
*
* @param pollingOverdueTime
* the new polling overdue time
*/
public void setPollingOverdueTime(String pollingOverdueTime) {
this.pollingOverdueTime = pollingOverdueTime;
}
/**
* Gets the default distribution set type.
*
* @return the default distribution set type
*/
public String getDefaultDistributionSetType() {
return defaultDistributionSetType;
}
/**
* Sets the default distribution set type.
*
* @param defaultDistributionSetType
* the new default distribution set type
*/
public void setDefaultDistributionSetType(String defaultDistributionSetType) {
this.defaultDistributionSetType = defaultDistributionSetType;
}
/**
* Gets the created by.
*
* @return the created by
*/
public String getCreatedBy() {
return createdBy;
}
/**
* Sets the created by.
*
* @param createdBy
* the new created by
*/
public void setCreatedBy(String createdBy) {
this.createdBy = createdBy;
}
/**
* Gets the last modified by.
*
* @return the last modified by
*/
public String getLastModifiedBy() {
return lastModifiedBy;
}
/**
* Sets the last modified by.
*
* @param lastModifiedBy
* the new last modified by
*/
public void setLastModifiedBy(String lastModifiedBy) {
this.lastModifiedBy = lastModifiedBy;
}
/**
* Gets the created at.
*
* @return the created at
*/
public Long getCreatedAt() {
return createdAt;
}
/**
* Sets the created at.
*
* @param createdAt
* the new created at
*/
public void setCreatedAt(Long createdAt) {
this.createdAt = createdAt;
}
/**
* Gets the last modified at.
*
* @return the last modified at
*/
public Long getLastModifiedAt() {
return lastModifiedAt;
}
/**
* Sets the last modified at.
*
* @param lastModifiedAt
* the new last modified at
*/
public void setLastModifiedAt(Long lastModifiedAt) {
this.lastModifiedAt = lastModifiedAt;
}
/**
* Sets the authentication configuration.
*
* @param authenticationConfiguration
* the authentication configuration
*/
public void setAuthenticationConfiguration(Map<String, Object> authenticationConfiguration) {
this.authenticationConfiguration = authenticationConfiguration;
}
/**
* Gets the authentication configuration.
*
* @return the authentication configuration
*/
public Map<String, Object> getAuthenticationConfiguration() {
return this.authenticationConfiguration;
}
}

View File

@@ -18,9 +18,9 @@ import org.eclipse.hawkbit.report.model.SystemUsageReport;
import org.eclipse.hawkbit.report.model.TenantUsage;
import org.eclipse.hawkbit.repository.SystemManagement;
import org.eclipse.hawkbit.repository.model.TenantConfiguration;
import org.eclipse.hawkbit.rest.resource.model.system.AuthenticationConfigurationRest;
import org.eclipse.hawkbit.rest.resource.model.system.CacheRest;
import org.eclipse.hawkbit.rest.resource.model.system.SystemStatisticsRest;
import org.eclipse.hawkbit.rest.resource.model.system.TenantConfigurationRest;
import org.eclipse.hawkbit.rest.resource.model.system.TenantSystemUsageRest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -141,7 +141,7 @@ public class SystemManagementResource {
*/
@RequestMapping(method = RequestMethod.PUT, value = "/conf/{key}")
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_SYSTEM_ADMIN)
public ResponseEntity<Void> addUpdateConfig(@RequestBody final TenantConfigurationRest configuration,
public ResponseEntity<Void> addUpdateConfig(@RequestBody final AuthenticationConfigurationRest configuration,
@PathVariable final String key) {
systemManagement.addOrUpdateConfiguration(new TenantConfiguration(key, configuration.getValue()));
return ResponseEntity.ok().build();

View File

@@ -0,0 +1,87 @@
package org.eclipse.hawkbit.rest.resource;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.hawkbit.repository.DistributionSetManagement;
import org.eclipse.hawkbit.repository.SystemManagement;
import org.eclipse.hawkbit.repository.model.TenantMetaData;
import org.eclipse.hawkbit.repository.model.helper.DurationHelper;
import org.eclipse.hawkbit.rest.resource.model.system.SystemConfigurationRequestBodyPut;
import org.eclipse.hawkbit.rest.resource.model.system.SystemConfigurationRest;
import org.eclipse.hawkbit.tenancy.configuration.TenantConfigurationKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
final public class SystemMapper {
private static final Logger LOG = LoggerFactory.getLogger(SystemMapper.class);
private SystemMapper() {
// Utility class
}
private static DurationHelper dh = new DurationHelper();
public static SystemConfigurationRest toResponse(SystemManagement systemManagement) {
TenantMetaData tenantMetaData = systemManagement.getTenantMetadata();
SystemConfigurationRest sysconf = new SystemConfigurationRest();
sysconf.setDefaultDistributionSetType(tenantMetaData.getDefaultDsType().getKey());
sysconf.setCreatedAt(tenantMetaData.getCreatedAt());
sysconf.setCreatedBy(tenantMetaData.getCreatedBy());
sysconf.setLastModifiedAt(tenantMetaData.getLastModifiedAt());
sysconf.setLastModifiedBy(tenantMetaData.getLastModifiedBy());
sysconf.setPollingOverdueTime(dh.durationToFormattedString(tenantMetaData.getPollingOverdueTime()));
sysconf.setPollingTime(dh.durationToFormattedString(tenantMetaData.getPollingTime()));
Map<String, Object> authconf = new HashMap<String, Object>();
for (TenantConfigurationKey key : TenantConfigurationKey.values()) {
Object value;
switch (key) {
case AUTHENTICATION_MODE_HEADER_ENABLED:
case AUTHENTICATION_MODE_TARGET_SECURITY_TOKEN_ENABLED:
case AUTHENTICATION_MODE_GATEWAY_SECURITY_TOKEN_ENABLED:
value = systemManagement.getConfigurationValue(key, Boolean.class);
break;
case AUTHENTICATION_MODE_GATEWAY_SECURITY_TOKEN_NAME:
case AUTHENTICATION_MODE_GATEWAY_SECURITY_TOKEN_KEY:
case AUTHENTICATION_MODE_HEADER_AUTHORITY_NAME:
value = systemManagement.getConfigurationValue(key, String.class);
break;
default:
LOG.warn("There is no data type specified for TenantConfigurationKey {}.", key.getKeyName());
value = systemManagement.getConfigurationValue(key, String.class);
}
authconf.put(key.getKeyName(), value);
}
sysconf.setAuthenticationConfiguration(authconf);
return sysconf;
}
public static TenantMetaData fromRequest(SystemManagement systemManagement,
SystemConfigurationRequestBodyPut systemConReq, DistributionSetManagement distributionSetManagement) {
TenantMetaData tenantMetaData = systemManagement.getTenantMetadata();
String ddstypeKey = systemConReq.getDefaultDistributionSetType();
if (distributionSetManagement.findDistributionSetTypeByKey(ddstypeKey) == null) {
throw new IllegalArgumentException(
String.format("The specified default distribution set type %s doe not exist.", ddstypeKey));
}
tenantMetaData.setPollingOverdueTime(dh.formattedStringToDuration(systemConReq.getPollingOverdueTime()));
tenantMetaData.setPollingTime(dh.formattedStringToDuration(systemConReq.getPollingTime()));
return null;
}
}

View File

@@ -0,0 +1,47 @@
package org.eclipse.hawkbit.rest.resource;
import org.eclipse.hawkbit.repository.DistributionSetManagement;
import org.eclipse.hawkbit.repository.SystemManagement;
import org.eclipse.hawkbit.rest.resource.model.system.SystemConfigurationRequestBodyPut;
import org.eclipse.hawkbit.rest.resource.model.system.SystemConfigurationRest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(RestConstants.SYSTEM_V1_REQUEST_MAPPING)
public class SystemResource {
private static final Logger LOGGER = LoggerFactory.getLogger(SystemResource.class);
@Autowired
private SystemManagement systemManagement;
@Autowired
private DistributionSetManagement distributionSetManagement;
@RequestMapping(method = RequestMethod.GET, value = "/conf", produces = { "application/hal+json",
MediaType.APPLICATION_JSON_VALUE })
public ResponseEntity<SystemConfigurationRest> getSystemConfiguration() {
return new ResponseEntity<>(SystemMapper.toResponse(systemManagement), HttpStatus.OK);
}
@RequestMapping(method = RequestMethod.PUT, value = "/conf", consumes = { "application/hal+json",
MediaType.APPLICATION_JSON_VALUE }, produces = { "application/hal+json", MediaType.APPLICATION_JSON_VALUE })
public ResponseEntity<SystemConfigurationRest> updateSoftwareModuleType(
@RequestBody final SystemConfigurationRequestBodyPut systemConReq) {
systemManagement.updateTenantConfiguration(systemConReq);
return new ResponseEntity<>(SystemMapper.toResponse(systemManagement), HttpStatus.OK);
}
}