mirror of
https://github.com/DSpace/DSpace.git
synced 2025-10-17 06:53:09 +00:00
CST-9634 Assure that REST API assumes all dates to be UTC if not specified
This commit is contained in:
@@ -7,7 +7,6 @@
|
|||||||
*/
|
*/
|
||||||
package org.dspace.app.rest.repository.patch.operation.resourcePolicy;
|
package org.dspace.app.rest.repository.patch.operation.resourcePolicy;
|
||||||
|
|
||||||
import java.text.ParseException;
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
import org.dspace.app.rest.exception.DSpaceBadRequestException;
|
import org.dspace.app.rest.exception.DSpaceBadRequestException;
|
||||||
@@ -15,6 +14,7 @@ import org.dspace.app.rest.model.patch.Operation;
|
|||||||
import org.dspace.app.rest.repository.patch.operation.PatchOperation;
|
import org.dspace.app.rest.repository.patch.operation.PatchOperation;
|
||||||
import org.dspace.authorize.ResourcePolicy;
|
import org.dspace.authorize.ResourcePolicy;
|
||||||
import org.dspace.core.Context;
|
import org.dspace.core.Context;
|
||||||
|
import org.dspace.util.MultiFormatDateParser;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
@@ -58,11 +58,10 @@ public class ResourcePolicyEndDateAddOperation<R> extends PatchOperation<R> {
|
|||||||
*/
|
*/
|
||||||
private void add(ResourcePolicy resourcePolicy, Operation operation) {
|
private void add(ResourcePolicy resourcePolicy, Operation operation) {
|
||||||
String dateS = (String) operation.getValue();
|
String dateS = (String) operation.getValue();
|
||||||
try {
|
Date date = MultiFormatDateParser.parse(dateS);
|
||||||
Date date = resourcePolicyUtils.simpleDateFormat.parse(dateS);
|
resourcePolicy.setEndDate(date);
|
||||||
resourcePolicy.setEndDate(date);
|
if (date == null) {
|
||||||
} catch (ParseException e) {
|
throw new DSpaceBadRequestException("Invalid endDate value " + dateS);
|
||||||
throw new DSpaceBadRequestException("Invalid endDate value", e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -7,7 +7,6 @@
|
|||||||
*/
|
*/
|
||||||
package org.dspace.app.rest.repository.patch.operation.resourcePolicy;
|
package org.dspace.app.rest.repository.patch.operation.resourcePolicy;
|
||||||
|
|
||||||
import java.text.ParseException;
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
import org.dspace.app.rest.exception.DSpaceBadRequestException;
|
import org.dspace.app.rest.exception.DSpaceBadRequestException;
|
||||||
@@ -15,6 +14,7 @@ import org.dspace.app.rest.model.patch.Operation;
|
|||||||
import org.dspace.app.rest.repository.patch.operation.PatchOperation;
|
import org.dspace.app.rest.repository.patch.operation.PatchOperation;
|
||||||
import org.dspace.authorize.ResourcePolicy;
|
import org.dspace.authorize.ResourcePolicy;
|
||||||
import org.dspace.core.Context;
|
import org.dspace.core.Context;
|
||||||
|
import org.dspace.util.MultiFormatDateParser;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
@@ -58,12 +58,11 @@ public class ResourcePolicyEndDateReplaceOperation<R> extends PatchOperation<R>
|
|||||||
*/
|
*/
|
||||||
private void replace(ResourcePolicy resourcePolicy, Operation operation) {
|
private void replace(ResourcePolicy resourcePolicy, Operation operation) {
|
||||||
String dateS = (String) operation.getValue();
|
String dateS = (String) operation.getValue();
|
||||||
try {
|
Date date = MultiFormatDateParser.parse(dateS);
|
||||||
Date date = resourcePolicyUtils.simpleDateFormat.parse(dateS);
|
if (date == null) {
|
||||||
resourcePolicy.setEndDate(date);
|
throw new DSpaceBadRequestException("Invalid endDate value " + dateS);
|
||||||
} catch (ParseException e) {
|
|
||||||
throw new DSpaceBadRequestException("Invalid endDate value", e);
|
|
||||||
}
|
}
|
||||||
|
resourcePolicy.setEndDate(date);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@@ -7,7 +7,6 @@
|
|||||||
*/
|
*/
|
||||||
package org.dspace.app.rest.repository.patch.operation.resourcePolicy;
|
package org.dspace.app.rest.repository.patch.operation.resourcePolicy;
|
||||||
|
|
||||||
import java.text.ParseException;
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
import org.dspace.app.rest.exception.DSpaceBadRequestException;
|
import org.dspace.app.rest.exception.DSpaceBadRequestException;
|
||||||
@@ -15,6 +14,7 @@ import org.dspace.app.rest.model.patch.Operation;
|
|||||||
import org.dspace.app.rest.repository.patch.operation.PatchOperation;
|
import org.dspace.app.rest.repository.patch.operation.PatchOperation;
|
||||||
import org.dspace.authorize.ResourcePolicy;
|
import org.dspace.authorize.ResourcePolicy;
|
||||||
import org.dspace.core.Context;
|
import org.dspace.core.Context;
|
||||||
|
import org.dspace.util.MultiFormatDateParser;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
@@ -59,12 +59,11 @@ public class ResourcePolicyStartDateAddOperation<R> extends PatchOperation<R> {
|
|||||||
*/
|
*/
|
||||||
private void add(ResourcePolicy resourcePolicy, Operation operation) {
|
private void add(ResourcePolicy resourcePolicy, Operation operation) {
|
||||||
String dateS = (String) operation.getValue();
|
String dateS = (String) operation.getValue();
|
||||||
try {
|
Date date = MultiFormatDateParser.parse(dateS);
|
||||||
Date date = resourcePolicyUtils.simpleDateFormat.parse(dateS);
|
if (date == null) {
|
||||||
resourcePolicy.setStartDate(date);
|
throw new DSpaceBadRequestException("Invalid startDate value " + dateS);
|
||||||
} catch (ParseException e) {
|
|
||||||
throw new DSpaceBadRequestException("Invalid startDate value", e);
|
|
||||||
}
|
}
|
||||||
|
resourcePolicy.setStartDate(date);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@@ -7,7 +7,6 @@
|
|||||||
*/
|
*/
|
||||||
package org.dspace.app.rest.repository.patch.operation.resourcePolicy;
|
package org.dspace.app.rest.repository.patch.operation.resourcePolicy;
|
||||||
|
|
||||||
import java.text.ParseException;
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
import org.dspace.app.rest.exception.DSpaceBadRequestException;
|
import org.dspace.app.rest.exception.DSpaceBadRequestException;
|
||||||
@@ -15,6 +14,7 @@ import org.dspace.app.rest.model.patch.Operation;
|
|||||||
import org.dspace.app.rest.repository.patch.operation.PatchOperation;
|
import org.dspace.app.rest.repository.patch.operation.PatchOperation;
|
||||||
import org.dspace.authorize.ResourcePolicy;
|
import org.dspace.authorize.ResourcePolicy;
|
||||||
import org.dspace.core.Context;
|
import org.dspace.core.Context;
|
||||||
|
import org.dspace.util.MultiFormatDateParser;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
@@ -58,12 +58,11 @@ public class ResourcePolicyStartDateReplaceOperation<R> extends PatchOperation<R
|
|||||||
*/
|
*/
|
||||||
private void replace(ResourcePolicy resourcePolicy, Operation operation) {
|
private void replace(ResourcePolicy resourcePolicy, Operation operation) {
|
||||||
String dateS = (String) operation.getValue();
|
String dateS = (String) operation.getValue();
|
||||||
try {
|
Date date = MultiFormatDateParser.parse(dateS);
|
||||||
Date date = resourcePolicyUtils.simpleDateFormat.parse(dateS);
|
if (date == null) {
|
||||||
resourcePolicy.setStartDate(date);
|
throw new DSpaceBadRequestException("Invalid startDate value " + dateS);
|
||||||
} catch (ParseException e) {
|
|
||||||
throw new DSpaceBadRequestException("Invalid startDate value", e);
|
|
||||||
}
|
}
|
||||||
|
resourcePolicy.setStartDate(date);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@@ -7,13 +7,12 @@
|
|||||||
*/
|
*/
|
||||||
package org.dspace.app.rest.repository.patch.operation.resourcePolicy;
|
package org.dspace.app.rest.repository.patch.operation.resourcePolicy;
|
||||||
|
|
||||||
import java.text.ParseException;
|
|
||||||
import java.text.SimpleDateFormat;
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
import org.dspace.app.rest.exception.DSpaceBadRequestException;
|
import org.dspace.app.rest.exception.DSpaceBadRequestException;
|
||||||
import org.dspace.app.rest.model.patch.Operation;
|
import org.dspace.app.rest.model.patch.Operation;
|
||||||
import org.dspace.authorize.ResourcePolicy;
|
import org.dspace.authorize.ResourcePolicy;
|
||||||
|
import org.dspace.util.MultiFormatDateParser;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -25,8 +24,6 @@ import org.springframework.stereotype.Component;
|
|||||||
@Component
|
@Component
|
||||||
public class ResourcePolicyUtils {
|
public class ResourcePolicyUtils {
|
||||||
|
|
||||||
public static final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Paths in json body of patched that use these resourcePolicy operations
|
* Paths in json body of patched that use these resourcePolicy operations
|
||||||
*/
|
*/
|
||||||
@@ -112,13 +109,12 @@ public class ResourcePolicyUtils {
|
|||||||
*/
|
*/
|
||||||
public void checkResourcePolicyForConsistentStartDateValue(ResourcePolicy resource, Operation operation) {
|
public void checkResourcePolicyForConsistentStartDateValue(ResourcePolicy resource, Operation operation) {
|
||||||
String dateS = (String) operation.getValue();
|
String dateS = (String) operation.getValue();
|
||||||
try {
|
Date date = MultiFormatDateParser.parse(dateS);
|
||||||
Date date = simpleDateFormat.parse(dateS);
|
if (date == null) {
|
||||||
if (resource.getEndDate() != null && resource.getEndDate().before(date)) {
|
throw new DSpaceBadRequestException("Invalid startDate value " + dateS);
|
||||||
throw new DSpaceBadRequestException("Attempting to set an invalid startDate greater than the endDate.");
|
}
|
||||||
}
|
if (resource.getEndDate() != null && resource.getEndDate().before(date)) {
|
||||||
} catch (ParseException e) {
|
throw new DSpaceBadRequestException("Attempting to set an invalid startDate greater than the endDate.");
|
||||||
throw new DSpaceBadRequestException("Invalid startDate value", e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -134,13 +130,12 @@ public class ResourcePolicyUtils {
|
|||||||
*/
|
*/
|
||||||
public void checkResourcePolicyForConsistentEndDateValue(ResourcePolicy resource, Operation operation) {
|
public void checkResourcePolicyForConsistentEndDateValue(ResourcePolicy resource, Operation operation) {
|
||||||
String dateS = (String) operation.getValue();
|
String dateS = (String) operation.getValue();
|
||||||
try {
|
Date date = MultiFormatDateParser.parse(dateS);
|
||||||
Date date = simpleDateFormat.parse(dateS);
|
if (date == null) {
|
||||||
if (resource.getStartDate() != null && resource.getStartDate().after(date)) {
|
throw new DSpaceBadRequestException("Invalid endDate value " + dateS);
|
||||||
throw new DSpaceBadRequestException("Attempting to set an invalid endDate smaller than the startDate.");
|
}
|
||||||
}
|
if (resource.getStartDate() != null && resource.getStartDate().after(date)) {
|
||||||
} catch (ParseException e) {
|
throw new DSpaceBadRequestException("Attempting to set an invalid endDate smaller than the startDate.");
|
||||||
throw new DSpaceBadRequestException("Invalid endDate value", e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -8,8 +8,6 @@
|
|||||||
package org.dspace.app.rest.submit.factory.impl;
|
package org.dspace.app.rest.submit.factory.impl;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
import java.text.SimpleDateFormat;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
@@ -29,6 +27,7 @@ import org.dspace.core.Context;
|
|||||||
import org.dspace.submit.model.AccessConditionConfiguration;
|
import org.dspace.submit.model.AccessConditionConfiguration;
|
||||||
import org.dspace.submit.model.AccessConditionConfigurationService;
|
import org.dspace.submit.model.AccessConditionConfigurationService;
|
||||||
import org.dspace.submit.model.AccessConditionOption;
|
import org.dspace.submit.model.AccessConditionOption;
|
||||||
|
import org.dspace.util.MultiFormatDateParser;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@@ -145,20 +144,12 @@ public class AccessConditionReplacePatchOperation extends ReplacePatchOperation<
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Date parseDate(String date) {
|
private Date parseDate(String dateString) {
|
||||||
List<SimpleDateFormat> knownPatterns = Arrays.asList(
|
Date date = MultiFormatDateParser.parse(dateString);
|
||||||
new SimpleDateFormat("yyyy-MM-dd"),
|
if (date == null) {
|
||||||
new SimpleDateFormat("dd-MM-yyyy"),
|
throw new UnprocessableEntityException("Provided format of date:" + dateString + " is not supported!");
|
||||||
new SimpleDateFormat("yyyy/MM/dd"),
|
|
||||||
new SimpleDateFormat("dd/MM/yyyy"));
|
|
||||||
for (SimpleDateFormat pattern : knownPatterns) {
|
|
||||||
try {
|
|
||||||
return pattern.parse(date);
|
|
||||||
} catch (ParseException e) {
|
|
||||||
log.error(e.getMessage(), e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
throw new UnprocessableEntityException("Provided format of date:" + date + " is not supported!");
|
return date;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getValue(Object value) {
|
private String getValue(Object value) {
|
||||||
|
Reference in New Issue
Block a user