Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restrict #411

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@
<spring-cloud-sleuth.version>2.2.8.RELEASE</spring-cloud-sleuth.version>

<!-- GBIF -->
<gbif-api.version>0.163</gbif-api.version>
<gbif-api.version>0.165</gbif-api.version>
<gbif-common.version>0.52</gbif-common.version>
<gbif-common-mybatis.version>1.1</gbif-common-mybatis.version>
<gbif-common-ws.version>1.19</gbif-common-ws.version>
<gbif-directory.version>1.12</gbif-directory.version>
<gbif-doi.version>2.16</gbif-doi.version>
<gbif-doi.version>2.17</gbif-doi.version>
<gbif-download-query-tools.version>1.45</gbif-download-query-tools.version>
<gbif-httputils.version>0.10</gbif-httputils.version>
<gbif-metrics.version>1.2</gbif-metrics.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public class DatasetDoiDataCiteHandlingServiceStub implements DatasetDoiDataCite
@Override
public void datasetChanged(Dataset dataset, @Nullable DOI previousDoi) {}

@Override
public void datasetDeleted(DOI doi) {}

@Override
public void scheduleDatasetRegistration(DOI doi, DataCiteMetadata metadata, UUID datasetKey) {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public void handleMessage(ChangeDoiMessage msg) {
if (HttpStatus.SC_REQUEST_TOO_LONG == e.getStatus()) {
LOG.warn(
DOI_SMTP,
"Metadata of length {} is exceeding max datacite limit in attempt #{} "
"Metadata of length {} is exceeding max DataCite limit in attempt #{} "
+ "while updating {} to {} with target {}. "
+ "Trying again {}",
msg.getMetadata().length(),
Expand Down Expand Up @@ -206,14 +206,19 @@ private void reserve(DOI doi, String xml, DoiData currState) throws DoiException
* @param currState current state of the DOI in the database
*/
private void delete(DOI doi, DoiData currState) throws DoiException {
// delete from DataCite
// findable DOIs will not be deleted
if (doiService.exists(doi)) {
doiService.delete(doi);
}

if (currState.getStatus() == DoiStatus.REGISTERED) {
// if registered - mark as deleted
DoiData newState = new DoiData(DoiStatus.DELETED, currState.getTarget());
doiMapper.update(doi, newState, null);
LOG.info("Marked registered doi {} as deleted", doi);
} else {
if (doiService.exists(doi)) {
doiService.delete(doi);
}
// otherwise, erase it from database
doiMapper.delete(doi);
LOG.info("Deleted doi {}", doi);
}
Expand Down Expand Up @@ -269,7 +274,7 @@ private void registerOrUpdate(DOI doi, URI target, String xml, DoiData currState
* to fix a RESERVED DOI that should be updated (will be rejected). As opposed to
* registerOrUpdate, this method will ask the doiService for the status of the DOI since when the
* status is FAILED we loose the 'real' status before the failure. If the DOI doesn't exist on the
* DOI Service (e.g. Datacite) it will register it. If the DOI already exist it will try an
* DOI Service (e.g. DataCite) it will register it. If the DOI already exist it will try an
* update. If any error occurs it will be logged and the will method with false.
*
* @param doi Digital Object Identifier
Expand All @@ -288,7 +293,7 @@ private boolean retryRegisterOrUpdate(DOI doi, URI target, String xml) throws Do
LOG.info("Updated doi {} with target {}", doi, target);
} else {
LOG.info(
"Failed to update doi {} with target {}. Only doi with state REGISTERED can be retried. Datacite status: {}. ",
"Failed to update doi {} with target {}. Only doi with state REGISTERED can be retried. DataCite status: {}. ",
doi,
target,
doiServiceData.getStatus());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import io.zonky.test.db.postgres.junit5.EmbeddedPostgresExtension;
import io.zonky.test.db.postgres.junit5.PreparedDbExtension;

import static org.gbif.api.model.common.DoiStatus.DELETED;
import static org.gbif.api.model.common.DoiStatus.FAILED;
import static org.gbif.api.model.common.DoiStatus.NEW;
import static org.gbif.api.model.common.DoiStatus.REGISTERED;
Expand Down Expand Up @@ -183,7 +184,7 @@ public void handleMessageRegisteredDoiAndMessageStatusDeletedShouldMarkDoiAsDele

// then
assertEquals(new DoiData(DoiStatus.DELETED, TEST_TARGET), getActualInDb(doi));
assertEquals(new DoiData(REGISTERED, TEST_TARGET), getActualInDataCite(doi));
assertEquals(new DoiData(DELETED, TEST_TARGET), getActualInDataCite(doi));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public interface DatasetDoiDataCiteHandlingService {
*/
void datasetChanged(Dataset dataset, @Nullable final DOI previousDoi);

/**
* Called when dataset to be deleted, tries to deactivate DOI.
*/
void datasetDeleted(DOI doi);

/**
* Directly schedule the registration of a Dataset DOI.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ public void datasetChanged(Dataset dataset, @Nullable DOI previousDoi) {
}
}

@Override
public void datasetDeleted(DOI doi) {
doiMessageManagingService.delete(doi);
}

@Override
public void scheduleDatasetRegistration(DOI doi, DataCiteMetadata metadata, UUID datasetKey) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,15 @@ public class BaseNetworkEntityResource<T extends NetworkEntity> implements Netwo
private final WithMyBatis withMyBatis;
private final Class<T> objectClass;

private final RestrictionsHandler restrictionsHandler;

protected BaseNetworkEntityResource(
BaseNetworkEntityMapper<T> mapper,
MapperServiceLocator mapperServiceLocator,
Class<T> objectClass,
EventManager eventManager,
WithMyBatis withMyBatis) {
BaseNetworkEntityMapper<T> mapper,
MapperServiceLocator mapperServiceLocator,
Class<T> objectClass,
EventManager eventManager,
WithMyBatis withMyBatis,
RestrictionsHandler restrictionsHandler) {
this.mapper = mapper;
this.commentMapper = mapperServiceLocator.getCommentMapper();
this.machineTagMapper = mapperServiceLocator.getMachineTagMapper();
Expand All @@ -129,6 +132,7 @@ protected BaseNetworkEntityResource(
this.objectClass = objectClass;
this.eventManager = eventManager;
this.withMyBatis = withMyBatis;
this.restrictionsHandler = restrictionsHandler;
}

/**
Expand Down Expand Up @@ -643,4 +647,8 @@ protected <D> PagingResponse<D> pagingResponse(Pageable page, Long count, List<D
}
return new PagingResponse<>(page, count, result);
}

public RestrictionsHandler getRestrictionsHandler() {
return restrictionsHandler;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ public class DatasetResource extends BaseNetworkEntityResource<Dataset>
private final TagMapper tagMapper;
private final NetworkMapper networkMapper;
private final DatasetProcessStatusMapper datasetProcessStatusMapper;
private final RestrictionsHandler restrictionsHandler;
private final DatasetDoiDataCiteHandlingService doiDataCiteHandlingService;
private final DataCiteMetadataBuilderService metadataBuilderService;
private final DoiIssuingService doiIssuingService;
Expand All @@ -169,13 +170,15 @@ public DatasetResource(
DataCiteMetadataBuilderService metadataBuilderService,
DoiIssuingService doiIssuingService,
WithMyBatis withMyBatis,
@Autowired(required = false) MessagePublisher messagePublisher) {
@Autowired(required = false) MessagePublisher messagePublisher,
RestrictionsHandler restrictionsHandler) {
super(
mapperServiceLocator.getDatasetMapper(),
mapperServiceLocator,
Dataset.class,
eventManager,
withMyBatis);
withMyBatis,
restrictionsHandler);
this.registryDatasetService = registryDatasetService;
this.searchService = searchService;
this.metadataMapper = mapperServiceLocator.getMetadataMapper();
Expand All @@ -190,6 +193,7 @@ public DatasetResource(
this.doiIssuingService = doiIssuingService;
this.messagePublisher = messagePublisher;
this.withMyBatis = withMyBatis;
this.restrictionsHandler = restrictionsHandler;
}

@GetMapping("search")
Expand Down Expand Up @@ -310,6 +314,19 @@ public byte[] getMetadataDocumentAsBytes(@PathVariable("key") UUID datasetKey) {
return null;
}

@DeleteMapping("{key}")
@Secured({ADMIN_ROLE, EDITOR_ROLE, IPT_ROLE})
@Transactional
@Override
public void delete(@PathVariable UUID key) {
Dataset dataset = get(key);
super.delete(key);

if (dataset != null && dataset.getDoi() != null) {
doiDataCiteHandlingService.datasetDeleted(dataset.getDoi());
}
}

@PostMapping(value = "{key}/document", consumes = MediaType.APPLICATION_XML_VALUE)
@Secured({ADMIN_ROLE, EDITOR_ROLE})
public Metadata insertMetadata(
Expand Down Expand Up @@ -444,6 +461,9 @@ private Dataset preserveGBIFDatasetProperties(Dataset updatedDataset, Dataset ex
*/
public void updateFromPreferredMetadata(UUID uuid, String user) {
Dataset dataset = super.get(uuid);

restrictionsHandler.checkDenyPublisher(dataset.getPublishingOrganizationKey());

if (dataset == null) {
throw new NotFoundException(
"Dataset " + uuid + " not existing", URI.create("/dataset/{key}/document"));
Expand Down Expand Up @@ -572,6 +592,8 @@ public UUID create(@RequestBody @Trim Dataset dataset) {
dataset.setLicense(License.CC_BY_4_0);
}

restrictionsHandler.checkDenyPublisher(dataset.getPublishingOrganizationKey());

final UUID key = super.create(dataset);
// now that we have a UUID schedule to scheduleRegistration the DOI
// to get the latest timestamps we need to read a new copy of the dataset
Expand All @@ -586,6 +608,9 @@ public void update(Dataset dataset) {
if (old == null) {
throw new IllegalArgumentException("Dataset " + dataset.getKey() + " not existing");
}

restrictionsHandler.checkDenyPublisher(dataset.getPublishingOrganizationKey());

// replace current license? Only if dataset being updated has a supported license
if (!replaceLicense(dataset.getLicense())) {
LOG.warn(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.gbif.api.model.registry.Dataset;
import org.gbif.api.model.registry.Installation;
import org.gbif.api.model.registry.Organization;
import org.gbif.api.model.registry.PrePersist;
import org.gbif.api.model.registry.metasync.MetasyncHistory;
import org.gbif.api.model.registry.search.KeyTitleResult;
import org.gbif.api.service.registry.InstallationService;
Expand All @@ -45,6 +46,7 @@

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.groups.Default;

import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
Expand Down Expand Up @@ -72,6 +74,8 @@

import static com.google.common.base.Preconditions.checkArgument;
import static org.gbif.registry.security.UserRoles.ADMIN_ROLE;
import static org.gbif.registry.security.UserRoles.EDITOR_ROLE;
import static org.gbif.registry.security.UserRoles.IPT_ROLE;

@Validated
@Primary
Expand All @@ -91,16 +95,18 @@ public class InstallationResource extends BaseNetworkEntityResource<Installation
private final MessagePublisher messagePublisher;

public InstallationResource(
MapperServiceLocator mapperServiceLocator,
EventManager eventManager,
WithMyBatis withMyBatis,
@Autowired(required = false) MessagePublisher messagePublisher) {
MapperServiceLocator mapperServiceLocator,
EventManager eventManager,
WithMyBatis withMyBatis,
@Autowired(required = false) MessagePublisher messagePublisher,
RestrictionsHandler restrictionsHandler) {
super(
mapperServiceLocator.getInstallationMapper(),
mapperServiceLocator,
Installation.class,
eventManager,
withMyBatis);
withMyBatis,
restrictionsHandler);
this.datasetMapper = mapperServiceLocator.getDatasetMapper();
this.installationMapper = mapperServiceLocator.getInstallationMapper();
this.organizationMapper = mapperServiceLocator.getOrganizationMapper();
Expand Down Expand Up @@ -287,4 +293,21 @@ public PagingResponse<Installation> listByType(InstallationType type, Pageable p
long total = installationMapper.countWithFilter(type);
return pagingResponse(page, total, installationMapper.listWithFilter(type, page));
}

@PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
@Validated({PrePersist.class, Default.class})
@Trim
@Transactional
@Secured({ADMIN_ROLE, EDITOR_ROLE, IPT_ROLE})
@Override
public UUID create(Installation entity) {
getRestrictionsHandler().checkDenyPublisher(entity.getOrganizationKey());
return super.create(entity);
}

@Override
public void update(Installation entity) {
getRestrictionsHandler().checkDenyPublisher(entity.getOrganizationKey());
super.update(entity);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,17 @@ public class NetworkResource extends BaseNetworkEntityResource<Network> implemen
private final EventManager eventManager;

public NetworkResource(
MapperServiceLocator mapperServiceLocator,
EventManager eventManager,
WithMyBatis withMyBatis) {
MapperServiceLocator mapperServiceLocator,
EventManager eventManager,
WithMyBatis withMyBatis,
RestrictionsHandler restrictionsHandler) {
super(
mapperServiceLocator.getNetworkMapper(),
mapperServiceLocator,
Network.class,
eventManager,
withMyBatis);
withMyBatis,
restrictionsHandler);
this.eventManager = eventManager;
this.datasetMapper = mapperServiceLocator.getDatasetMapper();
this.networkMapper = mapperServiceLocator.getNetworkMapper();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,15 @@ public NodeResource(
MapperServiceLocator mapperServiceLocator,
EventManager eventManager,
Augmenter nodeAugmenter,
WithMyBatis withMyBatis) {
WithMyBatis withMyBatis,
RestrictionsHandler restrictionsHandler) {
super(
mapperServiceLocator.getNodeMapper(),
mapperServiceLocator,
Node.class,
eventManager,
withMyBatis);
withMyBatis,
restrictionsHandler);
this.nodeMapper = mapperServiceLocator.getNodeMapper();
this.organizationMapper = mapperServiceLocator.getOrganizationMapper();
this.nodeAugmenter = nodeAugmenter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ public Map<Integer, Map<Integer, Long>> getDownloadedRecordsByDataset(
@RequestParam(value = "datasetKey", required = false) UUID datasetKey,
@RequestParam(value = "publishingOrgKey", required = false) UUID publishingOrgKey) {
return groupByYear(
occurrenceDownloadMapper.getDownloadsByDataset(
occurrenceDownloadMapper.getDownloadedRecordsByDataset(
fromDate,
toDate,
Optional.ofNullable(publishingCountry).map(Country::getIso2LetterCode).orElse(null),
Expand Down
Loading