AWS DataPipeline EvaluateExpression using Java SDK - java

Using the AWS DataPipeline API, I'm trying to programmatically evaluate an Expression like the following:
sometext-#{format(#scheduledStartTime, 'YYYYMMddHHmmss')
To evaluate the expression, I'm using a PipelineObject that looks something like the following:
Id:#MyPipelineObject_2018-08-26T01:00:00
Name:#MyPipelineObject_2018-08-26T01:00:00
- Key:#scheduledStartTime
- StringValue:2018-08-26T01:00:00
- Key:#scheduledEndTime
- StringValue:2018-08-27T01:00:00
How can I evaluate the expression, given that I know the pipelineId and pipelineObjectId? I'm using the Java AWS SDK, and creating an EvaluateExpressionRequest like so:
String expressionToBeEvaluated = "sometext-#{format(#scheduledStartTime, 'YYYYMMddHHmmss')";
String myPipelineObjectId = "#MyPipelineObject_2018-08-26T01:00:00";
EvaluateExpressionRequest evaluateExpressionRequest = new EvaluateExpressionRequest()
.withPipelineId(myPipelineId)
.withExpression(expressionToBeEvaluated)
.withObjectId(myPipelineObjectId);
However, from the docs it's not clear to me how to actually issue the request with the EvaluateExpressionRequest object. I've looked at EvaluateExpressionResult but the setEvaluatedExpression method only takes a String as input.
Am I doing something wrong, missing something fundamental, or does the SDK just not support what I'm trying to do?
Any input or suggestions would be really appreciated. Thanks!

So I figured it out a few minutes after posting my question. It turns out the answer is very simple and I've just been looking at this stuff for too long. The DataPipeline object has the method evaluateExpression() which takes an EvaluateExpressionRequest and returns the EvaluateExpressionResult. You get the evaluated result by calling getEvaluatedExpression on the returned object.
EvaluateExpressionRequest evaluateExpressionRequest = new EvaluateExpressionRequest()
.withPipelineId(myPipelineId)
.withExpression(expressionToBeEvaluated)
.withObjectId(myPipelineObjectId);
dataPipeline.evaluateExpression(evaluateExpressionRequest).getEvaluatedExpression(); //evaluates to sometext-20180826010000
Hope that helps anyone with a similar affliction!

Related

How to get requestUri out of Cocoon into my Java function?

I inherited some old Cocoon code, if it's obvious, please tell me where in the docs I could find it.
I have a function to clean up some page parameters. I want to hand over the content to that function, but I can't manage to get there.
This is the original code, the value must pass my "cleanPath" function.
<jdbp:page-param name="pageToLoad" value="${{request.requestURI}}/../{#page}"/>
Attempt 1:
<jdbp:page-param name="pageToLoad" value="cleanPath(${{request.requestURI}}/../{#page})"/>
My attempt was to add the function in and leave everything like that, but it's not working.
My next attempt is these nice xsp:logic blocks, where I can't get the requestURI. "request.requestURI" is unknown, evaluate complains
"Type mismatch: cannot convert from Object to String".
<xsp:logic>
String input2 = evaluate("${{request.requestURI}}", String.class);
String input = "/../<xsl:value-of select="#page"/>";
putVariable("Hase","Test "+input);
</xsp:logic>
<jdbp:page-param name="pageToLoad" value="${{request.requestURI}}/../{#page}"/>
<jdbp:page-param name="Hase" value="${{Hase}}"/>
It's easy to just call request.getRequestURI();. No need for complicated wrappers.

How do I convert a String to date in velocity template?

I want to convert $departureFromDate (format:yyyy-MM-dd) to date object so that I can perform increment operations on it. I have been trying to do it the following way:
#set($departureFromDate = "{{jsonPath request.body
'$.departureFromDate'}}")
#set($dateObj = $date.toDate('yyyy-MM-dd',"$departureFromDate"))
#set($calendar = $date.getCalendar())
$calendar.setTime($dateObj)
$calendar.add(6,5)
The above code works if give an actual date like:
#set($dateObj = $date.toDate('yyyy-MM-dd',"2018-09-22"))
But does not work when I try to use $departureFromDate
There are several problems in your code. First, as user7294900 noted, the right value of the first assignation seems quite weird. Then, you don't need to instanciate yourself a calendar (plus, you can write $date.calendar instead of $date.getCalendar(), and you don't need double quotes around string arguments).
#set($body = '{ "departureFromDate" : "2018-03-01" }')
$json.parse($body)
#set($departureFromDate = $json.departureFromDate)
#set($dateObj = $date.toDate('yyyy-MM-dd', $departureFromDate))
#set($calendar = $date.toCalendar($dateObj))
$calendar.add(6, 5)
The above code uses a JSON parsing tool, whose parse() method renders a json wrapper, that you shall provide in your context.
As a final advise, if you hadn't already thought of it, be sure to print $obj and $obj.class.name in your context as a trivial debugging technique if you don't understand what happens.

Error when using Esapi validation

I hope someone could help me with some issue.
I'm using OWASP ESAPI 2.1.0 with JavaEE, to help me to validate some entries in a web application. At some point I needed to validate a Windows file path, so I added a new property entry in the 'validation.properties' like this one:
Validator.PathFile=^([a-zA-Z]:)?(\\\\[\\w. -]+)+$
When I try to validate, for example, a string like "C:\TEMP\file.txt" via ESAPI, I get a ValidationException:
ESAPI.validator().getValidInput("PathFile", "C:\\TEMP\\file.txt", "PathFile", 100, false);
Alternatively, I also tried the java.util.regex.Pattern class to test the same regular expression with the same string example and it works OK:
Pattern.matches("^([a-zA-Z]:)?(\\\\[\\w. -]+)+$", "C:\\TEMP\\file.txt")
I must say that I added other regex in 'validation.properties' and worked OK. Why this one is so hard? Could anyone help me out with this one?
This is happening because the call to validator().getValidInput("PathFile", "C:\\TEMP\\file.txt", "PathFile", 100, false); wraps a call to ESAPI.encoder().canonicalize() that is transforming the input to the char sequence (Not literal String!) C:TEMP'0x0C'ile.txt before it passes to the regex engine.
Except for the second "\" getting converted to the char 0x0c this is normally desired behavior. That could be a bug in ESAPI.
What you want, is to make a call to ESAPI.validator().getValidDirectoryPath()

Create a string with the result of an expression and the expression that originated the value. Is it possible?

Like
String r = SomeThing.toExecString("new Object().toString()");
And when executed the value of r would be:
"new Object().toString() = java.lang.Object#c5e3974"
Is this even possible at all? Would it need a bunch of reflection? A built in compiler maybe?
ScriptEngine engine = new ScriptEngineManager().getEngineByName("beanshell");
Object result = engine.eval("new Object().toString();");
System.out.println(result);
You may get close to what you want using BeanShell. I ran the above code with Java 6 with BeanShell 2.0b4 and the JSR 223-based bsh-engine.jar engine on the classpath.
There is a great post here:
Generating Static Proxy Classes - http://www.javaspecialists.eu/archive/Issue180.html
Part one is enough for what you asked, I think
Don't know if you really wanted this. But your problem would be solved with this method:
String toExecString( String code ) {
return String.format(
"\"%s\" = %s#%x",
code,
code.getClass().getName(),
code.hashCode()
);
}

How can I get the string result of a python method from the XML-RPC client in Java

I wrote:
Object result = (Object)client.execute("method",params);
in java client.
Actually, the result should be printed in string format. But I can only output the address of "Object result", how can I get the content?
And I have tried String result = (String)client.execute("method",params);
It says lang.until.Object can not cast to lang.util.String.
As the server is written in Python, I was wondering how can I retrieve String from the method.
I'm hesitant to post this because it seems rather obvious - forgive me if you've tried this, but how about:
String result = (String)client.execute("method",params);
so maybe the object returned is not a string... are you sure that you're returning a string in your python application? I seriously doubt it.

Categories

Resources