-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from janvanmansum/DD-1575
DD-1575 validate dataset files
- Loading branch information
Showing
9 changed files
with
257 additions
and
39 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
examples/src/main/java/nl/knaw/dans/lib/dataverse/example/AdminValidateDatasetFiles.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright (C) 2021 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.lib.dataverse.example; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import nl.knaw.dans.lib.dataverse.DataverseResponseWithoutEnvelope; | ||
import nl.knaw.dans.lib.dataverse.ExampleBase; | ||
import nl.knaw.dans.lib.dataverse.model.DatasetFileValidationResultList; | ||
|
||
@Slf4j | ||
public class AdminValidateDatasetFiles extends ExampleBase { | ||
|
||
public static void main(String[] args) throws Exception { | ||
var id = args[0]; | ||
DataverseResponseWithoutEnvelope<DatasetFileValidationResultList> r; | ||
// If id is parseable as a number, it is assumed to be a dataset id. | ||
try { | ||
r = client.admin().validateDatasetFiles(Integer.parseInt(id)); | ||
} catch (NumberFormatException e) { | ||
r = client.admin().validateDatasetFiles(id); | ||
} | ||
log.info(r.getBodyAsJson().toPrettyString()); | ||
for (var result : r.getBodyAsObject().getDataFiles()) { | ||
log.info("File: {}", result.getDatafileId()); | ||
log.info(" Storage Identifier: {}", result.getStorageIdentifier()); | ||
log.info(" Status: {}", result.getStatus()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
lib/src/main/java/nl/knaw/dans/lib/dataverse/DataverseHttpResponseWithoutEnvelope.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright (C) 2021 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.lib.dataverse; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import lombok.SneakyThrows; | ||
import org.apache.hc.core5.http.HttpResponse; | ||
|
||
import java.io.IOException; | ||
|
||
public class DataverseHttpResponseWithoutEnvelope<D> extends DataverseResponseWithoutEnvelope<D> { | ||
private final HttpResponse httpResponse; | ||
|
||
DataverseHttpResponseWithoutEnvelope(DispatchResult dispatchResult, ObjectMapper customMapper, Class<?>... dataClass) throws IOException { | ||
super(dispatchResult.getBody(), customMapper, dataClass); | ||
this.httpResponse = dispatchResult.getResponse(); | ||
} | ||
|
||
public HttpResponse getHttpResponse() { | ||
return httpResponse; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
lib/src/main/java/nl/knaw/dans/lib/dataverse/DataverseResponseWithoutEnvelope.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright (C) 2021 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.lib.dataverse; | ||
|
||
import com.fasterxml.jackson.databind.JavaType; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.type.TypeFactory; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* Response from Dataverse that is <em>not</em> wrapped in an envelope. Normally, Dataverse responses are wrapped in an | ||
* envelope that contains metadata about the response, such as the status code and the message. In some cases, however, | ||
* the response is not wrapped in an envelope. This class is used to handle such responses. | ||
* | ||
* @param <B> the type of the data of the response body | ||
*/ | ||
|
||
@Slf4j | ||
public class DataverseResponseWithoutEnvelope<B> { | ||
private final ObjectMapper mapper; | ||
|
||
private final String bodyText; | ||
private final JavaType dataType; | ||
|
||
protected DataverseResponseWithoutEnvelope(String bodyText, ObjectMapper mapper, Class<?>... dataClass) { | ||
this.bodyText = bodyText; | ||
this.mapper = mapper; | ||
TypeFactory typeFactory = mapper.getTypeFactory(); | ||
switch (dataClass.length) { | ||
case 0: | ||
throw new IllegalArgumentException("No parameter type given"); | ||
case 1: | ||
this.dataType = typeFactory.constructType(dataClass[0]); | ||
break; | ||
case 2: | ||
this.dataType = typeFactory.constructParametricType(dataClass[0], dataClass[1]); | ||
break; | ||
case 3: | ||
JavaType nested = typeFactory.constructParametricType(dataClass[1], dataClass[2]); | ||
this.dataType = typeFactory.constructParametricType(dataClass[0], nested); | ||
break; | ||
default: | ||
throw new IllegalArgumentException("Currently no more than two nested parameter types supported"); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* @return the body as bean of type D | ||
* @throws com.fasterxml.jackson.core.JsonParseException if body cannot be processed properly as JSON | ||
*/ | ||
public B getBodyAsObject() throws IOException { | ||
return mapper.readValue(bodyText, dataType); | ||
} | ||
|
||
/** | ||
* @return the body as a JsonNode | ||
* @throws com.fasterxml.jackson.core.JsonParseException if body cannot be processed properly as JSON | ||
*/ | ||
public JsonNode getBodyAsJson() throws IOException { | ||
return mapper.readTree(bodyText); | ||
} | ||
|
||
/** | ||
* @return the body as a String | ||
*/ | ||
public String getBodyAsString() { | ||
return bodyText; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
lib/src/main/java/nl/knaw/dans/lib/dataverse/model/DatasetFileValidationResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright (C) 2021 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.lib.dataverse.model; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class DatasetFileValidationResult { | ||
private int datafileId; | ||
private String storageIdentifier; | ||
private String status; | ||
} |
25 changes: 25 additions & 0 deletions
25
lib/src/main/java/nl/knaw/dans/lib/dataverse/model/DatasetFileValidationResultList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright (C) 2021 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.lib.dataverse.model; | ||
|
||
import lombok.Data; | ||
|
||
import java.util.List; | ||
|
||
@Data | ||
public class DatasetFileValidationResultList { | ||
private List<DatasetFileValidationResult> dataFiles; | ||
} |