// POST api/PublishConfig
        public HttpResponseMessage PostPublishAccount(PublishAccount publishAccount)
        {
            if (ModelState.IsValid)
            {
                publishAccount.Account = db.Accounts.Find(CurrentUser.Account.Id);
                if (publishAccount.Account == null)
                {
                    FotoShoutUtils.Log.LogManager.Error(_logger, Errors.ERROR_NOACCOUNT);
                    return(Request.CreateErrorResponse(HttpStatusCode.NotFound, Errors.ERROR_NOACCOUNT));
                }
                db.PublishAccounts.Add(publishAccount);
                try {
                    db.SaveChanges();
                }
                catch (Exception ex) {
                    FotoShoutUtils.Log.LogManager.Error(_logger, ex.ToString());
                    throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message));
                }

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, publishAccount);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", null));
                return(response);
            }
            else
            {
                FotoShoutUtils.Log.LogManager.Error(_logger, ModelState.Values.SelectMany(e => e.Errors));
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
            }
        }
Esempio n. 2
0
        public ActionResult PublishConfiguration(PublishAccount model)
        {
            try {
                if (ModelState.IsValid)
                {
                    GeneratePublishService(model);
                    PublishAccount temp = fsWebService.PublishConfiguration(model);
                    if (temp == null && model.Id == 0)
                    {
                        this.AddModelError(Errors.ERROR_PUBLISHCONFIG, _logger);
                    }
                    else
                    {
                        if (temp != null)
                        {
                            model = temp;
                        }
                        this.AddMessage(Constants.INFO_PUBLISHCONFIG_SUCCESS);
                    }
                }
                else
                {
                    this.AddModelError(ModelState.Values.SelectMany(v => v.Errors).ToList(), _logger);
                }
            }
            catch (Exception ex) {
                this.AddModelError(ex.Message, _logger, ex);
            }

            return(View(model));
        }
        // PUT api/PublishConfig/5
        public HttpResponseMessage PutPublishAccount(int id, PublishAccount publishAccount)
        {
            if (!ModelState.IsValid)
            {
                FotoShoutUtils.Log.LogManager.Error(_logger, ModelState.Values.SelectMany(v => v.Errors));
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
            }

            if (id != publishAccount.Id)
            {
                FotoShoutUtils.Log.LogManager.Error(_logger, string.Format(Errors.ERROR_PUBLISHACCOUNT_UPDATE_IDSNOTIDENTICAL, id, publishAccount.Id));
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, string.Format(Errors.ERROR_PUBLISHACCOUNT_UPDATE_IDSNOTIDENTICAL, id, publishAccount.Id)));
            }

            db.Entry(publishAccount).State = EntityState.Modified;

            try {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex) {
                FotoShoutUtils.Log.LogManager.Error(_logger, ex.ToString());
                return(Request.CreateErrorResponse(HttpStatusCode.NotFound, ex));
            }
            catch (Exception ex) {
                FotoShoutUtils.Log.LogManager.Error(_logger, ex.ToString());
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message));
            }

            return(Request.CreateResponse(HttpStatusCode.OK));
        }
Esempio n. 4
0
        public PublishApiWebService GeneratePublishService(PublishAccount publishAccount)
        {
            string url = publishAccount.Url.EndsWith("/") ? publishAccount.Url.Substring(0, publishAccount.Url.Length - 1) : publishAccount.Url;

            if (!url.StartsWith("http://") && url.StartsWith("https://"))
            {
                throw new WebServiceException("The provided url must be started with 'http://' or 'https://'");
            }

            int lastIdx = url.LastIndexOf("/");

            if (lastIdx != -1)
            {
                string baseAddress = url.Substring(0, lastIdx + 1);
                if (baseAddress.Equals("http://") || baseAddress.Equals("https://"))
                {
                    throw new WebServiceException(string.Format("The \"{0}\" url is incorrect.", publishAccount.Url));
                }

                string prefix             = url.Substring(lastIdx + 1);
                PublishApiWebService temp = new PublishApiWebService(baseAddress, prefix, FotoShoutUtils.Constants.MEDIATYPE_APPLICATION_JSON);
                string ret = temp.Authenticate(publishAccount.ApiKey.ToString(), new LoginModel {
                    UserName = publishAccount.Email,
                    Password = publishAccount.Password
                });

                return(temp);
            }
            else
            {
                throw new WebServiceException(string.Format("The \"{0}\" url is incorrect.", publishAccount.Url));
            }
        }
Esempio n. 5
0
        private bool Authenticate(FotoShoutData.Models.Credentials credential)
        {
            try {
                FotoShoutUtils.Log.LogManager.Info(_logger, string.Format("Authenticating for the \"{0}\" user...", credential.Email));
                User = _fsWebService.Login(credential);
                if (User == null)
                {
                    FotoShoutUtils.Log.LogManager.Error(_logger, "Unexpected error - There is no user on the central fotoShout associated with the fotoShout Authorization key.\r\n");
                    return(false);
                }
                FotoShoutUtils.Log.LogManager.Info(_logger, "Succeeded logged");
                FotoShoutUtils.Log.LogManager.Info(_logger, "Getting publish configuration information...");
                PublishAccount publishAccount = _fsWebService.GetPublishConfiguration();
                if (publishAccount != null && !string.IsNullOrEmpty(publishAccount.Url))
                {
                    string url     = publishAccount.Url.EndsWith("/") ? publishAccount.Url.Substring(0, publishAccount.Url.Length - 1) : publishAccount.Url;
                    int    lastIdx = url.LastIndexOf("/");
                    if (lastIdx != -1)
                    {
                        string baseAddress = url.Substring(0, lastIdx + 1);
                        string prefix      = url.Substring(lastIdx + 1);
                        _c9WebService = new PublishApiWebService(baseAddress, prefix, FotoShoutUtils.Constants.MEDIATYPE_APPLICATION_JSON);
                        string ret = _c9WebService.Authenticate(publishAccount.ApiKey.ToString(), new LoginModel {
                            UserName = publishAccount.Email,
                            Password = publishAccount.Password
                        });

                        if (string.IsNullOrEmpty(ret))
                        {
                            FotoShoutUtils.Log.LogManager.Error(_logger, "Unable to authorize to the publishing API.\r\n");
                            return(false);
                        }
                    }
                    else
                    {
                        FotoShoutUtils.Log.LogManager.Error(_logger, string.Format("Unable to authorize to the publishing API - The \"{0}\" url is incorrect.\r\n", publishAccount.Url));
                        return(false);
                    }
                }
                else
                {
                    FotoShoutUtils.Log.LogManager.Error(_logger, string.Format("There is no publish configuration for the \"{0}\" user.\r\n", publishAccount.Url));
                    return(false);
                }

                FotoShoutUtils.Log.LogManager.Info(_logger, "Successfully authenticated.\r\n");

                return(true);
            }
            catch (HttpClientServiceException ex) {
                FotoShoutUtils.Log.LogManager.Error(_logger, string.Format("Failed to authenticate : {0} (Status Code: {1})\r\n", ex.Message, (int)ex.StatusCode));
            }
            catch (Exception ex) {
                FotoShoutUtils.Log.LogManager.Error(_logger, string.Format("Failed to authenticate: {0}\r\n", ex.ToString()));
            }

            return(false);
        }
Esempio n. 6
0
        public PublishApiWebService GeneratePublishService()
        {
            try {
                PublishAccount publishAccount = fsWebService.GetPublishConfiguration();
                if (publishAccount != null && !string.IsNullOrEmpty(publishAccount.Url))
                {
                    return(GeneratePublishService(publishAccount));
                }
            }
            catch (Exception ex) {
                if (!ex.Message.Equals("Not Found", StringComparison.InvariantCultureIgnoreCase))
                {
                    FotoShoutUtils.Log.LogManager.Error(_logger, ex.ToString());
                }
            }

            return(null);
        }
        // GET api/PublishConfig
        public PublishAccount GetPublishAccount()
        {
            PublishAccount publishAccount = null;

            try {
                publishAccount = db.PublishAccounts.Where(p => p.Account.Id == CurrentUser.Account.Id).SingleOrDefault();
            }
            catch (Exception ex) {
                FotoShoutUtils.Log.LogManager.Error(_logger, ex.ToString());
                this.GenerateException(HttpStatusCode.InternalServerError, ex.Message);
            }

            if (publishAccount == null)
            {
                this.GenerateException(HttpStatusCode.NotFound, "Publish Account has not been configured yet.");
            }

            return(publishAccount);
        }
Esempio n. 8
0
 public ActionResult PublishConfiguration()
 {
     try {
         PublishAccount model = fsWebService.GetPublishConfiguration();
         return(View(model));
     }
     catch (HttpClientServiceException ex) {
         if (ex.StatusCode != System.Net.HttpStatusCode.NotFound)
         {
             this.AddModelError(ex.Message, _logger, ex);
         }
     }
     catch (Exception ex) {
         FotoShoutUtils.Log.LogManager.Debug(_logger, ex.ToString());
     }
     return(View(new PublishAccount {
         ApiKey = Guid.Parse(AppConfigs.C9ApiKey)
     }));
 }
Esempio n. 9
0
 public PublishAccount PublishConfiguration(PublishAccount model)
 {
     return((model.Id != 0) ?
            Modify <PublishAccount>("PublishConfig/" + model.Id, model, Constants.INFO_PUBLISHCONFIG, "PUT") :
            Modify <PublishAccount>("PublishConfig", model, Constants.INFO_PUBLISHCONFIG));
 }