All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.sparqlanything.binary.BinaryTriplifier Maven / Gradle / Ivy

There is a newer version: 0.9.0
Show newest version
/*
 * Copyright (c) 2024 SPARQL Anything Contributors @ http://github.com/sparql-anything
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.github.sparqlanything.binary;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;
import java.util.Set;

import io.github.sparqlanything.model.*;
import io.github.sparqlanything.model.annotations.Option;
import org.apache.commons.codec.binary.Base64;
import org.apache.jena.datatypes.xsd.XSDDatatype;
import com.google.common.collect.Sets;
import org.apache.jena.graph.NodeFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@io.github.sparqlanything.model.annotations.Triplifier
public class BinaryTriplifier implements Triplifier {

	private static final Logger logger = LoggerFactory.getLogger(BinaryTriplifier.class);

	@Option(description = "The encoding to use for generating the representation of the file.", validValues = "BASE64")
	public static final IRIArgument ENCODING = new IRIArgument("bin.encoding", Encoding.BASE64.name());

	public enum Encoding {
		BASE64
	}

	@Override
	public void triplify(Properties properties, FacadeXGraphBuilder builder) throws IOException {

		URL url = Triplifier.getLocation(properties);
		if (url == null) {
			logger.warn("No location provided");
			return;
		}
		String encodingString = PropertyUtils.getStringProperty(properties, ENCODING);
		Encoding encoding = Encoding.valueOf(encodingString);
		String dataSourceId = SPARQLAnythingConstants.DATA_SOURCE_ID;

		String value;
		byte[] file = downloadUrl(url);

		switch (encoding) {
		case BASE64:
			value = Base64.encodeBase64String(file);
			break;
		default:
			value = Base64.encodeBase64String(file);
			break;
		}
		// Add root
		builder.addRoot(dataSourceId);
		// Add content
		builder.addValue(dataSourceId, SPARQLAnythingConstants.ROOT_ID, 1, NodeFactory.createLiteralByValue(value, XSDDatatype.XSDbase64Binary));
	}

	private byte[] downloadUrl(URL toDownload) {
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

		try {
			byte[] chunk = new byte[4096];
			int bytesRead;
			InputStream stream = toDownload.openStream();

			while ((bytesRead = stream.read(chunk)) > 0) {
				outputStream.write(chunk, 0, bytesRead);
			}

			stream.close();

			byte[] result = outputStream.toByteArray();
			outputStream.close();
			return result;
		} catch (IOException e) {
			logger.error("",e);
			return null;
		}
	}

	@Override
	public Set getMimeTypes() {
		return Sets.newHashSet("application/octet-stream");
	}

	@Override
	public Set getExtensions() {
		return Sets.newHashSet("bin", "dat");
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy