package com._3kbo.talis;
import org.apache.http.entity.FileEntity
import groovyx.net.http.RESTClient
import groovyx.net.http.HttpResponseException
/**
* TalisStoreLoader is a Groovy utility class for uploading RDF files to the Talis Platform
* See Using Groovy to Upload RDF files to the Talis Platform for details.
* @author Richard Hancock
*/
class TalisStoreLoader {
static final String TALIS_DEV_HOST = "http://api.talis.com/"
static final String APPLICATION_RDF = "application/rdf+xml"
static final String CONFIGURATION_FILE = "TalisConfig.groovy"
static final String TALIS_STORE_NAME = "mystore"
static final String TALIS_USER = "user"
static final String TALIS_PASSWORD = "password"
RESTClient talis = new RESTClient( TALIS_DEV_HOST )
String store = TALIS_STORE_NAME
String user = TALIS_USER
String password = TALIS_PASSWORD
public TalisStoreLoader() {
super();
init()
talis.auth.basic user, password
talis.encoder.'application/rdf+xml' = this.&encodeRDF
}
public TalisStoreLoader(String store, String user, String password) {
super();
this.store = store;
this.user = user;
this.password = password;
talis.auth.basic user, password
talis.encoder.'application/rdf+xml' = this.&encodeRDF
}
def encodeRDF( Object data ) throws UnsupportedEncodingException {
if ( data instanceof File ) {
def entity = new FileEntity( (File) data, "application/rdf+xml" );
entity.setContentType( "application/rdf+xml" );
return entity
} else {
throw new IllegalArgumentException(
"Don't know how to encode ${data.class.name} as application/rdf+xml" );
}
}
public String getMetaboxPath() {
return TALIS_DEV_HOST + "stores/" + store + "/meta"
}
public String toString() { "${store} ${user} ${password}" }
void init() {
def loader = talis.class.classLoader.rootLoader
if (loader == null) {
println("rootLoader is null")
loader = talis.class.classLoader
}
def url
def config
if (loader != null) {
println "Classpath:"
loader.URLs.each{ println it }
url = loader.getResource(CONFIGURATION_FILE)
}
if (url != null) {
println("Using config file from classpath : ${url}")
config = new ConfigSlurper().parse(url)
println(config)
}
else {
def userHome = System.getProperty("user.home")
def config_url = "file:${userHome}/.groovy/conf/${CONFIGURATION_FILE}"
println("Looking in user home ${userHome} for config file ${config_url}")
config = new ConfigSlurper().parse(new URL(config_url))
println(config)
}
if (config != null) {
store = config.talis.store
user = config.talis.user
password = config.talis.password
}
println "Using store: ${store}, metaboxPath: ${metaboxPath}, user : [${user}], password: [${password}]"
}
/**
* Load an rdf file
* @param file The RDF file to upload
* @return 1 if the RDF file was successfully uploaded, 0 otherwise.
*/
def int loadRdfFile(File file) {
int count = 0
if (file.file) {
println "${(new Date()).toTimestamp() }: Loading $file.absolutePath"
try {
def start = System.currentTimeMillis()
// def res = talis.post( path: TALIS_METABOX_PATH, body: file, requestContentType: APPLICATION_RDF )
def res = talis.post( path: metaboxPath, body: file, requestContentType: "application/rdf+xml" )
println "${(new Date()).toTimestamp() }: Loaded ${file.size()} bytes in ${System.currentTimeMillis() - start} milliseconds. (Status: ${res.status})"
count = 1
}
catch( HttpResponseException ex ) {
println ex.response.data
println ex.statusCode
ex.printStackTrace()
}
catch( ex ) {
println ex
ex.printStackTrace()
}
}
return count
}
/**
* Load only *.rdf files from the current directory
* @return count of the RDF files successfully uploaded.
*/
def int loadDirectory() {
return loadDirectory(new File("."))
}
/**
* Load only *.rdf files from the specified directory
* @param directory Path to the directory to scoan for RDF files to upload
* @return count of the RDF files successfully uploaded.
*/
def int loadDirectory(String directory) {
println "Loading RDF files from directory path $directory"
def begin = System.currentTimeMillis()
def pattern = ~/.*\.rdf/
new File(directory).eachFileMatch(pattern) {
loadRdfFile it
count++
}
println "Loaded ${count} files in ${System.currentTimeMillis() - begin} milliseconds."
return count
}
/**
* Load only *.rdf files from the specified directory
* @param directory The directory to scoan for RDF files to upload
* @return count of the RDF files successfully uploaded.
*/
def int loadDirectory(File directory) {
println "Loading RDF files from directory ${directory.absolutePath}"
def begin = System.currentTimeMillis()
def pattern = ~/.*\.rdf/
def count = 0
directory.eachFileMatch(pattern) {
loadRdfFile it
count++
}
println "Loaded ${count} files in ${System.currentTimeMillis() - begin} milliseconds."
return count
}
/**
* List *.rdf files in the specified directory
* @param directory Path to the directory to scoan for RDF files to upload
*/
def int listDirectory(String directory) {
println "Listing RDF files in directory ${new File(directory).absolutePath}"
def pattern = ~/.*\.rdf/
def count = 0
new File(directory).eachFileMatch(pattern) {
println it
count++
}
println "Total RDF files: ${count}"
return count
}
static main(args) {
def store = new TalisStoreLoader()
println store.metaboxPath
// store.listDirectory(".")
store.loadDirectory
}
}