Esempio n. 1
0
 protected PreflightResult SendPreflight(string path)
 {
     try
     {
         if (HeadRetryCounter == 0)
         {
             //HTTP GET
             return(SendPreflightRequest(path, System.Net.Http.HttpMethod.Get));
         }
         else
         {
             //HTTP HEAD
             PreflightResult preflightResult = SendPreflightRequest(path, System.Net.Http.HttpMethod.Head);
             if (preflightResult.StatusCode != 200)
             {
                 Log.Warning(ProxyHandler.TAG, "It seems, that remote API does not support HEAD. Retry with GET ...");
                 preflightResult = SendPreflightRequest(path, System.Net.Http.HttpMethod.Get);
                 if (HeadRetryCounter != -1)
                 {
                     HeadRetryCounter--;
                 }
             }
             return(preflightResult);
         }
     }
     catch (Exception e)
     {
         return(new PreflightResult(999, e.Message));
     }
 }
Esempio n. 2
0
        private void AddOneToOneReachabilityAssociations(JObject data, IList <KeyValuePair <PathSegment, string> > pathSegmentsRepresentingStableUriPath)
        {
            //extract path segment representing the queried or manipulated resource (a check whether it exists has been already done before)
            PathSegment pathSegmentRepresentingQueriedOrManipulatedResource = pathSegmentsRepresentingStableUriPath[pathSegmentsRepresentingStableUriPath.Count - 1].Key;

            foreach (ReachabilityPath reachabilityAssociation in ((CustomizedPathSegment)pathSegmentRepresentingQueriedOrManipulatedResource).ReachabilityPaths)
            {
                //filter for one-to-one reachability associations
                if (reachabilityAssociation.Type == ReachabilityPathType.oneToOne)
                {
                    //create stable URI path pointing on target
                    IList <KeyValuePair <PathSegment, string> > pathSegmentsToTarget = _uriModel.GetPathSegmentsByStablePathSegments(reachabilityAssociation.Target, ConvertListToDictionary(pathSegmentsRepresentingStableUriPath));
                    string pathToTarget = UriModel.BuildFullPath(pathSegmentsToTarget);

                    //send pre-flight request:
                    PreflightResult preflightResult = SendPreflight(pathToTarget);
                    if (preflightResult.StatusCode < 200 || preflightResult.StatusCode >= 300)
                    {
                        if (_hyperlinkWithDebugInformation)
                        {
                            Hyperlink hyperlink = new Hyperlink()
                            {
                                Href             = pathToTarget,
                                Rel              = pathSegmentRepresentingQueriedOrManipulatedResource.IsAncestorOf(reachabilityAssociation.Target) ? DESCENDANT_REL : ANCESTOR_REL,
                                Debug_statusCode = preflightResult.StatusCode,
                                Debug_msg        = preflightResult.Message
                            };
                            this.AddObjectToLinksArray(data, hyperlink);
                        }
                    }
                    else
                    {
                        Hyperlink hyperlink = new Hyperlink()
                        {
                            Href = pathToTarget,
                            Rel  = pathSegmentRepresentingQueriedOrManipulatedResource.IsAncestorOf(reachabilityAssociation.Target) ? DESCENDANT_REL : ANCESTOR_REL
                        };
                        this.AddObjectToLinksArray(data, hyperlink);
                    }
                }
            }
        }
Esempio n. 3
0
        public void AddOneToManyReachabilityAssociations(JObject data, IList <KeyValuePair <PathSegment, string> > pathSegmentsRepresentingStableUriPath, int statusCode, SKotstein.Net.Http.Context.HttpMethod method, string xPathPrefix)
        {
            //extract path segment representing the queried or manipulated resource (a check whether it exists has been already done before)
            PathSegment pathSegmentRepresentingQueriedOrManipulatedResource = pathSegmentsRepresentingStableUriPath[pathSegmentsRepresentingStableUriPath.Count - 1].Key;

            foreach (ReachabilityPath reachabilityAssociation in ((CustomizedPathSegment)pathSegmentRepresentingQueriedOrManipulatedResource).ReachabilityPaths)
            {
                //filter for one-to-one reachability associations
                if (reachabilityAssociation.Type == ReachabilityPathType.oneToMany)
                {
                    foreach (Link link in reachabilityAssociation.Links)
                    {
                        //check whether status code matches documented status code in link
                        if (link.StatusCode.CompareTo(statusCode + "") == 0)
                        {
                            //check whether method matches documented method in link
                            switch (method)
                            {
                            case SKotstein.Net.Http.Context.HttpMethod.GET:
                                if (link.Operation.Method != urimodel.HttpMethod.GET)
                                {
                                    continue;
                                }
                                break;

                            case SKotstein.Net.Http.Context.HttpMethod.PATCH:
                                if (link.Operation.Method != urimodel.HttpMethod.PATCH)
                                {
                                    continue;
                                }
                                break;

                            case SKotstein.Net.Http.Context.HttpMethod.POST:
                                if (link.Operation.Method != urimodel.HttpMethod.POST)
                                {
                                    continue;
                                }
                                break;

                            case SKotstein.Net.Http.Context.HttpMethod.PUT:
                                if (link.Operation.Method != urimodel.HttpMethod.PUT)
                                {
                                    continue;
                                }
                                break;

                            case SKotstein.Net.Http.Context.HttpMethod.DELETE:
                                if (link.Operation.Method != urimodel.HttpMethod.DELETE)
                                {
                                    continue;
                                }
                                break;
                            }
                        }
                        else
                        {
                            continue;
                        }

                        //load values
                        string xPath = link.XPath.Replace("$", xPathPrefix);
                        IEnumerable <JToken> values = data.SelectTokens(xPath);

                        //iterate over values
                        foreach (JToken value in values)
                        {
                            IDictionary <PathSegment, string> pathSegmentValues = ConvertListToDictionary(pathSegmentsRepresentingStableUriPath);

                            //subsitutute path parameter
                            IDictionary <PathParameter, string> pathParameterMapping = new Dictionary <PathParameter, string>();
                            pathParameterMapping.Add(link.PathParameter, (string)value);
                            string substitutedVariablePathSegment = link.PathParameter.PathSegment.BuildStablePathSegment(pathParameterMapping);

                            //add subsituted variable path segment
                            pathSegmentValues.Add(link.PathParameter.PathSegment, substitutedVariablePathSegment);

                            //create stable URI path pointing on target
                            IList <KeyValuePair <PathSegment, string> > pathSegmentsToTarget = _uriModel.GetPathSegmentsByStablePathSegments(reachabilityAssociation.Target, pathSegmentValues);
                            string pathToTarget = UriModel.BuildFullPath(pathSegmentsToTarget);

                            //send pre-flight request:
                            PreflightResult preflightResult = SendPreflight(pathToTarget);
                            if (preflightResult.StatusCode < 200 || preflightResult.StatusCode >= 300)
                            {
                                if (_hyperlinkWithDebugInformation)
                                {
                                    Hyperlink hyperlink = new Hyperlink()
                                    {
                                        Href             = pathToTarget,
                                        Rel              = ITEM_REL,
                                        Debug_statusCode = preflightResult.StatusCode,
                                        Debug_msg        = preflightResult.Message
                                    };


                                    this.AddObjectToLinksArray(value.Parent.Parent, hyperlink);
                                }
                            }
                            else
                            {
                                Hyperlink hyperlink = new Hyperlink()
                                {
                                    Href = pathToTarget,
                                    Rel  = ITEM_REL
                                };
                                this.AddObjectToLinksArray(value.Parent.Parent, hyperlink);
                            }
                        }
                    }
                }
            }
        }