Skip to content

Commit 9ee846b

Browse files
iroquetaBeta Bot
authored andcommitted
Cherry pick branch 'genexuslabs:feature/gxobject_secure_control_hook' into beta
1 parent fdb8708 commit 9ee846b

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

java/src/main/java/com/genexus/webpanels/FileItem.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,8 @@ public void write(String wFile)
137137
gxFile.delete();
138138
}
139139
}
140+
141+
public GXFile getFile() {
142+
return gxFile;
143+
}
140144
}

java/src/main/java/com/genexus/webpanels/GXObjectUploadServices.java

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@
44
import com.genexus.*;
55
import com.genexus.internet.HttpContext;
66

7+
import com.genexus.util.GXFile;
78
import org.json.JSONArray;
89
import com.genexus.json.JSONObjectWrapper;
910
import org.apache.commons.io.FilenameUtils;
1011

1112
import java.io.InputStream;
12-
13+
import java.lang.reflect.*;
1314

1415
public class GXObjectUploadServices extends GXWebObjectStub
15-
{
16+
{
17+
private static final String ENV_HOOK_CLASS = "GX_UPLOAD_SECURITY_HOOK_CLASS";
1618
boolean isRestCall = false;
1719
String keyId;
1820
String jsonResponse = null;
@@ -53,6 +55,8 @@ protected void doExecute(HttpContext context) throws Exception
5355
context.setContentType("text/plain");
5456
FileItemCollection postedFiles = context.getHttpRequest().getPostedparts();
5557
JSONArray jsonArray = new JSONArray();
58+
short[] status = new short[1];
59+
String[] body = new String[1];
5660
for (int i = 0, len = postedFiles.getCount(); i < len; i++)
5761
{
5862
keyId = HttpUtils.getUploadFileKey();
@@ -72,6 +76,10 @@ protected void doExecute(HttpContext context) throws Exception
7276
if (!savedFileName.isEmpty()){
7377
HttpUtils.CacheUploadFile(keyId, savedFileName, fileName, ext);
7478
}
79+
if (executeUploadHook(modelContext, file.getFile(), fileName, status, body)) {
80+
context.sendResponseStatus(status[0], body[0]);
81+
return;
82+
}
7583
}
7684
}
7785
JSONObjectWrapper jObjResponse = new JSONObjectWrapper();
@@ -265,5 +273,39 @@ private String getExtension(String contentType)
265273
}
266274
protected void init(HttpContext context )
267275
{
268-
}
276+
}
277+
278+
private static boolean executeUploadHook(
279+
ModelContext ctx,
280+
GXFile file,
281+
String originalFileName,
282+
short[] httpStatusCode,
283+
String[] httpResponseBody) {
284+
285+
try {
286+
String hookClassName = System.getenv(ENV_HOOK_CLASS);
287+
if (hookClassName == null || hookClassName.isEmpty())
288+
return false;
289+
290+
Class<?> hookClass = Class.forName(hookClassName);
291+
Object hookInstance = hookClass
292+
.getConstructor(int.class, ModelContext.class)
293+
.newInstance(-1, ctx);
294+
295+
Method executeUdp = hookClass.getMethod(
296+
"executeUdp",
297+
GXFile.class,
298+
String.class,
299+
short[].class,
300+
String[].class);
301+
302+
Object result = executeUdp.invoke(hookInstance, file, originalFileName, httpStatusCode, httpResponseBody);
303+
return (Boolean) result;
304+
305+
} catch (Exception e) {
306+
httpStatusCode[0] = 400;
307+
httpResponseBody[0] = e.getMessage();
308+
return true;
309+
}
310+
}
269311
}

0 commit comments

Comments
 (0)