/// <summary> /// This method translates error information from the response objects. /// /// Note: Per BusinessEdge, it is not expected that error information /// will go more then 2 levels deep (e.g. one level of ITVReturnCode /// and a single level of ReturnCode in child Item element). If this /// ever changes, then we need to revisit how this is done. However, /// at this time, ITV is driving the requirements and none is expected. /// </summary> /// <param name="response"></param> protected void checkForSystemException(ITVResponse itvResponse) { // set to defaults int errorCode = 0; string errorText = null; if (itvResponse != null && itvResponse.ITVReturnCode != null) { try { errorCode = Convert.ToInt32(itvResponse.ITVReturnCode.Trim()); } catch { // eat the error and set return to 2 errorCode = 2; errorText = __cannotTranslateErrorCode; } try{ errorText = itvResponse.ITVReturnMsg; } catch { /*do nothing*/ } if (errorCode != 0) { throw new SmpSystemException(errorCode, errorText); } } }
/// <summary> /// Sends in the request and returns back the inner response. /// </summary> /// <param name="smpRequest"></param> /// <returns></returns> public object Send(object smpRequest) { try { //wrap request in SMP request xml ITVRequest request = new SmpHelper(smpRequest); //create web request to send WebRequest webRequest = GetWebRequest(new Uri(Url)); //send SendRequest(webRequest, request); //get response back HttpWebResponse webResponse = (HttpWebResponse)GetWebResponse(webRequest); //get smpResponse out of response ITVResponse smpResponse = ReceiveResponse(webResponse); webResponse.Close(); // this checks for ITV Errors checkForSystemException(smpResponse); // this checks for Errors in Item Response. checkForResponseException(smpResponse.Item); // no errors, all responses should be in good shape. return(smpResponse.Item); } catch (Exception ex) { throw new SmpUnavailableException(ex); } }
/// <summary> /// This method reads the response stream and serializes the results /// into a proper SMP Response. /// </summary> /// <param name="response"> /// A standard web response object obtained from the above web request /// instance. /// </param> /// <returns> /// An ITVResponse with the appropriate elements set /// </returns> protected virtual ITVResponse ReceiveResponse(HttpWebResponse response) { //check http response before we try to deserialize if (response.StatusCode != HttpStatusCode.OK) { throw new SmpUnavailableException( string.Format("The call to the WOTL failed with return code '{0}' and message '{1}'", response.StatusCode.ToString(), response.StatusDescription)); } ITVResponse smpResponse = null; Stream stream = null; StreamReader streamReader = null; try { stream = response.GetResponseStream(); // NOTE: Cannot use XmlTextReader. It complains and gives // an empty string. instead use the generic StreamReader streamReader = new StreamReader(stream); smpResponse = (ITVResponse)ObjectSerializerWithEncoding.Deserialize(streamReader.ReadToEnd(), typeof(ITVResponse)); } catch (IOException ioEx) { throw new SmpUnavailableException(ioEx); } catch (InvalidOperationException ivoEx) { throw new SmpUnavailableException(ivoEx); } catch (Exception ex) { throw new SmpProxyException(ex); } finally { //clean up stream reader and stream if (null != streamReader) { streamReader.Close(); } if (null != stream) { stream.Close(); } } return(smpResponse); }