ctkSimpleSoapClient.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) 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 "ctkSimpleSoapClient.h"
  16. #include "ctkDicomAppHostingTypes.h"
  17. #include "ctkSoapLog.h"
  18. #include <QApplication>
  19. #include <QCursor>
  20. #include <QNetworkReply>
  21. #include <QtSoapHttpTransport>
  22. //----------------------------------------------------------------------------
  23. class ctkSimpleSoapClientPrivate
  24. {
  25. public:
  26. QEventLoop BlockingLoop;
  27. QtSoapHttpTransport Http;
  28. int Port;
  29. QString Path;
  30. };
  31. //----------------------------------------------------------------------------
  32. ctkSimpleSoapClient::ctkSimpleSoapClient(int port, QString path)
  33. : d_ptr(new ctkSimpleSoapClientPrivate())
  34. {
  35. Q_D(ctkSimpleSoapClient);
  36. d->Port = port;
  37. d->Path = path;
  38. connect(&d->Http, SIGNAL(responseReady()), this, SLOT(responseReady()));
  39. d->Http.setHost("127.0.0.1", false, port);
  40. }
  41. //----------------------------------------------------------------------------
  42. ctkSimpleSoapClient::~ctkSimpleSoapClient()
  43. {
  44. }
  45. //----------------------------------------------------------------------------
  46. void ctkSimpleSoapClient::responseReady()
  47. {
  48. Q_D(ctkSimpleSoapClient);
  49. d->BlockingLoop.exit();
  50. }
  51. //----------------------------------------------------------------------------
  52. const QtSoapType & ctkSimpleSoapClient::submitSoapRequest(const QString& methodName,
  53. QtSoapType* soapType )
  54. {
  55. QList<QtSoapType*> list;
  56. list.append(soapType);
  57. return submitSoapRequest(methodName,list);
  58. }
  59. //----------------------------------------------------------------------------
  60. const QtSoapType & ctkSimpleSoapClient::submitSoapRequest(const QString& methodName,
  61. const QList<QtSoapType*>& soapTypes )
  62. {
  63. Q_D(ctkSimpleSoapClient);
  64. QString action="\"";
  65. //action.append(methodName);
  66. action.append("\"");
  67. d->Http.setAction(action);
  68. CTK_SOAP_LOG( << "Submitting action " << action
  69. << " method " << methodName
  70. << " to path " << d->Path );
  71. QtSoapMessage request;
  72. request.setMethod(QtSoapQName(methodName,"http://wg23.dicom.nema.org/"));
  73. if(!soapTypes.isEmpty())
  74. {
  75. for (QList<QtSoapType*>::ConstIterator it = soapTypes.begin();
  76. it < soapTypes.constEnd(); it++)
  77. {
  78. request.addMethodArgument(*it);
  79. CTK_SOAP_LOG( << " Argument type added " << (*it)->typeName() << ". "
  80. << " Argument name is " << (*it)->name().name() );
  81. }
  82. }
  83. CTK_SOAP_LOG_LOWLEVEL( << request.toXmlString());
  84. d->Http.submitRequest(request, d->Path);;
  85. CTK_SOAP_LOG_LOWLEVEL( << "Submitted request " << methodName);
  86. QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
  87. d->BlockingLoop.exec(QEventLoop::ExcludeUserInputEvents | QEventLoop::WaitForMoreEvents);
  88. QApplication::restoreOverrideCursor();
  89. //qDebug() << "Reply error: " << reply->errorString();
  90. //qDebug() << reply->readAll();
  91. const QtSoapMessage& response = d->Http.getResponse();
  92. CTK_SOAP_LOG( << "Got Response." );
  93. if (response.isFault())
  94. {
  95. qCritical() << "ctkSimpleSoapClient: server error (response.IsFault())";
  96. CTK_SOAP_LOG_LOWLEVEL( << response.faultString().toString().toLatin1().constData() << endl );
  97. CTK_SOAP_LOG_LOWLEVEL( << response.toXmlString() );
  98. return response.returnValue();
  99. // throw std::runtime_error("ctkSimpleSoapClient: server error (response.IsFault())");
  100. }
  101. CTK_SOAP_LOG_LOWLEVEL( << "Response: " << response.toXmlString() );
  102. const QtSoapType &returnValue = response.returnValue();
  103. CTK_SOAP_LOG( << " ReturnValue valid:" << returnValue.isValid() << " "
  104. << "Name: " << returnValue.name().name() << " "
  105. << "Value:" << returnValue.value().toString() );
  106. return returnValue;
  107. }