2017-11-16 21:17:15 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Copyright (C) 2017 Edouard Griffiths, F4EXB. //
|
|
|
|
// //
|
|
|
|
// Swagger server adapter interface //
|
|
|
|
// //
|
|
|
|
// 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 as version 3 of the License, or //
|
|
|
|
// //
|
|
|
|
// 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 V3 for more details. //
|
|
|
|
// //
|
|
|
|
// You should have received a copy of the GNU General Public License //
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>. //
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2017-11-20 12:38:26 -05:00
|
|
|
#include "httpdocrootsettings.h"
|
2017-11-16 21:17:15 -05:00
|
|
|
#include "webapirequestmapper.h"
|
2017-11-18 13:34:47 -05:00
|
|
|
#include "SWGInstanceSummaryResponse.h"
|
|
|
|
#include "SWGErrorResponse.h"
|
2017-11-16 21:17:15 -05:00
|
|
|
|
|
|
|
WebAPIRequestMapper::WebAPIRequestMapper(QObject* parent) :
|
|
|
|
HttpRequestHandler(parent),
|
|
|
|
m_adapter(0)
|
2017-11-20 12:38:26 -05:00
|
|
|
{
|
|
|
|
qtwebapp::HttpDocrootSettings docrootSettings;
|
|
|
|
docrootSettings.path = ":/";
|
|
|
|
m_staticFileController = new qtwebapp::StaticFileController(docrootSettings, parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
WebAPIRequestMapper::~WebAPIRequestMapper()
|
|
|
|
{
|
|
|
|
delete m_staticFileController;
|
|
|
|
}
|
2017-11-16 21:17:15 -05:00
|
|
|
|
2017-11-18 13:34:47 -05:00
|
|
|
void WebAPIRequestMapper::service(qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response)
|
2017-11-16 21:17:15 -05:00
|
|
|
{
|
|
|
|
if (m_adapter == 0) // format service unavailable if adapter is null
|
|
|
|
{
|
2017-11-18 13:34:47 -05:00
|
|
|
response.write("Service not available");
|
2017-11-17 02:52:15 -05:00
|
|
|
response.setStatus(500,"Service not available");
|
2017-11-16 21:17:15 -05:00
|
|
|
}
|
|
|
|
else // normal processing
|
|
|
|
{
|
|
|
|
QByteArray path=request.getPath();
|
|
|
|
|
2017-11-18 13:34:47 -05:00
|
|
|
if (path == WebAPIAdapterInterface::instanceSummaryURL)
|
2017-11-16 21:17:15 -05:00
|
|
|
{
|
2017-11-18 19:05:16 -05:00
|
|
|
if (request.getMethod() == "GET")
|
|
|
|
{
|
|
|
|
Swagger::SWGInstanceSummaryResponse normalResponse;
|
|
|
|
Swagger::SWGErrorResponse errorResponse;
|
2017-11-16 21:17:15 -05:00
|
|
|
|
2017-11-18 19:05:16 -05:00
|
|
|
int status = m_adapter->instanceSummary(normalResponse, errorResponse);
|
2017-11-18 13:34:47 -05:00
|
|
|
|
2017-11-18 19:05:16 -05:00
|
|
|
if (status == 200) {
|
|
|
|
response.write(normalResponse.asJson().toUtf8());
|
|
|
|
} else {
|
|
|
|
response.write(errorResponse.asJson().toUtf8());
|
|
|
|
}
|
2017-11-18 13:34:47 -05:00
|
|
|
|
2017-11-18 19:05:16 -05:00
|
|
|
response.setStatus(status);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
response.write("Invalid HTTP method");
|
|
|
|
response.setStatus(405,"Invalid HTTP method");
|
|
|
|
}
|
2017-11-16 21:17:15 -05:00
|
|
|
}
|
2017-11-17 02:52:15 -05:00
|
|
|
else
|
|
|
|
{
|
2017-11-20 17:57:53 -05:00
|
|
|
QByteArray path = "/index.html";
|
2017-11-20 12:38:26 -05:00
|
|
|
m_staticFileController->service(path, response);
|
|
|
|
//response.setStatus(404,"Not found");
|
2017-11-17 02:52:15 -05:00
|
|
|
}
|
2017-11-16 21:17:15 -05:00
|
|
|
}
|
|
|
|
}
|