QGIS API Documentation  3.24.2-Tisler (13c1a02865)
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 
25 void 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 
38 qint64 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 
48 qint64 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 
58 qint64 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 
69 {
70 
71 }
72 
74 {
75 
76 }
77 
78 qint64 QgsServerResponse::write( const std::string data )
79 {
80  return write( data.c_str() );
81 }
82 
84 {
85  QString responseFormat;
86  const QByteArray ba = ex.formatResponse( responseFormat );
87 
88  if ( headersSent() )
89  {
90  QgsMessageLog::logMessage( QStringLiteral( "Error: Cannot write exception after header sent !" ) );
91  return;
92  }
93 
94  clear();
96  setHeader( "Content-Type", responseFormat );
97  write( ba );
98 }
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
Exception base class for server exceptions.
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() SIP_THROW(QgsServerException)
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 void clear()=0
Reset all headers and content for this response.
virtual QIODevice * io()=0
Returns the underlying QIODevice.
virtual bool headersSent() const =0
Returns true if the headers have already been sent.
virtual void setStatusCode(int code)=0
Set the http status code.
virtual void finish() SIP_THROW(QgsServerException)
Finish the response, ending the transaction.