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