public IHttpHandler GetHandler(HttpContext httpContext, string requestType, string url, string pathTranslated)
        {
            ExTraceGlobals.OehCallTracer.TraceDebug(0L, "OwaEventHandlerFactory.GetHandler");
            string          queryStringParameter  = Utilities.GetQueryStringParameter(httpContext.Request, "ns");
            string          queryStringParameter2 = Utilities.GetQueryStringParameter(httpContext.Request, "ev");
            ISessionContext sessionContext        = OwaContext.Get(httpContext).SessionContext;

            ExTraceGlobals.OehDataTracer.TraceDebug <string, string>(0L, "Request namespace: '{0}', event: '{1}'", queryStringParameter, queryStringParameter2);
            OwaEventNamespaceAttribute owaEventNamespaceAttribute = OwaEventRegistry.FindNamespaceInfo(queryStringParameter);

            if (owaEventNamespaceAttribute == null)
            {
                throw new OwaInvalidRequestException(string.Format(CultureInfo.InvariantCulture, "Namespace '{0}' doesn't exist", new object[]
                {
                    queryStringParameter
                }), null, this);
            }
            if (sessionContext != null && !sessionContext.IsProxy && !sessionContext.AreFeaturesEnabled(owaEventNamespaceAttribute.SegmentationFlags))
            {
                Utilities.EndResponse(httpContext, HttpStatusCode.Forbidden);
                return(null);
            }
            OwaEventAttribute owaEventAttribute = owaEventNamespaceAttribute.FindEventInfo(queryStringParameter2);

            if (owaEventAttribute == null)
            {
                throw new OwaInvalidRequestException(string.Format(CultureInfo.InvariantCulture, "Event '{0}' doesn't exist", new object[]
                {
                    queryStringParameter2
                }), null, this);
            }
            if (Globals.OwaVDirType == OWAVDirType.Calendar && !owaEventAttribute.AllowAnonymousAccess)
            {
                Utilities.EndResponse(httpContext, HttpStatusCode.BadRequest);
                return(null);
            }
            if (sessionContext != null && !sessionContext.IsProxy && !sessionContext.AreFeaturesEnabled(owaEventAttribute.SegmentationFlags))
            {
                Utilities.EndResponse(httpContext, HttpStatusCode.Forbidden);
                return(null);
            }
            OwaEventVerb owaEventVerb = OwaEventVerbAttribute.Parse(httpContext.Request.HttpMethod);

            ExTraceGlobals.OehDataTracer.TraceDebug <string>(0L, "Request verb: {0}", httpContext.Request.HttpMethod);
            if ((owaEventAttribute.AllowedVerbs & owaEventVerb) == OwaEventVerb.Unsupported)
            {
                ExTraceGlobals.OehTracer.TraceDebug <OwaEventVerb, OwaEventVerb>(0L, "Verb is not allowed, returning 405. Actual verb: {0}. Allowed: {1}.", owaEventVerb, owaEventAttribute.AllowedVerbs);
                Utilities.EndResponse(httpContext, HttpStatusCode.MethodNotAllowed);
                return(null);
            }
            OwaEventHandlerBase owaEventHandlerBase = (OwaEventHandlerBase)Activator.CreateInstance(owaEventNamespaceAttribute.HandlerType);

            owaEventHandlerBase.EventInfo  = owaEventAttribute;
            owaEventHandlerBase.OwaContext = OwaContext.Current;
            owaEventHandlerBase.Verb       = owaEventVerb;
            if (Globals.CollectPerRequestPerformanceStats)
            {
                OwaContext.Current.OwaPerformanceData.SetOehRequestType(owaEventNamespaceAttribute.HandlerType.Name, owaEventAttribute.IsAsync ? owaEventAttribute.BeginMethodInfo.Name : owaEventAttribute.MethodInfo.Name);
            }
            if (owaEventAttribute.IsAsync)
            {
                OwaContext.Current.TryReleaseBudgetAndStopTiming();
                ExTraceGlobals.OehTracer.TraceDebug(0L, "Created async HTTP handler to server OEH request");
                OwaContext.Current.IsAsyncRequest = true;
                return(new OwaEventAsyncHttpHandler(owaEventHandlerBase));
            }
            ExTraceGlobals.OehTracer.TraceDebug(0L, "Created sync HTTP handler to serve OEH request");
            return(new OwaEventHttpHandler(owaEventHandlerBase));
        }
Пример #2
0
        public static void RegisterHandler(Type handlerType)
        {
            ExTraceGlobals.OehCallTracer.TraceDebug(0L, "OwaEventRegistry.RegisterHandler");
            if (handlerType == null)
            {
                throw new ArgumentNullException("handlerType");
            }
            object[] customAttributes = handlerType.GetCustomAttributes(typeof(OwaEventNamespaceAttribute), false);
            if (customAttributes == null || customAttributes.Length == 0)
            {
                throw new OwaNotSupportedException("Internal error: Handler is missing OwaEventNamespaceAttribute attribute");
            }
            OwaEventNamespaceAttribute owaEventNamespaceAttribute = (OwaEventNamespaceAttribute)customAttributes[0];

            owaEventNamespaceAttribute.HandlerType = handlerType;
            customAttributes = handlerType.GetCustomAttributes(typeof(OwaEventSegmentationAttribute), false);
            if (customAttributes != null && customAttributes.Length > 0)
            {
                OwaEventSegmentationAttribute owaEventSegmentationAttribute = (OwaEventSegmentationAttribute)customAttributes[0];
                owaEventNamespaceAttribute.SegmentationFlags = owaEventSegmentationAttribute.SegmentationFlags;
            }
            else
            {
                owaEventNamespaceAttribute.SegmentationFlags = 0UL;
            }
            Type objectIdType = null;

            customAttributes = handlerType.GetCustomAttributes(typeof(OwaEventObjectIdAttribute), false);
            if (customAttributes != null && customAttributes.Length > 0)
            {
                objectIdType = ((OwaEventObjectIdAttribute)customAttributes[0]).ObjectIdType;
            }
            ExTraceGlobals.OehDataTracer.TraceDebug <string>(0L, "Handler type: '{0}'", handlerType.ToString());
            foreach (MethodInfo methodInfo in handlerType.GetMethods(BindingFlags.Instance | BindingFlags.Public))
            {
                customAttributes = methodInfo.GetCustomAttributes(typeof(OwaEventAttribute), false);
                if (customAttributes != null && customAttributes.Length > 0)
                {
                    OwaEventAttribute owaEventAttribute = (OwaEventAttribute)customAttributes[0];
                    ParameterInfo[]   parameters        = methodInfo.GetParameters();
                    if (methodInfo.ReturnType == typeof(IAsyncResult))
                    {
                        if (parameters.Length != 2 || parameters[0].ParameterType != typeof(AsyncCallback) || parameters[1].ParameterType != typeof(object))
                        {
                            throw new OwaNotSupportedException("Wrong signature for async event handler method.");
                        }
                        owaEventAttribute.IsAsync         = true;
                        owaEventAttribute.BeginMethodInfo = methodInfo;
                    }
                    else if (methodInfo.ReturnType == typeof(void))
                    {
                        if (parameters.Length == 1 && parameters[0].ParameterType == typeof(IAsyncResult))
                        {
                            owaEventAttribute.EndMethodInfo = methodInfo;
                            owaEventAttribute.IsAsync       = true;
                        }
                        else
                        {
                            if (parameters.Length != 0)
                            {
                                throw new OwaNotSupportedException("Wrong signature for event handler method.");
                            }
                            owaEventAttribute.MethodInfo = methodInfo;
                            owaEventAttribute.IsAsync    = false;
                        }
                    }
                    if (!owaEventAttribute.IsAsync || !(null != owaEventAttribute.EndMethodInfo))
                    {
                        OwaEventRegistry.ScanHandlerAttributes(methodInfo, owaEventAttribute, objectIdType);
                    }
                    owaEventNamespaceAttribute.AddEventInfo(owaEventAttribute);
                }
            }
            foreach (object obj in owaEventNamespaceAttribute.EventInfoTable.Values)
            {
                OwaEventAttribute owaEventAttribute2 = obj as OwaEventAttribute;
                if (owaEventAttribute2.MethodInfo != null)
                {
                    if (owaEventAttribute2.BeginMethodInfo != null || owaEventAttribute2.EndMethodInfo != null)
                    {
                        throw new OwaNotSupportedException("Namespace defines the same event both sync and async");
                    }
                }
                else if (owaEventAttribute2.BeginMethodInfo != null)
                {
                    if (owaEventAttribute2.EndMethodInfo == null)
                    {
                        throw new OwaNotSupportedException(string.Format("Begin async method {0} for event {1} is missing its corresponding End method", owaEventAttribute2.BeginMethodInfo.Name, owaEventAttribute2.Name));
                    }
                }
                else if (owaEventAttribute2.EndMethodInfo != null && owaEventAttribute2.BeginMethodInfo == null)
                {
                    throw new OwaNotSupportedException(string.Format("End async method {0} for event {1} is missing its corresponding Begin method", owaEventAttribute2.EndMethodInfo.Name, owaEventAttribute2.Name));
                }
            }
            OwaEventRegistry.handlerTable.Add(owaEventNamespaceAttribute.Name, owaEventNamespaceAttribute);
        }