QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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  //QTextStream stream( iodev );
31  //stream << data;
32  iodev->write( data.toUtf8() );
33  }
34  else
35  {
36  QgsMessageLog::logMessage( "Error: No IODevice in QgsServerResponse !!!" );
37  }
38 }
39 
40 
41 qint64 QgsServerResponse::write( const QByteArray &byteArray )
42 {
43  QIODevice *iodev = io();
44  if ( iodev )
45  {
46  return iodev->write( byteArray );
47  }
48  return 0;
49 }
50 
51 
52 qint64 QgsServerResponse::write( const char *data, qint64 maxsize )
53 {
54  QIODevice *iodev = io();
55  if ( iodev )
56  {
57  return iodev->write( data, maxsize );
58  }
59  return 0;
60 }
61 
62 qint64 QgsServerResponse::write( const char *data )
63 {
64  QIODevice *iodev = io();
65  if ( iodev )
66  {
67  return iodev->write( data );
68  }
69  return 0;
70 }
71 
73 {
74  QString responseFormat;
75  QByteArray ba = ex.formatResponse( responseFormat );
76 
77  if ( headersSent() )
78  {
79  QgsMessageLog::logMessage( QStringLiteral( "Error: Cannot write exception after header sent !" ) );
80  return;
81  }
82 
83  clear();
85  setHeader( "Content-Type", responseFormat );
86  write( ba );
87 }
88 
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.
int responseCode() const
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
Format 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.