Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mlindeman committed Jan 10, 2022
0 parents commit a27ec15
Show file tree
Hide file tree
Showing 13 changed files with 521 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.env

lib/build
lib/bin

.vscode/settings.json

.gradle
gradle
gradlew
gradlew.bat
.DS_Store
.vscode/launch.json
cantaloupe.minimal.properties
.idea
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Delegate for Cantaloupe / Nuxeo

This repository contains code to create a jar file that can be used as a [Delegate](https://cantaloupe-project.github.io/manual/5.0/delegate-system.html) for [Cantaloupe's](https://cantaloupe-project.github.io/) Delegate System to load source from [Nuxeo](https://www.nuxeo.com/).

## Before building, testing, running, etc
- [Download](https://github.com/cantaloupe-project/cantaloupe/releases) a recent Cantaloupe build (v.50 or higher) or build from source
- change the value of *cantaloupe_jar* in _lib/build/build-example.gradle_ to match the path of your Cantaloupe Jar file
- Rename _lib/build/build-example.gradle_ to _lib/build/build.gradle_
- Modify if needed and rename _cantaloupe.properties.sample_ to _cantaloupe.properties_

## Build
To build the jar file in lib/build/libs/:

`gradle jar`

## Run Cantaloupe with this Delegate
There is a minimal settings file for Cantaloupe in this repository "cantaloupe.minimal.properties"

`java -cp \
"/opt/cantaloupe/cantaloupe-6.0-SNAPSHOT.jar":"./lib/build/libs/cantaloupe-nuxeo-delegate-1.0.jar" \
-Dcantaloupe.config=cantaloupe.minmal.properties \
edu.illinois.library.cantaloupe.StandaloneEntry
`
19 changes: 19 additions & 0 deletions cantaloupe.properties.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# These are the minimal properties for Cantaloupe to run against Nuxeo using this Delegate
# and start Cantaloupe as a standalone server
# See https://github.com/cantaloupe-project/cantaloupe/blob/develop/cantaloupe.properties.sample

http.enabled = true
http.host = 0.0.0.0
http.port = 8182

max_pixels = 10000000
source.static = HttpSource
delegate_script.enabled = true

HttpSource.lookup_strategy = ScriptLookupStrategy

# Specific properties for the Nuxeo Delegate:
nuxeo.url=http:/example.com/nuxeo
nuxeo.username=Username
nuxeo.secret=Password

44 changes: 44 additions & 0 deletions lib/build-example.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Change path to Cantaloupe Jar to feed your needs and rename to 'build.gradle'

def cantaloupe_jar = "/opt/cantaloupe/cantaloupe-6.0-SNAPSHOT.jar"

plugins {
id 'java-library'
}

group "io.memorix.cantaloupe"
version "1.0"
archivesBaseName = "io.memorix.cantaloupe"

repositories {
mavenCentral()
}

test {
testLogging.showStandardStreams = true
jvmArgs '-Dcantaloupe.config=cantaloupe.minimal.properties'
}

dependencies {
api 'com.google.code.gson:gson:2.8.9'
api 'io.github.cdimascio:dotenv-java:2.2.0'

implementation(files(cantaloupe_jar))
testImplementation(files(cantaloupe_jar))
testImplementation 'org.junit.jupiter:junit-jupiter:5.7.2'


}

tasks.named('test') {
// Use JUnit Platform for unit tests.
useJUnitPlatform()
}

jar {
manifest {
attributes(
'Main-Class': 'io.memorix.cantaloupe.Main'
)
}
}
32 changes: 32 additions & 0 deletions lib/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
plugins {
id 'java-library'
}

def CANTALOUPE_JAR = "/Users/mlindeman/Code/software/cantaloupe/target/cantaloupe-6.0-SNAPSHOT.jar"
group "io.memorix"
version "1.0"
archivesBaseName = "cantaloupe-nuxeo-delegate"

repositories {
// Use Maven Central for resolving dependencies.
mavenCentral()
}

test {
testLogging.showStandardStreams = true
jvmArgs '-Dcantaloupe.config=cantaloupe.minimal.properties'
}

dependencies {
api 'com.google.code.gson:gson:2.8.9'

compileOnly(files(CANTALOUPE_JAR))
testImplementation(files(CANTALOUPE_JAR))
testImplementation 'org.junit.jupiter:junit-jupiter:5.7.2'
testImplementation 'commons-cli:commons-cli:1.5.0'
}

tasks.named('test') {
// Use JUnit Platform for unit tests.
useJUnitPlatform()
}
111 changes: 111 additions & 0 deletions lib/src/main/java/io/memorix/cantaloupe/CantaloupeNuxeoDelegate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package io.memorix.cantaloupe;

import edu.illinois.library.cantaloupe.config.Configuration;
import edu.illinois.library.cantaloupe.delegate.AbstractJavaDelegate;
import edu.illinois.library.cantaloupe.delegate.JavaDelegate;
import edu.illinois.library.cantaloupe.delegate.Logger;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CantaloupeNuxeoDelegate extends AbstractJavaDelegate implements JavaDelegate {

protected Configuration config = Configuration.getInstance();
final NuxeoClient nuxeoClient = new NuxeoClient();

@Override
public Boolean preAuthorize() {

return true;
}

@Override
public Boolean authorize() {
return true;
}

@Override
public Map<String,Object> getHTTPSourceResourceInfo() {
Map<String,Object> resource = new HashMap<>();
resource.put("uri", nuxeoClient.getFileUrlForIdentifier(getContext().getIdentifier()));
resource.put("username", config.getString(NuxeoKey.NX_USERNAME.key()));
resource.put("secret", config.getString(NuxeoKey.NX_SECRET.key()));
return resource;
}

@Override
public Map<String,Object> getExtraIIIF2InformationResponseKeys() {
return getExtraIIIF3InformationResponseKeys();
}

@Override
public Map<String,Object> getExtraIIIF3InformationResponseKeys() {
if (!config.getBoolean(NuxeoKey.NX_LOAD_PROPERTIES.key(), false)) return Collections.emptyMap();
else return nuxeoClient.getMetadata(getContext().getIdentifier());
}

@Override
public String getSource() {
//NB this will only be used when source.delegate = true
return "HttpSource";
}

@Override
public String getMetadata() {
Logger.debug(getContext().getMetadata().toString());
return null;
}

@Override
public String getAzureStorageSourceBlobKey() {
return null;
}

@Override
public String getFilesystemSourcePathname() {
return null;
}

@Override
public String getJDBCSourceDatabaseIdentifier() {
return null;
}

@Override
public String getJDBCSourceMediaType() {
return null;
}

@Override
public String getJDBCSourceLookupSQL() {
return null;
}

@Override
public Map<String,String> getS3SourceObjectInfo() {
return null;
}

@Override
public Map<String,Object> getOverlay() {
return null;
}

@Override
public List<Map<String,Long>> getRedactions() {
return Collections.emptyList();
}

@Override
public String serializeMetaIdentifier(Map<String,Object> metaIdentifier) {
return null;
}

@Override
public Map<String,Object> deserializeMetaIdentifier(String metaIdentifier) {
return null;
}

}
103 changes: 103 additions & 0 deletions lib/src/main/java/io/memorix/cantaloupe/NuxeoClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package io.memorix.cantaloupe;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.net.Proxy;
import java.nio.charset.StandardCharsets;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.Base64;
import java.util.Collections;
import java.util.HashMap;
import java.io.IOException;
import java.net.InetSocketAddress;

import edu.illinois.library.cantaloupe.config.Configuration;
import edu.illinois.library.cantaloupe.delegate.Logger;


public final class NuxeoClient {

public static final String NX_FILE_PATH = "/nxfile/default/";
public static final String NX_API_PATH = "/api/v1/id/";

protected final Configuration config = Configuration.getInstance();
public final String url = config.getString(NuxeoKey.NX_URL.key()).replaceAll("/+$", "");


public static String getUrl()
{
final NuxeoClient self = new NuxeoClient();
return self.url;
}

private OkHttpClient getHttpClient() {
final OkHttpClient.Builder builder = new OkHttpClient.Builder();
final boolean httpProxyEnabled = config.getBoolean(NuxeoKey.NX_PROXY_ENABLED.key(), false);
if (httpProxyEnabled) {
final String httpProxyServer = config.getString(NuxeoKey.NX_PROXY_SERVER.key());
if (httpProxyServer == null) {
throw new RuntimeException(String.format("proxy server setting '%s' should not be empty", NuxeoKey.NX_PROXY_SERVER.key()));
}
final int httpProxyPort = config.getInt(NuxeoKey.NX_PROXY_PORT.key(), 8080);
Logger.debug(String.format("******* [MRX:getHTTPClient] Using HTTP Proxy at server %s on port %d", httpProxyServer, httpProxyPort));
Proxy httpProxy = new Proxy(Proxy.Type.HTTP,new InetSocketAddress(httpProxyServer, httpProxyPort));
builder.proxy(httpProxy);
}
return builder.build();
}

public String getFileUrlForIdentifier(String identifier) {
return String.format("%s%s%s", url, NX_FILE_PATH, identifier);
}

public Map<String,Object> getMetadata(String identifier) {
if (!config.getBoolean(NuxeoKey.NX_LOAD_PROPERTIES.key(), false)) return Collections.emptyMap();

String url = String.format("%s%s%s?properties=*", this.url, NX_API_PATH, identifier);

Request.Builder builder = new Request.Builder().method("GET", null).url(url);
byte[] BasicAuthHeader = String.format("%s:%s",
config.getString(NuxeoKey.NX_USERNAME.key()),
config.getString(NuxeoKey.NX_SECRET.key())
).getBytes(StandardCharsets.UTF_8);
builder.addHeader("Authorization", "Basic " + Base64.getEncoder().encodeToString(BasicAuthHeader));

Response response;

Map<String, Object> metadata = new HashMap<>();

try {
response = getHttpClient().newCall(builder.build()).execute();
} catch (IOException e) {
metadata.put("nuxeoPropertiesError", "Failed to load properties from Nuxeo" + e.getMessage());
return metadata;
}

if (!response.isSuccessful()) {
metadata.put("nuxeoPropertiesError", "Failed to load properties from Nuxeo");
return metadata;
}

String body;
try {
body = response.body().string();
} catch (IOException e) {
metadata.put("nuxeoPropertiesError", "Failed to load body from Nuxeo response");
return metadata;
}
Map<String, Object> nuxeoProperties = new Gson().fromJson(
body, new TypeToken<HashMap<String, Object>>() {}.getType()
);

metadata.put("nuxeoProperties", nuxeoProperties);

return metadata;
}

}
26 changes: 26 additions & 0 deletions lib/src/main/java/io/memorix/cantaloupe/NuxeoKey.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.memorix.cantaloupe;

public enum NuxeoKey {

NX_URL("nuxeo.url"),
NX_USERNAME("nuxeo.username"),
NX_SECRET("nuxeo.secret"),
NX_LOAD_PROPERTIES("nuxeo.load_properties"),
NX_PROXY_ENABLED("nuxeo.proxy.enabled"),
NX_PROXY_SERVER("nuxeo.proxy.server"),
NX_PROXY_PORT("nuxeo.proxy.port");

private final String key;

NuxeoKey(String key) {
this.key = key;
}

public String key() {
return key;
}

public String toString() {
return key();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.memorix.cantaloupe.CantaloupeNuxeoDelegate
Loading

0 comments on commit a27ec15

Please sign in to comment.