Example #1
0
        internal void AddMethodDetails(object T, MethodInfo info, String URI, EndPointAttribute endPtAttr)
        {
            m_obj = T;

            m_methodInfo = info;

            m_Consumes = endPtAttr.Consumes;
            m_Produces = endPtAttr.Produces;

            //m_delegate = Delegate.CreateDelegate(Program.getDelegateFactory().CreateDelegateType(info),
            //    T, info);

            ParameterInfo[] paramInfo = m_methodInfo.GetParameters();

            foreach (ParameterInfo Param in paramInfo)
            {
                object[]      baseAttr = Param.GetCustomAttributes(typeof(BaseAttribute), true);
                BaseAttribute attrib   = null;

                if (0 != baseAttr.Length)
                {
                    attrib = baseAttr[0] as BaseAttribute;
                    attrib.setType(Param.ParameterType);
                }
                else
                {
                    if (Param.ParameterType == typeof(HttpResp))
                    {
                        m_SequenceOfParams.Add(new HttpResp());
                    }

                    continue;
                }


                if (attrib.GetType() == typeof(BodyQueryParam))
                {
                    m_SequenceOfParams.Add(attrib);
                    continue;
                }

                if (attrib.GetType() == typeof(PathQueryVariable))
                {
                    var variable = attrib as PathQueryVariable;
                    variable.setPosInURL(Param.Position);
                    m_SequenceOfParams.Add(variable);
                    continue;
                }

                if (attrib.GetType() == typeof(BodyParam))
                {
                    m_SequenceOfParams.Add(attrib);
                    continue;
                }

                if (attrib.GetType() == typeof(PathVariable))
                {
                    var      variable = attrib as PathVariable;
                    Regex    parser   = new Regex("{" + variable.getName() + "}");
                    String[] splits   = parser.Split(URI);
                    variable.setPosInURL(splits[0].Length);

                    m_SequenceOfParams.Add(variable);

                    continue;
                }

                if (attrib.GetType() == typeof(HeaderParam))
                {
                    var param = attrib as HeaderParam;
                    m_SequenceOfParams.Add(param);
                    continue;
                }
            }
        }
        private void ScanAssemblyForAttributes(Assembly exA)
        {
            Type[] types = exA.GetTypes();

            foreach (Type T in types)
            {
                //System.Console.WriteLine(T.Name);

                //detecting controller class
                Object[] AttribOnClass = T.GetCustomAttributes(typeof(RouteAttribute), false);

                if (AttribOnClass.Length > 0)
                {
                    var obj = Activator.CreateInstance(T);

                    MethodInfo[] mInfo = T.GetMethods();

                    foreach (MethodInfo info in mInfo)
                    {
                        Attribute AttrbOnMethod = info.GetCustomAttribute(typeof(EndPointAttribute), false);

                        if (null != AttrbOnMethod)
                        {
                            EndPointAttribute CntrlMthdAttr = (EndPointAttribute)AttrbOnMethod;
                            Dictionary <String, ComponentMethodMapper> refHandle = null;
                            switch (CntrlMthdAttr.Method)
                            {
                            case Method.GET:
                                refHandle = m_MapOfGetControllers;
                                break;

                            case Method.POST:
                                refHandle = m_MapOfPostControllers;
                                break;

                            case Method.PUT:
                                refHandle = m_MapOfPutControllers;
                                break;

                            default:
                                throw new Exception();
                            }

                            //split the CntrlMthdAttr route to take only the context path
                            //void of path variables
                            Regex         parser = new Regex("[a-z A-z 0-9]*{[a-z A-z 0-9]*}");
                            String[]      str    = parser.Split(CntrlMthdAttr.getName());
                            StringBuilder bldr   = new StringBuilder();
                            for (int i = 0; i < str.Length; ++i)
                            {
                                bldr.Append(str[i]);
                                if (i < str.Length - 1)
                                {
                                    bldr.Append(".*");
                                }
                            }

                            String mapperKey = ((RouteAttribute)AttribOnClass[0]).getName() +
                                               bldr.ToString();

                            var hdlr = new ComponentMethodMapper();
                            hdlr.AddMethodDetails(obj, info as MethodInfo,
                                                  ((RouteAttribute)AttribOnClass[0]).getName() + CntrlMthdAttr.getName(),
                                                  CntrlMthdAttr);

                            try
                            {
                                refHandle.Add(mapperKey, hdlr);
                            }
                            catch (ArgumentException err)
                            {
                                throw new SameEndPointURL(((RouteAttribute)AttribOnClass[0]).getName() + CntrlMthdAttr.getName());
                            }
                        }
                    }
                }
            }
        }
Example #3
0
 private static string GetPath(EndPointAttribute attribute)
 {
     return(attribute.Path);
 }