You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
431 lines
14 KiB
431 lines
14 KiB
15 years ago
|
/* This file is part of the KDE libraries
|
||
|
Copyright (C) 2004 Reinhold Kainhofer <reinhold@kainhofer.com>
|
||
|
|
||
|
Based on the davjob:
|
||
|
Copyright (C) 2002 Jan-Pascal van Best <janpascal@vanbest.org>
|
||
|
XML-RPC specific parts taken from the xmlrpciface:
|
||
|
Copyright (C) 2003 - 2004 by Frerich Raabe <raabe@kde.org>
|
||
|
Tobias Koenig <tokoe@kde.org>
|
||
|
|
||
|
|
||
|
This library is free software; you can redistribute it and/or
|
||
|
modify it under the terms of the GNU Library General Public
|
||
|
License as published by the Free Software Foundation; either
|
||
|
version 2 of the License, or (at your option) any later version.
|
||
|
|
||
|
This library 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
|
||
|
Library General Public License for more details.
|
||
|
|
||
|
You should have received a copy of the GNU Library General Public License
|
||
|
along with this library; see the file COPYING.LIB. If not, write to
|
||
|
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||
|
Boston, MA 02110-1301, USA.
|
||
|
*/
|
||
|
|
||
|
#include "xmlrpcjob.h"
|
||
|
|
||
15 years ago
|
#include <tqvariant.h>
|
||
|
#include <tqregexp.h>
|
||
15 years ago
|
|
||
|
#include <kdebug.h>
|
||
|
#include <klocale.h>
|
||
|
#include <kio/http.h>
|
||
|
#include <kmdcodec.h>
|
||
|
#include <kio/davjob.h>
|
||
|
|
||
|
|
||
15 years ago
|
#define KIO_ARGS TQByteArray packedArgs; \
|
||
|
TQDataStream stream( packedArgs, IO_WriteOnly ); stream
|
||
15 years ago
|
|
||
12 years ago
|
using namespace TDEIO;
|
||
15 years ago
|
|
||
12 years ago
|
namespace TDEIO {
|
||
15 years ago
|
class XMLRPCResult
|
||
|
{
|
||
|
friend class XmlrpcJob;
|
||
|
public:
|
||
|
XMLRPCResult() {}
|
||
|
bool success() const { return m_success; }
|
||
|
int errorCode() const { return m_errorCode; }
|
||
15 years ago
|
TQString errorString() const { return m_errorString; }
|
||
|
TQValueList<TQVariant> data() const { return m_data; }
|
||
15 years ago
|
private:
|
||
|
bool m_success;
|
||
|
int m_errorCode;
|
||
15 years ago
|
TQString m_errorString;
|
||
|
TQValueList<TQVariant> m_data;
|
||
15 years ago
|
};
|
||
|
}
|
||
|
|
||
|
class XmlrpcJob::XmlrpcJobPrivate
|
||
|
{
|
||
|
public:
|
||
15 years ago
|
// TQByteArray savedStaticData;
|
||
15 years ago
|
};
|
||
|
|
||
|
|
||
15 years ago
|
XmlrpcJob::XmlrpcJob( const KURL& url, const TQString& method,
|
||
|
const TQValueList<TQVariant> ¶ms, bool showProgressInfo)
|
||
12 years ago
|
: TransferJob( url, TDEIO::CMD_SPECIAL, TQByteArray(), TQByteArray(),
|
||
15 years ago
|
showProgressInfo )
|
||
|
{
|
||
|
d = new XmlrpcJobPrivate;
|
||
13 years ago
|
// We couldn't set the args when calling the parent constructor,
|
||
15 years ago
|
// so do it now.
|
||
15 years ago
|
TQDataStream stream( m_packedArgs, IO_WriteOnly );
|
||
15 years ago
|
stream << (int)1 << url;
|
||
|
kdDebug()<<"XMLrpcJob::url="<<url.url()<<endl;
|
||
|
kdDebug()<<"XmlrpcJob::XmlrpcJob, method="<<method<<endl;
|
||
|
// Same for static data
|
||
|
if ( ! method.isEmpty() ) {
|
||
|
kdDebug()<<"XmlrpcJob::XmlrpcJob, method not empty."<<endl;
|
||
|
|
||
15 years ago
|
TQString call = markupCall( method, params );
|
||
15 years ago
|
staticData = call.utf8();
|
||
|
staticData.truncate( staticData.size() - 1 );
|
||
|
kdDebug() << "Message: " << call << endl;
|
||
|
// d->savedStaticData = staticData.copy();
|
||
|
}
|
||
|
addMetaData( "UserAgent", "KDE XML-RPC TransferJob" );
|
||
|
addMetaData( "content-type", "Content-Type: text/xml; charset=utf-8" );
|
||
|
addMetaData( "ConnectTimeout", "50" );
|
||
|
}
|
||
|
|
||
|
XmlrpcJob::~XmlrpcJob()
|
||
|
{
|
||
|
delete d;
|
||
|
d = 0;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQString XmlrpcJob::markupCall( const TQString &cmd,
|
||
|
const TQValueList<TQVariant> &args )
|
||
15 years ago
|
{
|
||
|
kdDebug()<<"XmlrpcJob::markupCall, cmd="<<cmd<<endl;
|
||
15 years ago
|
TQString markup = "<?xml version=\"1.0\" ?>\r\n<methodCall>\r\n";
|
||
15 years ago
|
|
||
|
markup += "<methodName>" + cmd + "</methodName>\r\n";
|
||
|
|
||
|
if ( !args.isEmpty() )
|
||
|
{
|
||
|
markup += "<params>\r\n";
|
||
15 years ago
|
TQValueList<TQVariant>::ConstIterator it = args.begin();
|
||
|
TQValueList<TQVariant>::ConstIterator end = args.end();
|
||
15 years ago
|
for ( ; it != end; ++it )
|
||
|
markup += "<param>\r\n" + marshal( *it ) + "</param>\r\n";
|
||
|
markup += "</params>\r\n";
|
||
|
}
|
||
|
|
||
|
markup += "</methodCall>\r\n";
|
||
|
|
||
|
return markup;
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
15 years ago
|
void XmlrpcJob::slotData( const TQByteArray& data )
|
||
15 years ago
|
{
|
||
|
kdDebug()<<"XmlrpcJob::slotData()"<<endl;
|
||
|
if ( m_redirectionURL.isEmpty() || !m_redirectionURL.isValid() || m_error )
|
||
15 years ago
|
m_str_response.append( TQString( data ) );
|
||
15 years ago
|
}
|
||
|
|
||
|
void XmlrpcJob::slotFinished()
|
||
|
{
|
||
|
kdDebug() << "XmlrpcJob::slotFinished()" << endl;
|
||
|
kdDebug() << m_str_response << endl;
|
||
|
|
||
|
// TODO: Redirection with XML-RPC??
|
||
|
/* if (! m_redirectionURL.isEmpty() && m_redirectionURL.isValid() ) {
|
||
15 years ago
|
TQDataStream istream( m_packedArgs, IO_ReadOnly );
|
||
15 years ago
|
int s_cmd, s_method;
|
||
|
KURL s_url;
|
||
|
istream >> s_cmd;
|
||
|
istream >> s_url;
|
||
|
istream >> s_method;
|
||
|
// PROPFIND
|
||
12 years ago
|
if ( (s_cmd == 7) && (s_method == (int)TDEIO::HTTP_POST) ) {
|
||
15 years ago
|
m_packedArgs.truncate(0);
|
||
15 years ago
|
TQDataStream stream( m_packedArgs, IO_WriteOnly );
|
||
12 years ago
|
stream << (int)7 << m_redirectionURL << (int)TDEIO::HTTP_POST;
|
||
15 years ago
|
}
|
||
|
} else */
|
||
|
|
||
|
kdDebug() << "\033[35;40mResult: " << m_str_response << "\033[0;0m" << endl;
|
||
15 years ago
|
TQDomDocument doc;
|
||
|
TQString errMsg;
|
||
15 years ago
|
int errLine, errCol;
|
||
|
if ( doc.setContent( m_str_response, false, &errMsg, &errLine, &errCol ) ) {
|
||
|
if ( isMessageResponse( doc ) ) {
|
||
|
m_response = parseMessageResponse( doc ).data();
|
||
|
m_responseType = XMLRPCMessageResponse;
|
||
|
} else if ( isFaultResponse( doc ) ) {
|
||
|
// TODO: Set the error of the job
|
||
|
m_response.clear();
|
||
15 years ago
|
m_response << TQVariant( parseFaultResponse( doc ).errorString() );
|
||
15 years ago
|
m_responseType = XMLRPCFaultResponse;
|
||
|
} else {
|
||
|
// TODO: Set the error of the job
|
||
|
m_response.clear();
|
||
15 years ago
|
m_response << TQVariant( i18n( "Unknown type of XML markup received. "
|
||
13 years ago
|
"Markup: \n %1" ).arg( m_str_response ) );
|
||
15 years ago
|
m_responseType = XMLRPCUnknownResponse;
|
||
|
}
|
||
|
|
||
|
} else {
|
||
|
// TODO: if we can't parse the XML response, set the correct error message!
|
||
|
// emit fault( -1, i18n( "Received invalid XML markup: %1 at %2:%3" )
|
||
13 years ago
|
// .arg( errMsg ).arg( errLine ).arg( errCol ), m_id );
|
||
15 years ago
|
}
|
||
|
|
||
|
TransferJob::slotFinished();
|
||
|
// TODO: Redirect: if( d ) staticData = d->savedStaticData.copy();
|
||
|
// Need to send XMLRPC request to this host too
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
15 years ago
|
bool XmlrpcJob::isMessageResponse( const TQDomDocument &doc )
|
||
15 years ago
|
{
|
||
|
return doc.documentElement().firstChild().toElement()
|
||
|
.tagName().lower() == "params";
|
||
|
}
|
||
|
|
||
15 years ago
|
XMLRPCResult XmlrpcJob::parseMessageResponse( const TQDomDocument &doc )
|
||
15 years ago
|
{
|
||
|
XMLRPCResult response;
|
||
|
response.m_success = true;
|
||
|
|
||
15 years ago
|
TQDomNode paramNode = doc.documentElement().firstChild().firstChild();
|
||
15 years ago
|
while ( !paramNode.isNull() ) {
|
||
|
response.m_data << demarshal( paramNode.firstChild().toElement() );
|
||
|
paramNode = paramNode.nextSibling();
|
||
|
}
|
||
|
|
||
|
return response;
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
15 years ago
|
bool XmlrpcJob::isFaultResponse( const TQDomDocument &doc )
|
||
15 years ago
|
{
|
||
|
return doc.documentElement().firstChild().toElement()
|
||
|
.tagName().lower() == "fault";
|
||
|
}
|
||
|
|
||
15 years ago
|
XMLRPCResult XmlrpcJob::parseFaultResponse( const TQDomDocument &doc )
|
||
15 years ago
|
{
|
||
|
XMLRPCResult response;
|
||
|
response.m_success = false;
|
||
|
|
||
15 years ago
|
TQDomNode errorNode = doc.documentElement().firstChild().firstChild();
|
||
|
const TQVariant errorVariant = demarshal( errorNode.toElement() );
|
||
15 years ago
|
response.m_errorCode = errorVariant.toMap() [ "faultCode" ].toInt();
|
||
|
response.m_errorString = errorVariant.toMap() [ "faultString" ].toString();
|
||
|
|
||
|
return response;
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
15 years ago
|
TQString XmlrpcJob::marshal( const TQVariant &arg )
|
||
15 years ago
|
{
|
||
|
switch ( arg.type() )
|
||
|
{
|
||
15 years ago
|
case TQVariant::String:
|
||
|
case TQVariant::CString:
|
||
15 years ago
|
return "<value><string>" + arg.toString() + "</string></value>\r\n";
|
||
15 years ago
|
case TQVariant::Int:
|
||
|
return "<value><int>" + TQString::number( arg.toInt() ) +
|
||
15 years ago
|
"</int></value>\r\n";
|
||
15 years ago
|
case TQVariant::Double:
|
||
|
return "<value><double>" + TQString::number( arg.toDouble() ) +
|
||
15 years ago
|
"</double></value>\r\n";
|
||
15 years ago
|
case TQVariant::Bool:
|
||
15 years ago
|
{
|
||
15 years ago
|
TQString markup = "<value><boolean>";
|
||
15 years ago
|
markup += arg.toBool() ? "1" : "0";
|
||
|
markup += "</boolean></value>\r\n";
|
||
|
return markup;
|
||
|
}
|
||
15 years ago
|
case TQVariant::ByteArray:
|
||
15 years ago
|
return "<value><base64>" + KCodecs::base64Encode( arg.toByteArray() ) +
|
||
|
"</base64></value>\r\n";
|
||
15 years ago
|
case TQVariant::DateTime:
|
||
15 years ago
|
return "<value><datetime.iso8601>" +
|
||
14 years ago
|
arg.toDateTime().toString( TQt::ISODate ) +
|
||
15 years ago
|
"</datetime.iso8601></value>\r\n";
|
||
15 years ago
|
case TQVariant::List:
|
||
15 years ago
|
{
|
||
15 years ago
|
TQString markup = "<value><array><data>\r\n";
|
||
|
const TQValueList<TQVariant> args = arg.toList();
|
||
|
TQValueList<TQVariant>::ConstIterator it = args.begin();
|
||
|
TQValueList<TQVariant>::ConstIterator end = args.end();
|
||
15 years ago
|
for ( ; it != end; ++it )
|
||
|
markup += marshal( *it );
|
||
|
markup += "</data></array></value>\r\n";
|
||
|
return markup;
|
||
|
}
|
||
15 years ago
|
case TQVariant::Map:
|
||
15 years ago
|
{
|
||
15 years ago
|
TQString markup = "<value><struct>\r\n";
|
||
|
TQMap<TQString, TQVariant> map = arg.toMap();
|
||
|
TQMap<TQString, TQVariant>::ConstIterator it = map.begin();
|
||
|
TQMap<TQString, TQVariant>::ConstIterator end = map.end();
|
||
15 years ago
|
for ( ; it != end; ++it )
|
||
|
{
|
||
|
markup += "<member>\r\n";
|
||
|
markup += "<name>" + it.key() + "</name>\r\n";
|
||
|
markup += marshal( it.data() );
|
||
|
markup += "</member>\r\n";
|
||
|
}
|
||
|
markup += "</struct></value>\r\n";
|
||
|
return markup;
|
||
|
}
|
||
|
default:
|
||
|
kdWarning() << "Failed to marshal unknown variant type: "
|
||
|
<< arg.type() << endl;
|
||
|
};
|
||
14 years ago
|
return TQString();
|
||
15 years ago
|
}
|
||
|
|
||
15 years ago
|
TQVariant XmlrpcJob::demarshal( const TQDomElement &elem )
|
||
15 years ago
|
{
|
||
|
Q_ASSERT( elem.tagName().lower() == "value" );
|
||
|
|
||
|
if ( !elem.hasChildNodes() ) {
|
||
|
// it doesn't have child nodes, so no explicit type name was given,
|
||
|
// i.e. <value>here comes the value</value> instead of
|
||
|
// <value><string>here comes the value</string></value>
|
||
|
// Assume <string> in that case:
|
||
|
// Actually, the element will still have a child node, so this will not help here.
|
||
|
// The dirty hack is at the end of this method.
|
||
|
kdDebug()<<"XmlrpcJob::demarshal: No child nodes, assume type=string. Text: "<<elem.text()<<endl;
|
||
15 years ago
|
return TQVariant( elem.text() );
|
||
15 years ago
|
}
|
||
|
|
||
|
kdDebug()<<"Demarshalling element \"" << elem.text() <<"\"" << endl;
|
||
|
|
||
15 years ago
|
const TQDomElement typeElement = elem.firstChild().toElement();
|
||
|
const TQString typeName = typeElement.tagName().lower();
|
||
15 years ago
|
|
||
|
if ( typeName == "string" )
|
||
15 years ago
|
return TQVariant( typeElement.text() );
|
||
15 years ago
|
else if ( typeName == "i4" || typeName == "int" )
|
||
15 years ago
|
return TQVariant( typeElement.text().toInt() );
|
||
15 years ago
|
else if ( typeName == "double" )
|
||
15 years ago
|
return TQVariant( typeElement.text().toDouble() );
|
||
15 years ago
|
else if ( typeName == "boolean" )
|
||
|
{
|
||
|
if ( typeElement.text().lower() == "true" || typeElement.text() == "1" )
|
||
15 years ago
|
return TQVariant( true );
|
||
15 years ago
|
else
|
||
15 years ago
|
return TQVariant( false );
|
||
15 years ago
|
}
|
||
|
else if ( typeName == "base64" )
|
||
15 years ago
|
return TQVariant( KCodecs::base64Decode( typeElement.text().latin1() ) );
|
||
15 years ago
|
|
||
|
else if ( typeName == "datetime" || typeName == "datetime.iso8601" ) {
|
||
|
|
||
15 years ago
|
TQString text( typeElement.text() );
|
||
13 years ago
|
if ( text.find( TQRegExp("^[0-9]{8,8}T") ) >= 0 ) {
|
||
15 years ago
|
// It's in the format 20041120T...., so adjust it to correct
|
||
15 years ago
|
// ISO 8601 Format 2004-11-20T..., else TQDateTime::fromString won't work:
|
||
15 years ago
|
text = text.insert( 6, '-' );
|
||
|
text = text.insert( 4, '-' );
|
||
|
}
|
||
14 years ago
|
return TQVariant( TQDateTime::fromString( text, TQt::ISODate ) );
|
||
15 years ago
|
|
||
|
} else if ( typeName == "array" ) {
|
||
|
|
||
15 years ago
|
TQValueList<TQVariant> values;
|
||
|
TQDomNode valueNode = typeElement.firstChild().firstChild();
|
||
15 years ago
|
while ( !valueNode.isNull() ) {
|
||
|
values << demarshal( valueNode.toElement() );
|
||
|
valueNode = valueNode.nextSibling();
|
||
|
}
|
||
15 years ago
|
return TQVariant( values );
|
||
15 years ago
|
|
||
|
} else if ( typeName == "struct" ) {
|
||
|
|
||
15 years ago
|
TQMap<TQString, TQVariant> map;
|
||
|
TQDomNode memberNode = typeElement.firstChild();
|
||
15 years ago
|
while ( !memberNode.isNull() ) {
|
||
15 years ago
|
const TQString key = memberNode.toElement().elementsByTagName( "name" ).item( 0 ).toElement().text();
|
||
|
const TQVariant data = demarshal( memberNode.toElement().elementsByTagName( "value" ).item( 0 ).toElement() );
|
||
15 years ago
|
map[ key ] = data;
|
||
|
memberNode = memberNode.nextSibling();
|
||
|
}
|
||
15 years ago
|
return TQVariant( map );
|
||
15 years ago
|
|
||
|
} else {
|
||
|
|
||
|
kdWarning() << "Cannot demarshal unknown type " << typeName << ", text= " << typeElement.text() << endl;
|
||
|
// FIXME: This is just a workaround, for the issue mentioned at the beginning of this method.
|
||
15 years ago
|
return TQVariant( elem.text() );
|
||
15 years ago
|
}
|
||
|
|
||
15 years ago
|
return TQVariant();
|
||
15 years ago
|
}
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
/* Convenience methods */
|
||
|
|
||
12 years ago
|
XmlrpcJob* TDEIO::xmlrpcCall( const KURL& url, const TQString &method, const TQValueList<TQVariant> ¶ms, bool showProgressInfo )
|
||
15 years ago
|
{
|
||
|
if ( url.isEmpty() ) {
|
||
|
kdWarning() << "Cannot execute call to " << method << ": empty server URL" << endl;
|
||
|
return 0;
|
||
|
}
|
||
|
XmlrpcJob *job = new XmlrpcJob( url, method, params, showProgressInfo );
|
||
|
// job->addMetaData( "xmlrpcDepth", depth );
|
||
|
|
||
|
return job;
|
||
|
}
|
||
|
|
||
12 years ago
|
XmlrpcJob* TDEIO::xmlrpcCall( const KURL& url, const TQString &method,
|
||
15 years ago
|
const TQVariant &arg, bool showProgressInfo )
|
||
15 years ago
|
{
|
||
15 years ago
|
TQValueList<TQVariant> args;
|
||
15 years ago
|
args << arg;
|
||
12 years ago
|
return TDEIO::xmlrpcCall( url, method, args, showProgressInfo );
|
||
15 years ago
|
}
|
||
|
|
||
12 years ago
|
XmlrpcJob* TDEIO::xmlrpcCall( const KURL& url, const TQString &method,
|
||
15 years ago
|
const TQStringList &arg, bool showProgressInfo )
|
||
15 years ago
|
{
|
||
15 years ago
|
TQValueList<TQVariant> args;
|
||
|
TQStringList::ConstIterator it = arg.begin();
|
||
|
TQStringList::ConstIterator end = arg.end();
|
||
15 years ago
|
for ( ; it != end; ++it )
|
||
15 years ago
|
args << TQVariant( *it );
|
||
12 years ago
|
return TDEIO::xmlrpcCall( url, method, args, showProgressInfo );
|
||
15 years ago
|
}
|
||
|
|
||
|
template <typename T>
|
||
12 years ago
|
XmlrpcJob* TDEIO::xmlrpcCall( const KURL& url, const TQString &method,
|
||
15 years ago
|
const TQValueList<T>&arg, bool showProgressInfo )
|
||
15 years ago
|
{
|
||
15 years ago
|
TQValueList<TQVariant> args;
|
||
15 years ago
|
|
||
15 years ago
|
typename TQValueList<T>::ConstIterator it = arg.begin();
|
||
|
typename TQValueList<T>::ConstIterator end = arg.end();
|
||
15 years ago
|
for ( ; it != end; ++it )
|
||
15 years ago
|
args << TQVariant( *it );
|
||
12 years ago
|
return TDEIO::xmlrpcCall( url, method, args, showProgressInfo );
|
||
15 years ago
|
}
|
||
|
|
||
|
#include "xmlrpcjob.moc"
|