2015-09-07 14:50:01 +03:00
|
|
|
/*
|
|
|
|
* Copyright (C) by Roeland Jago Douma <roeland@famdouma.nl>
|
|
|
|
*
|
|
|
|
* 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; version 2 of the License.
|
|
|
|
*
|
|
|
|
* This program 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ocsjob.h"
|
|
|
|
#include "networkjobs.h"
|
|
|
|
#include "account.h"
|
|
|
|
#include "json.h"
|
|
|
|
|
|
|
|
#include <QBuffer>
|
|
|
|
|
|
|
|
namespace OCC {
|
|
|
|
|
2015-11-11 15:28:20 +03:00
|
|
|
OcsJob::OcsJob(AccountPtr account)
|
|
|
|
: AbstractNetworkJob(account, "")
|
2015-09-07 14:50:01 +03:00
|
|
|
{
|
2016-03-14 17:41:20 +03:00
|
|
|
_passStatusCodes.append(OCS_SUCCESS_STATUS_CODE);
|
2015-09-07 14:50:01 +03:00
|
|
|
setIgnoreCredentialFailure(true);
|
|
|
|
}
|
|
|
|
|
2015-10-15 21:54:52 +03:00
|
|
|
void OcsJob::setVerb(const QByteArray &verb)
|
2015-09-07 14:50:01 +03:00
|
|
|
{
|
|
|
|
_verb = verb;
|
|
|
|
}
|
|
|
|
|
2015-10-15 22:58:16 +03:00
|
|
|
void OcsJob::addParam(const QString &name, const QString &value)
|
2015-09-07 14:50:01 +03:00
|
|
|
{
|
2015-10-15 22:58:16 +03:00
|
|
|
_params.append(qMakePair(name, value));
|
2015-09-07 14:50:01 +03:00
|
|
|
}
|
|
|
|
|
2015-10-15 21:54:52 +03:00
|
|
|
void OcsJob::addPassStatusCode(int code)
|
2015-09-07 14:50:01 +03:00
|
|
|
{
|
|
|
|
_passStatusCodes.append(code);
|
|
|
|
}
|
|
|
|
|
2015-10-29 13:09:10 +03:00
|
|
|
void OcsJob::appendPath(const QString &id)
|
2015-10-16 09:28:13 +03:00
|
|
|
{
|
2015-10-29 18:03:47 +03:00
|
|
|
setPath(path() + QLatin1Char('/') + id);
|
2015-10-16 09:28:13 +03:00
|
|
|
}
|
|
|
|
|
2016-07-12 15:25:33 +03:00
|
|
|
static QList<QPair<QByteArray, QByteArray>>
|
|
|
|
percentEncodeQueryItems(
|
|
|
|
const QList<QPair<QString, QString>> & items)
|
|
|
|
{
|
|
|
|
QList<QPair<QByteArray, QByteArray>> result;
|
|
|
|
foreach (const auto& item, items) {
|
|
|
|
result.append(qMakePair(
|
|
|
|
QUrl::toPercentEncoding(item.first),
|
|
|
|
QUrl::toPercentEncoding(item.second)));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-10-15 21:54:52 +03:00
|
|
|
void OcsJob::start()
|
2015-09-07 14:50:01 +03:00
|
|
|
{
|
|
|
|
QNetworkRequest req;
|
2015-10-15 21:54:52 +03:00
|
|
|
req.setRawHeader("Ocs-APIREQUEST", "true");
|
2015-09-07 14:50:01 +03:00
|
|
|
req.setRawHeader("Content-Type", "application/x-www-form-urlencoded");
|
|
|
|
|
2015-10-15 22:58:16 +03:00
|
|
|
QUrl url = Account::concatUrlPath(account()->url(), path());
|
|
|
|
QBuffer *buffer = new QBuffer;
|
|
|
|
|
|
|
|
if (_verb == "GET") {
|
2016-07-12 15:25:33 +03:00
|
|
|
// Note: QUrl::setQueryItems() does not fully percent encode
|
|
|
|
// the query items, see #5042
|
|
|
|
url.setEncodedQueryItems(percentEncodeQueryItems(_params));
|
2015-10-15 22:58:16 +03:00
|
|
|
} else if (_verb == "POST" || _verb == "PUT") {
|
|
|
|
// Url encode the _postParams and put them in a buffer.
|
|
|
|
QByteArray postData;
|
|
|
|
Q_FOREACH(auto tmp, _params) {
|
|
|
|
if (!postData.isEmpty()) {
|
|
|
|
postData.append("&");
|
|
|
|
}
|
|
|
|
postData.append(QUrl::toPercentEncoding(tmp.first));
|
|
|
|
postData.append("=");
|
|
|
|
postData.append(QUrl::toPercentEncoding(tmp.second));
|
2015-09-07 14:50:01 +03:00
|
|
|
}
|
2015-10-15 22:58:16 +03:00
|
|
|
buffer->setData(postData);
|
2015-09-07 14:50:01 +03:00
|
|
|
}
|
|
|
|
|
2015-10-15 22:58:16 +03:00
|
|
|
//We want json data
|
2016-07-12 15:25:33 +03:00
|
|
|
auto queryItems = url.encodedQueryItems();
|
|
|
|
queryItems.append(qMakePair(QByteArray("format"), QByteArray("json")));
|
|
|
|
url.setEncodedQueryItems(queryItems);
|
2015-09-07 14:50:01 +03:00
|
|
|
|
2015-10-15 22:58:16 +03:00
|
|
|
setReply(davRequest(_verb, url, req, buffer));
|
2015-09-07 14:50:01 +03:00
|
|
|
setupConnections(reply());
|
|
|
|
buffer->setParent(reply());
|
|
|
|
AbstractNetworkJob::start();
|
|
|
|
}
|
|
|
|
|
2015-10-15 21:54:52 +03:00
|
|
|
bool OcsJob::finished()
|
2015-09-07 14:50:01 +03:00
|
|
|
{
|
|
|
|
const QString replyData = reply()->readAll();
|
|
|
|
|
|
|
|
bool success;
|
|
|
|
QVariantMap json = QtJson::parse(replyData, success).toMap();
|
|
|
|
if (!success) {
|
2015-10-15 22:58:16 +03:00
|
|
|
qDebug() << "Could not parse reply to"
|
|
|
|
<< _verb
|
|
|
|
<< Account::concatUrlPath(account()->url(), path())
|
|
|
|
<< _params
|
2015-09-07 14:50:01 +03:00
|
|
|
<< ":" << replyData;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString message;
|
|
|
|
const int statusCode = getJsonReturnCode(json, message);
|
|
|
|
if (!_passStatusCodes.contains(statusCode)) {
|
2015-10-15 22:58:16 +03:00
|
|
|
qDebug() << "Reply to"
|
|
|
|
<< _verb
|
|
|
|
<< Account::concatUrlPath(account()->url(), path())
|
|
|
|
<< _params
|
2015-09-07 14:50:01 +03:00
|
|
|
<< "has unexpected status code:" << statusCode << replyData;
|
2015-10-29 18:56:23 +03:00
|
|
|
emit ocsError(statusCode, message);
|
|
|
|
} else {
|
|
|
|
emit jobFinished(json);
|
2015-09-07 14:50:01 +03:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-10-15 21:54:52 +03:00
|
|
|
int OcsJob::getJsonReturnCode(const QVariantMap &json, QString &message)
|
2015-09-07 14:50:01 +03:00
|
|
|
{
|
|
|
|
//TODO proper checking
|
|
|
|
int code = json.value("ocs").toMap().value("meta").toMap().value("statuscode").toInt();
|
|
|
|
message = json.value("ocs").toMap().value("meta").toMap().value("message").toString();
|
|
|
|
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|