QGIS API Documentation 3.43.0-Master (32433f7016e)
qgsserverresponse.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsserverresponse.h
3
4 Define response class for services
5 -------------------
6 begin : 2016-12-05
7 copyright : (C) 2016 by David Marteau
8 email : david dot marteau at 3liz dot com
9 ***************************************************************************/
10
11/***************************************************************************
12 * *
13 * This program is free software; you can redistribute it and/or modify *
14 * it under the terms of the GNU General Public License as published by *
15 * the Free Software Foundation; either version 2 of the License, or *
16 * (at your option) any later version. *
17 * *
18 ***************************************************************************/
19
20#include "qgsserverresponse.h"
21#include "qgsmessagelog.h"
22#include "qgsserverexception.h"
23
24
25void QgsServerResponse::write( const QString &data )
26{
27 QIODevice *iodev = io();
28 if ( iodev )
29 {
30 iodev->write( data.toUtf8() );
31 }
32 else
33 {
34 QgsMessageLog::logMessage( "Error: No IODevice in QgsServerResponse !!!" );
35 }
36}
37
38qint64 QgsServerResponse::write( const QByteArray &byteArray )
39{
40 QIODevice *iodev = io();
41 if ( iodev )
42 {
43 return iodev->write( byteArray );
44 }
45 return 0;
46}
47
48qint64 QgsServerResponse::write( const char *data, qint64 maxsize )
49{
50 QIODevice *iodev = io();
51 if ( iodev )
52 {
53 return iodev->write( data, maxsize );
54 }
55 return 0;
56}
57
58qint64 QgsServerResponse::write( const char *data )
59{
60 QIODevice *iodev = io();
61 if ( iodev )
62 {
63 return iodev->write( data );
64 }
65 return 0;
66}
67
71
75
76qint64 QgsServerResponse::write( const std::string data )
77{
78 return write( data.c_str() );
79}
80
82{
83 QString responseFormat;
84 const QByteArray ba = ex.formatResponse( responseFormat );
85
86 if ( headersSent() )
87 {
88 QgsMessageLog::logMessage( QStringLiteral( "Error: Cannot write exception after header sent !" ) );
89 return;
90 }
91
92 clear();
94 setHeader( "Content-Type", responseFormat );
95 write( ba );
96}
97
99{
100 return nullptr;
101}
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition qgsfeedback.h:44
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true, const char *file=__builtin_FILE(), const char *function=__builtin_FUNCTION(), int line=__builtin_LINE())
Adds a message to the log instance (and creates it if necessary).
Exception base class for server exceptions.
int responseCode() const
Returns the return HTTP response code associated with this exception.
virtual QByteArray formatResponse(QString &responseFormat) const
Formats the exception for sending to client.
virtual void write(const QString &data)
Write string This is a convenient method that will write directly to the underlying I/O device.
virtual void flush()
Flushes the current output buffer to the network.
virtual QByteArray data() const =0
Gets the data written so far.
virtual void setHeader(const QString &key, const QString &value)=0
Set Header entry Add Header entry to the response Note that it is usually an error to set Header afte...
virtual QgsFeedback * feedback() const
Returns the socket feedback if any.
virtual void clear()=0
Reset all headers and content for this response.
virtual void finish()
Finish the response, ending the transaction.
virtual bool headersSent() const =0
Returns true if the headers have already been sent.
virtual QIODevice * io()=0
Returns the underlying QIODevice.
virtual void setStatusCode(int code)=0
Set the http status code.