|
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 bucketNameExp;
|
|
Expression keyNameExp;
|
|
Expression filePathExp;
|
|
XdmMap metadataMap = null;
|
|
|
|
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.bucketNameExp = makeAttributeValueTemplate(bn, bucketName);
|
|
|
|
AttributeInfo keyName = atts.get(NamespaceUri.NULL, "keyName");
|
|
String kn = null;
|
|
|
|
if (keyName == null) {
|
|
reportAbsence("keyName");
|
|
} else {
|
|
kn = keyName.getValue();
|
|
}
|
|
|
|
this.keyNameExp = makeAttributeValueTemplate(kn, keyName);
|
|
|
|
AttributeInfo filePath = atts.get(NamespaceUri.NULL, "filePath");
|
|
String fp = null;
|
|
|
|
if (filePath == null) {
|
|
reportAbsence("filePath");
|
|
} else {
|
|
fp = filePath.getValue();
|
|
}
|
|
|
|
this.filePathExp = makeAttributeValueTemplate(fp, filePath);
|
|
}
|
|
|
|
public void validate(ComponentDeclaration decl) throws XPathException {
|
|
super.validate(decl);
|
|
|
|
this.bucketNameExp = typeCheck("bucketName", this.bucketNameExp);
|
|
this.keyNameExp = typeCheck("keyName", this.keyNameExp);
|
|
this.filePathExp = typeCheck("filePath", this.filePathExp);
|
|
}
|
|
|
|
public Expression compile(Compilation exec, ComponentDeclaration decl) throws XPathException {
|
|
return (Expression) new UploadToS3BucketInstruction(this.bucketNameExp, this.keyNameExp,
|
|
this.filePathExp, this.metadataMap);
|
|
}
|
|
|
|
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;
|
|
XdmMap metadata = null;
|
|
|
|
private UploadToS3BucketInstruction() {
|
|
}
|
|
|
|
public UploadToS3BucketInstruction(Expression bucketName, Expression keyName, Expression filePath,
|
|
XdmMap metadataMap) {
|
|
Expression[] subs = { bucketName, keyName, filePath };
|
|
|
|
setArguments(subs);
|
|
this.metadata = metadataMap;
|
|
}
|
|
|
|
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[BUCKET_NAME]);
|
|
String keyName = str(arguments[KEY_NAME]);
|
|
String filePath = str(arguments[FILE_PATH]);
|
|
String result = "result"; // unset as of yet
|
|
|
|
try {
|
|
if (bucketName.length() > 0 && keyName.length() > 0 && filePath.length() > 0 && metadata != null)
|
|
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();
|
|
}
|
|
}
|
|
}
|