/**
  @file
  @author Stefan Frings
*/

#include <logging/filelogger.h>
#include "requesthandler.h"
#include "qjsonobject.h"
#include "qjsondocument.h"
#include "qmap.h"
#include "webservercontrol.h"

RequestHandler::RequestHandler(QObject* parent)
    :HttpRequestHandler(parent)
{
    qDebug("RequestHandler: created");
}


RequestHandler::~RequestHandler()
{
    qDebug("RequestHandler: deleted");
}

void RequestHandler::service(HttpRequest& request, HttpResponse& response)
{    
	QMultiMap<QByteArray,QByteArray> qmapheaders =  request.getHeaderMap();

	/** Get the map of cookies */
	QMap<QByteArray,QByteArray> qmapcookies  =  request.getCookieMap();


	QMultiMap<QByteArray,QByteArray> qmapParameter = request.getParameterMap();

	QByteArray data = request.getBody();
		//½â¾ö¿çÓòÎÊÌâ
	response.setHeader("Access-Control-Allow Origin","*");
	response.setHeader("Access-Control-Allow-Methods","POST, GET,OPTIONS,DELETE");
	response.setHeader("Access-Control-Max-Age","3600");
	response.setHeader("Access-Control-Allow-Headers","x-requested-with,content-type");
	response.setHeader("Access-Control-Allow-Credentials","true");	

	//QJsonParseError error;
	//QJsonDocument doucment= QJsonDocument::fromJson(data,&error);


    // Set a response header
    //response.setHeader("Content-Type", "text/html; charset=ISO-8859-1");

	WebServerControl::GetInstance()->ParseJson(data);
	
	QJsonObject jsonobject;
	jsonobject.insert("code", "200");
	jsonobject.insert("msg", "success");

	QJsonDocument doc(jsonobject);

    // Return a simple HTML document
    response.write(doc.toJson(),true);
}
