44import com .genexus .*;
55import com .genexus .internet .HttpContext ;
66
7+ import com .genexus .util .GXFile ;
78import org .json .JSONArray ;
89import com .genexus .json .JSONObjectWrapper ;
910import org .apache .commons .io .FilenameUtils ;
1011
1112import java .io .InputStream ;
12-
13+ import java . lang . reflect .*;
1314
1415public 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