Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
QmitkHttpStatusCodeHandler.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
14 #include "QMessageBox"
15 #include <mitkCommon.h>
16 #include <sstream>
17 
18 QmitkHttpStatusCodeHandler::QmitkHttpStatusCodeHandler()
19 {
20 }
21 
22 QmitkHttpStatusCodeHandler::~QmitkHttpStatusCodeHandler()
23 {
24 }
25 
26 static void CreateMessageBox(int statusCode, std::string errorMessage)
27 {
28  std::stringstream ss;
29  // Qt uses different type of status code numbers.
30 
31  switch (statusCode)
32  {
33  case 1:
34  QMessageBox::warning(nullptr,
35  "ConnectionRefusedError - 1",
36  "The remote server refused the connection (the server is not accepting requests).");
37  break;
38  case 2:
39  QMessageBox::warning(
40  nullptr,
41  "RemoteHostClosedError - 2",
42  "The remote server closed the connection prematurely, before the entire reply was received and processed.");
43  break;
44  case 3:
45  QMessageBox::warning(nullptr, "HostNotFoundError - 3", "The remote host name was not found (invalid hostname).");
46  break;
47  case 4:
48  QMessageBox::warning(nullptr, "TimeoutError - 4", "The connection to the remote server timed out.");
49  break;
50  case 5:
51  QMessageBox::warning(nullptr,
52  "OperationCanceledError - 5",
53  "The operation was canceled via calls to abort() or close() before it was finished.");
54  break;
55  case 6:
56  QMessageBox::warning(nullptr, "SslHandshakeFailedError - 6", "The SSL/TLS handshake failed and the encrypted "
57  "channel could not be established. The sslErrors() "
58  "signal should have been emitted.");
59  break;
60  case 7:
61  QMessageBox::warning(nullptr,
62  "TemporaryNetworkFailureError - 7",
63  "The connection was broken due to disconnection from the network, however the system has "
64  "initiated roaming to another access point. The request should be resubmitted and will be "
65  "processed as soon as the connection is re-established.");
66  break;
67  case 8:
68  QMessageBox::warning(
69  nullptr,
70  "NetworkSessionFailedError - 8",
71  "The connection was broken due to disconnection from the network or failure to start the network.");
72  break;
73  case 9:
74  QMessageBox::warning(nullptr,
75  "BackgroundRequestNotAllowedError - 9",
76  "The background request is not currently allowed due to platform policy.");
77  break;
78  case 10:
79  QMessageBox::warning(nullptr, "TooManyRedirectsError - 10 ", "While following redirects, the maximum limit was "
80  "reached. The limit is by default set to 50 or as "
81  "set by QNetworkRequest::setMaxRedirectsAllowed().");
82  break;
83  case 11:
84  QMessageBox::warning(nullptr, "InsecureRedirectError - 11 ", "While following redirects, the network access API "
85  "detected a redirect from a encrypted protocol "
86  "(https) to an unencrypted one (http).");
87  break;
88  case 101:
89  QMessageBox::warning(
90  nullptr,
91  "ProxyConnectionRefusedError - 101",
92  "The connection to the proxy server was refused (the proxy server is not accepting requests).");
93  break;
94  case 102:
95  QMessageBox::warning(
96  nullptr,
97  "ProxyConnectionClosedError - 102",
98  "The proxy server closed the connection prematurely, before the entire reply was received and processed.");
99  break;
100  case 103:
101  QMessageBox::warning(
102  nullptr, "ProxyNotFoundError - 103", "The proxy host name was not found (invalid proxy hostname).");
103  break;
104  case 104:
105  QMessageBox::warning(
106  nullptr,
107  "ProxyTimeoutError - 104",
108  "The connection to the proxy timed out or the proxy did not reply in time to the request sent.");
109  break;
110  case 105:
111  QMessageBox::warning(nullptr, "ProxyAuthenticationRequiredError - 105", "The proxy requires authentication in "
112  "order to honour the request but did not "
113  "accept any credentials offered (if "
114  "any).");
115  break;
116  case 201:
117  QMessageBox::warning(nullptr,
118  "ContentAccessDenied - 201",
119  "The access to the remote content was denied (similar to HTTP error 401).");
120  break;
121  case 202:
122  QMessageBox::warning(nullptr,
123  "ContentOperationNotPermittedError - 202",
124  "The operation requested on the remote content is not permitted.");
125  break;
126  case 203:
127  QMessageBox::warning(nullptr,
128  "ContentNotFoundError - 203",
129  "The remote content was not found at the server (similar to HTTP error 404).");
130  break;
131  case 204:
132  QMessageBox::warning(nullptr, "AuthenticationRequiredError - 204", "The remote server requires authentication to "
133  "serve the content but the credentials "
134  "provided were not accepted (if any).");
135  break;
136  case 205:
137  QMessageBox::warning(nullptr, "ContentReSendError - 205", "The request needed to be sent again, but this failed "
138  "for example because the upload data could not be read "
139  "a second time.");
140  break;
141  case 206:
142  QMessageBox::warning(
143  nullptr,
144  "ContentConflictError - 206",
145  "The request could not be completed due to a conflict with the current state of the resource.");
146  break;
147  case 207:
148  QMessageBox::warning(
149  nullptr, "ContentGoneError - 207", "The requested resource is no longer available at the server.");
150  break;
151  case 401:
152  QMessageBox::warning(
153  nullptr,
154  "InternalServerError - 401",
155  "The server encountered an unexpected condition which prevented it from fulfilling the request.");
156  break;
157  case 402:
158  QMessageBox::warning(nullptr,
159  "OperationNotImplementedError - 402",
160  "The server does not support the functionality required to fulfill the request.");
161  break;
162  case 403:
163  QMessageBox::warning(
164  nullptr, "ServiceUnavailableError - 403", "The server is unable to handle the request at this time.");
165  break;
166  case 301:
167  QMessageBox::warning(nullptr,
168  "ProtocolUnknownError - 301",
169  "The Network Access API cannot honor the request because the protocol is not known.");
170  break;
171  case 302:
172  QMessageBox::warning(
173  nullptr, "ProtocolInvalidOperationError - 302", "The requested operation is invalid for this protocol.");
174  break;
175  case 99:
176  QMessageBox::warning(nullptr, "UnknownNetworkError - 99", "An unknown network-related error was detected.");
177  break;
178  case 199:
179  QMessageBox::warning(nullptr, "UnknownProxyError - 199", "An unknown proxy-related error was detected.");
180  break;
181  case 299:
182  QMessageBox::warning(
183  nullptr, "UnknownContentError - 299", "An unknown error related to the remote content was detected.");
184  break;
185  case 399:
186  QMessageBox::warning(
187  nullptr,
188  "ProtocolFailure - 399",
189  "A breakdown in protocol was detected (parsing error, invalid or unexpected responses, etc.).");
190  break;
191  case 499:
192  QMessageBox::warning(
193  nullptr, "UnknownServerError - 499", "An unknown error related to the server response was detected.");
194  break;
195  default:
196  ss << "An Http Error occured with error code " << statusCode << " and server message: " << errorMessage;
197  QMessageBox::warning(nullptr, "HTTP ERROR", ss.str().c_str());
198  break;
199  }
200 }
201 
203 {
204  static int lastCode = 0;
205  std::string errorMsg(_errorMsg, strnlen(_errorMsg, strlen(_errorMsg)));
206  bool success = true;
207  /*
208  * sample error response:
209  * ERROR: An error occurred: ctkRuntimeException: Syncing with http request failed.
210  * {d55ec279-8a65-46d6-80d3-cec079066109}: 202: Error downloading
211  * https:... - server replied: Forbidden
212  */
213 
214  if (errorMsg.find("request failed.") == std::string::npos)
215  success = false;
216 
217  std::string::size_type indexOfErrorCode = errorMsg.find(": Error") - 3;
218  std::string::size_type indexOfServerResponse =
219  errorMsg.rfind("server replied: ") + 16; // Length of "server replied : " is 16
220 
221  if (indexOfErrorCode == std::string::npos || indexOfServerResponse == std::string::npos)
222  success = false;
223 
224  std::string statusCodeString = errorMsg.substr(indexOfErrorCode, 3);
225  std::stringstream str;
226  str << statusCodeString;
227  int statusCode;
228  str >> statusCode;
229 
230  std::string serverResponse = errorMsg.substr(indexOfServerResponse);
231 
232  if (lastCode != statusCode)
233  ::CreateMessageBox(statusCode, serverResponse);
234 
235  if (!success && lastCode != statusCode)
236  {
237  QMessageBox::warning(nullptr, "General Error", errorMsg.c_str());
238  }
239 
240  if (lastCode != statusCode)
241  lastCode = statusCode;
242  else
243  lastCode = 0;
244 
245  return success;
246 }
static void CreateMessageBox(int statusCode, std::string errorMessage)
static bool HandleErrorMessage(const char *_errorMsg)