QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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  QString responseFormat;
71  QByteArray ba = ex.formatResponse( responseFormat );
72 
73  if ( headersSent() )
74  {
75  QgsMessageLog::logMessage( QStringLiteral( "Error: Cannot write exception after header sent !" ) );
76  return;
77  }
78 
79  clear();
81  setHeader( "Content-Type", responseFormat );
82  write( ba );
83 }
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 void write(const QString &data)
Write string This is a convenient method that will write directly to the underlying I/O device...
virtual void setStatusCode(int code)=0
Set the http status code.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
Exception base class for server exceptions.
virtual QByteArray data() const =0
Gets the data written so far.
virtual QByteArray formatResponse(QString &responseFormat) const
Formats the exception for sending to client.
virtual QIODevice * io()=0
Returns the underlying QIODevice.
virtual bool headersSent() const =0
Returns true if the headers have already been sent.