Notification text and HTML interface.
authorJiří Filipovič <fila@ics.muni.cz>
Thu, 9 Oct 2008 11:17:55 +0000 (11:17 +0000)
committerJiří Filipovič <fila@ics.muni.cz>
Thu, 9 Oct 2008 11:17:55 +0000 (11:17 +0000)
org.glite.lb.server/src/lb_html.c
org.glite.lb.server/src/lb_html.h
org.glite.lb.server/src/lb_proto.c
org.glite.lb.server/src/lb_proto.h
org.glite.lb.server/src/lb_text.c
org.glite.lb.server/src/lb_text.h

index 3ac7ddb..ccb4ab4 100644 (file)
 #define UNUSED_VAR
 #endif
 
+static char *xmlToHTML(char *xml) {
+       char *html = strdup("");
+       int i = 0;
+       int j = 0;
+       while (xml[i]){
+               if (xml[i] == '<'){
+                       html = realloc(html, (j+strlen("&lt;")+1)*sizeof(*html) );
+                       strcpy(html+j, "&lt;");
+                       j += strlen("&lt;");
+               }
+               else if (xml[i] == '>'){
+                       html = realloc(html, (j+strlen("&gt;")+1)*sizeof(*html) );
+                       strcpy(html+j, "&gt;");
+                       j += strlen("&gt;");
+               }
+               else{
+                       html = realloc(html, (j+2)*sizeof(*html));
+                       html[j] = xml[i];
+                       j++;
+               }
+               i++;
+       }
+       html[j] = 0;
+
+       return html;
+}
+
 int edg_wll_QueryToHTML(edg_wll_Context ctx UNUSED_VAR, edg_wll_Event *eventsOut UNUSED_VAR, char **message UNUSED_VAR)
 {
 /* not implemented yet */
@@ -23,38 +50,110 @@ int edg_wll_QueryToHTML(edg_wll_Context ctx UNUSED_VAR, edg_wll_Event *eventsOut
 }
 
 /* construct Message-Body of Response-Line for edg_wll_UserJobs */
-int edg_wll_UserJobsToHTML(edg_wll_Context ctx, edg_wlc_JobId *jobsOut, char **message)
+int edg_wll_UserInfoToHTML(edg_wll_Context ctx UNUSED_VAR, edg_wlc_JobId *jobsOut, char **notifids, char **message)
 {
-        char *pomA, *pomB;
+        char *pomA = NULL, *pomB;
         int i = 0;
 
-       /* head */
-       pomB = strdup("");      
+        /* head */
+        pomB = strdup("");
 
         while (jobsOut[i]) {
-               char    *chid = edg_wlc_JobIdUnparse(jobsOut[i]);
+                char    *chid = edg_wlc_JobIdUnparse(jobsOut[i]);
 
                 asprintf(&pomA,"%s\t\t <li> <a href=\"%s\">%s</a>\r\n",
                         pomB, chid,chid);
 
-               free(chid);
+                free(chid);
                 free(pomB);
                 pomB = pomA;
                 i++;
         }
 
-        asprintf(&pomA, "<html>\r\n\t<body>\r\n"
-                       "<h2><B>User jobs</B></h2>\r\n"
-                       "User subject: %s<p>"
-                       "<ul>%s</ul>"
-                       "\t</body>\r\n</html>",ctx->peerName?ctx->peerName: "&lt;anonymous&gt;",pomB);
-        free(pomB);
+       char *pomC = NULL, *pomD;
+       pomD = strdup("");
+       i = 0;
+
+       while(notifids[i]){
+               asprintf(&pomC, "%s\t\t <li> <a href=\"/notif/%s\">%s</a>\r\n",
+                               pomD,
+                               notifids[i],
+                               notifids[i]
+                       );
+               free(pomD);
+               pomD = pomC;
+               i++;
+       }
 
-        *message = pomA;
+       char *ret;
+       asprintf(&ret, "<html>\r\n\t<body>\r\n");
+       pomA = ret;
+       if (pomB[0]){
+               asprintf(&ret, "%s<h2><B>User jobs</B></h2>\r\n"
+                       "<ul>%s</ul>",
+                       pomA, pomB
+               );
+               free(pomA);
+               free(pomB);
+       }
+       pomA = ret;
+       if (pomC){
+               asprintf(&ret, "%s<h2><B>User notifications</B></h2>\r\n"
+                        "<ul>%s</ul>",
+                        pomA, pomD
+                );
+               free(pomA);
+               free(pomD);
+       }
+       pomA = ret;
+       asprintf(&ret, "%sUser subject: %s<p>"
+               "\t</body>\r\n</html>",
+               pomA, ctx->peerName?ctx->peerName: "&lt;anonymous&gt;"
+       );
+       free(pomA);
+
+        *message = ret;
 
         return 0;
 }
 
+#define TR(name,type,field)             \
+        if (field) {            \
+                asprintf(&pomA,"%s<tr><th align=\"left\">" name ":</th>"        \
+                        "<td>" type "</td></tr>",pomB,(field)); \
+                free(pomB);                                     \
+                pomB = pomA;                                    \
+        }
+
+int edg_wll_NotificationToHTML(edg_wll_Context ctx UNUSED_VAR, notifInfo *ni, char **message){
+       char *pomA, *pomB;
+       pomB = strdup("");
+
+       TR("Destination", "%s", ni->destination);
+       TR("Valid until", "%s", ni->valid);
+       char *cond = xmlToHTML(ni->conditions);
+       asprintf(&pomA, "%s<h3>Conditions</h3>\r\n<pre>%s</pre>\r\n",
+               pomB, cond);
+       free(cond);
+       free(pomB);
+       pomB = pomA;
+       if (ni->JDL_VirtualOrganisation && ni->JDL_VirtualOrganisation[0])
+               TR("JDL VirtualOrganisation", "%s", ni->JDL_VirtualOrganisation);
+       if (ni->STD_owner && ni->STD_owner[0])
+               TR("STD owner", "%s", ni->STD_owner);
+       if (ni->STD_network_server && ni->STD_network_server[0])
+               TR("STD network server", "%s", ni->STD_network_server);
+
+       asprintf(&pomA, "<html>\r\n\t<body>\r\n"
+               "<h2>Norification %s</h2>\r\n"
+               "<table halign=\"left\">%s</table>"
+               "\t</body>\r\n</html>",
+               ni->notifid, pomB);
+       
+       *message = pomA;
+
+       return 0;
+}
 
 /* construct Message-Body of Response-Line for edg_wll_JobStatus */
 int edg_wll_JobStatusToHTML(edg_wll_Context ctx UNUSED_VAR, edg_wll_JobStat stat, char **message)
@@ -70,14 +169,6 @@ int edg_wll_JobStatusToHTML(edg_wll_Context ctx UNUSED_VAR, edg_wll_JobStat stat
 
         chid = edg_wlc_JobIdUnparse(stat.jobId);
 
-#define TR(name,type,field)            \
-       if (field) {            \
-               asprintf(&pomA,"%s<tr><th align=\"left\">" name ":</th>"        \
-                       "<td>" type "</td></tr>",pomB,(field)); \
-               free(pomB);                                     \
-               pomB = pomA;                                    \
-       }
-
        TR("Status","%s",(chstat = edg_wll_StatToString(stat.state)));
        free(chstat);
        TR("owner","%s",stat.owner);
index ada457c..335b6ac 100644 (file)
@@ -9,7 +9,7 @@
 
 int edg_wll_QueryToHTML(edg_wll_Context,edg_wll_Event *,char **);
 int edg_wll_JobStatusToHTML(edg_wll_Context, edg_wll_JobStat, char **);
-int edg_wll_UserJobsToHTML(edg_wll_Context, edg_wlc_JobId *, char **);
+int edg_wll_UserInfoToHTML(edg_wll_Context, edg_wlc_JobId *, char **, char **);
 char *edg_wll_ErrorToHTML(edg_wll_Context,int);
 
 #endif /* GLITE_LB_HTML_H */
index e039c2c..8db8237 100644 (file)
@@ -11,6 +11,8 @@
 #include "glite/lb/context-int.h"
 #include "glite/lb/mini_http.h"
 #include "glite/lb/xml_conversions.h"
+#include "glite/jobid/strmd5.h"
+#include "glite/lbu/trio.h"
 
 #include "lb_proto.h"
 #include "lb_text.h"
@@ -21,6 +23,7 @@
 #include "purge.h"
 #include "lb_xml_parse.h"
 #include "lb_xml_parse_V21.h"
+#include "db_supp.h"
 
 
 #define METHOD_GET      "GET "
@@ -207,6 +210,78 @@ static int drain_text_request(char *request){
                return 0;
 }
 
+static int getUserNotifications(edg_wll_Context ctx, char *user, char ***notifids){
+        char *q = NULL;
+        glite_lbu_Statement notifs = NULL;
+        char *notifc[1] = {NULL};
+
+        trio_asprintf(&q, "select notifid "
+                "from notif_registrations "
+                "where userid='%s'",
+                user);
+        if (edg_wll_ExecSQL(ctx, q, &notifs) < 0) goto err;
+        free(q); q = NULL;
+
+        int n = 0;
+        *notifids = NULL;
+        while(edg_wll_FetchRow(ctx, notifs, sizeof(notifc)/sizeof(notifc[0]), NULL, notifc)){
+                n++;
+                *notifids = realloc(*notifids, n*sizeof(**notifids));
+                (*notifids)[n-1] = strdup(notifc[n-1]);
+                printf("Notif %s found\n", notifc[n-1]);
+        }
+       if (n){
+               *notifids = realloc(*notifids, (n+1)*sizeof(**notifids));
+               (*notifids)[n] = NULL;
+       }
+        return n;
+
+err:
+        return 0;
+}
+
+static int getNotifInfo(edg_wll_Context ctx, char *notifId, notifInfo *ni){
+       char *q = NULL;
+        glite_lbu_Statement notif = NULL;
+       char *notifc[6] = {NULL, NULL, NULL, NULL, NULL, NULL};
+
+       trio_asprintf(&q, "select destination, valid, conditions, "
+               "JDL_VirtualOrganisation, STD_owner, STD_network_server "
+                "from notif_registrations "
+                "where notifid='%s'",
+                notifId);
+       if (edg_wll_ExecSQL(ctx, q, &notif) < 0) goto err;
+        free(q); q = NULL;
+
+       ni->notifid = strdup(notifId);
+       if (edg_wll_FetchRow(ctx, notif, sizeof(notifc)/sizeof(notifc[0]), NULL, notifc)){
+               ni->destination = notifc[0];
+               ni->valid = notifc[1];
+               ni->conditions = notifc[2];
+               ni->JDL_VirtualOrganisation = notifc[3];
+               ni->STD_owner = notifc[4];
+               ni->STD_network_server = notifc[5];
+       }
+       else 
+               goto err;
+
+       printf("%s\n%s\n", ni->destination, ni->valid);
+
+       return 0;
+
+err:
+       return  edg_wll_Error(ctx, NULL, NULL);
+}
+
+static void freeNotifInfo(notifInfo *ni){
+       if (ni->notifid) free(ni->notifid);
+       if (ni->destination) free(ni->destination);
+       if (ni->valid) free(ni->valid);
+       if (ni->conditions) free(ni->conditions);
+       if (ni->JDL_VirtualOrganisation) free(ni->JDL_VirtualOrganisation);
+       if (ni->STD_owner) free(ni->STD_owner);
+       if (ni->STD_network_server) free(ni->STD_network_server);
+}
 
 edg_wll_ErrorCode edg_wll_ProtoV21(edg_wll_Context ctx,
        char *request,char **headers,char *messageBody,
@@ -453,12 +528,18 @@ edg_wll_ErrorCode edg_wll_Proto(edg_wll_Context ctx,
                        
                        flags = (requestPTR[1]=='?') ? edg_wll_string_to_stat_flags(requestPTR + 2) : 0;
 
+                       char **notifids = NULL;
+                       char *can_peername = edg_wll_gss_normalize_subj(ctx->peerName, 0);
+                       char *userid = strmd5(can_peername, NULL);
+                       free(can_peername);
+                       getUserNotifications(ctx, userid, &notifids);
+
 // FIXME: edg_wll_UserJobs should take flags as parameter
                        switch (edg_wll_UserJobsServer(ctx,&jobsOut,NULL)) {
                                case 0: if (text)
-                                               edg_wll_UserJobsToText(ctx, jobsOut, &message);
+                                               edg_wll_UserInfoToText(ctx, jobsOut, notifids, &message);
                                        else if (html)
-                                               edg_wll_UserJobsToHTML(ctx, jobsOut, &message);
+                                               edg_wll_UserInfoToHTML(ctx, jobsOut, notifids, &message);
                                        else ret = HTTP_OK;
                                        break;
                                case ENOENT: ret = HTTP_NOTFOUND; break;
@@ -474,10 +555,16 @@ edg_wll_ErrorCode edg_wll_Proto(edg_wll_Context ctx,
                                for (i=0; jobsOut[i]; i++) edg_wlc_JobIdFree(jobsOut[i]);
                                free(jobsOut);
                        }
+                       if (notifids){
+                               for (i = 0; notifids[i]; i++) 
+                                       free(notifids[i]);
+                               free(notifids);
+                       }
                } 
 
        /* GET /[jobId]: Job Status */
-               else if (*requestPTR=='/') {
+               else if (*requestPTR=='/' 
+                       && strncmp(requestPTR, "/notif/", strlen("/notif/"))) {
                        edg_wlc_JobId jobId = NULL;
                        char *pom1,*fullid = NULL;
                        edg_wll_JobStat stat;
@@ -520,6 +607,22 @@ edg_wll_ErrorCode edg_wll_Proto(edg_wll_Context ctx,
                        free(fullid);
                        edg_wlc_JobIdFree(jobId);
                        edg_wll_FreeStatus(&stat);
+       /*GET /notif/[notifId]: Norification info*/
+               } else if (strncmp(requestPTR, "/notif/", strlen("/notif/")) == 0){
+                       notifInfo ni;
+                       char *pomCopy, *pom;
+                       pomCopy = strdup(requestPTR + 1);
+                        for (pom=pomCopy; *pom && !isspace(*pom); pom++);
+                        *pom = 0;
+                       getNotifInfo(ctx, strrchr(pomCopy, '/')+1, &ni);
+                       free(pomCopy);
+
+                       if (text)
+                               edg_wll_NotificationToText(ctx, &ni, &message);
+                       else
+                               edg_wll_NotificationToHTML(ctx, &ni, &message);
+
+                       freeNotifInfo(&ni);
 
        /* GET [something else]: not understood */
                } else ret = HTTP_BADREQ;
index 13dcd5b..412f931 100644 (file)
@@ -20,6 +20,16 @@ extern edg_wll_ErrorCode edg_wll_Proto(
        char **         /* OUT: HTTP response body */
 );
 
+typedef struct _notifInfo{
+        char *notifid;
+        char *destination;
+        char *valid;
+        char *conditions;
+        char *JDL_VirtualOrganisation;
+        char *STD_owner;
+        char *STD_network_server;
+} notifInfo;
+
 extern char *edg_wll_HTTPErrorMessage(int);
 
 extern int edg_wll_UserJobsServer(edg_wll_Context ctx, edg_wlc_JobId  **jobs, edg_wll_JobStat **states);
index 95089f7..0a9b5ee 100644 (file)
@@ -4,6 +4,8 @@
 #include "lb_proto.h"
 
 #include "glite/lb/context-int.h"
+#include "glite/lbu/trio.h"
+#include "glite/lbu/db.h"
 
 #include <string.h>
 #include <stdio.h>
@@ -53,7 +55,7 @@ int edg_wll_QueryToText(edg_wll_Context ctx UNUSED_VAR, edg_wll_Event *eventsOut
 }
 
 /* construct Message-Body of Response-Line for edg_wll_UserJobs */
-int edg_wll_UserJobsToText(edg_wll_Context ctx, edg_wlc_JobId *jobsOut, char **message)
+/*int edg_wll_UserJobsToText(edg_wll_Context ctx, edg_wlc_JobId *jobsOut, char **message)
 {
        char *a, *b;
        int i = 0;
@@ -80,9 +82,84 @@ int edg_wll_UserJobsToText(edg_wll_Context ctx, edg_wlc_JobId *jobsOut, char **m
         *message = a;
 
         return 0;
+}*/
+
+int edg_wll_UserInfoToText(edg_wll_Context ctx, edg_wlc_JobId *jobsOut, char **notifids, char **message)
+{
+        char *a = NULL, *b;
+        int i = 0;
+        b = strdup("");
+
+        while (jobsOut[i]){
+                char *chid = edg_wlc_JobIdUnparse(jobsOut[i]);
+
+                if (i == 0)
+                        asprintf(&a, "%s%s", b, chid);
+                else
+                        asprintf(&a, "%s,%s", b, chid);
+
+                free(chid);
+                free(b);
+                b = a;
+                i++;
+        }
+
+       char *c = NULL, *d;
+       i = 0;
+       d = strdup("");
+       while(notifids[i]){
+               if (i == 0)
+                       asprintf(&c, "%s", notifids[i]);
+               else
+                       asprintf(&c, "%s,%s", d, notifids[i]);
+               free(d);
+               d = c;
+               i++;
+       }
+
+       if (a){
+               asprintf(&a, "User_jobs=%s\n", b);
+               b = a;
+               if (c)
+                       asprintf(&a, "%sUser_notifications=%s\n", b, d);
+       }
+       else if (c)
+               asprintf(&a, "User_notifications=%s\n", d);
+       b = a;
+
+        if (a)
+               asprintf(&a, "%sUser_subject=%s\n", b, ctx->peerName ? ctx->peerName: "<anonymous>");
+       else
+               asprintf(&a, "User_subject=%s\n", ctx->peerName ? ctx->peerName: "<anonymous>");
+
+        *message = a;
+
+        return 0;
 }
 
 
+int edg_wll_NotificationToText(edg_wll_Context ctx UNUSED_VAR, notifInfo *ni, char **message){
+       char *a = NULL, *b = NULL;
+       asprintf(&a, "Notif_id=%s\n", ni->notifid);
+       asprintf(&b, "%sDestination=%s\n", a, ni->destination);
+       asprintf(&a, "%sValid_until=%s\n", b, ni->valid);
+       char *cond = escape_text(ni->conditions);
+       asprintf(&b, "%sConditions=%s\n", a, cond);
+       free(cond);
+       a = b;
+       if (ni->JDL_VirtualOrganisation && ni->JDL_VirtualOrganisation[0])
+               asprintf(&a, "%sJDL_VirtualOrganisation=%s\n", b, ni->JDL_VirtualOrganisation);
+       b = a;
+       if (ni->STD_owner && ni->STD_owner[0])
+               asprintf(&a, "%sSTD_owner=%s\n", b, ni->STD_owner);
+       b = a;
+       if (ni->STD_network_server && ni->STD_network_server[0])
+               asprintf(&a, "%sSTD_network_server=%s\n", b, ni->STD_network_server);
+       *message = a;
+
+       return 0;
+}
+
 /* construct Message-Body of Response-Line for edg_wll_JobStatus */
 int edg_wll_JobStatusToText(edg_wll_Context ctx UNUSED_VAR, edg_wll_JobStat stat, char **message)
 {
@@ -143,7 +220,6 @@ int edg_wll_JobStatusToText(edg_wll_Context ctx UNUSED_VAR, edg_wll_JobStat stat
                asprintf(&jdl,"Job_description=%s\n", my_jdl);
                free(my_jdl);
        }
-
        if (stat.rsl) asprintf(&rsl,"RSL=%s\n", stat.rsl);
 
         asprintf(&a, "Job=%s\n"
index 042e1fb..8d9eace 100644 (file)
@@ -7,7 +7,7 @@
 
 int edg_wll_QueryToText(edg_wll_Context,edg_wll_Event *,char **);
 int edg_wll_JobStatusToText(edg_wll_Context, edg_wll_JobStat, char **);
-int edg_wll_UserJobsToText(edg_wll_Context, edg_wlc_JobId *, char **);
+int edg_wll_UserInfoToText(edg_wll_Context, edg_wlc_JobId *, char **, char **);
 char *edg_wll_ErrorToText(edg_wll_Context,int);
 
 #endif /* GLITE_LB_TEXT */