Skip to content

Commit

Permalink
Standardized some code, using facilities from dans-java-utils
Browse files Browse the repository at this point in the history
  • Loading branch information
janvanmansum committed Jun 18, 2024
1 parent cd5afd0 commit 91e90c7
Show file tree
Hide file tree
Showing 22 changed files with 81 additions and 244 deletions.
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@
<groupId>nl.knaw.dans</groupId>
<artifactId>dans-dataverse-client-lib</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
Expand Down
18 changes: 8 additions & 10 deletions src/main/java/nl/knaw/dans/virusscan/DdVirusScanApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,36 @@

package nl.knaw.dans.virusscan;

import io.dropwizard.client.JerseyClientBuilder;
import io.dropwizard.core.Application;
import io.dropwizard.core.setup.Environment;
import nl.knaw.dans.lib.util.DataverseHealthCheck;
import nl.knaw.dans.virusscan.config.DdVirusScanConfig;
import nl.knaw.dans.virusscan.core.service.ClamdServiceImpl;
import nl.knaw.dans.virusscan.core.service.DatasetResumeTaskFactoryImpl;
import nl.knaw.dans.virusscan.core.service.DatasetScanTaskFactoryImpl;
import nl.knaw.dans.virusscan.core.service.DataverseApiServiceImpl;
import nl.knaw.dans.virusscan.core.service.VirusScannerImpl;
import nl.knaw.dans.virusscan.health.ClamdHealthCheck;
import nl.knaw.dans.virusscan.health.DataverseHealthCheck;
import nl.knaw.dans.virusscan.resource.InvokeResourceImpl;

public class DdVirusScanApplication extends Application<DdVirusScanConfiguration> {
public class DdVirusScanApplication extends Application<DdVirusScanConfig> {

public static void main(final String[] args) throws Exception {
new DdVirusScanApplication().run(args);
}

@Override
public String getName() {
return "Dd Virus Scan";
return "DD Virus Scan";
}

@Override
public void run(final DdVirusScanConfiguration configuration, final Environment environment) {
final var client = new JerseyClientBuilder(environment).using(configuration.getJerseyClientConfiguration())
.build(getName());

public void run(final DdVirusScanConfig configuration, final Environment environment) {
var scanDatasetTaskQueue = configuration.getVirusscanner().getScanDatasetTaskQueue().build(environment);
var resumeDatasetTaskQueue = configuration.getVirusscanner().getResumeDatasetTaskQueue().build(environment);
var clamdService = new ClamdServiceImpl(configuration.getVirusscanner().getClamd());
var dataverseApiService = new DataverseApiServiceImpl(configuration.getDataverse(), client);
var dataverseClient = configuration.getDataverse().build(environment, "dd-virus-scan/dataverse");
var dataverseApiService = new DataverseApiServiceImpl(dataverseClient);
var virusScanner = new VirusScannerImpl(configuration.getVirusscanner(), clamdService);

var datasetResumeTaskFactory = new DatasetResumeTaskFactoryImpl(dataverseApiService, resumeDatasetTaskQueue, configuration.getVirusscanner().getResumeTasks());
Expand All @@ -58,6 +56,6 @@ public void run(final DdVirusScanConfiguration configuration, final Environment
environment.jersey().register(resource);

environment.healthChecks().register("Clamd", new ClamdHealthCheck(clamdService));
environment.healthChecks().register("Dataverse", new DataverseHealthCheck(dataverseApiService));
environment.healthChecks().register("Dataverse", new DataverseHealthCheck(dataverseClient));
}
}
75 changes: 0 additions & 75 deletions src/main/java/nl/knaw/dans/virusscan/DdVirusScanConfiguration.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nl.knaw.dans.virusscan.core.config;
package nl.knaw.dans.virusscan.config;

import javax.validation.Valid;
import javax.validation.constraints.Max;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nl.knaw.dans.virusscan.core.config;
package nl.knaw.dans.virusscan.config;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/nl/knaw/dans/virusscan/config/DdVirusScanConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (C) 2022 DANS - Data Archiving and Networked Services ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package nl.knaw.dans.virusscan.config;

import io.dropwizard.client.JerseyClientConfiguration;
import io.dropwizard.core.Configuration;
import lombok.Data;
import lombok.EqualsAndHashCode;
import nl.knaw.dans.lib.util.DataverseClientFactory;

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

@Data
@EqualsAndHashCode(callSuper = false)
public class DdVirusScanConfig extends Configuration {

@Valid
@NotNull
private JerseyClientConfiguration jerseyClient = new JerseyClientConfiguration();

@Valid
@NotNull
private DataverseClientFactory dataverse;
@Valid
@NotNull
private VirusScannerConfig virusscanner;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nl.knaw.dans.virusscan.core.config;
package nl.knaw.dans.virusscan.config;

import javax.validation.Valid;
import javax.validation.constraints.Min;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nl.knaw.dans.virusscan.core.config;
package nl.knaw.dans.virusscan.config;

import nl.knaw.dans.lib.util.ExecutorServiceFactory;
import nl.knaw.dans.virusscan.core.config.validation.ValidClamdBufferSize;
import nl.knaw.dans.virusscan.config.validation.ValidClamdBufferSize;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nl.knaw.dans.virusscan.core.config.validation;
package nl.knaw.dans.virusscan.config.validation;

import javax.validation.Constraint;
import javax.validation.Payload;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nl.knaw.dans.virusscan.core.config.validation;
package nl.knaw.dans.virusscan.config.validation;

import nl.knaw.dans.virusscan.core.config.ClamdConfig;
import nl.knaw.dans.virusscan.config.ClamdConfig;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package nl.knaw.dans.virusscan.core.service;

import nl.knaw.dans.virusscan.core.config.ClamdConfig;
import nl.knaw.dans.virusscan.config.ClamdConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package nl.knaw.dans.virusscan.core.service;

import nl.knaw.dans.virusscan.core.config.ResumeTasksConfig;
import nl.knaw.dans.virusscan.config.ResumeTasksConfig;
import nl.knaw.dans.virusscan.core.model.DatasetResumeTaskPayload;
import nl.knaw.dans.virusscan.core.task.DatasetResumeTask;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,48 +15,26 @@
*/
package nl.knaw.dans.virusscan.core.service;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import nl.knaw.dans.lib.dataverse.DataverseClient;
import nl.knaw.dans.lib.dataverse.DataverseClientConfig;
import nl.knaw.dans.lib.dataverse.DataverseException;
import nl.knaw.dans.lib.dataverse.model.file.FileMeta;
import nl.knaw.dans.lib.dataverse.model.workflow.ResumeMessage;
import nl.knaw.dans.virusscan.core.config.DataverseConfig;
import org.apache.hc.core5.http.io.HttpClientResponseHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.ws.rs.client.Client;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.List;

@Slf4j
@AllArgsConstructor
public class DataverseApiServiceImpl implements DataverseApiService {
private static final Logger log = LoggerFactory.getLogger(DataverseApiServiceImpl.class);

private final DataverseConfig dataverseConfig;
private final Client httpClient;

private DataverseClient client;

public DataverseApiServiceImpl(DataverseConfig dataverseConfig, Client client) {
this.dataverseConfig = dataverseConfig;
this.httpClient = client;
}

DataverseClient getDataverseClient() {
if (this.client == null) {
var config = new DataverseClientConfig(URI.create(dataverseConfig.getBaseUrl()), dataverseConfig.getApiToken());
this.client = new DataverseClient(config);
}

return client;
}

@Override
public List<FileMeta> listFiles(String datasetId, String invocationId, String version) throws IOException, DataverseException {
log.trace("Getting list of files for data set {}, invocation id {} and version :draft", datasetId, invocationId);
var dataset = getDataverseClient().dataset(datasetId, invocationId);
var dataset = client.dataset(datasetId, invocationId);
var files = dataset.getFiles(":draft");

return files.getData();
Expand All @@ -65,26 +43,26 @@ public List<FileMeta> listFiles(String datasetId, String invocationId, String ve
@Override
public <T> T getFile(int fileId, HttpClientResponseHandler<T> handler) throws IOException, DataverseException {
log.trace("Getting file with id {}", fileId);
return getDataverseClient().basicFileAccess(fileId).getFile(handler);
return client.basicFileAccess(fileId).getFile(handler);
}

@Override
public void completeWorkflow(String invocationId, String reason, String message) throws IOException, DataverseException {
var resumeMessage = new ResumeMessage("Success", reason, message);
log.trace("Completing workflow with status Success, invocation id is {}", invocationId);
this.getDataverseClient().workflows().resume(invocationId, resumeMessage);
this.client.workflows().resume(invocationId, resumeMessage);
}

@Override
public void failWorkflow(String invocationId, String reason, String message) throws IOException, DataverseException {
var resumeMessage = new ResumeMessage("Failure", reason, message);
log.trace("Completing workflow with status Failure, reasin is '{}', message is '{}', invocation id is {}", reason, message, invocationId);
this.getDataverseClient().workflows().resume(invocationId, resumeMessage);
this.client.workflows().resume(invocationId, resumeMessage);
}

@Override
public void checkConnection() throws IOException, DataverseException {
this.getDataverseClient().checkConnection();
this.client.checkConnection();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package nl.knaw.dans.virusscan.core.service;

import nl.knaw.dans.virusscan.core.config.VirusScannerConfig;
import nl.knaw.dans.virusscan.config.VirusScannerConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import nl.knaw.dans.lib.dataverse.DataverseException;
import nl.knaw.dans.lib.dataverse.model.file.FileMeta;
import nl.knaw.dans.virusscan.core.config.ResumeTasksConfig;
import nl.knaw.dans.virusscan.config.ResumeTasksConfig;
import nl.knaw.dans.virusscan.core.model.DatasetResumeTaskPayload;
import nl.knaw.dans.virusscan.core.service.DataverseApiService;
import org.slf4j.Logger;
Expand Down
Loading

0 comments on commit 91e90c7

Please sign in to comment.