/// <summary>
 /// Downloads the File from Repository
 /// </summary>
 /// <param name="file">File object.</param>
 /// <param name="repository">Repository instance.</param>
 /// <param name="user">User instance.</param>
 /// <param name="credentials">credentials required by the repository.</param>
 /// <returns>DataFile containing the file data.</returns>
 public virtual DataFile DownLoadFileFromRepository(DM.File file, Repository repository, User user, RepositoryCredentials credentials)
 {
     throw new NotSupportedException();
 }
Exemplo n.º 2
0
        /// <summary>
        /// Downloads the File from Repository
        /// </summary>
        /// <param name="file">File object.</param>
        /// <param name="repository">Repository instance.</param>
        /// <param name="user">User instance.</param>
        /// <param name="credentials">credentials required by the repository.</param>
        /// <returns>DataFile containing the file data.</returns>
        public override DataFile DownLoadFileFromRepository(DomainModel.File file, Repository repository, User user, RepositoryCredentials credentials)
        {
            string userName = string.Empty;
            string password = string.Empty;

            if ((bool)repository.IsImpersonating)
            {
                userName = repository.ImpersonatingUserName;
                password = repository.ImpersonatingPassword;
            }
            else
            {
                userName = credentials[BasicAuthenticationCredentialKeys.UserName];
                password = credentials[BasicAuthenticationCredentialKeys.Password];

                if (string.IsNullOrEmpty(userName))
                {
                    throw new ArgumentNullException(BasicAuthenticationCredentialKeys.UserName);
                }

                if (string.IsNullOrEmpty(password))
                {
                    throw new ArgumentNullException(BasicAuthenticationCredentialKeys.Password);
                }
            }

            var authorization = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(userName /*UserName*/ + ":" + password/*Password*/));

            if (string.IsNullOrEmpty(repository.HttpGetUriTemplate))
            {
                throw new FileDownloadException()
                {
                    FileId = file.FileId,
                    RepositoryId = repository.RepositoryId,
                    FileDownloadExceptionType = FileDownloadExceptionType.DownloadUrlNotFound.ToString()
                };
            }

            var downloadURL = string.Join(string.Empty, repository.HttpGetUriTemplate, HttpUtility.UrlEncode(file.Identifier));
            IRepositoryAdapter repAdapter = this.RepositoryAdapterFactory.GetRepositoryAdapter(repository.BaseRepository.Name);
            DataFile dataFile = repAdapter.DownloadFile(downloadURL, authorization, file.Name);
            return dataFile;
        }
        /// <summary>
        /// Method to set Request Details
        /// </summary>
        /// <param name="requestParam">Request parameters</param>
        /// <param name="repositoryCredentials">Repository Credentials</param>
        public void SetRequestDetails(RequestParams requestParam, RepositoryCredentials repositoryCredentials = null)
        {
            Utility.Check.IsNotNull(requestParam, "RequestParams");

            var jwtToken = HttpContext.Current.Request.Cookies[Constants.APPJWTCOOKIETOKEN].Value;
            WebRequest request = System.Net.WebRequest.Create(requestParam.RequestURL);

            webRequest = request as HttpWebRequest;

            webRequest.Headers.Add(HttpRequestHeader.Authorization, jwtToken);

            if (repositoryCredentials != null)
            {
                string serilizedRepositoryCredentials = new JavaScriptSerializer().Serialize(repositoryCredentials);
                webRequest.Headers.Add(Utility.Constants.RepositoryCredentialHeaderName, serilizedRepositoryCredentials);
            }

            webRequest.Method = requestParam.RequestMode.ToString();
            if (!string.IsNullOrEmpty(requestParam.ContentType))
            {
                webRequest.ContentType = requestParam.ContentType;
            }
            if (requestParam.TimeOut > 0)
            {
                webRequest.Timeout = requestParam.TimeOut;
            }

            webRequest.AllowAutoRedirect = requestParam.AllowAutoRedirect;

            // TODO
            if (requestParam.Values != null && requestParam.Values.Count>0)
            {
                UTF8Encoding encoding = new UTF8Encoding();
                data = encoding.GetBytes(requestParam.Values[0]);
                request.ContentLength = data.Length;
                Stream postStream = webRequest.GetRequestStream();
                postStream.Write(data, 0, data.Length);
                postStream.Close();
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Downloads the File from Repository
        /// </summary>
        /// <param name="file">File object.</param>
        /// <param name="repository">Repository instance.</param>
        /// <param name="user">User instance.</param>
        /// <param name="credentials">credentials required by the repository.</param>
        /// <returns>DataFile containing the file data.</returns>
        public override DataFile DownLoadFileFromRepository(File file, Repository repository, User user, RepositoryCredentials credentials)
        {
            // construct the AuthToken object
            AuthToken authToken = new AuthToken()
            {
                UserId = user.UserId,
                RespositoryId = repository.RepositoryId
            };

            if (credentials != null)
            {
                authToken.AccessToken = credentials[WindowsLiveAuthenticationCredentialKeys.AccessToken];
                authToken.RefreshToken = credentials[WindowsLiveAuthenticationCredentialKeys.RefreshToken];
                
                if (credentials[WindowsLiveAuthenticationCredentialKeys.TokenExpiresOn] != null)
                {
                    DateTime tokenExpiryDate;
                    if (DateTime.TryParse(credentials[WindowsLiveAuthenticationCredentialKeys.TokenExpiresOn], out tokenExpiryDate))
                    {
                        authToken.TokenExpiresOn = tokenExpiryDate;
                    };
                }
            }
            
            this.RepositoryAdapter = this.RepositoryAdapterFactory.GetRepositoryAdapter(repository.BaseRepository.Name);

            // Retreive the AuthToken from database or save the token to database if access token is present in the credentials.
            authToken = this.GetOrUpdateAuthTokens(repository, authToken);
           
            DataFile dataFile = this.RepositoryAdapter.DownloadFile(file.Identifier, authToken.AccessToken, file.Name);
            return dataFile;           
        }
Exemplo n.º 5
0
        /// <summary>
        /// Retreives the Repository Credentials from Header
        /// </summary>
        /// <returns>string header value</returns>
        private RepositoryCredentials GetRepsitoryCredentials()
        {

            IEnumerable<string> header = null;
            if (!this.Request.Headers.TryGetValues(Constants.RepositoryCredentialHeaderName, out header))
            {
                return null;
            }

            var credentials = header.FirstOrDefault();
            RepositoryCredentials repositoryCredentials = new RepositoryCredentials();
            //Dictionary<string, string> attributes = JsonConvert.DeserializeObject<Dictionary<string, string>>(credentials);
            repositoryCredentials.Attributes = new JavaScriptSerializer().Deserialize<Dictionary<string, string>>(credentials);
            //  attributes = new JavaScriptSerializer().Deserialize<Dictionary<string, string>>(credentials);
            //repositoryCredentials = new RepositoryCredentials();

            //JArray myObjects = JsonConvert.DeserializeObject<JArray>(credentials);

            //foreach (string v in myObjects)
            //{
            //    JToken  token = JObject.Parse(v);
            //    repositoryCredentials[token.SelectToken("key").Value<string>()] = token.SelectToken("value").Value<string>();
            //}

            return repositoryCredentials;
        }
Exemplo n.º 6
0
        public void Allow_Download()
        {
            BaseRepository baseRepository = new BaseRepository()
            {
                BaseRepositoryId = 2,
                Name = "SkyDrive"
            };

            this.repository = new Repository() { BaseRepositoryId = 2, Name = "test", IsImpersonating = false, BaseRepository = baseRepository };
            RepositoryCredentials repositoryCredentials = new RepositoryCredentials();
            File file = new File()
            {
                FileId = 1
            };

            User user = new User()
            {
                UserId = 1
            };
          
            var fileProvider = this.GetSkyDriveFileService();
            this.skyDriveAdapter = new Microsoft.Research.DataOnboarding.RepositoryAdapters.Interfaces.Fakes.StubIRepositoryAdapter()
            {
                PostFilePublishFileModel = (publishFileModel) =>
                    {
                        OperationStatus status = OperationStatus.CreateFailureStatus("error");
                        return status;
                    },
                DownloadFileStringStringString = (identifier, accessToken, fileName) =>
                    {
                        return new DataFile();
                    }
            };

            DataFile result;
            using (ShimsContext.Create())
            {
                ShimSkyDriveFileService.AllInstances.GetOrUpdateAuthTokensRepositoryAuthToken = (skyDriveFileService, repository, authToken) => new AuthToken();
                result = fileProvider.DownLoadFileFromRepository(file, this.repository, user, repositoryCredentials);
            }

            Assert.IsNotNull(result);
        }