Skip to content

Commit

Permalink
Merge pull request #5 from EGI-Federation/parser-esrf
Browse files Browse the repository at this point in the history
Implemented ESRF parsing
  • Loading branch information
thebe14 authored Apr 20, 2023
2 parents 746b89f + 04b0a16 commit 0e43444
Show file tree
Hide file tree
Showing 17 changed files with 530 additions and 11 deletions.
6 changes: 4 additions & 2 deletions .github/linters/sun_checks.xml
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,14 @@
<module name="ConstantName"/>
<module name="LocalFinalVariableName"/>
<module name="LocalVariableName"/>
<module name="MemberName"/>
<module name="MethodName"/>
<module name="PackageName"/>
<module name="ParameterName"/>
<module name="StaticVariableName"/>
<module name="TypeName"/>
<!--
<module name="MemberName"/>
-->

<!-- Checks for imports -->
<!-- See https://checkstyle.org/config_imports.html -->
Expand Down Expand Up @@ -178,13 +180,13 @@
<module name="EqualsHashCode"/>
<module name="IllegalInstantiation"/>
<module name="InnerAssignment"/>
<module name="MissingSwitchDefault"/>
<module name="MultipleVariableDeclarations"/>
<module name="SimplifyBooleanExpression"/>
<module name="SimplifyBooleanReturn"/>
<!--
<module name="MagicNumber"/>
<module name="HiddenField"/>
<module name="MissingSwitchDefault"/>
-->

<!-- Checks for class design -->
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/eosc/eu/ActionError.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import javax.ws.rs.core.Response.Status;

import parser.b2share.B2ShareException;
import parser.esrf.EsrfException;
import parser.zenodo.ZenodoException;
import egi.fts.FileTransferServiceException;

Expand Down Expand Up @@ -48,6 +49,7 @@ public class ActionError {

/**
* Copy constructor does deep copy
* @param error Error to copy
*/
public ActionError(ActionError error) {

Expand All @@ -67,6 +69,8 @@ public ActionError(ActionError error) {

/**
* Copy but change id
* @param error Error to copy
* @param newId New error id
*/
public ActionError(ActionError error, String newId) {
this(error);
Expand All @@ -75,6 +79,7 @@ public ActionError(ActionError error, String newId) {

/**
* Construct with error id
* @param id Error id
*/
public ActionError(String id) {
this.id = id;
Expand All @@ -84,6 +89,8 @@ public ActionError(String id) {

/**
* Construct with error id and description
* @param id Error id
* @param description Error description
*/
public ActionError(String id, String description) {
this.id = id;
Expand All @@ -93,13 +100,17 @@ public ActionError(String id, String description) {

/**
* Construct with error id and detail
* @param id Error id
* @param detail Key-value pair to add to the details of the error
*/
public ActionError(String id, Tuple2<String, String> detail) {
this(id, Arrays.asList(detail));
}

/**
* Construct with error id and details
* @param id Error id
* @param details Key-value pairs to add to the details of the error
*/
public ActionError(String id, List<Tuple2<String, String>> details) {
this.id = id;
Expand All @@ -115,13 +126,19 @@ public ActionError(String id, List<Tuple2<String, String>> details) {

/**
* Construct with error id, description, and detail
* @param id Error id
* @param description Error description
* @param detail Key-value pair to add to the details of the error
*/
public ActionError(String id, String description, Tuple2<String, String> detail) {
this(id, description, Arrays.asList(detail));
}

/**
* Construct with error id, description, and details
* @param id Error id
* @param description Error description
* @param details Key-value pairs to add to the details of the error
*/
public ActionError(String id, String description, List<Tuple2<String, String>> details) {
this.id = id;
Expand All @@ -137,6 +154,7 @@ public ActionError(String id, String description, List<Tuple2<String, String>> d

/**
* Construct from exception
* @param t The exception to wrap
*/
public ActionError(Throwable t) {
this.id = "exception";
Expand All @@ -151,6 +169,7 @@ public ActionError(Throwable t) {
var type = t.getClass();
if (type.equals(ZenodoException.class) ||
type.equals(B2ShareException.class) ||
type.equals(EsrfException.class) ||
type.equals(FileTransferServiceException.class) ||
type.equals(ClientWebApplicationException.class) ||
type.equals(WebApplicationException.class) ) {
Expand Down Expand Up @@ -217,13 +236,17 @@ else if(type.equals(ProcessingException.class)) {

/**
* Construct from exception and detail
* @param t The exception to wrap
* @param detail Key-value pair to add to the details of the error
*/
public ActionError(Throwable t, Tuple2<String, String> detail) {
this(t, Arrays.asList(detail));
}

/**
* Construct from exception and details
* @param t The exception to wrap
* @param details Key-value pair to add to the details of the error
*/
public ActionError(Throwable t, List<Tuple2<String, String>> details) {
this(t);
Expand Down Expand Up @@ -265,13 +288,16 @@ else if(keys.contains("seUrl"))

/**
* Retrieve the HTTP status code
* @return HTTP status code
*/
public Status getStatus() {
return this.status;
}

/**
* Update the HTTP status code
* @param status New HTTP status
* @return Instance to allow for fluent calls (with .)
*/
public ActionError setStatus(Status status) {
this.status = status;
Expand All @@ -280,13 +306,16 @@ public ActionError setStatus(Status status) {

/**
* Convert to Response that can be returned by a REST endpoint
* @return Response object
*/
public Response toResponse() {
return Response.ok(this).status(this.status).build();
}

/**
* Convert to Response with new status that can be returned by a REST endpoint
* @param status New HTTP status
* @return Response object with new HTTP status code
*/
public Response toResponse(Status status) {
return Response.ok(this).status(status).build();
Expand Down
26 changes: 25 additions & 1 deletion src/main/java/eosc/eu/model/StorageElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.eclipse.microprofile.openapi.annotations.media.Schema;
import parser.zenodo.model.ZenodoFile;
import parser.b2share.model.B2ShareFile;
import parser.esrf.model.EsrfDataFile;
import egi.fts.model.ObjectInfo;


Expand Down Expand Up @@ -55,7 +56,9 @@ public class StorageElement extends StorageElementBase {
public StorageElement() { super("StorageElement"); }

/**
* Constructor
* Construct using access URL and media type
* @param url Access URL for the storage element
* @param type Media type
*/
public StorageElement(String url, String type) {
super("StorageElement");
Expand All @@ -65,6 +68,7 @@ public StorageElement(String url, String type) {

/**
* Construct from Zenodo file
* @param zf Zenodo file
*/
public StorageElement(ZenodoFile zf) {
super("StorageElement", zf.filename);
Expand All @@ -76,8 +80,27 @@ public StorageElement(ZenodoFile zf) {
}
}

/**
* Construct from ESRF file
* @param ef ESRF file
* @param baseUrl The base URL for the access URL, as the ESRF file
* only contains the path to the file
*/
public StorageElement(EsrfDataFile ef, String baseUrl) {
super("StorageElement", ef.Datafile.name);
this.size = ef.Datafile.fileSize;
this.accessUrl = baseUrl + ef.Datafile.location;

if(null != ef.Datafile.createTime)
this.createdAt = ef.Datafile.createTime;

if(null != ef.Datafile.modTime)
this.modifiedAt = ef.Datafile.modTime;
}

/**
* Construct from B2Share file
* @param b2sf B2SHARE file
*/
public StorageElement(B2ShareFile b2sf) {
super("StorageElement", b2sf.name);
Expand All @@ -94,6 +117,7 @@ public StorageElement(B2ShareFile b2sf) {

/**
* Construct from FTS object
* @param obj Information about an object returned by FTS
*/
public StorageElement(ObjectInfo obj) {
super("StorageElement");
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/parser/b2share/B2ShareException.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,19 @@ public B2ShareException() {
super();
}

/***
* Construct from a response
* @param resp Response object
* @param body Response body
*/
public B2ShareException(Response resp, String body) {
super(resp);
this.responseBody = body;
}

/***
* Get the response body
* @return Body of the response
*/
String responseBody() { return responseBody; }
}
5 changes: 3 additions & 2 deletions src/main/java/parser/b2share/B2ShareParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class B2ShareParser implements ParserService {

/***
* Constructor
* @param id The key of the parser in the config file
*/
public B2ShareParser(String id) { this.id = id; }

Expand All @@ -47,7 +48,6 @@ public class B2ShareParser implements ParserService {
* @return true on success
*/
public boolean init(ParserConfig config, PortConfig port) {

this.name = config.name();
this.timeout = config.timeout();

Expand Down Expand Up @@ -122,7 +122,8 @@ else if(!doi.equals(redirectedToUrl))
LOG.debugf("Redirected DOI %s", redirectedToUrl);

// Validate URL
Pattern p = Pattern.compile("^(https?://[^/:]*b2share[^/:]*:?[\\d]*)/records/(.+)", Pattern.CASE_INSENSITIVE);
Pattern p = Pattern.compile("^(https?://[^/:]*b2share[^/:]*:?[\\d]*)/records/(.+)",
Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(redirectedToUrl);
boolean isSupported = m.matches();

Expand Down
38 changes: 38 additions & 0 deletions src/main/java/parser/esrf/Esrf.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package parser.esrf;

import io.smallrye.mutiny.Uni;
import org.eclipse.microprofile.rest.client.annotation.RegisterProvider;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;

import org.jboss.resteasy.reactive.RestPath;
import org.jboss.resteasy.reactive.RestQuery;
import parser.esrf.model.*;

import java.util.List;


/***
* REST client for ESRF
*/
@RegisterProvider(value = EsrfExceptionMapper.class)
@Produces(MediaType.APPLICATION_JSON)
public interface Esrf {

@POST
@Path("/session")
@Consumes(MediaType.APPLICATION_JSON)
Uni<EsrfSession> getSessionAsync(EsrfCredentials credentials);

@GET
@Path("/doi/{authority}/{recordId}/datasets")
Uni<List<EsrfDataSet>> getDataSetsAsync(@RestQuery("sessionId") String sessionId,
@RestPath("authority") String authority,
@RestPath("recordId") String recordId);

@GET
@Path("/catalogue/{sessionId}/dataset/id/{datasetId}/datafile")
Uni<List<EsrfDataFile>> getDataFilesAsync(@RestPath("sessionId") String sessionId,
@RestPath("datasetId") String datasetId);
}
33 changes: 33 additions & 0 deletions src/main/java/parser/esrf/EsrfException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package parser.esrf;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;


/**
* Exception class for ESRF API calls
*/
public class EsrfException extends WebApplicationException {

private String responseBody;

public EsrfException() {
super();
}

/***
* Construct from a response
* @param resp Response object
* @param body Response body
*/
public EsrfException(Response resp, String body) {
super(resp);
this.responseBody = body;
}

/***
* Get the response body
* @return Body of the response
*/
String responseBody() { return responseBody; }
}
Loading

0 comments on commit 0e43444

Please sign in to comment.