ctkSoapConnectionRunnable.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) 2010 German Cancer Research Center,
  4. Division of Medical and Biological Informatics
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. #include "ctkSoapConnectionRunnable_p.h"
  16. #include <QTcpSocket>
  17. ctkSoapConnectionRunnable::ctkSoapConnectionRunnable(int socketDescriptor)
  18. : socketDescriptor(socketDescriptor)
  19. {
  20. }
  21. ctkSoapConnectionRunnable::~ctkSoapConnectionRunnable()
  22. {
  23. }
  24. void ctkSoapConnectionRunnable::run()
  25. {
  26. QTcpSocket tcpSocket;
  27. if (!tcpSocket.setSocketDescriptor(socketDescriptor))
  28. {
  29. // error handling
  30. return;
  31. }
  32. while (tcpSocket.state() == QTcpSocket::ConnectedState)
  33. {
  34. //const int timeout = 5 * 1000;
  35. tcpSocket.waitForReadyRead(-1);
  36. readClient(tcpSocket);
  37. }
  38. }
  39. void ctkSoapConnectionRunnable::readClient(QTcpSocket& socket)
  40. {
  41. //qDebug() << socket->readAll();
  42. while (socket.canReadLine()) {
  43. QString line = socket.readLine();
  44. qDebug() << line;
  45. if (line.trimmed().isEmpty())
  46. {
  47. // Read the http body, which contains the soap message
  48. QByteArray body = socket.readAll();
  49. qDebug() << body;
  50. if (body.trimmed().isEmpty())
  51. {
  52. qDebug() << "Message body empty";
  53. return;
  54. }
  55. QtSoapMessage msg;
  56. if (!msg.setContent(body))
  57. {
  58. qDebug() << "QtSoap import failed:" << msg.errorString();
  59. return;
  60. }
  61. QtSoapMessage reply;
  62. emit incomingSoapMessage(msg, &reply);
  63. if (reply.isFault())
  64. {
  65. qDebug() << "QtSoap reply faulty";
  66. return;
  67. }
  68. qDebug() << "SOAP reply:";
  69. QString soapContent = reply.toXmlString();
  70. QByteArray block;
  71. block.append("HTTP/1.1 200 OK\n");
  72. block.append("Content-Type: text/xml;charset=utf-8\n");
  73. block.append("Content-Length: ").append(QString::number(soapContent.size())).append("\n");
  74. block.append("\n");
  75. block.append(soapContent);
  76. qDebug() << block;
  77. socket.write(block);
  78. }
  79. }
  80. }