66import edu .cmu .oli .content .models .persistance .entities .ContentPackage ;
77import edu .cmu .oli .content .models .persistance .entities .ErrorLevel ;
88import edu .cmu .oli .content .models .persistance .entities .Resource ;
9+ import edu .cmu .oli .content .resource .builders .Xml2Json ;
910import org .apache .tika .Tika ;
1011import org .apache .xml .resolver .tools .CatalogResolver ;
1112import org .jdom2 .Attribute ;
2223import javax .json .JsonValue ;
2324import javax .naming .InitialContext ;
2425import javax .naming .NamingException ;
25-
26+ import javax .ws .rs .client .ClientBuilder ;
27+ import javax .ws .rs .client .Entity ;
28+ import javax .ws .rs .client .WebTarget ;
29+ import javax .ws .rs .core .MediaType ;
30+ import javax .ws .rs .core .Response ;
2631import java .io .*;
2732import java .nio .file .Files ;
2833import java .nio .file .Path ;
2934import java .security .MessageDigest ;
3035import java .security .NoSuchAlgorithmException ;
3136import java .security .SecureRandom ;
32- import java .util .ArrayList ;
33- import java .util .HashMap ;
34- import java .util .List ;
35- import java .util .Map ;
36- import java .util .Optional ;
37- import java .util .Random ;
38- import java .util .UUID ;
37+ import java .util .*;
3938import java .util .stream .Collectors ;
40- import edu .cmu .oli .content .resource .builders .Xml2Json ;
4139
4240/**
4341 * @author Raphael Gachuhi
@@ -92,7 +90,7 @@ public static String inputStreamToString(InputStream input) throws IOException {
9290 }
9391
9492 public static void addToPackageError (ContentPackage contentPackage , String message , String source ,
95- ErrorLevel level ) {
93+ ErrorLevel level ) {
9694 if (contentPackage .getErrors () == null ) {
9795 JsonObject errors = new JsonObject ();
9896 errors .addProperty ("contentPackageErrors" , contentPackage .getId () + "_" + contentPackage .getVersion ());
@@ -129,13 +127,13 @@ public static void addToResourceError(Resource resource, String message, String
129127
130128 private static boolean isQuestionWithParts (Element element ) {
131129 if (element .getName ().equals ("multiple_choice" )
132- || element .getName ().equals ("ordering" )
133- || element .getName ().equals ("short_answer" )
134- || element .getName ().equals ("essay" )
135- || element .getName ().equals ("numeric" )
136- || element .getName ().equals ("text" )
137- || element .getName ().equals ("fill_in_the_blank" )
138- || element .getName ().equals ("question" )) {
130+ || element .getName ().equals ("ordering" )
131+ || element .getName ().equals ("short_answer" )
132+ || element .getName ().equals ("essay" )
133+ || element .getName ().equals ("numeric" )
134+ || element .getName ().equals ("text" )
135+ || element .getName ().equals ("fill_in_the_blank" )
136+ || element .getName ().equals ("question" )) {
139137 return true ;
140138 }
141139
@@ -169,8 +167,8 @@ private static String getQuestionLabel(Element question) {
169167
170168 switch (question .getName ()) {
171169 case "multiple_choice" :
172- if (question .getAttribute ("select" ) != null
173- && question .getAttribute ("select" ).getValue ().equals ("single" )) {
170+ if (question .getAttribute ("select" ) != null
171+ && question .getAttribute ("select" ).getValue ().equals ("single" )) {
174172 return "Multiple Choice" ;
175173 }
176174 return "Check All That Apply" ;
@@ -219,16 +217,16 @@ public static int countPoolQuestions(Element element) {
219217 if (element .getName ().equals ("pool" )) {
220218 for (Element c : element .getChildren ()) {
221219 switch (c .getName ()) {
222- case "multiple_choice" :
223- case "ordering" :
224- case "short_answer" :
225- case "essay" :
226- case "numeric" :
227- case "text" :
228- case "fill_in_the_blank" :
229- case "question" :
230- count = count + 1 ;
231- default :
220+ case "multiple_choice" :
221+ case "ordering" :
222+ case "short_answer" :
223+ case "essay" :
224+ case "numeric" :
225+ case "text" :
226+ case "fill_in_the_blank" :
227+ case "question" :
228+ count = count + 1 ;
229+ default :
232230 }
233231 }
234232 }
@@ -368,29 +366,26 @@ public enum EmbedActivityType {
368366 UNKNOWN ("UNKNOWN" );
369367
370368 private String type ;
371-
369+
372370 EmbedActivityType (String type ) {
373371 this .type = type ;
374372 }
375-
373+
376374 public String getAsString () {
377375 return type ;
378376 }
379-
377+
380378 private static final Map <String , EmbedActivityType > reverseLookup = new HashMap <>();
381-
379+
382380 // Populate the reverse lookup table on loading time
383- static
384- {
385- for (EmbedActivityType activityType : EmbedActivityType .values ())
386- {
381+ static {
382+ for (EmbedActivityType activityType : EmbedActivityType .values ()) {
387383 reverseLookup .put (activityType .getAsString (), activityType );
388384 }
389385 }
390-
386+
391387 //This method can be used for reverse lookup purpose
392- public static EmbedActivityType fromString (String type )
393- {
388+ public static EmbedActivityType fromString (String type ) {
394389 return reverseLookup .get (type );
395390 }
396391 }
@@ -399,7 +394,7 @@ public static EmbedActivityType inferEmbedActivityType(JsonObject embedActivity)
399394 // use activity_type property or infer type based on content
400395 if (embedActivity .has ("@activity_type" )) {
401396 // use activity_type attribute to determine type
402- switch (embedActivity .get ("@activity_type" ).getAsString ().toLowerCase ()) {
397+ switch (embedActivity .get ("@activity_type" ).getAsString ().toLowerCase ()) {
403398 case "repl" :
404399 return EmbedActivityType .REPL ;
405400 default :
@@ -424,7 +419,7 @@ public static EmbedActivityType inferEmbedActivityType(JsonObject embedActivity)
424419 String source = itemObj .get ("source" ).getAsJsonObject ().get ("#text" ).getAsString ();
425420 if (source .endsWith ("activity.js" ) || source .endsWith ("repl.js" )) {
426421 flags = flags | 0b1;
427-
422+
428423 if (flags == REPL_FLAGS ) {
429424 return EmbedActivityType .REPL ;
430425 }
@@ -467,7 +462,20 @@ public static EmbedActivityType inferEmbedActivityType(JsonObject embedActivity)
467462 }
468463
469464 }
470-
465+
471466 return EmbedActivityType .UNKNOWN ;
472467 }
468+
469+ public static Response .Status sendSlackAlert (JsonObject message ) {
470+ String slackHook = System .getenv ().get ("slack_alert_hook" );
471+ if (slackHook == null || slackHook .isEmpty () || slackHook .equalsIgnoreCase ("none" )) {
472+ return Response .Status .FORBIDDEN ;
473+ }
474+ WebTarget target = ClientBuilder .newClient ().target (slackHook );
475+ Response response = target .request (MediaType .APPLICATION_JSON )
476+ .post (Entity .json (AppUtils .gsonBuilder ().create ().toJson (message )));
477+ log .info ("response code " + response .getStatusInfo () + " code " + response .getStatus ());
478+
479+ return Response .Status .fromStatusCode (response .getStatus ());
480+ }
473481}
0 commit comments