view data/fileio/HttpClient.cpp @ 227:59d84a8bb76c

compatibility with changes done for the web site: - easaier servlet name changed - new queryfield.xml ("range" attributes form)
author lbajardsilogic
date Wed, 27 Feb 2008 16:00:02 +0000
parents 987ff9c8b7c3
children
line wrap: on
line source
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */

/*	Sound Access	
		EASAIER client application.	
		Silogic 2007. Laure Bajard. 
	
	This program 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 2 of the    
	License, or (at your option) any later version.  See the file    
	COPYING included with this distribution for more information.
*/

#include "HttpClient.h"

#include <iostream>


HttpClient::HttpClient(const QString& configFileName) : QHttp()
{
	QFile *config = new QFile(configFileName);

	m_useProxy = false;
	m_proxy = "";
	m_proxyPort = 0;
	m_userProxyId = "";
	m_pwdProxyId = "";

	m_host = "easaier.silogic.fr";
	m_hostPort = 9876;

	m_servletName = "/easaier/RetrieverService";
	
	if (config->exists())
	{
		fromXmlString(config);
	}

	if (useProxy())
		setProxy(m_proxy, m_proxyPort, m_userProxyId, m_pwdProxyId);
	setHost(m_host, m_hostPort);
}


HttpClient::~HttpClient()
{}

void HttpClient::valuesChanged(ConnectionSettings* dialogBox)
{
	setUseProxy(dialogBox->useProxy());
	if (useProxy())
	{
		m_proxy = dialogBox->getProxy();
		m_proxyPort = (dialogBox->getProxyPort()).toInt();
		m_userProxyId = dialogBox->getUserProxyId();
		m_pwdProxyId = dialogBox->getPwdProxyId();

		setProxy(m_proxy, m_proxyPort, m_userProxyId, m_pwdProxyId);
	}

	m_host = dialogBox->getHost();
	m_hostPort = (dialogBox->getHostPort()).toUShort();

	setHost(m_host, m_hostPort);
}

QString HttpClient::toXmlString(QString indent, QString extraAttributes) const
{
    QString s;
    
    s += indent;

    s += QString("<connexionSettings>\n");

	if (useProxy())
	{
		s += indent;
		s += QString("\t");
		s += QString("<proxy name=\"%1\" port=\"%2\" user=\"%3\" pwd=\"%4\" />\n")
			.arg(getProxy())
			.arg(QString::number(getProxyPort()))
			.arg(encodeString(getUserProxyId()))
			.arg(encodeString(getPwdProxyId()));
	}

	s += indent;
	s += QString("\t");
	s += QString("<host name=\"%1\" port=\"%2\" />\n")
		.arg(getHost())
		.arg(QString::number(getHostPort()));

	s += indent;
	s += QString("\t");
	s += QString("<servlet adresse=\"%1\" />\n")
		.arg(getServletName());

	s += indent;
	s += QString("</connexionSettings>\n");
	
    return s;
}

bool HttpClient::fromXmlString(QIODevice* file)
{
	HttpClientConfigXmlHandler handler(this);
    QXmlSimpleReader reader;
    reader.setContentHandler(&handler);
    reader.setErrorHandler(&handler);

    if (!file->open(QFile::ReadOnly | QFile::Text)) {
        return false;
    }

    QXmlInputSource xmlInputSource(file);
    if (reader.parse(xmlInputSource))
        return true;

	return false;
}

HttpClientConfigXmlHandler::HttpClientConfigXmlHandler(HttpClient* httpClient) : QXmlDefaultHandler()
{
	m_httpClient = httpClient;
}

bool HttpClientConfigXmlHandler::startElement(const QString &namespaceURI, const QString &localName,
						  const QString &qName, const QXmlAttributes &attributes)
{
	QString name = qName.toLower();
    bool ok = false;

    // Valid element names:
    // config
    // connexionSettings
    // proxy
    // host

    if (name == "config") {
		// nothing needed
		ok = true;

    } else if (name == "connexionsettings") {
		// nothing needed
		ok = true;

    } else if (name == "proxy") {

		m_httpClient->setProxyInfo(attributes.value("name"));
		if (attributes.value("name") != "")
			m_httpClient->setUseProxy(true);
		m_httpClient->setProxyPortInfo(attributes.value("port").toInt());
		m_httpClient->setUserProxyId(HttpClient::decodeString(attributes.value("user")));
		m_httpClient->setPwdProxyId(HttpClient::decodeString(attributes.value("pwd")));
		ok = true;

    } else if (name == "host") {
		
		m_httpClient->setHostInfo(attributes.value("name"));
		m_httpClient->setHostPortInfo(attributes.value("port").toUShort());
		ok = true;

    } else if (name == "servlet") {
		
		m_httpClient->setServletNameInfo(attributes.value("adresse"));
		ok = true;
    }

    if (!ok) {
	std::cerr << "WARNING: connexion config-XML: Failed to completely process element \""
		  << name.toLocal8Bit().data() << "\"" << std::endl;
    }

    return true;
}

bool HttpClientConfigXmlHandler::endElement(const QString &namespaceURI, const QString &localName,
						const QString &qName)
{
	return true;
}

bool HttpClientConfigXmlHandler::characters(const QString &str)
{
	return true;
}

bool HttpClientConfigXmlHandler::error(const QXmlParseException &exception)
{
	QString errorString;
	errorString += QString("ERROR: connexion config-XML: %1 at line %2, column %3")
	.arg(exception.message())
	.arg(exception.lineNumber())
	.arg(exception.columnNumber());
    std::cerr << errorString.toLocal8Bit().data() << std::endl;
    return QXmlDefaultHandler::error(exception);
}

bool HttpClientConfigXmlHandler::fatalError(const QXmlParseException &exception)
{
	QString errorString;
	errorString += QString("FATAL ERROR: connexion config-XML: %1 at line %2, column %3")
	.arg(exception.message())
	.arg(exception.lineNumber())
	.arg(exception.columnNumber());
    std::cerr << errorString.toLocal8Bit().data() << std::endl;
    return QXmlDefaultHandler::fatalError(exception);
}

QString HttpClient::encodeString(const QString& str){
	QByteArray bytes = str.toAscii();
	for(int i=0; i<bytes.count();i++){
			bytes[i] = bytes[i] ^ 1;	
	}
	return QString(bytes.toBase64());
}

QString HttpClient::decodeString(const QString& str){
	QByteArray bytes = str.toAscii().fromBase64(str.toAscii());
	for(int i=0; i<bytes.count();i++){
			bytes[i] = bytes[i] ^ 1;	
	}
	return QString(bytes);
}