main / Source / WebService / Web.Admin / RepositoryService.svc.cs

using System;
using System.Data.Services;
using System.Data.Services.Common;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Web;
using System.Xml;
using DataUp.Web.Admin.DataLayer;
using DataUp.Web.Common;
using DataUp.Web.Service.Model;
using System.Diagnostics;

namespace DataUp.Web.Admin
{
    [ServiceBehavior(IncludeExceptionDetailInFaults = false)]
    public class RepositoryService : DataService<RepositoryDataContext>
    {
        public static void InitializeService(DataServiceConfiguration config)
        {
            config.SetEntitySetAccessRule("Repositories", EntitySetRights.AllRead);
            config.SetServiceOperationAccessRule("GetIdentifier", ServiceOperationRights.All);
            config.SetServiceOperationAccessRule("UploadFile", ServiceOperationRights.All);
            config.SetServiceOperationAccessRule("GetUserAgreement", ServiceOperationRights.All);
            config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
        }

        [WebInvoke(Method = "POST")]
        public string GetIdentifier()
        {
            Trace.TraceInformation("Get identifier called");

            string queryDataString = HttpContext.Current.Request["QueryData"];
            string repositoryModelString = HttpContext.Current.Request["RepositoryModel"];

            if (!string.IsNullOrWhiteSpace(queryDataString) && !string.IsNullOrWhiteSpace(repositoryModelString))
            {
                DQueryData queryData = DeSerializeObject<DQueryData>(queryDataString.DecodeFrom64(), "queryData");
                DRepositoryBase repositoryModel = DeSerializeObject<DRepositoryBase>(repositoryModelString.DecodeFrom64(), "repositoryModel"); ;

                IRepositoryDetailsDB _repositoryDetailsDB = new RepositoryDetailsDB(new DCXLAdminEntities());
                var repository = _repositoryDetailsDB.GetRespositories().FirstOrDefault(r => string.Compare(r.RepositoryName, repositoryModel.RepositoryName, true) == 0);

                // Get Authorization for ONEShare repositoryType.
                if (repository != null)
                {
                    IRepository instance = RepositoryFactory.GetRepositoryInstance(repositoryModel.RepositoryName);
                    repositoryModel.RepositoryUrl = repository.GetIdentifierUrl;

                    if (repository.RepositoryType.PasswordRequired.HasValue && !repository.RepositoryType.PasswordRequired.Value)
                    {
                        var userPassword = String.IsNullOrEmpty(repository.Password) ? "" : StringExtensions.DecodeFrom64(repository.Password);
                        repositoryModel.Authorization = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(repository.UserId + ":" + userPassword));
                    }

                    if (instance != null)
                    {
                        Trace.TraceInformation("Calling GetIdentifier on Repository");
                        string returnValue = instance.GetIdentifier(queryData, repositoryModel);
                        Trace.TraceInformation("Returning value from service : " + returnValue);
                            return returnValue;
                    }
                }
            }

            return "false|Error occurred";
        }

        [WebInvoke(Method = "POST")]
        public string UploadFile()
        {
            string queryDataString = HttpContext.Current.Request["QueryData"];
            string repositoryModelString = HttpContext.Current.Request["RepositoryModel"];
            string fileString = HttpContext.Current.Request["File"];

            if (!string.IsNullOrWhiteSpace(queryDataString) && !string.IsNullOrWhiteSpace(repositoryModelString) && !string.IsNullOrWhiteSpace(fileString))
            {
                DQueryData queryData = DeSerializeObject<DQueryData>(queryDataString.DecodeFrom64(), "queryData");
                DRepositoryBase repositoryModel = DeSerializeObject<DRepositoryBase>(repositoryModelString.DecodeFrom64(), "repositoryModel");
                DFile file = DeSerializeObject<DFile>(fileString.DecodeFrom64(), "file");

                // Get Authorisation for ONEShare repositoryType.
                IRepositoryDetailsDB _repositoryDetailsDB = new RepositoryDetailsDB(new DCXLAdminEntities());
                var repository = _repositoryDetailsDB.GetRespositories().FirstOrDefault(r => string.Compare(r.RepositoryName, repositoryModel.RepositoryName, true) == 0);

                // Get Authorisation for ONEShare repositoryType.
                if (repository != null)
                {
                    IRepository instance = RepositoryFactory.GetRepositoryInstance(repositoryModel.RepositoryName);
                    repositoryModel.RepositoryUrl = repository.PostFileUrl;

                    if (repository.RepositoryType.PasswordRequired.HasValue && !repository.RepositoryType.PasswordRequired.Value)
                    {
                        var userPassword = String.IsNullOrEmpty(repository.Password) ? "" : StringExtensions.DecodeFrom64(repository.Password);
                        repositoryModel.Authorization = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(repository.UserId + ":" + userPassword));
                    }

                    if (instance != null)
                    {
                        return instance.PostFile(queryData, repositoryModel, file);
                    }
                }
            }

            return "false|Error occurred";
        }

        [WebInvoke(Method = "POST")]
        public string GetUserAgreement()
        {
            string repositoryName = HttpContext.Current.Request["RepositoryName"];
            if (!string.IsNullOrWhiteSpace(repositoryName))
            {
                IRepositoryDetailsDB _repositoryDetailsDB = new RepositoryDetailsDB(new DCXLAdminEntities());
                var repository = _repositoryDetailsDB.GetRespositories().FirstOrDefault(r => string.Compare(r.RepositoryName, repositoryName, true) == 0);
                if (repository != null)
                {
                    return repository.UserAgreement;
                }
            }

            return string.Empty;
        }

        private static T DeSerializeObject<T>(string xml, string rootName)
        {
            DataContractSerializer dataContractSerializer;
            if (string.IsNullOrEmpty(rootName))
                dataContractSerializer = new DataContractSerializer(typeof(T));
            else
                dataContractSerializer = new DataContractSerializer(typeof(T), rootName, "");

            object output = null;
            using (MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(xml)))
            {
                memoryStream.Position = 0L;

                XmlDictionaryReaderQuotas readerQuota = new XmlDictionaryReaderQuotas();
                readerQuota.MaxArrayLength = int.MaxValue;
                readerQuota.MaxStringContentLength = int.MaxValue;

                XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(memoryStream, Encoding.UTF8, readerQuota, null);
                output = dataContractSerializer.ReadObject(reader);
            }

            return (T)output;
        }
    }
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.