protected ElasticsearchResponse <T> StreamToTypedResponse <T>( ElasticsearchResponse <Stream> streamResponse, ITransportRequestState requestState, byte[] readBytes ) { //set response if (typeof(T) == typeof(string) || typeof(T) == typeof(byte[])) { var clone = ElasticsearchResponse.CloneFrom <T>(streamResponse, default(T)); this.SetStringOrByteResult(clone, readBytes); return(clone); } var typedResponse = ElasticsearchResponse.CloneFrom <T>(streamResponse, default(T)); using (streamResponse.Response) { var deserializationState = requestState.ResponseCreationOverride; var customConverter = deserializationState as Func <IElasticsearchResponse, Stream, T>; if (customConverter != null) { var t = customConverter(typedResponse, streamResponse.Response); typedResponse.Response = t; return(typedResponse); } var deserialized = this._serializer.Deserialize <T>(streamResponse.Response); typedResponse.Response = deserialized; return(typedResponse); } }
private Task <ElasticsearchResponse <T> > StreamToTypedResponseAsync <T>( ElasticsearchResponse <Stream> streamResponse, ITransportRequestState requestState ) { var tcs = new TaskCompletionSource <ElasticsearchResponse <T> >(); //if the user explicitly wants a stream return the undisposed stream if (typeof(Stream).IsAssignableFrom(typeof(T))) { tcs.SetResult(streamResponse as ElasticsearchResponse <T>); return(tcs.Task); } var cs = ElasticsearchResponse.CloneFrom <T>(streamResponse, default(T)); if (typeof(T) == typeof(VoidResponse)) { tcs.SetResult(cs); if (streamResponse.Response != null) { streamResponse.Response.Dispose(); } return(tcs.Task); } if (!(this.Settings.KeepRawResponse || this.TypeOfResponseCopiesDirectly <T>())) { return(_deserializeAsyncToResponse(streamResponse.Response, requestState, cs)); } var memoryStream = new MemoryStream(); return(this.Iterate(this.ReadStreamAsync(streamResponse.Response, memoryStream), memoryStream) .ContinueWith(t => { var readStream = t.Result; readStream.Position = 0; var bytes = readStream.ToArray(); cs.ResponseRaw = this.Settings.KeepRawResponse ? bytes : null; var type = typeof(T); if (type == typeof(string)) { this.SetStringResult(cs as ElasticsearchResponse <string>, bytes); tcs.SetResult(cs); return tcs.Task; } if (type == typeof(byte[])) { this.SetByteResult(cs as ElasticsearchResponse <byte[]>, bytes); tcs.SetResult(cs); return tcs.Task; } return _deserializeAsyncToResponse(readStream, requestState, cs); }) .Unwrap()); }
private Task <ReadResponse <T> > ReturnVoidResponse <T>(ElasticsearchResponse <Stream> streamResponse) { streamResponse.Response.Close(); var voidResponse = ElasticsearchResponse.CloneFrom <VoidResponse>(streamResponse, null); return(this.ReturnCompletedTaskFor(new ReadResponse <T>() { Response = voidResponse as ElasticsearchResponse <T> })); }
private Task <ReadResponse <T> > ReturnTypedResponse <T>(ElasticsearchResponse <Stream> streamResponse, TransportRequestState <T> requestState) { // Read to memory stream if needed Task <Stream> getStream = null; var response = new ReadResponse <T>(); var hasResponse = streamResponse.Response != null; var forceRead = this._settings.KeepRawResponse || typeof(T) == typeof(string) || typeof(T) == typeof(byte[]); if (hasResponse && forceRead) { var memoryStream = this._memoryStreamProvider.New(); getStream = this.Iterate(this.ReadStreamAsync(streamResponse.Response, memoryStream), memoryStream) .ContinueWith(streamReadTask => { response.Bytes = streamReadTask.Result.ToArray(); streamResponse.Response.Close(); streamReadTask.Result.Position = 0; return(streamReadTask.Result as Stream); }); } else { getStream = this.ReturnCompletedTaskFor(streamResponse.Response); } return(getStream.ContinueWith(delegate(Task <Stream> gotStream) { var isValidResponse = IsValidResponse(requestState, streamResponse); var typedResponse = ElasticsearchResponse.CloneFrom <T>(streamResponse, default(T)); if (!isValidResponse) { response.Error = GetErrorFromStream <T>(gotStream.Result); this.SetStringOrByteResult(typedResponse, response.Bytes); if (gotStream.Result != null) { gotStream.Result.Close(); } response.Response = typedResponse; return this.ReturnCompletedTaskFor(response); } if (this.SetStringOrByteResult(typedResponse, response.Bytes)) { if (gotStream.Result != null) { gotStream.Result.Close(); } response.Response = typedResponse; return this.ReturnCompletedTaskFor(response); } return this.DeserializeAsyncToResponse <T>(gotStream.Result, requestState, typedResponse, response); }).Unwrap()); }
protected ElasticsearchResponse <T> HandleAuthenticationException <T>(TransportRequestState <T> requestState, ElasticsearchAuthException exception) { if (requestState.ClientSettings.ThrowOnElasticsearchServerExceptions) { throw exception.ToElasticsearchServerException(); } var response = ElasticsearchResponse.CloneFrom <T>(exception.Response, default(T)); response.Request = requestState.PostData; response.RequestUrl = requestState.Path; response.RequestMethod = requestState.Method; return(response); }
private ElasticsearchResponse <T> StreamToTypedResponse <T>( ElasticsearchResponse <Stream> streamResponse, ITransportRequestState requestState ) { //if the user explicitly wants a stream returned the undisposed stream if (typeof(Stream).IsAssignableFrom(typeof(T))) { return(streamResponse as ElasticsearchResponse <T>); } var cs = ElasticsearchResponse.CloneFrom <T>(streamResponse, default(T)); using (streamResponse.Response) using (var memoryStream = new MemoryStream()) { if (typeof(T) == typeof(VoidResponse)) { return(cs); } var type = typeof(T); if (!(this.Settings.KeepRawResponse || this.TypeOfResponseCopiesDirectly <T>())) { return(this._deserializeToResponse(cs, streamResponse.Response, requestState)); } if (streamResponse.Response != null) { streamResponse.Response.CopyTo(memoryStream); } memoryStream.Position = 0; var bytes = memoryStream.ToArray(); cs.ResponseRaw = this.Settings.KeepRawResponse ? bytes : null; if (type == typeof(string)) { this.SetStringResult(cs as ElasticsearchResponse <string>, bytes); return(cs); } if (type == typeof(byte[])) { this.SetByteResult(cs as ElasticsearchResponse <byte[]>, bytes); return(cs); } return(this._deserializeToResponse(cs, memoryStream, requestState)); } }
private ElasticsearchResponse <T> ReturnTypedResponse <T>( TransportRequestState <T> requestState, ElasticsearchResponse <Stream> streamResponse, out ElasticsearchServerError error) { error = null; // Read to memory stream if needed var hasResponse = streamResponse.Response != null; var forceRead = this._settings.KeepRawResponse || typeof(T) == typeof(string) || typeof(T) == typeof(byte[]); byte[] bytes = null; if (hasResponse && forceRead) { var ms = this._memoryStreamProvider.New(); streamResponse.Response.CopyTo(ms); bytes = ms.ToArray(); streamResponse.Response.Close(); streamResponse.Response = ms; streamResponse.Response.Position = 0; } // Set rawresponse if needed if (this._settings.KeepRawResponse) { streamResponse.ResponseRaw = bytes; } var isValidResponse = IsValidResponse(requestState, streamResponse); if (isValidResponse) { return(this.StreamToTypedResponse <T>(streamResponse, requestState, bytes)); } // If error read error error = GetErrorFromStream <T>(streamResponse.Response); var typedResponse = ElasticsearchResponse.CloneFrom <T>(streamResponse, default(T)); this.SetStringOrByteResult(typedResponse, bytes); return(typedResponse); }
private ElasticsearchResponse <T> ReturnStreamOrVoidResponse <T>( TransportRequestState <T> requestState, ElasticsearchResponse <Stream> streamResponse) { // If the response never recieved a status code and has a caught exception make sure we throw it if (streamResponse.HttpStatusCode.GetValueOrDefault(-1) <= 0 && streamResponse.OriginalException != null) { throw streamResponse.OriginalException; } // If the user explicitly wants a stream returned the undisposed stream if (typeof(Stream).IsAssignableFrom(typeof(T))) { return(streamResponse as ElasticsearchResponse <T>); } if (!typeof(VoidResponse).IsAssignableFrom(typeof(T))) { return(null); } var voidResponse = ElasticsearchResponse.CloneFrom <VoidResponse>(streamResponse, null); return(voidResponse as ElasticsearchResponse <T>); }
private ElasticsearchResponse <ExistsResponse> ToExistsResponse(IElasticsearchResponse existsDispatch) { return(ElasticsearchResponse.CloneFrom <ExistsResponse>(existsDispatch, new ExistsResponse(existsDispatch))); }