Skip to content

Commit

Permalink
move ReservedWord enum to ExternalTool #3657
Browse files Browse the repository at this point in the history
  • Loading branch information
pdurbin committed Jan 17, 2018
1 parent f9d11d9 commit 05dd4bf
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,53 @@ public JsonObjectBuilder toJson() {
return jab;
}

public enum ReservedWord {

// TODO: Research if a format like "{reservedWord}" is easily parse-able or if another format would be
// better. The choice of curly braces is somewhat arbitrary, but has been observed in documenation for
// various REST APIs. For example, "Variable substitutions will be made when a variable is named in {brackets}."
// from https://swagger.io/specification/#fixed-fields-29 but that's for URLs.
FILE_ID("fileId"),
SITE_URL("siteUrl"),
API_TOKEN("apiToken");

private final String text;
private final String START = "{";
private final String END = "}";

private ReservedWord(final String text) {
this.text = START + text + END;
}

/**
* This is a centralized method that enforces that only reserved words
* are allowed to be used by external tools. External tool authors
* cannot pass their own query parameters through Dataverse such as
* "mode=mode1".
*
* @throws IllegalArgumentException
*/
public static ReservedWord fromString(String text) throws IllegalArgumentException {
if (text != null) {
for (ReservedWord reservedWord : ReservedWord.values()) {
if (text.equals(reservedWord.text)) {
return reservedWord;
}
}
}
// TODO: Consider switching to a more informative message that enumerates the valid reserved words.
boolean moreInformativeMessage = false;
if (moreInformativeMessage) {
throw new IllegalArgumentException("Unknown reserved word: " + text + ". A reserved word must be one of these values: " + Arrays.asList(ReservedWord.values()) + ".");
} else {
throw new IllegalArgumentException("Unknown reserved word: " + text);
}
}

@Override
public String toString() {
return text;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import edu.harvard.iq.dataverse.DataFile;
import edu.harvard.iq.dataverse.authorization.users.ApiToken;
import edu.harvard.iq.dataverse.externaltools.ExternalToolServiceBean.ReservedWord;
import edu.harvard.iq.dataverse.externaltools.ExternalTool.ReservedWord;
import edu.harvard.iq.dataverse.util.SystemConfig;
import java.io.StringReader;
import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package edu.harvard.iq.dataverse.externaltools;

import edu.harvard.iq.dataverse.DataFile;
import edu.harvard.iq.dataverse.externaltools.ExternalTool.ReservedWord;
import static edu.harvard.iq.dataverse.externaltools.ExternalTool.DESCRIPTION;
import static edu.harvard.iq.dataverse.externaltools.ExternalTool.DISPLAY_NAME;
import static edu.harvard.iq.dataverse.externaltools.ExternalTool.TOOL_PARAMETERS;
import static edu.harvard.iq.dataverse.externaltools.ExternalTool.TOOL_URL;
import static edu.harvard.iq.dataverse.externaltools.ExternalTool.TYPE;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.logging.Logger;
Expand Down Expand Up @@ -80,55 +80,6 @@ public ExternalTool save(ExternalTool externalTool) {
return em.merge(externalTool);
}

public enum ReservedWord {

// TODO: Research if a format like "{reservedWord}" is easily parse-able or if another format would be
// better. The choice of curly braces is somewhat arbitrary, but has been observed in documenation for
// various REST APIs. For example, "Variable substitutions will be made when a variable is named in {brackets}."
// from https://swagger.io/specification/#fixed-fields-29 but that's for URLs.
FILE_ID("fileId"),
SITE_URL("siteUrl"),
API_TOKEN("apiToken");

private final String text;
private final String START = "{";
private final String END = "}";

private ReservedWord(final String text) {
this.text = START + text + END;
}

/**
* This is a centralized method that enforces that only reserved words
* are allowed to be used by external tools. External tool authors
* cannot pass their own query parameters through Dataverse such as
* "mode=mode1".
*
* @throws IllegalArgumentException
*/
public static ReservedWord fromString(String text) throws IllegalArgumentException {
if (text != null) {
for (ReservedWord reservedWord : ReservedWord.values()) {
if (text.equals(reservedWord.text)) {
return reservedWord;
}
}
}
// TODO: Consider switching to a more informative message that enumerates the valid reserved words.
boolean moreInformativeMessage = false;
if (moreInformativeMessage) {
throw new IllegalArgumentException("Unknown reserved word: " + text + ". A reserved word must be one of these values: " + Arrays.asList(ReservedWord.values()) + ".");
} else {
throw new IllegalArgumentException("Unknown reserved word: " + text);
}
}

@Override
public String toString() {
return text;
}
}

/**
* This method takes a list of tools and a file and returns which tools that file supports
* The list of tools is passed in so it doesn't hit the database each time
Expand Down Expand Up @@ -171,7 +122,7 @@ public static ExternalTool parseAddExternalToolManifest(String manifest) {
}
if (!allRequiredReservedWordsFound) {
// Some day there might be more reserved words than just {fileId}.
throw new IllegalArgumentException("Required reserved word not found: " + ReservedWord.FILE_ID.text);
throw new IllegalArgumentException("Required reserved word not found: " + ReservedWord.FILE_ID.toString());
}
String toolParameters = toolParametersObj.toString();
return new ExternalTool(displayName, description, type, toolUrl, toolParameters);
Expand Down

0 comments on commit 05dd4bf

Please sign in to comment.