Search in sources :

Example 1 with ReservedWord

use of edu.harvard.iq.dataverse.externaltools.ExternalTool.ReservedWord in project dataverse by IQSS.

the class ExternalToolServiceBean method parseAddExternalToolManifest.

public static ExternalTool parseAddExternalToolManifest(String manifest) {
    if (manifest == null || manifest.isEmpty()) {
        throw new IllegalArgumentException("External tool manifest was null or empty!");
    }
    JsonReader jsonReader = Json.createReader(new StringReader(manifest));
    JsonObject jsonObject = jsonReader.readObject();
    String displayName = getRequiredTopLevelField(jsonObject, DISPLAY_NAME);
    String description = getRequiredTopLevelField(jsonObject, DESCRIPTION);
    String typeUserInput = getRequiredTopLevelField(jsonObject, TYPE);
    // Allow IllegalArgumentException to bubble up from ExternalTool.Type.fromString
    ExternalTool.Type type = ExternalTool.Type.fromString(typeUserInput);
    String toolUrl = getRequiredTopLevelField(jsonObject, TOOL_URL);
    JsonObject toolParametersObj = jsonObject.getJsonObject(TOOL_PARAMETERS);
    JsonArray queryParams = toolParametersObj.getJsonArray("queryParameters");
    boolean allRequiredReservedWordsFound = false;
    for (JsonObject queryParam : queryParams.getValuesAs(JsonObject.class)) {
        Set<String> keyValuePair = queryParam.keySet();
        for (String key : keyValuePair) {
            String value = queryParam.getString(key);
            ReservedWord reservedWord = ReservedWord.fromString(value);
            if (reservedWord.equals(ReservedWord.FILE_ID)) {
                allRequiredReservedWordsFound = true;
            }
        }
    }
    if (!allRequiredReservedWordsFound) {
        // Some day there might be more reserved words than just {fileId}.
        throw new IllegalArgumentException("Required reserved word not found: " + ReservedWord.FILE_ID.toString());
    }
    String toolParameters = toolParametersObj.toString();
    return new ExternalTool(displayName, description, type, toolUrl, toolParameters);
}
Also used : JsonArray(javax.json.JsonArray) StringReader(java.io.StringReader) JsonReader(javax.json.JsonReader) JsonObject(javax.json.JsonObject) ReservedWord(edu.harvard.iq.dataverse.externaltools.ExternalTool.ReservedWord)

Example 2 with ReservedWord

use of edu.harvard.iq.dataverse.externaltools.ExternalTool.ReservedWord in project dataverse by IQSS.

the class ExternalToolHandler method getQueryParam.

private String getQueryParam(String key, String value) {
    ReservedWord reservedWord = ReservedWord.fromString(value);
    switch(reservedWord) {
        case FILE_ID:
            // getDataFile is never null because of the constructor
            return key + "=" + getDataFile().getId();
        case SITE_URL:
            return key + "=" + SystemConfig.getDataverseSiteUrlStatic();
        case API_TOKEN:
            String apiTokenString = null;
            ApiToken theApiToken = getApiToken();
            if (theApiToken != null) {
                apiTokenString = theApiToken.getTokenString();
                return key + "=" + apiTokenString;
            }
            break;
        default:
            break;
    }
    return null;
}
Also used : ApiToken(edu.harvard.iq.dataverse.authorization.users.ApiToken) ReservedWord(edu.harvard.iq.dataverse.externaltools.ExternalTool.ReservedWord)

Aggregations

ReservedWord (edu.harvard.iq.dataverse.externaltools.ExternalTool.ReservedWord)2 ApiToken (edu.harvard.iq.dataverse.authorization.users.ApiToken)1 StringReader (java.io.StringReader)1 JsonArray (javax.json.JsonArray)1 JsonObject (javax.json.JsonObject)1 JsonReader (javax.json.JsonReader)1