protected void OptionallyCloseResponseStreamAndSetSuccess <T>(
            ITransportRequestState requestState,
            OrientdbServerError error,
            OrientdbResponse <T> typedResponse,
            OrientdbResponse <Stream> streamResponse)
        {
            if (streamResponse.Response != null && !typeof(Stream).IsAssignableFrom(typeof(T)))
            {
                streamResponse.Response.Close();
            }

            if (error != null)
            {
                typedResponse.Success = false;
                if (typedResponse.OriginalException == null)
                {
                    typedResponse.OriginalException = new OrientdbServerException(error);
                }
            }

            //TODO UNIT TEST OR BEGONE
            if (!typedResponse.Success &&
                requestState.RequestConfiguration != null &&
                requestState.RequestConfiguration.AllowedStatusCodes.HasAny(i => i == streamResponse.HttpStatusCode))
            {
                typedResponse.Success = true;
            }
        }
Beispiel #2
0
        private OrientdbResponse <T> CoordinateRequest <T>(TransportRequestState <T> requestState, int maxRetries,
                                                           int retried, ref bool aliveResponse)
        {
            OrientdbResponse <T> pingRetryRequest = SelectNextNode(requestState);

            if (pingRetryRequest != null)
            {
                return(pingRetryRequest);
            }

            OrientdbResponse <Stream> streamResponse = DoOrientdbCall(requestState);

            aliveResponse = streamResponse.SuccessOrKnownError;

            if (!DoneProcessing(streamResponse, requestState, maxRetries, retried))
            {
                return(null);
            }

            OrientdbServerError  error         = null;
            OrientdbResponse <T> typedResponse = ReturnStreamOrVoidResponse(requestState, streamResponse)
                                                 ?? ReturnTypedResponse(requestState, streamResponse, out error);

            OptionallyCloseResponseStreamAndSetSuccess(requestState, error, typedResponse, streamResponse);
            if (error != null && _settings.ThrowOnOrientdbServerExceptions)
            {
                throw new OrientdbServerException(error);
            }
            return(typedResponse);
        }
 protected OrientdbServerError GetErrorFromStream <T>(Stream stream)
 {
     try
     {
         var e = _serializer.Deserialize <OneToOneServerException>(stream);
         return(OrientdbServerError.Create(e));
     }
     // ReSharper disable once EmptyGeneralCatchClause
     // parsing failure of exception should not be fatal, its a best case helper.
     catch
     {
     }
     return(null);
 }
Beispiel #4
0
        private OrientdbResponse <T> ReturnTypedResponse <T>(
            TransportRequestState <T> requestState,
            OrientdbResponse <Stream> streamResponse,
            out OrientdbServerError error)
        {
            error = null;

            // Read to memory stream if needed
            bool hasResponse = streamResponse.Response != null;
            bool forceRead   = _settings.KeepRawResponse || typeof(T) == typeof(string) || typeof(T) == typeof(byte[]);

            byte[] bytes = null;
            if (hasResponse && forceRead)
            {
                MemoryStream ms = _memoryStreamProvider.New();
                streamResponse.Response.CopyTo(ms);
                bytes = ms.ToArray();
                streamResponse.Response.Close();
                streamResponse.Response          = ms;
                streamResponse.Response.Position = 0;
            }
            // Set rawresponse if needed
            if (_settings.KeepRawResponse)
            {
                streamResponse.ResponseRaw = bytes;
            }

            bool isValidResponse = IsValidResponse(requestState, streamResponse);

            if (isValidResponse)
            {
                return(StreamToTypedResponse <T>(streamResponse, requestState, bytes));
            }

            // If error read error
            error = GetErrorFromStream <T>(streamResponse.Response);
            OrientdbResponse <T> typedResponse = OrientdbResponse.CloneFrom(streamResponse, default(T));

            SetStringOrByteResult(typedResponse, bytes);
            return(typedResponse);
        }