Files
BeWoPlaner/Service/ServiceImplementations/DownloadBeWoServiceImp.cs

239 lines
7.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.ServiceModel;
using System.Xml;
using BeWo.Data;
using BeWo.Service.ServiceContracts;
using BeWo.Service.ServiceProxy;
using BeWo.Service.ServiceUtils.Paths;
using BeWo.Service.ServiceUtils.Update;
using BS.Shared;
using BS.SharedLauncher.DataContract;
using BS.SharedLauncher.Exceptions;
using BS.SharedLauncher.Information;
namespace BeWo.Service.ServiceImplementations
{
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Single)]
public class DownloadBeWoServiceImp : IDownloadBeWoService
{
private string GETSERVER_URL = "https://support.bewoplaner.de/api/getserver.php?k={0}&t=0";
public string Tenant => MultitenancyOperationContextExt.Current.Tenant;
public UpdatePlanResponse GetUpdatePlan(UpdatePlanRequest request)
{
GenericTextLogger.Init(ServerFilePaths.GetLogFolderPath(Tenant));
GenericTextLogger.PostImportantInfo("GetUpdatePlan Anfrage");
var response = new UpdatePlanResponse(false);
try
{
GenericTextLogger.Post("Validiere Plan Request.");
ValidatePlanRequest(request);
GenericTextLogger.Post("Plan Request erfolreich validiert. Verifiziere Login.");
VerifyLogin(request.Login);
GenericTextLogger.Post("Login verifiziert. Baue Antwort.");
UpdatePlanResponseBuilder.BuildResponse(request, Tenant, response);
response.Successful = true;
response.Error = null;
GenericTextLogger.Post("Antwort erfolgreich gebaut.");
GenericTextLogger.PostLine();
}
catch (HideClientUpdateException e)
{
// Serverseitig loggen
GenericTextLogger.PostException(e);
response.Error = "Serverseitiger Fehler";
}
catch (UpdateException e)
{
// Muss nachher nicht
GenericTextLogger.PostException(e);
response.Error = e.ToString();
}
catch (Exception e)
{
// Unerwartet - nicht gewollt
GenericTextLogger.PostException(e);
response.Error = e.ToString();
}
return response;
}
private void ValidatePlanRequest(UpdatePlanRequest request)
{
if (!TryValidatePlanRequest(request))
throw new UpdateException("Kann Request nicht bestätigen");
}
private bool TryValidatePlanRequest(UpdatePlanRequest request)
{
if (request.PlanerRootFolderExists)
{
// ...
}
else
{
// Root Folder ex. nicht
if (request.DownloadFolderExists)
return false;
if (!request.DownloadFolderIsEmpty)
return false;
if (request.ApplyFolderExists)
return false;
if (!request.ApplyFolderIsEmpty)
return false;
}
return true;
}
private void VerifyLogin(LoginDC login) => VerifyLogin(login.Username, login.Password, Tenant, login.Pin, login.ClientID);
private void VerifyLogin(string username, string password, string tenant, string pin, string clientid)
{
string server_address = string.Empty;
try
{
SessionInformation.Login.Username = username;
SessionInformation.Login.Password = password;
SessionInformation.Login.Tenant = tenant;
server_address = GetServerFromTenant();
#if DEBUG
server_address = "http://localhost:3777/Host";
#endif
ServiceFacade.UpdateServerAddresses(server_address);
var result = ServiceFacade.DoLauncherServiceSync(x => x.IsUserValid(username, password, pin, clientid, true));
if (result != BS.SharedLauncher.Enums.UserValidationResult.UserValid)
throw new HideClientUpdateException("Kann Anmeldung nicht bestätigen!");
}
catch (Exception e)
{
throw new HideClientUpdateException("Kann Anmeldung nicht bestätigen! ", e);
}
}
public UpdateFileResponse GetUpdateFile(UpdateFileRequest request)
{
GenericTextLogger.Init(ServerFilePaths.GetLogFolderPath(Tenant));
GenericTextLogger.PostImportantInfo("GetUpdateFile Anfrage");
var response = new UpdateFileResponse(false);
try
{
GenericTextLogger.Post("Validiere File Request.");
// ValidatePlanRequest(request);
GenericTextLogger.Post("Plan Request erfolreich validiert.");
GenericTextLogger.PostArrow($"File: {request.UpdateFile.RelativePath}");
GenericTextLogger.Post("Verifiziere Login.");
VerifyLogin(request.Login);
GenericTextLogger.Post("Login verifiziert. Baue Antwort.");
UpdateFileResponseBuilder.BuildResponse(request, Tenant, response);
response.Successful = true;
response.Error = null;
GenericTextLogger.Post("Antwort erfolgreich gebaut.");
GenericTextLogger.PostLine();
}
catch (HideClientUpdateException e)
{
// Serverseitig loggen
GenericTextLogger.PostException(e);
response.Error = "Serverseitiger Fehler";
}
catch (UpdateException e)
{
// Muss nachher nicht
GenericTextLogger.PostException(e);
response.Error = e.ToString();
}
catch (Exception e)
{
// Unerwartet - nicht gewollt
GenericTextLogger.PostException(e);
response.Error = e.ToString();
}
return response;
}
private bool isUpdateFileRequestValid(UpdateFileRequest request)
{
// Check for Login
// Check for Correct Version
// Check for File Permissions
return true;
}
private string GetServerFromTenant(string tenant = null)
{
if (tenant is null)
tenant = SessionInformation.Login.Tenant;
if (tenant == "demo")
return null;
try
{
String url = String.Format(GETSERVER_URL, tenant);
using (WebClient client = new WebClient())
{
//MessageBox.Show(hostAddress);
byte[] response = client.UploadValues(url, "POST", new NameValueCollection());
String server = System.Text.Encoding.ASCII.GetString(response);
return $"https://{server}/service";
}
}
catch (Exception ex)
{
throw new HideClientUpdateException("Login konnte nicht verifiziert werden.", ex);
}
}
}
}