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

prompto.intrinsic.PromptoBinary Maven / Gradle / Ivy

The newest version!
package prompto.intrinsic;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import prompto.error.ReadWriteError;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;

/** a data structure which ties a piece of binary data to a mime type 
 * This is a bridge between BinaryValue and any IStore implementation
 * It is also a valid response type for user defined services
 */
public class PromptoBinary {

	private String mimeType;
	private byte[] bytes;
	private String sourceTable;
	private Object sourceDbId;
	private String sourceAttribute;
	
	public PromptoBinary() {
	}
	
	public PromptoBinary(String mimeType, byte[] bytes) {
		this.mimeType = mimeType;
		this.bytes = bytes;
	}
	
	public String getMimeType() {
		return mimeType;
	}
	
	public byte[] getBytes() {
		return bytes;
	}
	
	public String getSourceTable() {
		return sourceTable;
	}
	
	public Object getSourceDbId() {
		return sourceDbId;
	}
	
	public String getSourceAttribute() {
		return sourceAttribute;
	}
	
	public void setSource(String table, Object dbId, String attribute) {
		sourceTable = table;
		sourceDbId = dbId;
		sourceAttribute = attribute;
	}
	
	public String getSourceUrl() {
		if(sourceDbId!=null && sourceAttribute!=null)
			return "/ws/bin/data?dbId=" + sourceDbId + "&attribute=" + sourceAttribute + (sourceTable==null ? "" : "&table=" + sourceTable);
		else
			return null;
	}
	
	@SuppressWarnings("unchecked")
	public void populateFrom(Object value) {
		if(value instanceof PromptoDocument)
			populateFromDocument((PromptoDocument)value);
		else
			throw new UnsupportedOperationException();
	}

	private void populateFromDocument(PromptoDocument value) {
		try {
			Map datas = collectData(value);
			bytes = zipData(datas);
			mimeType = "application/zip";
		} catch(IOException e) {
			throw new ReadWriteError(e.getMessage());
		}
	}

	private Map collectData(PromptoDocument value) throws IOException {
		Map binaries = new HashMap<>();
		// create textual data
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		JsonGenerator generator = new JsonFactory().createGenerator(output);
		value.toJson(generator, null, null, true, binaries);
		generator.flush();
		generator.close();
		// add it
		binaries.put("value.json", output.toByteArray());
		return binaries;
	}

	private byte[] zipData(Map datas) throws IOException {
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		ZipOutputStream zip = new ZipOutputStream(output);
		for(Map.Entry part : datas.entrySet()) {
			ZipEntry entry = new ZipEntry(part.getKey());
			zip.putNextEntry(entry);
			zip.write(part.getValue());
			zip.closeEntry();
		}
		zip.close();
		return output.toByteArray();
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy