Exemplo n.º 1
0
 private static void CheckNullInvalidCast(PathValue value)
 {
     if (value == null)
     {
         throw new InvalidCastException();
     }
 }
Exemplo n.º 2
0
        public PathAttribute(string name, PathValue value)
        {
            if (String.IsNullOrEmpty(name))
                throw new ArgumentNullException("name");

            if (value == null)
                value = new PathValue(null);

            this.name = name;
            this.value = value;
        }
Exemplo n.º 3
0
        public PathAttribute(string name, PathValue value)
        {
            if (String.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name");
            }

            if (value == null)
            {
                value = new PathValue(null);
            }

            this.name  = name;
            this.value = value;
        }
Exemplo n.º 4
0
        public object ToObject(Type type)
        {
            object obj = Activator.CreateInstance(type, true);

            MemberInfo[] memberInfos = type.FindMembers(MemberTypes.Field | MemberTypes.Property, BindingFlags.Public, null, null);

            int sz = memberInfos.Length;

            if (sz == 0)
            {
                return(obj);
            }

            for (int i = 0; i < sz; i++)
            {
                MemberInfo memberInfo = memberInfos[i];

                if (!HasMember(memberInfo.Name))
                {
                    continue;
                }

                Type memberType;
                if (memberInfo is FieldInfo)
                {
                    memberType = ((FieldInfo)memberInfo).FieldType;
                }
                else
                {
                    memberType = ((PropertyInfo)memberInfo).PropertyType;
                }

                PathValue pathValue = GetValue(memberInfo.Name);
                object    value     = Convert.ChangeType(pathValue, memberType);

                if (memberInfo is FieldInfo)
                {
                    ((FieldInfo)memberInfo).SetValue(obj, value);
                }
                else
                {
                    ((PropertyInfo)memberInfo).SetValue(obj, value, null);
                }
            }

            return(obj);
        }
Exemplo n.º 5
0
 public void SetValue(string memberName, PathValue value)
 {
     members[memberName] = value;
 }
Exemplo n.º 6
0
            public void Work(object state)
            {
                HttpListenerContext context = (HttpListenerContext)state;

                try {
                    // The main command dispatch loop for this connection,

                    string method = context.Request.HttpMethod;
                    RequestType requestType = (RequestType)Enum.Parse(typeof(RequestType), method, true);

                    string pathName = context.Request.Url.PathAndQuery;
                    string resourceId = null;

                    if (String.IsNullOrEmpty(pathName))
                        throw new InvalidOperationException("None path specified.");

                    if (pathName[0] == '/')
                        pathName = pathName.Substring(1);
                    if (pathName[pathName.Length - 1] == '/')
                        pathName = pathName.Substring(0, pathName.Length - 1);

                    int index = pathName.IndexOf('?');

                    Dictionary<string, PathValue> args = new Dictionary<string, PathValue>();
                    if (index != -1) {
                        //TODO: extract the transaction id from the query ...
                        string query = pathName.Substring(index + 1);
                        pathName = pathName.Substring(0, index);

                        if (!String.IsNullOrEmpty(query)) {
                            string[] sp = query.Split('&');
                            for (int i = 0; i < sp.Length; i++) {
                                string s = sp[i].Trim();
                                int idx = s.IndexOf('=');
                                if (idx == -1) {
                                    args[s] = PathValue.Null;
                                } else {
                                    string key = s.Substring(0, index);
                                    string value = s.Substring(index + 1);
                                    args[key] = new PathValue(value);
                                }
                            }
                        }
                    }

                    index = pathName.IndexOf('/');
                    if (index != -1) {
                        resourceId = pathName.Substring(index + 1);
                        pathName = pathName.Substring(0, index);
                    }

                    if (!String.IsNullOrEmpty(resourceId)) {
                        index = resourceId.IndexOf('/');
                        if (index != -1) {
                            string id = resourceId.Substring(index + 1);
                            resourceId = resourceId.Substring(0, index);
                            args[RequestMessage.ItemIdName] = new PathValue(id);
                        }

                        args[RequestMessage.ResourceIdName] = new PathValue(resourceId);
                    }

                    Stream requestStream = null;
                    if (requestType == RequestType.Post ||
                        requestType == RequestType.Put)
                        requestStream = context.Request.InputStream;

                    ResponseMessage response = service.HandleRequest(context, requestType, pathName, args, requestStream);

                    if (requestStream != null)
                        requestStream.Close();

                    if (response.Code == MessageResponseCode.Unauthorized) {
                        context.Response.StatusCode = 401;
                        context.Response.Close();
                        return;
                    } else if (response.Code == MessageResponseCode.NotFound) {
                        context.Response.StatusCode = 404;
                    } else if (response.Code == MessageResponseCode.UnsupportedFormat) {
                        context.Response.StatusCode = 415;
                    } else if (response.Code == MessageResponseCode.Error) {
                        context.Response.StatusCode = 500;
                        if (response.Arguments.Contains("message")) {
                            MessageArgument messageArg = response.Arguments["message"];
                            byte[] bytes = context.Response.ContentEncoding.GetBytes(messageArg.ToString());
                            context.Response.OutputStream.Write(bytes, 0, bytes.Length);
                            context.Response.OutputStream.Flush();
                        }
                    } else if (response.Code == MessageResponseCode.Success) {
                        if (requestType == RequestType.Post ||
                            requestType == RequestType.Put)
                            context.Response.StatusCode = 201;
                        else if (requestType == RequestType.Delete) {
                            context.Response.StatusCode = 204;
                            context.Response.Close();
                            return;
                        } else
                            context.Response.StatusCode = 200;

                        // TODO: make it recurive ...
                        if (!response.Request.HasItemId) {
                            foreach(MessageArgument argument in response.Arguments) {
                                if (argument.HasId) {
                                    StringBuilder href = new StringBuilder(service.Address.ToString());
                                    if (href[href.Length - 1] != '/')
                                        href.Append("/");
                                    href.Append(response.Request.ResourceId);
                                    href.Append("/");
                                    href.Append(argument.Id);
                                    href.Append("/");
                                    argument.Attributes["href"] = href.ToString();
                                }
                            }
                        }

                        // Write and flush the output message,
                        Stream responseStream = context.Response.OutputStream;
                        service.MessageSerializer.Serialize(response, responseStream);
                        responseStream.Flush();
                        responseStream.Close();
                    }
                } catch (IOException e) {
                    context.Response.StatusCode = 500;

                    if (e is EndOfStreamException) {
                        // Ignore this one also,
                    } else {
                        //TODO: ERROR log ...
                    }
                } finally {
                    // Make sure the socket is closed before we return from the thread,
                    try {
                        context.Response.Close();
                    } catch (IOException e) {
                        //TODO: ERROR log ...
                    }
                }
            }
Exemplo n.º 7
0
 public void SetValue(string memberName, PathValue value)
 {
     members[memberName] = value;
 }
Exemplo n.º 8
0
 private static void CheckNullInvalidCast(PathValue value)
 {
     if (value == null)
         throw new InvalidCastException();
 }
Exemplo n.º 9
0
 public void Write(PathValue value)
 {
     output.Write((byte)value.ValueType);
     WriteValue(value.Value);
 }
Exemplo n.º 10
0
 public void Write(PathValue value)
 {
     output.Write((byte)value.ValueType);
     WriteValue(value.Value);
 }
Exemplo n.º 11
0
        private PathValue ReadValue(PathValueType type)
        {
            if (type == PathValueType.Null)
            {
                return(PathValue.Null);
            }

            if (type == PathValueType.Boolean)
            {
                return(new PathValue(input.ReadBoolean()));
            }

            if (type == PathValueType.Byte)
            {
                return(new PathValue(input.ReadByte()));
            }
            if (type == PathValueType.Int16)
            {
                return(new PathValue(input.ReadInt16()));
            }
            if (type == PathValueType.Int32)
            {
                return(new PathValue(input.ReadInt32()));
            }
            if (type == PathValueType.Int64)
            {
                return(new PathValue(input.ReadInt64()));
            }
            if (type == PathValueType.Single)
            {
                return(new PathValue(input.ReadSingle()));
            }
            if (type == PathValueType.Double)
            {
                return(new PathValue(input.ReadDouble()));
            }

            if (type == PathValueType.String)
            {
                int           length = input.ReadInt32();
                StringBuilder sb     = new StringBuilder(length);
                for (int i = 0; i < length; i++)
                {
                    sb.Append(input.ReadChar());
                }

                return(new PathValue(sb.ToString()));
            }

            if (type == PathValueType.DateTime)
            {
                return(new PathValue(UnixDateTime.ToDateTime(input.ReadInt64())));
            }

            if (type == PathValueType.Struct)
            {
                int        memberCount = input.ReadInt32();
                PathStruct pathStruct  = new PathStruct();
                for (int i = 0; i < memberCount; i++)
                {
                    string    memberName = ReadValue(PathValueType.String);
                    PathValue value      = ReadValue();
                    pathStruct.SetValue(memberName, value);
                }

                return(new PathValue(pathStruct));
            }

            if (type == PathValueType.Array)
            {
                PathValueType elementType = (PathValueType)input.ReadByte();
                int           length      = input.ReadInt32();
                PathValue     array       = PathValue.CreateArray(elementType, length);

                for (int i = 0; i < length; i++)
                {
                    PathValue value = ReadValue(elementType);
                    array.SetValue(i, value.Value);
                }

                return(array);
            }

            throw new NotSupportedException();
        }
Exemplo n.º 12
0
        public override bool Equals(object obj)
        {
            PathValue other = obj as PathValue;

            if (other == null)
            {
                return(false);
            }

            if (IsArray)
            {
                if (!other.IsArray)
                {
                    return(false);
                }

                if (!valueType.Equals(other.valueType))
                {
                    return(false);
                }

                if (array.Length != other.array.Length)
                {
                    return(false);
                }

                for (int i = 0; i < array.Length; i++)
                {
                    object value1 = array.GetValue(i);
                    object value2 = other.array.GetValue(i);

                    if (value1 == null && value2 == null)
                    {
                        continue;
                    }

                    if (value1 != null && !value1.Equals(value2))
                    {
                        return(false);
                    }
                }

                return(true);
            }

            if (valueTypeCode != other.valueTypeCode)
            {
                return(false);
            }

            if (value == null && other.value == null)
            {
                return(true);
            }
            if (value == null)
            {
                return(false);
            }

            return(value.Equals(other.value));
        }