|
package us.ajbconsulting.xslt.extensions.exposures.instructions.aws.s3;
|
|
|
|
import net.sf.saxon.expr.Expression;
|
|
import net.sf.saxon.expr.SimpleExpression;
|
|
import net.sf.saxon.expr.XPathContext;
|
|
import net.sf.saxon.expr.parser.RebindingMap;
|
|
import net.sf.saxon.om.AttributeInfo;
|
|
import net.sf.saxon.om.AttributeMap;
|
|
import net.sf.saxon.om.Item;
|
|
import net.sf.saxon.om.NamespaceUri;
|
|
import net.sf.saxon.om.Sequence;
|
|
import net.sf.saxon.s9api.XdmMap;
|
|
import net.sf.saxon.style.Compilation;
|
|
import net.sf.saxon.style.ComponentDeclaration;
|
|
import net.sf.saxon.style.ExtensionInstruction;
|
|
import net.sf.saxon.trans.XPathException;
|
|
import net.sf.saxon.value.StringValue;
|
|
|
|
import us.ajbconsulting.xslt.extensions.Constants;
|
|
import us.ajbconsulting.xslt.extensions.core.AmazonWebServicesOps;
|
|
|
|
public class UploadToS3Bucket extends ExtensionInstruction {
|
|
Expression bucketName;
|
|
Expression keyName;
|
|
Expression filePath;
|
|
Expression metadata;
|
|
|
|
protected boolean mayContainSequenceConstructor() {
|
|
return false;
|
|
}
|
|
|
|
protected void prepareAttributes() {
|
|
AttributeMap atts = attributes();
|
|
|
|
AttributeInfo bucketName = atts.get(NamespaceUri.NULL, "bucketName");
|
|
String bn = null;
|
|
|
|
if (bucketName == null) {
|
|
reportAbsence("bucketName");
|
|
} else {
|
|
bn = bucketName.getValue();
|
|
}
|
|
|
|
this.bucketName = makeAttributeValueTemplate(bn, bucketName);
|
|
|
|
AttributeInfo keyName = atts.get(NamespaceUri.NULL, "keyName");
|
|
String kn = null;
|
|
|
|
if (keyName == null) {
|
|
reportAbsence("keyName");
|
|
} else {
|
|
kn = keyName.getValue();
|
|
}
|
|
|
|
this.keyName = makeAttributeValueTemplate(kn, keyName);
|
|
|
|
AttributeInfo filePath = atts.get(NamespaceUri.NULL, "filePath");
|
|
String fp = null;
|
|
|
|
if (filePath == null) {
|
|
reportAbsence("filePath");
|
|
} else {
|
|
fp = filePath.getValue();
|
|
}
|
|
|
|
this.filePath = makeAttributeValueTemplate(fp, filePath);
|
|
|
|
AttributeInfo metadata = atts.get(NamespaceUri.NULL, "metadata");
|
|
String md = null;
|
|
|
|
if (metadata == null) {
|
|
reportAbsence("metadata");
|
|
} else {
|
|
md = metadata.getValue();
|
|
}
|
|
|
|
this.metadata = makeAttributeValueTemplate(md, metadata);
|
|
}
|
|
|
|
public void validate(ComponentDeclaration decl) throws XPathException {
|
|
super.validate(decl);
|
|
|
|
this.bucketName = typeCheck("bucketName", this.bucketName);
|
|
this.keyName = typeCheck("keyName", this.keyName);
|
|
this.filePath = typeCheck("filePath", this.filePath);
|
|
this.metadata = typeCheck("metadata", this.metadata);
|
|
}
|
|
|
|
public Expression compile(Compilation exec, ComponentDeclaration decl) throws XPathException {
|
|
return (Expression) new UploadToS3BucketInstruction(this.bucketName, this.keyName, this.filePath, this.metadata);
|
|
}
|
|
|
|
public static class UploadToS3BucketInstruction extends SimpleExpression {
|
|
public static final int BUCKET_NAME = 0;
|
|
public static final int KEY_NAME = 1;
|
|
public static final int FILE_PATH = 2;
|
|
public static final int METADATA = 3;
|
|
|
|
private UploadToS3BucketInstruction() {
|
|
}
|
|
|
|
public UploadToS3BucketInstruction(Expression bucketName, Expression keyName, Expression filePath, Expression metadata) {
|
|
Expression[] subs = { bucketName, keyName, filePath, metadata };
|
|
setArguments(subs);
|
|
}
|
|
|
|
public int getImplementationMethod() {
|
|
return 1;
|
|
}
|
|
|
|
protected int computeCardinality() {
|
|
return 16384;
|
|
}
|
|
|
|
public String getExpressionType() {
|
|
return Constants.AJB_PREFIX + ":" + Constants.AWS_UPLOAD_TO_S3_BUCKET_LOCAL_NAME;
|
|
}
|
|
|
|
public UploadToS3BucketInstruction copy(RebindingMap rebindings) {
|
|
return (UploadToS3BucketInstruction) (new UploadToS3BucketInstruction()).copyOperandsFrom(this);
|
|
}
|
|
|
|
@Override
|
|
public Sequence call(XPathContext context, Sequence[] arguments) throws XPathException {
|
|
String bucketName = str(arguments[0]);
|
|
String keyName = str(arguments[1]);
|
|
String filePath = str(arguments[2]);
|
|
XdmMap metadata = (XdmMap) arguments[3].head().itemAt(0);
|
|
String result = "result"; // unset as of yet
|
|
|
|
try {
|
|
if (bucketName.length() > 0 && keyName.length() > 0 && filePath.length() > 0 && metadata.size() > 0)
|
|
AmazonWebServicesOps.uploadToS3Bucket(bucketName, keyName, filePath, metadata);
|
|
} catch (Exception e) {
|
|
throw (new XPathException("Problem executing aws-upload-to-s3-bucket command", e))
|
|
.withXPathContext(context).withErrorCode("AJB002").withLocation(getLocation());
|
|
}
|
|
|
|
return StringValue.makeStringValue(result);
|
|
}
|
|
|
|
private String str(Sequence iterator) throws XPathException {
|
|
Item item = iterator.head();
|
|
return (item == null) ? "" : item.getStringValue();
|
|
}
|
|
}
|
|
}
|