/*
*
* ProLinga-Web
*
* Copyright (C) 2002-2008 Xobas Software.
* All rights reserved.
*
* This file is part of ProLinga-Web.
*
* ProLinga-Web is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ProLinga-Web is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ProLinga-Web. If not, see .
*
* More information is available at the following addresses:
*
* Website : http://www.prolinga.org
*
* Email : prolinga-list@prolinga.org
*
*
*/
#include "WebCommon.h"
#include
#include
#include
#include "WebService.hpp"
#include "WCWebAccess.hpp"
xmlDocPtr CreateWebRequest(char **cgiVars, char **serviceName)
{
xmlDocPtr docReq = NULL;
xmlNodePtr curReq, curCmd, curObj;
int i;
/* Create Default Web Command Document */
docReq = xmlNewDoc((const xmlChar *)"1.0");
docReq->children = xmlNewDocNode(docReq, NULL, (const xmlChar *)"ProLinga", NULL);
curReq = xmlDocGetRootElement(docReq);
curReq = xmlNewTextChild (curReq, NULL, (const xmlChar *)"Web", (const xmlChar *)"");
xmlNewProp (curReq, (const xmlChar *)"Version", (const xmlChar *)"1.0");
curReq = xmlNewTextChild (curReq, NULL, (const xmlChar *)"Command", (const xmlChar *)"");
curCmd = curReq;
xmlNewProp (curReq, (const xmlChar *)"Name", (const xmlChar *)"Default");
xmlNewProp (curReq, (const xmlChar *)"Mode", (const xmlChar *)"Request");
xmlNewProp (curReq, (const xmlChar *)"SessionId", (const xmlChar *)"None");
xmlNewProp (curReq, (const xmlChar *)"Format", (const xmlChar *)"HTML");
curReq = xmlNewTextChild (curReq, NULL, (const xmlChar *)"Object", (const xmlChar *)"");
curObj = curReq;
xmlNewProp (curReq, (const xmlChar *)"Service", (const xmlChar *)"Default");
xmlNewProp (curReq, (const xmlChar *)"Application", (const xmlChar *)"Default");
xmlNewProp (curReq, (const xmlChar *)"Name", (const xmlChar *)"Default");
curReq = xmlNewTextChild (curReq, NULL, (const xmlChar *)"Parameters", (const xmlChar *)"");
/* Get all cgi values and place them in request document */
for (i=0; cgiVars[i]; i+=2)
{
if (strcmp(cgiVars[i], "webcmd") == 0)
xmlSetProp(curCmd, (const xmlChar *)"Name", (const xmlChar *)cgiVars[i+1]);
else if (strcmp(cgiVars[i], "session") == 0)
xmlSetProp(curCmd, (const xmlChar *)"SessionId", (const xmlChar *)cgiVars[i+1]);
else if (strcmp(cgiVars[i], "format") == 0)
xmlSetProp(curCmd, (const xmlChar *)"Format", (const xmlChar *)cgiVars[i+1]);
else if (strcmp(cgiVars[i], "service") == 0)
{
xmlSetProp(curObj, (const xmlChar *)"Service", (const xmlChar *)cgiVars[i+1]);
/* Set Service Name */
*serviceName = new char[strlen(cgiVars[i+1])+1];
strcpy(*serviceName, cgiVars[i+1]);
}
else if (strcmp(cgiVars[i], "appn") == 0)
xmlSetProp(curObj, (const xmlChar *)"Application", (const xmlChar *)cgiVars[i+1]);
else if (strcmp(cgiVars[i], "name") == 0)
xmlSetProp(curObj, (const xmlChar *)"Name", (const xmlChar *)cgiVars[i+1]);
else
{
curReq = xmlNewTextChild (curReq, NULL, (const xmlChar *)"Parameter", (const xmlChar *)cgiVars[i+1]);
xmlNewProp (curReq, (const xmlChar *)"Name", (const xmlChar *)cgiVars[i]);
curReq = curReq->parent;
}
/* Clean up */
free(cgiVars[i]);
free(cgiVars[i+1]);
}
if (*serviceName == NULL)
{
*serviceName = new char[7];
strcpy(*serviceName, "Default");
}
/* Clean up */
free(cgiVars) ;
/* Return */
return docReq;
}
#if !defined(BUILD_RUN)
char *GetServiceEndPoint(char *serviceName, xmlDocPtr docSvc)
{
xmlXPathContextPtr ctxSvc;
xmlXPathObjectPtr resSvc;
char *serviceEndPoint = NULL;
char hostName[32], hostPort[32], tmpValue[255], tmpEndPoint[255];
/* Init XPath */
xmlXPathInit();
/* Create XPath environment */
ctxSvc = xmlXPathNewContext(docSvc);
/* Get service host and port */
sprintf(tmpValue, "/ProLinga/WebServices/WebService[@Name='%s']/ServiceHost", serviceName);
resSvc = xmlXPathEval((const xmlChar *)tmpValue, ctxSvc);
strcpy(hostName, (char *)xmlXPathCastToString(resSvc));
sprintf(tmpValue, "/ProLinga/WebServices/WebService[@Name='%s']/ServicePort", serviceName);
resSvc = xmlXPathEval((const xmlChar *)tmpValue, ctxSvc);
strcpy(hostPort, (char *)xmlXPathCastToString(resSvc));
/* Get + Set EndPoint */
sprintf(tmpEndPoint, "%s:%s", hostName, hostPort);
serviceEndPoint = new char[strlen(tmpEndPoint) + 1];
strcpy(serviceEndPoint, tmpEndPoint);
/* Return */
return serviceEndPoint;
}
#endif
#if !defined(BUILD_RUN)
int GetServiceCompressionLevel(char *serviceName, xmlDocPtr docSvc)
{
xmlXPathContextPtr ctxSvc;
xmlXPathObjectPtr resSvc;
char *serviceEndPoint = NULL;
char tmpValue[255];
int compLevel;
/* Create XPath environment */
ctxSvc = xmlXPathNewContext(docSvc);
/* Get service compression level */
sprintf(tmpValue, "/ProLinga/WebServices/WebService[@Name='%s']/ServiceCompressionLevel", serviceName);
resSvc = xmlXPathEval((const xmlChar *)tmpValue, ctxSvc);
compLevel = (int)xmlXPathCastToNumber(resSvc);
/* Return */
return compLevel;
}
#endif
xmlDocPtr WCWebCommand(char **cgiVars, xmlDocPtr docSvc)
{
xmlDocPtr docReq, docRes = NULL;
char *serviceName = NULL;
#if !defined(BUILD_RUN)
char *serviceEndPoint = NULL;
int serviceCompressionLevel = 0;
#endif
/* Create Web Command */
docReq = CreateWebRequest(cgiVars, &serviceName);
#if !defined(BUILD_RUN)
/* GetServiceEndPoint */
serviceEndPoint = GetServiceEndPoint(serviceName, docSvc);
/* Get Service Compression Level */
serviceCompressionLevel = GetServiceCompressionLevel(serviceName, docSvc);
#endif
/* Send request, get response */
#if BUILD_RUN
docRes = WCWebAccess(docReq);
#else
docRes = WCWebAccess(docReq, serviceEndPoint, serviceCompressionLevel);
#endif
/* Clean up */
#if !defined(BUILD_RUN)
delete serviceEndPoint;
#endif
delete serviceName;
/* Return */
return docRes;
}