Exemple #1
0
        //static IFormatter GetEncoder(WADLParam param)
        //{
        //    Type type = Type.GetType(TemplateConfigurationHandler.GetConfiguration().Encoders[param.Encoding].Type);
        //    return (IFormatter)Activator.CreateInstance(type);
        //}

        static void Encode(Stream stream, WADLRepresentation param, object value)
        {
            TemplateConfigurationHandler handler = TemplateConfigurationHandler.GetConfiguration();
            var encoderCnfg = handler.Encoders[param.ContentType];

            if (!String.IsNullOrEmpty(encoderCnfg.Type))
            {
                Type            encoder   = Type.GetType(encoderCnfg.Type);
                INetHttpEncoder formatter = Activator.CreateInstance(encoder) as INetHttpEncoder;
                formatter.Encode(stream, param, value);
            }
        }
        public void Encode(Stream stream, WADLRepresentation param, object value)
        {
            switch (System.Type.GetTypeCode(value.GetType()))
            {
                case TypeCode.SByte:
                case TypeCode.Byte:
                    stream.WriteByte((byte)value);
                break;
                case TypeCode.Object:
                if (value is Byte[])
                    {
                        byte[] bytes = (byte[])value;
                        stream.Write(bytes, 0, bytes.Length);
                    }
                    else
                    {
                        StreamWriter objWriter = new StreamWriter(stream);
                        objWriter.AutoFlush = true;
                        objWriter.Write(value.ToString());
                    }
                    break;
                case TypeCode.Empty:
                case TypeCode.DBNull:
                    break;
                case TypeCode.Char:
                case TypeCode.UInt16:
                case TypeCode.UInt32:
                case TypeCode.UInt64:
                case TypeCode.Boolean:
                case TypeCode.Int16:
                case TypeCode.Int32:
                case TypeCode.Int64:
                case TypeCode.Single:
                case TypeCode.Double:
                case TypeCode.Decimal:
                case TypeCode.DateTime:
                case TypeCode.String:
                default:
                    StreamWriter defaultWriter = new StreamWriter(stream);
                    defaultWriter.AutoFlush = true;
                    defaultWriter.Write(value.ToString());
                    
                    break;
            }

           

        }
Exemple #3
0
        public void Encode(Stream stream, WADLRepresentation param, object value)
        {
            switch (System.Type.GetTypeCode(value.GetType()))
            {
            case TypeCode.SByte:
            case TypeCode.Byte:
                stream.WriteByte((byte)value);
                break;

            case TypeCode.Object:
                if (value is Byte[])
                {
                    byte[] bytes = (byte[])value;
                    stream.Write(bytes, 0, bytes.Length);
                }
                else
                {
                    StreamWriter objWriter = new StreamWriter(stream);
                    objWriter.AutoFlush = true;
                    objWriter.Write(value.ToString());
                }
                break;

            case TypeCode.Empty:
            case TypeCode.DBNull:
                break;

            case TypeCode.Char:
            case TypeCode.UInt16:
            case TypeCode.UInt32:
            case TypeCode.UInt64:
            case TypeCode.Boolean:
            case TypeCode.Int16:
            case TypeCode.Int32:
            case TypeCode.Int64:
            case TypeCode.Single:
            case TypeCode.Double:
            case TypeCode.Decimal:
            case TypeCode.DateTime:
            case TypeCode.String:
            default:
                StreamWriter defaultWriter = new StreamWriter(stream);
                defaultWriter.AutoFlush = true;
                defaultWriter.Write(value.ToString());

                break;
            }
        }
Exemple #4
0
 public void Encode(Stream stream, WADLRepresentation param, object value)
 {
     if (param.ParameterInfo.ParameterType == typeof(String) && param.Path != SchemaTypes.@string)
     {
         StreamWriter objWriter = new StreamWriter(stream);
         objWriter.AutoFlush = true;
         objWriter.Write(value.ToString());
     }
     else
     {
         XmlSerializer sr = new XmlSerializer(value.GetType());
         sr.Serialize(stream, value);
         stream.Flush();
         stream.Position = 0;
     }
 }
        public void Encode(Stream stream, WADLRepresentation param, object value)
        {
            if (param.ParameterInfo.ParameterType == typeof(String) && param.Path != SchemaTypes.@string)
            {
                StreamWriter objWriter = new StreamWriter(stream);
                objWriter.AutoFlush = true;
                objWriter.Write(value.ToString());
            }
            else
            {
                XmlSerializer sr = new XmlSerializer(value.GetType());
                sr.Serialize(stream, value);
                stream.Flush();
                stream.Position = 0;
            }

        }
Exemple #6
0
        //TODO: Method needs to be evaluated
        //deserialize parameters
        public void Invoke(NetHttpContext context)
        {
            List <object> parameters = new List <object>();

            ParameterInfo[] @params = Method.MethodInfo.GetParameters();
            Array.ForEach(@params, delegate(ParameterInfo param) {
                WADLParam[] attributes = WADLParam.GetWADLParams(param);
                if (param.ParameterType.IsArray)
                {
                    Type type   = Type.GetType(param.ParameterType.AssemblyQualifiedName.Replace("[]", ""));
                    Array array = Array.CreateInstance(type, attributes.Length);
                    IList list  = Activator.CreateInstance(typeof(List <>).MakeGenericType(type)) as IList;
                    Resolve(context, list, attributes);
                    list.CopyTo(array, 0);
                    parameters.Add(array);
                }
                else
                {
                    if (attributes.Length == 0)
                    {
                        WADLParam wadlParam = WADLParam.CreateWADLParam(param);
                        parameters.Add(Resolve(context, wadlParam));
                    }
                    else
                    {
                        parameters.Add(Resolve(context, attributes.Single()));
                    }
                }
            });

            NetHttpChannelManager instanceToInvoke = Instance;

            if (Instance.GetInstancingModeAttribute() == InstancingMode.PerCall)
            {
                instanceToInvoke = Activator.CreateInstance(Instance.GetType()) as NetHttpChannelManager;
            }
            var result = Method.MethodInfo.Invoke(instanceToInvoke, parameters.ToArray());

            if (result == null)
            {
                WebOperationContext.Current.OutgoingResponse.SetStatusAsNotFound();
                WebOperationContext.Current.OutgoingResponse.SuppressEntityBody = true;
            }
            else
            {
                var outputs = (from representation in Method.Representations
                               select representation).Where <WADLRepresentation>(delegate(WADLRepresentation rep)
                {
                    return(rep.Status == System.Net.HttpStatusCode.OK);
                });

                WADLRepresentation output;

                if (outputs.Count() == 1)
                {
                    output = outputs.Single();
                    context.Response.ContentType = output.ContentType;
                }
                else
                {
                    output = outputs.DefaultIfEmpty(null).SingleOrDefault(o => o.ContentType == context.Response.ContentType);
                    if (output == null)
                    {
                        output = new WADLRepresentation(ContentTypes.unknown);
                        context.Response.ContentType = output.ContentType;
                    }
                }

                Encode(context.Response.Buffer, output, result);
            }
        }
        //static IFormatter GetEncoder(WADLParam param)
        //{
        //    Type type = Type.GetType(TemplateConfigurationHandler.GetConfiguration().Encoders[param.Encoding].Type);
        //    return (IFormatter)Activator.CreateInstance(type);
        //}

        static void Encode(Stream stream, WADLRepresentation param, object value)
        {
            TemplateConfigurationHandler handler = TemplateConfigurationHandler.GetConfiguration();
            var encoderCnfg = handler.Encoders[param.ContentType];
            if (!String.IsNullOrEmpty(encoderCnfg.Type))
            {
                Type encoder = Type.GetType(encoderCnfg.Type);
                INetHttpEncoder formatter = Activator.CreateInstance(encoder) as INetHttpEncoder;
                formatter.Encode(stream, param, value);
            }
        }
        //TODO: Method needs to be evaluated
        //deserialize parameters
        public void Invoke(NetHttpContext context)
        {
            List<object> parameters =  new List<object>();
            ParameterInfo[] @params = Method.MethodInfo.GetParameters();
            Array.ForEach(@params, delegate(ParameterInfo param) {
                WADLParam[] attributes = WADLParam.GetWADLParams(param);
                if (param.ParameterType.IsArray)
                {
                    Type type = Type.GetType(param.ParameterType.AssemblyQualifiedName.Replace("[]", ""));
                    Array array = Array.CreateInstance(type,attributes.Length);
                    IList list = Activator.CreateInstance(typeof(List<>).MakeGenericType(type)) as IList;
                    Resolve(context, list, attributes);
                    list.CopyTo(array, 0);
                    parameters.Add(array);
                }
                else
                {
                    if (attributes.Length == 0)
                    {
                        WADLParam wadlParam = WADLParam.CreateWADLParam(param);
                        parameters.Add(Resolve(context, wadlParam));
                    }
                    else
                    {
                        parameters.Add(Resolve(context, attributes.Single()));
                    }
                }
             });

            NetHttpChannelManager instanceToInvoke = Instance;
            if (Instance.GetInstancingModeAttribute() == InstancingMode.PerCall)
            {
                instanceToInvoke = Activator.CreateInstance(Instance.GetType()) as NetHttpChannelManager;
            }
            var result = Method.MethodInfo.Invoke(instanceToInvoke, parameters.ToArray());
            if (result == null)
            {
                WebOperationContext.Current.OutgoingResponse.SetStatusAsNotFound();
                WebOperationContext.Current.OutgoingResponse.SuppressEntityBody = true;
            }
            else 
            {
                var outputs = (from representation in Method.Representations
                               select representation).Where<WADLRepresentation>(delegate(WADLRepresentation rep)
                {
                    return rep.Status == System.Net.HttpStatusCode.OK;
                });

                WADLRepresentation output;

                if (outputs.Count() == 1)
                {
                    output = outputs.Single();
                    context.Response.ContentType = output.ContentType;
                }
                else
                {
                    output = outputs.DefaultIfEmpty(null).SingleOrDefault(o => o.ContentType == context.Response.ContentType);
                    if (output == null)
                    {
                        output = new WADLRepresentation(ContentTypes.unknown);
                        context.Response.ContentType = output.ContentType;
                    }
                }

                Encode(context.Response.Buffer, output, result);
            }

        }