Merge branch 'master' into fix_wrong_error_response

This commit is contained in:
kaizimmerm
2016-09-14 09:26:24 +02:00
62 changed files with 259 additions and 1298 deletions

View File

@@ -11,6 +11,7 @@ package org.eclipse.hawkbit.repository;
import java.net.URI;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import javax.validation.constraints.NotNull;
@@ -19,7 +20,7 @@ import org.eclipse.hawkbit.repository.eventbus.event.DownloadProgressEvent;
import org.eclipse.hawkbit.repository.exception.EntityAlreadyExistsException;
import org.eclipse.hawkbit.repository.exception.EntityNotFoundException;
import org.eclipse.hawkbit.repository.exception.ToManyAttributeEntriesException;
import org.eclipse.hawkbit.repository.exception.ToManyStatusEntriesException;
import org.eclipse.hawkbit.repository.exception.TooManyStatusEntriesException;
import org.eclipse.hawkbit.repository.model.Action;
import org.eclipse.hawkbit.repository.model.Action.Status;
import org.eclipse.hawkbit.repository.model.ActionStatus;
@@ -95,7 +96,7 @@ public interface ControllerManagement {
*
* @throws EntityAlreadyExistsException
* if a given entity already exists
* @throws ToManyStatusEntriesException
* @throws TooManyStatusEntriesException
* if more than the allowed number of status entries are
* inserted
*/
@@ -111,7 +112,18 @@ public interface ControllerManagement {
* @return a list of actions assigned to given target which are active
*/
@PreAuthorize(SpringEvalExpressions.IS_CONTROLLER)
List<Action> findActionByTargetAndActive(@NotNull Target target);
List<Action> findActiveActionByTarget(@NotNull Target target);
/**
* Retrieves oldest {@link Action} that is active and assigned to a
* {@link Target}.
*
* @param target
* the target to retrieve the actions from
* @return a list of actions assigned to given target which are active
*/
@PreAuthorize(SpringEvalExpressions.IS_CONTROLLER)
Optional<Action> findOldestActiveActionByTarget(@NotNull Target target);
/**
* Get the {@link Action} entity for given actionId with all lazy

View File

@@ -18,7 +18,7 @@ import org.eclipse.hawkbit.exception.AbstractServerRtException;
*
*
*/
public final class ToManyStatusEntriesException extends AbstractServerRtException {
public final class TooManyStatusEntriesException extends AbstractServerRtException {
/**
*
*/
@@ -28,7 +28,7 @@ public final class ToManyStatusEntriesException extends AbstractServerRtExceptio
* Creates a new FileUploadFailedException with
* {@link SpServerError#SP_REST_BODY_NOT_READABLE} error.
*/
public ToManyStatusEntriesException() {
public TooManyStatusEntriesException() {
super(SpServerError.SP_ACTION_STATUS_TO_MANY_ENTRIES);
}
@@ -36,7 +36,7 @@ public final class ToManyStatusEntriesException extends AbstractServerRtExceptio
* @param cause
* for the exception
*/
public ToManyStatusEntriesException(final Throwable cause) {
public TooManyStatusEntriesException(final Throwable cause) {
super(SpServerError.SP_ACTION_STATUS_TO_MANY_ENTRIES, cause);
}
@@ -44,7 +44,7 @@ public final class ToManyStatusEntriesException extends AbstractServerRtExceptio
* @param message
* of the error
*/
public ToManyStatusEntriesException(final String message) {
public TooManyStatusEntriesException(final String message) {
super(message, SpServerError.SP_ACTION_STATUS_TO_MANY_ENTRIES);
}
}

View File

@@ -10,6 +10,7 @@ package org.eclipse.hawkbit.repository.jpa;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import org.eclipse.hawkbit.repository.jpa.model.JpaAction;
import org.eclipse.hawkbit.repository.jpa.model.JpaDistributionSet;
@@ -79,21 +80,31 @@ public interface ActionRepository extends BaseEntityRepository<JpaAction, Long>,
Slice<Action> findByTarget(Pageable pageable, JpaTarget target);
/**
* Retrieves all {@link Action}s which are active and referring the given
* {@link Target} in a specified order. Loads also the lazy
* {@link Action#getDistributionSet()} field.
* Retrieves all {@link Action}s which are active and referring to the given
* {@link Target} order by ID ascending.
*
* @param pageable
* page parameters
* @param target
* the target to find assigned actions
* @param active
* the action active flag
* @return the found {@link Action}s
*/
@EntityGraph(value = "Action.ds", type = EntityGraphType.LOAD)
List<Action> findByTargetAndActiveOrderByIdAsc(final JpaTarget target, boolean active);
/**
* Retrieves the oldest {@link Action} that is active and referring to the
* given {@link Target}.
*
* @param target
* the target to find assigned actions
* @param active
* the action active flag
*
* @return the found {@link Action}
*/
@EntityGraph(value = "Action.ds", type = EntityGraphType.LOAD)
Optional<Action> findFirstByTargetAndActiveOrderByIdAsc(final JpaTarget target, boolean active);
/**
* Retrieves latest {@link UpdateAction} for given target and
* {@link SoftwareModule}.

View File

@@ -12,6 +12,7 @@ import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder;
@@ -26,7 +27,7 @@ import org.eclipse.hawkbit.repository.TargetManagement;
import org.eclipse.hawkbit.repository.TenantConfigurationManagement;
import org.eclipse.hawkbit.repository.exception.EntityNotFoundException;
import org.eclipse.hawkbit.repository.exception.ToManyAttributeEntriesException;
import org.eclipse.hawkbit.repository.exception.ToManyStatusEntriesException;
import org.eclipse.hawkbit.repository.exception.TooManyStatusEntriesException;
import org.eclipse.hawkbit.repository.jpa.cache.CacheWriteNotify;
import org.eclipse.hawkbit.repository.jpa.model.JpaAction;
import org.eclipse.hawkbit.repository.jpa.model.JpaActionStatus;
@@ -156,10 +157,15 @@ public class JpaControllerManagement implements ControllerManagement {
}
@Override
public List<Action> findActionByTargetAndActive(final Target target) {
public List<Action> findActiveActionByTarget(final Target target) {
return actionRepository.findByTargetAndActiveOrderByIdAsc((JpaTarget) target, true);
}
@Override
public Optional<Action> findOldestActiveActionByTarget(final Target target) {
return actionRepository.findFirstByTargetAndActiveOrderByIdAsc((JpaTarget) target, true);
}
@Override
public List<SoftwareModule> findSoftwareModulesByDistributionSet(final DistributionSet distributionSet) {
return new ArrayList<>(softwareModuleRepository.findByAssignedTo((JpaDistributionSet) distributionSet));
@@ -326,7 +332,7 @@ public class JpaControllerManagement implements ControllerManagement {
LOG_DOS.error(
"Potential denial of service (DOS) attack identfied. More status entries in the system than permitted ({})!",
securityProperties.getDos().getMaxStatusEntriesPerAction());
throw new ToManyStatusEntriesException(
throw new TooManyStatusEntriesException(
String.valueOf(securityProperties.getDos().getMaxStatusEntriesPerAction()));
}
}

View File

@@ -186,8 +186,8 @@ public class JpaAction extends AbstractJpaTenantAwareBaseEntity implements Actio
@Override
public void fireUpdateEvent(final DescriptorEvent descriptorEvent) {
EventBusHolder.getInstance().getEventBus().post(new ActionPropertyChangeEvent(this,
EntityPropertyChangeHelper.getChangeSet(Action.class, descriptorEvent)));
EventBusHolder.getInstance().getEventBus()
.post(new ActionPropertyChangeEvent(this, EntityPropertyChangeHelper.getChangeSet(descriptorEvent)));
}
@Override

View File

@@ -73,8 +73,6 @@ import org.eclipse.persistence.descriptors.DescriptorEvent;
public class JpaDistributionSet extends AbstractJpaNamedVersionedEntity implements DistributionSet, EventAwareEntity {
private static final long serialVersionUID = 1L;
private static final String COMPLETE_PROPERTY = "complete";
private static final String DELETED_PROPERTY = "deleted";
@Column(name = "required_migration_step")
@@ -301,8 +299,7 @@ public class JpaDistributionSet extends AbstractJpaNamedVersionedEntity implemen
@Override
public void fireUpdateEvent(final DescriptorEvent descriptorEvent) {
final Map<String, PropertyChange> changeSet = EntityPropertyChangeHelper.getChangeSet(JpaDistributionSet.class,
descriptorEvent);
final Map<String, PropertyChange> changeSet = EntityPropertyChangeHelper.getChangeSet(descriptorEvent);
EventBusHolder.getInstance().getEventBus().post(new DistributionSetUpdateEvent(this));
if (changeSet.containsKey(DELETED_PROPERTY)) {

View File

@@ -214,8 +214,8 @@ public class JpaRollout extends AbstractJpaNamedEntity implements Rollout, Event
@Override
public void fireUpdateEvent(final DescriptorEvent descriptorEvent) {
EventBusHolder.getInstance().getEventBus().post(new RolloutPropertyChangeEvent(this,
EntityPropertyChangeHelper.getChangeSet(Rollout.class, descriptorEvent)));
EventBusHolder.getInstance().getEventBus()
.post(new RolloutPropertyChangeEvent(this, EntityPropertyChangeHelper.getChangeSet(descriptorEvent)));
}

View File

@@ -252,8 +252,8 @@ public class JpaRolloutGroup extends AbstractJpaNamedEntity implements RolloutGr
@Override
public void fireUpdateEvent(final DescriptorEvent descriptorEvent) {
EventBusHolder.getInstance().getEventBus().post(new RolloutGroupPropertyChangeEvent(this,
EntityPropertyChangeHelper.getChangeSet(RolloutGroup.class, descriptorEvent)));
EventBusHolder.getInstance().getEventBus().post(
new RolloutGroupPropertyChangeEvent(this, EntityPropertyChangeHelper.getChangeSet(descriptorEvent)));
}
@Override

View File

@@ -12,7 +12,6 @@ import java.util.Map;
import java.util.stream.Collectors;
import org.eclipse.hawkbit.repository.eventbus.event.AbstractPropertyChangeEvent.PropertyChange;
import org.eclipse.hawkbit.repository.model.TenantAwareBaseEntity;
import org.eclipse.persistence.descriptors.DescriptorEvent;
import org.eclipse.persistence.internal.sessions.ObjectChangeSet;
import org.eclipse.persistence.queries.UpdateObjectQuery;
@@ -21,9 +20,12 @@ import org.eclipse.persistence.sessions.changesets.DirectToFieldChangeRecord;
/**
* Helper class to get the change set for the property changes in the Entity.
*
* @param <T>
*/
public class EntityPropertyChangeHelper<T extends TenantAwareBaseEntity> {
public final class EntityPropertyChangeHelper {
private EntityPropertyChangeHelper() {
// noop
}
/**
* To get the map of entity property change set
@@ -32,14 +34,11 @@ public class EntityPropertyChangeHelper<T extends TenantAwareBaseEntity> {
* @param event
* @return the map of the changeSet
*/
public static <T extends TenantAwareBaseEntity> Map<String, PropertyChange> getChangeSet(final Class<T> clazz,
final DescriptorEvent event) {
final T rolloutGroup = clazz.cast(event.getObject());
public static Map<String, PropertyChange> getChangeSet(final DescriptorEvent event) {
final ObjectChangeSet changeSet = ((UpdateObjectQuery) event.getQuery()).getObjectChangeSet();
return changeSet.getChanges().stream().filter(record -> record instanceof DirectToFieldChangeRecord)
.map(record -> (DirectToFieldChangeRecord) record)
.collect(Collectors.toMap(record -> record.getAttribute(),
record -> new PropertyChange(record.getOldValue(), record.getNewValue())));
}
}