Exemple #1
0
        internal static Action <Stream> HandleBeforeWritingException(Exception exception, IDataService service)
        {
            string   str;
            Encoding encoding;
            string   str2;
            Version  responseVersion    = null;
            DataServiceHostWrapper host = service.OperationContext.Host;

            TryGetResponseFormatForError(service, host, RequestDescription.DataServiceDefaultResponseVersion, out str, out encoding, out str2, out responseVersion);
            bool verboseResponse     = (service.Configuration != null) ? service.Configuration.UseVerboseErrors : false;
            HandleExceptionArgs args = new HandleExceptionArgs(exception, false, str2, verboseResponse);

            service.InternalHandleException(args);
            host.ResponseVersion = responseVersion.ToString(2) + ";";
            host.ProcessException(args);
            Action <Stream> action = ProcessBenignException(exception, service);
            ODataFormat     atom   = ODataFormat.Atom;

            if (WebUtil.GetContentFormat(str) == ContentFormat.VerboseJson)
            {
                atom = ODataFormat.VerboseJson;
            }
            return(action ?? CreateErrorSerializer(atom, args, encoding));
        }
 /// <summary>Declares a new <see cref="QueryInterceptorAttribute"/> instance.</summary>
 /// <param name="entitySetName">Entity set name that the method intercepts queries for.</param>
 public QueryInterceptorAttribute(string entitySetName)
 {
     this.entitySetName = WebUtil.CheckArgumentNull(entitySetName, "entitySetName");
 }
Exemple #3
0
 public static bool PathHasFilter(IEnumerable <ExpandSegment> path)
 {
     WebUtil.CheckArgumentNull <IEnumerable <ExpandSegment> >(path, "path");
     return(path.Any <ExpandSegment>(segment => segment.HasFilter));
 }
        internal void Write(Stream stream)
        {
            IExceptionWriter exceptionWriter = null;
            Stream           output          = null;
            Serializer       serializer      = null;

            System.Data.Services.ODataResponseMessage responseMessage = this.responseMessage as System.Data.Services.ODataResponseMessage;
            if (responseMessage != null)
            {
                responseMessage.SetStream(stream);
            }
            try
            {
                BinarySerializer   serializer2;
                MetadataSerializer serializer7;
                switch (this.payloadKind)
                {
                case ODataPayloadKind.Feed:
                case ODataPayloadKind.Entry:
                {
                    EntitySerializer serializer6 = new EntitySerializer(this.requestDescription, this.AbsoluteServiceUri, this.service, this.service.OperationContext.Host.ResponseETag, this.messageWriter, this.contentFormat);
                    serializer = serializer6;
                    serializer6.WriteRequest(this.queryResults, this.hasMoved);
                    return;
                }

                case ODataPayloadKind.Property:
                case ODataPayloadKind.EntityReferenceLink:
                case ODataPayloadKind.EntityReferenceLinks:
                case ODataPayloadKind.Collection:
                {
                    NonEntitySerializer serializer5 = new NonEntitySerializer(this.requestDescription, this.AbsoluteServiceUri, this.service, this.messageWriter);
                    serializer = serializer5;
                    serializer5.WriteRequest(this.queryResults, this.hasMoved);
                    return;
                }

                case ODataPayloadKind.Value:
                    new TextSerializer(this.messageWriter).WriteRequest(this.queryResults.Current);
                    return;

                case ODataPayloadKind.BinaryValue:
                    output          = this.responseMessage.GetStream();
                    serializer2     = new BinarySerializer(output);
                    exceptionWriter = serializer2;
                    if (this.requestDescription.TargetKind != RequestTargetKind.MediaResource)
                    {
                        break;
                    }
                    if (this.mediaResourceStream != null)
                    {
                        serializer2.WriteRequest(this.mediaResourceStream, this.service.StreamProvider.StreamBufferSize);
                    }
                    return;

                case ODataPayloadKind.ServiceDocument:
                    new ServiceDocumentSerializer(this.messageWriter).WriteServiceDocument(this.service.Provider);
                    return;

                default:
                    goto Label_0194;
                }
                serializer2.WriteRequest(this.queryResults.Current);
                return;

                Label_0194:
                serializer7 = new MetadataSerializer(this.messageWriter);
                serializer7.WriteMetadataDocument(this.service);
            }
            catch (Exception exception)
            {
                if (!CommonUtil.IsCatchableExceptionType(exception))
                {
                    throw;
                }
                if (serializer != null)
                {
                    serializer.Flush();
                }
                string contentType = this.responseMessage.GetHeader("Content-Type").StartsWith("application/json", StringComparison.OrdinalIgnoreCase) ? "application/json;odata=verbose" : "application/xml";
                if (this.messageWriter != null)
                {
                    ErrorHandler.HandleDuringWritingException(exception, this.service, contentType, this.messageWriter, stream, this.encoding);
                }
                else
                {
                    ErrorHandler.HandleDuringWritingException(exception, this.service, contentType, exceptionWriter);
                }
            }
            finally
            {
                WebUtil.Dispose(this.messageWriter);
                WebUtil.Dispose(this.queryResults);
                WebUtil.Dispose(this.mediaResourceStream);
                if ((output != null) && (responseMessage == null))
                {
                    output.Dispose();
                }
            }
        }
Exemple #5
0
        private static bool TryParseFromUri(string text, bool allowNamedValues, bool allowNull, out KeyInstance instance)
        {
            Dictionary <string, object> dictionary = null;
            List <object>   list         = null;
            ExpressionLexer lexer        = new ExpressionLexer(text);
            Token           currentToken = lexer.CurrentToken;

            if (currentToken.Id == TokenId.End)
            {
                instance = Empty;
                return(true);
            }
            instance = null;
            do
            {
                if ((currentToken.Id == TokenId.Identifier) && allowNamedValues)
                {
                    if (list != null)
                    {
                        return(false);
                    }
                    string identifier = lexer.CurrentToken.GetIdentifier();
                    lexer.NextToken();
                    if (lexer.CurrentToken.Id != TokenId.Equal)
                    {
                        return(false);
                    }
                    lexer.NextToken();
                    if (!lexer.CurrentToken.IsKeyValueToken)
                    {
                        return(false);
                    }
                    string str2 = lexer.CurrentToken.Text;
                    WebUtil.CreateIfNull <Dictionary <string, object> >(ref dictionary);
                    if (dictionary.ContainsKey(identifier))
                    {
                        return(false);
                    }
                    dictionary.Add(identifier, str2);
                }
                else
                {
                    if (!currentToken.IsKeyValueToken && (!allowNull || (currentToken.Id != TokenId.NullLiteral)))
                    {
                        return(false);
                    }
                    if (dictionary != null)
                    {
                        return(false);
                    }
                    WebUtil.CreateIfNull <List <object> >(ref list);
                    list.Add(lexer.CurrentToken.Text);
                }
                lexer.NextToken();
                currentToken = lexer.CurrentToken;
                if (currentToken.Id == TokenId.Comma)
                {
                    lexer.NextToken();
                    currentToken = lexer.CurrentToken;
                    if (currentToken.Id == TokenId.End)
                    {
                        return(false);
                    }
                }
            }while (currentToken.Id != TokenId.End);
            instance = new KeyInstance(dictionary, list);
            return(true);
        }
Exemple #6
0
        /// <summary>Attempts to parse key values from the specified text.</summary>
        /// <param name='text'>Text to parse (not null).</param>
        /// <param name="allowNamedValues">Set to true if the parser should accept named values
        ///   so syntax like Name='value'. If this is false, the parsing will fail on such constructs.</param>
        /// <param name="allowNull">Set to true if the parser should accept null values.
        ///   If set to false, the parser will fail on null values.</param>
        /// <param name='instance'>After invocation, the parsed key instance.</param>
        /// <returns>
        /// true if the key instance was parsed; false if there was a
        /// syntactic error.
        /// </returns>
        /// <remarks>
        /// The returned instance contains only string values. To get typed values, a call to
        /// <see cref="TryConvertValues"/> is necessary.
        /// </remarks>
        private static bool TryParseFromUri(string text, bool allowNamedValues, bool allowNull, out KeyInstance instance)
        {
            Debug.Assert(text != null, "text != null");

            Dictionary <string, object> namedValues = null;
            List <object> positionalValues          = null;

            ExpressionLexer lexer        = new ExpressionLexer(text);
            Token           currentToken = lexer.CurrentToken;

            if (currentToken.Id == TokenId.End)
            {
                instance = Empty;
                return(true);
            }

            instance = null;
            do
            {
                if (currentToken.Id == TokenId.Identifier && allowNamedValues)
                {
                    // Name-value pair.
                    if (positionalValues != null)
                    {
                        // We cannot mix named and non-named values.
                        return(false);
                    }

                    string identifier = lexer.CurrentToken.GetIdentifier();
                    lexer.NextToken();
                    if (lexer.CurrentToken.Id != TokenId.Equal)
                    {
                        return(false);
                    }

                    lexer.NextToken();
                    if (!lexer.CurrentToken.IsKeyValueToken)
                    {
                        return(false);
                    }

                    string namedValue = lexer.CurrentToken.Text;
                    WebUtil.CreateIfNull(ref namedValues);
                    if (namedValues.ContainsKey(identifier))
                    {
                        // Duplicate name.
                        return(false);
                    }

                    namedValues.Add(identifier, namedValue);
                }
                else if (currentToken.IsKeyValueToken || (allowNull && currentToken.Id == TokenId.NullLiteral))
                {
                    // Positional value.
                    if (namedValues != null)
                    {
                        // We cannot mix named and non-named values.
                        return(false);
                    }

                    WebUtil.CreateIfNull(ref positionalValues);
                    positionalValues.Add(lexer.CurrentToken.Text);
                }
                else
                {
                    return(false);
                }

                // Read the next token. We should be at the end, or find
                // we have a comma followed by something.
                lexer.NextToken();
                currentToken = lexer.CurrentToken;
                if (currentToken.Id == TokenId.Comma)
                {
                    lexer.NextToken();
                    currentToken = lexer.CurrentToken;
                    if (currentToken.Id == TokenId.End)
                    {
                        // Trailing comma.
                        return(false);
                    }
                }
            }while (currentToken.Id != TokenId.End);

            instance = new KeyInstance(namedValues, positionalValues);
            return(true);
        }
Exemple #7
0
 internal static bool TryGetMinResponseVersionForError(DataServiceHostWrapper host, Version maxProtocolVersion, out Version responseVersion)
 {
     responseVersion = null;
     try
     {
         Version version;
         if (((maxProtocolVersion > RequestDescription.Version2Dot0) && (host.RequestMaxVersion > RequestDescription.Version2Dot0)) && WebUtil.ResponseMediaTypeWouldBeJsonLight(host.RequestAccept, false))
         {
             responseVersion = RequestDescription.Version3Dot0;
         }
         if (!host.TryGetMinDataServiceVersionFromWrappedHost(out version))
         {
             return(responseVersion != null);
         }
         if ((responseVersion == null) || (version > responseVersion))
         {
             responseVersion = version;
         }
         if (maxProtocolVersion < RequestDescription.Version3Dot0)
         {
             responseVersion = RequestDescription.DataServiceDefaultResponseVersion;
             return(true);
         }
         if (!RequestDescription.IsKnownRequestVersion(responseVersion) || (responseVersion > maxProtocolVersion))
         {
             return(false);
         }
         return(true);
     }
     catch (Exception exception)
     {
         if (!CommonUtil.IsCatchableExceptionType(exception))
         {
             throw;
         }
     }
     return(false);
 }