Ejemplo n.º 1
0
        /// <summary>
        ///     Выполняет синхронный запрос ресурса
        /// </summary>
        /// <returns></returns>
        public async Task <IResourceResponse> GetResponse(IResourceConfig config = null)
        {
            if (CheckCurrentCanBeReturned())
            {
                return(Response);
            }
            config = config ?? Config ?? ResourceConfig.Default;
            if (ResourceRequestState.Init == State)
            {
                if (null == Response)
                {
                    Response = await InternalGetResponse(config);
                }
            }
            if (ResourceRequestState.Error == State)
            {
                if (null == LastError)
                {
                    LastError = new ResourceException("some error in request");
                }
                throw LastError;
            }

            return(Response);
        }
Ejemplo n.º 2
0
        public void TestAtResourceExceptionKanSerialiseresOgDeserialiseres()
        {
            var fixture        = new Fixture();
            var message        = fixture.Create <string>();
            var innerException = fixture.Create <Exception>();

            var exception = new ResourceException(message, innerException);

            Assert.That(exception, Is.Not.Null);
            var memoryStream = new MemoryStream();

            try
            {
                var serializer = new SoapFormatter();
                serializer.Serialize(memoryStream, exception);
                Assert.That(memoryStream.Length, Is.GreaterThan(0));

                memoryStream.Seek(0, SeekOrigin.Begin);
                Assert.That(memoryStream.Position, Is.EqualTo(0));

                var deserializedException = (ResourceException)serializer.Deserialize(memoryStream);
                Assert.That(deserializedException, Is.Not.Null);
                Assert.That(deserializedException.Message, Is.Not.Null);
                Assert.That(deserializedException.Message, Is.EqualTo(exception.Message));
                Assert.That(deserializedException.InnerException, Is.Not.Null);
                Assert.That(deserializedException.InnerException.GetType(), Is.EqualTo(typeof(Exception)));
            }
            finally
            {
                memoryStream.Close();
            }
        }
Ejemplo n.º 3
0
        private async Task <IResourceResponse> InternalGetResponse(IResourceConfig config)
        {
            try {
                if (config.UseQwebAuthentication)
                {
                    PerformQwebAuthentiacation(config);
                }
                config = config ?? ResourceConfig.Default;
                State  = ResourceRequestState.Creating;
                var nativeRequest = WebRequest.Create(Uri);
                SetupNativeRequest(nativeRequest, config);
                State = ResourceRequestState.Created;
                await PostDataToServer(config, nativeRequest);

                State = ResourceRequestState.Get;
                if (config.AcceptAllCeritficates)
                {
                    nativeRequest.Headers[ALLOW_ALL_CERTIFICATES_HEADER] = "1";
                }
                var nativeResponse = await nativeRequest.GetResponseAsync();

                State = ResourceRequestState.Finished;
                return(new WebResourceResponse(nativeResponse, nativeRequest, config));
            }
            catch (Exception ex) {
                State     = ResourceRequestState.Error;
                LastError = new ResourceException("erorr in get response", ex);
            }
            return(null);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Синхронный доступ к ресурсу
        /// </summary>
        /// <returns></returns>
#pragma warning disable 1998
        public async Task <IResource> GetResource()
        {
#pragma warning restore 1998
            if (CheckCanReturnCurrent())
            {
                return(Resource);
            }
            if (ResourceResponseState.Init == State)
            {
                if (null == Resource)
                {
                    Resource = InternalGetResource();
                    State    = ResourceResponseState.Finished;
                }
            }

            if (ResourceResponseState.Error == State)
            {
                if (null == LastError)
                {
                    LastError = new ResourceException("some error in request");
                }
                throw LastError;
            }

            return(Resource);
        }
Ejemplo n.º 5
0
        public Stream GetConfiguration()
        {
            string configuration = ScanForTilesConfiguration(_inAssembly);

            if (configuration == null)
            {
                throw ResourceException.ConfigFileNotFoundInAssembly(CONFIG_FILE, _inAssembly);
            }
            return(GetNewLocator().GetStream(configuration));
        }
Ejemplo n.º 6
0
 public void TestResouceNotFoundCompiled()
 {
     try
     {
         new ResourceBundle("wrong", null, new AssemblyBasedResourceLocator(GetType().Assembly, null));
         Assert.Fail("Expected exception");
     }
     catch (ResourceException Te)
     {
         Assert.That(Te.Message,
                     Is.EqualTo(ResourceException.ResourceBundleNotFound("wrong").Message));
     }
 }
Ejemplo n.º 7
0
 public void TestFileNotFoundCompiled()
 {
     try
     {
         new ResourceBundle("wrong", null);
         Assert.Fail("Expected exception");
     }
     catch (ResourceException Te)
     {
         Assert.That(Te.Message,
                     Is.EqualTo(ResourceException.ResourceBundleNotFound("wrong").Message));
     }
 }
Ejemplo n.º 8
0
        public void TestAtResourceExceptionKanInstantieres()
        {
            var fixture        = new Fixture();
            var message        = fixture.Create <string>();
            var innerException = fixture.Create <Exception>();

            var exception = new ResourceException(message);

            Assert.That(exception, Is.Not.Null);
            Assert.That(exception.Message, Is.EqualTo(message));
            Assert.That(exception.InnerException, Is.Null);

            exception = new ResourceException(message, innerException);
            Assert.That(exception, Is.Not.Null);
            Assert.That(exception.Message, Is.EqualTo(message));
            Assert.That(exception.InnerException, Is.Not.Null);
            Assert.That(exception.InnerException.GetType(), Is.EqualTo(typeof(Exception)));
        }
Ejemplo n.º 9
0
 public void WrongFileName()
 {
     try
     {
         new TemplateTile("name", new FileTemplate("somepath.htm"), null);
         Assert.Fail("Expected exception");
     }
     catch (TemplateException Te)
     {
         var expected = ResourceException.FileNotFound(Path.GetFullPath("somepath.htm"));
         Assert.That(
             Te.InnerException.Message,
             Is.EqualTo(expected.Message)
             );
         Assert.That(
             Te.Message,
             Is.EqualTo(TemplateException.TemplateFailedToInitialize(Path.GetFullPath("somepath.htm"), expected).Message));
     }
 }
Ejemplo n.º 10
0
 public void DeserializeFromAssemlbyWithoutConfigFileShouldThrowException()
 {
     try
     {
         new TileXmlConfigurator(_lib,
                                 Assembly.GetAssembly(typeof(String)));
         Assert.Fail("Expected exception");
     }
     catch (ResourceException e)
     {
         Assert.That(
             e.Message,
             Is.EqualTo(
                 ResourceException.ConfigFileNotFoundInAssembly(
                     "tiles.xml",
                     Assembly.GetAssembly(typeof(String))
                     ).Message
                 )
             );
     }
 }
Ejemplo n.º 11
0
 private bool CheckCanReturnCurrent()
 {
     if (ResourceResponseState.Init == State)
     {
         return(false);
     }
     if (ResourceResponseState.Error == State)
     {
         if (null == LastError)
         {
             LastError = new ResourceException("some error in request");
         }
         throw LastError;
     }
     if (ResourceResponseState.Finished == State)
     {
         {
             return(true);
         }
     }
     throw new ResourceException("insuficient state for GetResource call " + State);
 }
Ejemplo n.º 12
0
        public int Build(string parentUnid, string parentType, string[] childUnids, string childType)
        {
            if (childUnids == null || childUnids.Length == 0)
            {
                return(0);
            }

            // 不允许parentUnid, parentType, childType任一参数为空
            if (string.IsNullOrEmpty(parentUnid) ||
                string.IsNullOrEmpty(parentType) ||
                string.IsNullOrEmpty(childType))
            {
                ResourceException e = new ResourceException("ARGUMENTS.ERROR.EMPTY", "[parentUnid, parentType, childType]");
                logger.Error(e.Message, e);
                throw e;
            }

            RelationShip relationShip;
            int          count = 0;

            foreach (string childUnid in childUnids)
            {
                relationShip = this.relationShipDao.Get(parentUnid, childUnid);
                if (relationShip != null)
                {
                    // 关联关系已存在,仅作更新
                    relationShip.Update(parentUnid, parentType, childUnid, childType);
                }
                else
                {
                    // 创建新的关联关系
                    relationShip = new RelationShip(parentUnid, parentType, childUnid, childType);
                }
                this.relationShipDao.Save(relationShip);
                count++;
            }
            return(count);
        }
 public static Task <bool> CreateFromApiError(IOwinEnvironment context, ResourceException rex, CancellationToken cancellationToken)
 => Create(context, rex.HttpStatus, rex.Message, cancellationToken);
Ejemplo n.º 14
0
        public void TestLastExceptionIsFilled()
        {
            var tempTile = Path.GetTempFileName();

            File.Copy("a.htm", tempTile, true);
            try
            {
                var ft = new FileTemplate(tempTile);
                Assert.That(ft.TileLastModified, Is.EqualTo(ft.ResourceLastModified));
                File.Delete(tempTile);
                Assert.That(ft.RefreshException, Is.Null);
                ft.Refresh();
                Assert.That(ft.RefreshException, Is.Not.Null);
                Assert.That(ft.RefreshException.Message, Is.EqualTo(TemplateException.TemplateFailedToInitialize(tempTile, ResourceException.FileNotFound(tempTile)).Message));
            }
            finally
            {
                File.Delete(tempTile);
            }
        }