-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for listeners to function S3ListBucket
- Loading branch information
1 parent
6d2cd73
commit c00f8f8
Showing
8 changed files
with
243 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
#Build Number for ANT. Do not edit! | ||
#Wed Nov 22 15:12:11 CET 2023 | ||
build.number=13 | ||
#Wed Nov 22 15:37:52 CET 2023 | ||
build.number=14 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 43 additions & 19 deletions
62
source/java/src/org/lucee/extension/resource/s3/function/S3ListBucket.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,69 @@ | ||
package org.lucee.extension.resource.s3.function; | ||
|
||
import org.lucee.extension.resource.s3.S3; | ||
import org.lucee.extension.resource.s3.listener.ComponentListListener; | ||
import org.lucee.extension.resource.s3.listener.ListListener; | ||
import org.lucee.extension.resource.s3.listener.UDFListListener; | ||
|
||
import lucee.loader.engine.CFMLEngine; | ||
import lucee.loader.engine.CFMLEngineFactory; | ||
import lucee.runtime.Component; | ||
import lucee.runtime.PageContext; | ||
import lucee.runtime.exp.PageException; | ||
import lucee.runtime.type.Query; | ||
import lucee.runtime.type.UDF; | ||
import lucee.runtime.util.Cast; | ||
|
||
public class S3ListBucket extends S3Function { | ||
|
||
private static final long serialVersionUID = 3486553628255584848L; | ||
|
||
public static Query call(PageContext pc, String bucketName, String accessKeyId, String secretAccessKey, String host, double timeout) throws PageException { | ||
|
||
@Override | ||
public Object invoke(PageContext pc, Object[] args) throws PageException { | ||
CFMLEngine eng = CFMLEngineFactory.getInstance(); | ||
// for backward compatibility, when host was not existing | ||
if (eng.getDecisionUtil().isNumber(host)) { | ||
timeout = eng.getCastUtil().toDoubleValue(host); | ||
host = null; | ||
} | ||
Cast cast = eng.getCastUtil(); | ||
if (args.length < 1 || args.length > 7) throw eng.getExceptionUtil().createFunctionException(pc, "S3ListBucket", 1, 7, args.length); | ||
|
||
// required | ||
String bucketName = cast.toString(args[0]); | ||
|
||
// optional | ||
Object listener = args.length > 1 && args[1] != null ? args[1] : null; | ||
int blockSize = args.length > 2 && args[2] != null ? cast.toIntValue(args[2]) : 1000; | ||
String accessKeyId = args.length > 3 && args[3] != null ? cast.toString(args[3]) : null; | ||
String secretAccessKey = args.length > 4 && args[4] != null ? cast.toString(args[4]) : null; | ||
String host = args.length > 5 && args[5] != null ? cast.toString(args[5]) : null; | ||
double timeout = args.length > 6 && !isEmpty(args[6]) ? cast.toDoubleValue(args[6]) : 0; | ||
|
||
// validate | ||
|
||
try { | ||
S3 s3 = S3.getInstance(toS3Properties(pc, accessKeyId, secretAccessKey, host), toTimeout(timeout), pc.getConfig()); | ||
return s3.listObjectsAsQuery(bucketName); | ||
|
||
// no listener | ||
if (listener == null) { | ||
return s3.listObjectsAsQuery(bucketName, blockSize, null); | ||
} | ||
else { | ||
ListListener list = toListener(eng, pc, listener); | ||
list.before(); | ||
s3.listObjectsAsQuery(bucketName, blockSize, toListener(eng, pc, listener)); | ||
list.after(); | ||
} | ||
} | ||
catch (Exception e) { | ||
throw CFMLEngineFactory.getInstance().getCastUtil().toPageException(e); | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public Object invoke(PageContext pc, Object[] args) throws PageException { | ||
CFMLEngine engine = CFMLEngineFactory.getInstance(); | ||
Cast cast = engine.getCastUtil(); | ||
if (args.length == 5) return call(pc, cast.toString(args[0]), cast.toString(args[1]), cast.toString(args[2]), cast.toString(args[3]), cast.toDoubleValue(args[4])); | ||
if (args.length == 4) return call(pc, cast.toString(args[0]), cast.toString(args[1]), cast.toString(args[2]), cast.toString(args[3]), 0); | ||
if (args.length == 3) return call(pc, cast.toString(args[0]), cast.toString(args[1]), cast.toString(args[2]), null, 0); | ||
if (args.length == 2) return call(pc, cast.toString(args[0]), cast.toString(args[1]), null, null, 0); | ||
if (args.length == 1) return call(pc, cast.toString(args[0]), null, null, null, 0); | ||
throw engine.getExceptionUtil().createFunctionException(pc, "S3ListBucket", 1, 5, args.length); | ||
private ListListener toListener(CFMLEngine eng, PageContext pc, Object listener) throws PageException { | ||
if (listener instanceof UDF) { | ||
return new UDFListListener(eng, pc, (UDF) listener); | ||
} | ||
if (listener instanceof Component) { | ||
return new ComponentListListener(eng, pc, (Component) listener); | ||
} | ||
throw CFMLEngineFactory.getInstance().getExceptionUtil().createFunctionException(pc, "S3ListBucket", 2, "listener", | ||
"invalid listener type [" + listener.getClass().getName() + "], only functions and components are supported as listeners", ""); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
source/java/src/org/lucee/extension/resource/s3/listener/ComponentListListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package org.lucee.extension.resource.s3.listener; | ||
|
||
import org.lucee.extension.resource.s3.function.S3Download; | ||
|
||
import lucee.loader.engine.CFMLEngine; | ||
import lucee.loader.util.Util; | ||
import lucee.runtime.Component; | ||
import lucee.runtime.PageContext; | ||
import lucee.runtime.exp.PageException; | ||
import lucee.runtime.type.Collection.Key; | ||
import lucee.runtime.type.Query; | ||
|
||
public class ComponentListListener implements ListListener { | ||
|
||
private PageContext pc; | ||
private Component listener; | ||
private Component csa; | ||
private Key INVOKE; | ||
private Key BEFORE; | ||
private Key AFTER; | ||
private CFMLEngine eng; | ||
|
||
public ComponentListListener(CFMLEngine eng, PageContext pc, Component listener) throws PageException { | ||
INVOKE = eng.getCastUtil().toKey("invoke"); | ||
BEFORE = eng.getCastUtil().toKey("before"); | ||
AFTER = eng.getCastUtil().toKey("after"); | ||
this.eng = eng; | ||
this.pc = pc; | ||
this.listener = listener; | ||
csa = S3Download.toComponentSpecificAccess(Component.ACCESS_PRIVATE, listener); | ||
|
||
} | ||
|
||
@Override | ||
public void before() throws PageException { | ||
if (S3Download.toFunction(csa.get(BEFORE, null), null) != null) { | ||
listener.call(pc, BEFORE, new Object[] {}); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean invoke(Query data) throws PageException { | ||
if (S3Download.toFunction(csa.get(INVOKE, null), null) != null) { | ||
Object res = listener.call(pc, INVOKE, new Object[] { data }); | ||
if (res == null || Util.isEmpty(res.toString())) return true; | ||
return eng.getCastUtil().toBooleanValue(res); | ||
} | ||
else { | ||
throw eng.getExceptionUtil().createFunctionException(pc, "S3ListBucket", 2, "component", | ||
"the listener component does not contain a instance function with name [invoke] that is required", null); | ||
} | ||
} | ||
|
||
@Override | ||
public void after() throws PageException { | ||
if (S3Download.toFunction(csa.get(AFTER, null), null) != null) { | ||
listener.call(pc, AFTER, new Object[] {}); | ||
} | ||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
source/java/src/org/lucee/extension/resource/s3/listener/ListListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package org.lucee.extension.resource.s3.listener; | ||
|
||
import lucee.runtime.exp.PageException; | ||
import lucee.runtime.type.Query; | ||
|
||
public interface ListListener { | ||
|
||
public void before() throws PageException; | ||
|
||
public boolean invoke(Query data) throws PageException; | ||
|
||
public void after() throws PageException; | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
source/java/src/org/lucee/extension/resource/s3/listener/UDFListListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package org.lucee.extension.resource.s3.listener; | ||
|
||
import lucee.loader.engine.CFMLEngine; | ||
import lucee.loader.util.Util; | ||
import lucee.runtime.PageContext; | ||
import lucee.runtime.exp.PageException; | ||
import lucee.runtime.type.Query; | ||
import lucee.runtime.type.UDF; | ||
|
||
public class UDFListListener implements ListListener { | ||
|
||
private PageContext pc; | ||
private UDF listener; | ||
private CFMLEngine eng; | ||
|
||
public UDFListListener(CFMLEngine eng, PageContext pc, UDF listener) { | ||
this.eng = eng; | ||
this.pc = pc; | ||
this.listener = listener; | ||
} | ||
|
||
@Override | ||
public boolean invoke(Query data) throws PageException { | ||
Object res = listener.call(pc, new Object[] { data }, true); | ||
if (res == null || Util.isEmpty(res.toString())) return true; | ||
return eng.getCastUtil().toBooleanValue(res); | ||
} | ||
|
||
@Override | ||
public void before() { | ||
} | ||
|
||
@Override | ||
public void after() { | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters