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

DD-387 Add license entity and api #57

Merged
merged 9 commits into from
Mar 31, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
33 changes: 18 additions & 15 deletions src/main/java/edu/harvard/iq/dataverse/License.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package edu.harvard.iq.dataverse;

import java.net.URI;
import java.net.URL;
import java.net.URISyntaxException;
import java.util.Objects;
import javax.persistence.Column;
import javax.persistence.Entity;
Expand All @@ -21,9 +21,12 @@
query="SELECT l FROM License l"),
@NamedQuery( name="License.findById",
query = "SELECT l FROM License l WHERE l.id=:id"),
@NamedQuery( name="License.findByName",
query = "SELECT l FROM License l WHERE l.name=:name"),
@NamedQuery( name="License.deleteById",
query="DELETE FROM License l WHERE l.id=:id")

query="DELETE FROM License l WHERE l.id=:id"),
@NamedQuery( name="License.deleteByName",
query="DELETE FROM License l WHERE l.name=:name")
})
@Entity
@Table(uniqueConstraints = {
Expand All @@ -43,22 +46,22 @@ public class License {
private String shortDescription;

@Column(columnDefinition="TEXT", nullable = false)
private URI uri;
private String uri;
janvanmansum marked this conversation as resolved.
Show resolved Hide resolved

@Column(columnDefinition="TEXT")
private URL iconUrl;
private String iconUrl;
janvanmansum marked this conversation as resolved.
Show resolved Hide resolved

@Column(nullable = false)
private boolean active;

public License() {
}

public License(String name, String shortDescription, URI uri, URL iconUrl, boolean active) {
public License(String name, String shortDescription, URI uri, URI iconUrl, boolean active) {
this.name = name;
this.shortDescription = shortDescription;
this.uri = uri;
this.iconUrl = iconUrl;
this.uri = uri.toASCIIString();
this.iconUrl = iconUrl.toASCIIString();
this.active = active;
}

Expand Down Expand Up @@ -86,20 +89,20 @@ public void setShortDescription(String shortDescription) {
this.shortDescription = shortDescription;
}

public URI getUri() {
return uri;
public URI getUri() throws URISyntaxException {
return new URI(uri);
}

public void setUri(URI uri) {
this.uri = uri;
this.uri = uri.toASCIIString();
}

public URL getIconUrl() {
return iconUrl;
public URI getIconUrl() throws URISyntaxException {
Copy link

@janvanmansum janvanmansum Mar 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When would this throw a URISyntaxException? And how could this realistically happen?

Copy link
Author

@JingMa87 JingMa87 Mar 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@janvanmansum This would throw a URISyntaxException if the URI String in the DB isn't a valid URI. Realistically this would never happen because a URI can only be inserted or updated in the DB if it's a correct URI object, I tested this. However, if someone directly tampers with the data in the DB, the exception might be thrown. Also, the exception is automatically thrown when trying to form a URI object so it has to be handled.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed, please wrap in an IllegalStateException. Rationale: the database has been corrupted in some way.

return new URI(iconUrl);
}

public void setIconUrl(URL iconUrl) {
this.iconUrl = iconUrl;
public void setIconUrl(URI iconUrl) {
this.iconUrl = iconUrl.toASCIIString();
}

public boolean isActive() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public License save(License license) throws PersistenceException, RequestBodyExc
}
}

public License setById(long id, String name, String shortDescription, URI uri, URL iconUrl, boolean active) throws UpdateException {
public void setById(long id, String name, String shortDescription, URI uri, URI iconUrl, boolean active) throws UpdateException {
janvanmansum marked this conversation as resolved.
Show resolved Hide resolved
List<License> tokens = em.createNamedQuery("License.findById", License.class)
.setParameter("id", id )
.getResultList();
Expand All @@ -76,13 +76,12 @@ public License setById(long id, String name, String shortDescription, URI uri, U
em.merge(license);
actionLogSvc.log( new ActionLogRecord(ActionLogRecord.ActionType.Admin, "set")
.setInfo(name + ": " + shortDescription + ": " + uri + ": " + iconUrl + ": " + active));
return license;
} else {
throw new UpdateException("There is no existing License with that ID. To add a license use POST.");
}
}

public License setByName(String name, String shortDescription, URI uri, URL iconUrl, boolean active) throws UpdateException {
public void setByName(String name, String shortDescription, URI uri, URI iconUrl, boolean active) throws UpdateException {
List<License> tokens = em.createNamedQuery("License.findByName", License.class)
.setParameter("name", name )
.getResultList();
Expand All @@ -96,7 +95,6 @@ public License setByName(String name, String shortDescription, URI uri, URL icon
em.merge(license);
actionLogSvc.log( new ActionLogRecord(ActionLogRecord.ActionType.Admin, "set")
.setInfo(name + ": " + shortDescription + ": " + uri + ": " + iconUrl + ": " + active));
return license;
} else {
throw new UpdateException("There is no existing License with that name. To add a license use POST.");
}
Expand Down
29 changes: 22 additions & 7 deletions src/main/java/edu/harvard/iq/dataverse/api/Admin.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import edu.harvard.iq.dataverse.engine.command.impl.PublishDataverseCommand;
import edu.harvard.iq.dataverse.settings.Setting;
import edu.harvard.iq.dataverse.util.json.JsonPrinter;
import java.net.URISyntaxException;
import javax.json.Json;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObjectBuilder;
Expand Down Expand Up @@ -1924,9 +1925,15 @@ public Response getBannerMessages(@PathParam("id") Long id) throws WrappedRespon
@GET
@Path("/licenses")
public Response getLicenses() {
return ok(licenseService.listAll().stream()
.map(JsonPrinter::json)
.collect(toJsonArray()));
JsonArrayBuilder arrayBuilder = Json.createArrayBuilder();
for(License license : licenseService.listAll()) {
try {
arrayBuilder.add(JsonPrinter.json(license));
} catch (URISyntaxException e) {
return error(Status.INTERNAL_SERVER_ERROR, "Incorrect URI in JSON");
}
}
return ok(arrayBuilder);
}

@GET
Expand All @@ -1937,8 +1944,10 @@ public Response getLicenseById(@PathParam("id") long id) {
return ok(json(license));
} catch (FetchException e) {
return error(Response.Status.NOT_FOUND, e.getMessage());
} catch (URISyntaxException e) {
return error(Response.Status.BAD_REQUEST, "Incorrect URI in JSON");
}
janvanmansum marked this conversation as resolved.
Show resolved Hide resolved
}
}

@GET
@Path("/licenses/name/{name}")
Expand All @@ -1948,8 +1957,10 @@ public Response getLicenseByName(@PathParam("name") String name) {
return ok(json(license));
} catch (FetchException e) {
return error(Response.Status.NOT_FOUND, e.getMessage());
} catch (URISyntaxException e) {
return error(Response.Status.BAD_REQUEST, "Incorrect URI in JSON");
}
}
}

@POST
@Path("/licenses")
Expand All @@ -1971,8 +1982,10 @@ public Response putLicenseById(@PathParam("id") long id, License license) {
licenseService.setById(id, license.getName(), license.getShortDescription(), license.getUri(), license.getIconUrl(), license.isActive());
} catch (UpdateException e) {
return error(Response.Status.BAD_REQUEST, e.getMessage());
janvanmansum marked this conversation as resolved.
Show resolved Hide resolved
} catch (URISyntaxException e) {
return error(Response.Status.BAD_REQUEST, "Incorrect URI in JSON");
}
return ok("License with ID " + id + " was replaced.");
return ok("License with ID " + id + " was replaced.");
}

@PUT
Expand All @@ -1982,8 +1995,10 @@ public Response putLicenseByName(@PathParam("name") String name, License license
licenseService.setByName(license.getName(), license.getShortDescription(), license.getUri(), license.getIconUrl(), license.isActive());
} catch (UpdateException e) {
return error(Response.Status.BAD_REQUEST, e.getMessage());
} catch (URISyntaxException e) {
return error(Response.Status.BAD_REQUEST, "Incorrect URI in JSON");
}
return ok("License with name " + name + " was replaced.");
return ok("License with name " + name + " was replaced.");
}

@DELETE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import edu.harvard.iq.dataverse.workflow.Workflow;
import edu.harvard.iq.dataverse.workflow.step.WorkflowStepData;

import java.net.URISyntaxException;
import java.util.*;
import javax.json.Json;
import javax.json.JsonArrayBuilder;
Expand Down Expand Up @@ -777,14 +778,14 @@ public static JsonObjectBuilder json( DataverseFacet aFacet ) {
.add("name", aFacet.getDatasetFieldType().getDisplayName());
}

public static JsonObjectBuilder json(License l) {
public static JsonObjectBuilder json(License license) throws URISyntaxException {
return jsonObjectBuilder()
.add("id", l.getId())
.add("name", l.getName())
.add("shortDescription", l.getShortDescription())
.add("uri", l.getUri().toString())
.add("iconUrl", l.getIconUrl().toString())
.add("active", l.isActive());
.add("id", license.getId())
.add("name", license.getName())
.add("shortDescription", license.getShortDescription())
.add("uri", license.getUri().toString())
.add("iconUrl", license.getIconUrl().toString())
.add("active", license.isActive());
}

public static Collector<String, JsonArrayBuilder, JsonArrayBuilder> stringsToJsonArray() {
Expand Down