Beispiel #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="entityNames"></param>
        /// <param name="teamIds"></param>
        /// <returns></returns>
        public Message GetAllEntitiesByNameByTeam(WebOperationContext ctx, string entityNames, string teamIds)
        {
            List <string>   names;
            List <int>      teams;
            EntityContainer entityContainer = new EntityContainer();

            List <KPListItem> listItems = new List <KPListItem>();

            if (string.IsNullOrEmpty(entityNames))
            {
                ctx.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.BadRequest;
                return(ctx.CreateJsonResponse <string>("Error: No entity names specified"));
            }
            if (string.IsNullOrEmpty(teamIds))
            {
                ctx.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.BadRequest;
                return(ctx.CreateJsonResponse <string>("Error: No teams specified"));
            }

            // validate data
            if (entityNames.Contains(GlobalConstants.MULTIVALUE_DELIMITER))
            {
                names = entityNames.Split(GlobalConstants.MULTIVALUE_DELIMITER).ToList();
            }
            else
            {
                names = new List <string>()
                {
                    entityNames
                }
            };
            if (teamIds.Contains(GlobalConstants.MULTIVALUE_DELIMITER))
            {
                teams = teamIds.Split(GlobalConstants.MULTIVALUE_DELIMITER).Select(Int32.Parse).ToList();
            }
            else
            {
                teams = new List <int>()
                {
                    Int32.Parse(teamIds)
                }
            };

            try
            {
                foreach (string name in names)
                {
                    // get entities by team & aggregate
                    this.GetUserEntities(entityContainer, name, teams);
                }
            }
            catch (Exception ex)
            {
                ctx.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.BadRequest;
                return(ctx.CreateJsonResponse <string>(string.Format("{0}: Stack Trace: {1}", ex.Message, ex.StackTrace)));
            }

            return(HttpUtilities.GenerateResponse <EntityContainer>(ctx, entityContainer, this.timer));
        }
Beispiel #2
0
        /// <summary>
        /// Get list of worksheets in the specified file
        /// </summary>
        /// <param name="fileName"></param>
        public Message GetSheets(string listName, string fileName)
        {
            Message            responseMsg        = null;
            SPDataAccess       spDataAccess       = new SPDataAccess();
            WorksheetUtilities worksheetUtilities = new WorksheetUtilities(new SPDataAccess());
            List <string>      worksheets         = worksheetUtilities.GetWorksheetNames(listName, fileName);

            responseMsg = ctx.CreateJsonResponse <List <string> >(worksheets);
            return(responseMsg);
        }
Beispiel #3
0
        public Message GetUser(String userID)
        {
            ResponseJson <GetUserResponse> json = new ResponseJson <GetUserResponse>();
            WebOperationContext            ctx  = WebOperationContext.Current;

            ctx.OutgoingResponse.StatusCode = HttpStatusCode.OK;

            try
            {
                var user = this.AccountService.GetUser(Convert.ToInt32(userID));
                if (user == null)
                {
                    throw new BusinessException("用户不存在!");//The user is not exist!
                }
                json.Data = new GetUserResponse()
                {
                    LoginName = user.LoginName,
                    UserID    = user.ID
                };
            }
            catch (Exception ex)
            {
                json.Status  = false;
                json.Message = ex.Message;
            }

            return(ctx.CreateJsonResponse <ResponseJson <GetUserResponse> >(json));
        }
Beispiel #4
0
        /// <summary>
        /// Overloaded - generates an exception response object that serializes out the exception and inner exception message
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="ex"></param>
        /// <param name="httpAction"></param>
        /// <param name="httpStatusCode"></param>
        /// <returns></returns>
        public static Message GenerateExceptionResponse(WebOperationContext ctx, Exception ex, string httpAction, HttpStatusCode httpStatusCode)
        {
            ErrorResponse response = new ErrorResponse(ex, httpAction);

            ctx.OutgoingResponse.StatusCode = httpStatusCode;
            return(ctx.CreateJsonResponse <ErrorResponse>(response));
        }
Beispiel #5
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="ctx"></param>
        /// <param name="items"></param>
        /// <param name="timer"></param>
        /// <returns></returns>
        internal static Message GenerateResponse <T>(WebOperationContext ctx, List <T> items, KPTimer timer)
        {
            Response <T> response;

            response = new Response <T>(items, timer);
            ctx.OutgoingResponse.StatusCode = HttpStatusCode.OK;
            return(ctx.CreateJsonResponse <Response <T> >(response));
        }
        public override Message CreateResponse(object response)
        {
            var stream = response as Stream;

            if (stream == null)
            {
                var serializer = new DataContractJsonSerializer(response.GetType());
                return(_webOperationContext.CreateJsonResponse(response, serializer));
            }
            return(_webOperationContext.CreateStreamResponse(stream, GetResponseContentType()));
        }
        private Message CreateValidResponse(WebOperationContext context, object value)
        {
            switch (context.IncomingRequest.Accept)
            {
            case "application/xml":
                return(context.CreateXmlResponse(value.ToString()));

            case "application/json":
                return(context.CreateJsonResponse(value.ToString()));

            default:
                return(context.CreateTextResponse(value.ToString()));
            }
        }
Beispiel #8
0
        /// <summary>
        /// Overloaded: generates a successful response with the serialized <paramref name="Response"/>Response object
        /// </summary>
        /// <typeparam name="T">Object Type</typeparam>
        /// <param name="ctx">The current WebOperationContext</param>
        /// <param name="items">Collection of objects</param>
        /// <param name="diagnostics">Currently is time in milliseconds</param>
        /// <returns>Serilaized Message object</returns>
        internal static Message GenerateResponse <T>(WebOperationContext ctx, List <T> items, string diagnostics)
        {
            Response <T> response;

            if (items.Count == 1)
            {
                response = new Response <T>(items[0], diagnostics);
            }
            else
            {
                response = new Response <T>(items, diagnostics);
            }

            ctx.OutgoingResponse.StatusCode = HttpStatusCode.OK;
            return(ctx.CreateJsonResponse <Response <T> >(response));
        }
Beispiel #9
0
        public Message Register(RegisterRequest request)
        {
            ResponseJson <LoginResponse> json = new ResponseJson <LoginResponse>();
            WebOperationContext          ctx  = WebOperationContext.Current;

            try
            {
                throw new BusinessException("账户不存在!");
            }
            catch (Exception ex)
            {
                json.Status  = false;
                json.Message = ex.Message;
            }

            return(ctx.CreateJsonResponse <ResponseJson <LoginResponse> >(json));
        }
Beispiel #10
0
        public Message GetUserRanking(String userID, String tabFlag)
        {
            ResponseJson <GetUserRankingResponse> json = new ResponseJson <GetUserRankingResponse>();
            WebOperationContext ctx = WebOperationContext.Current;

            ctx.OutgoingResponse.StatusCode = HttpStatusCode.OK;

            try
            {
            }
            catch (Exception ex)
            {
                json.Status  = false;
                json.Message = ex.Message;
            }

            return(ctx.CreateJsonResponse <ResponseJson <GetUserRankingResponse> >(json));
        }
        public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
        {
            if (version != MessageVersion.None || error == null)
            {
                return;
            }

            // If the exception is not derived from FaultException and the fault message is already present
            //   then only another error handler could have provided the fault so we should not replace it
            FaultException errorAsFaultException = error as FaultException;

            if (errorAsFaultException == null && fault != null)
            {
                return;
            }

            try
            {
                if (error is IWebFaultException)
                {
                    IWebFaultException  webFaultException = (IWebFaultException)error;
                    WebOperationContext context           = WebOperationContext.Current;
                    context.OutgoingResponse.StatusCode = webFaultException.StatusCode;
                    string operationName;
                    if (OperationContext.Current.IncomingMessageProperties.TryGetValue <string>(WebHttpDispatchOperationSelector.HttpOperationNamePropertyName, out operationName))
                    {
                        OperationDescription description = this.contractDescription.Operations.Find(operationName);
                        bool isXmlSerializerFaultFormat  = WebHttpBehavior.IsXmlSerializerFaultFormat(description);
                        if (isXmlSerializerFaultFormat && WebOperationContext.Current.OutgoingResponse.Format == WebMessageFormat.Json)
                        {
                            throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR2.GetString(SR2.JsonFormatRequiresDataContract, description.Name, description.DeclaringContract.Name, description.DeclaringContract.Namespace)));
                        }
                        WebMessageFormat?nullableFormat = !isXmlSerializerFaultFormat ? context.OutgoingResponse.Format : WebMessageFormat.Xml;
                        WebMessageFormat format         = nullableFormat.HasValue ? nullableFormat.Value : this.webHttpBehavior.GetResponseFormat(description);
                        if (webFaultException.DetailObject != null)
                        {
                            switch (format)
                            {
                            case WebMessageFormat.Json:
                                fault = context.CreateJsonResponse(webFaultException.DetailObject, new DataContractJsonSerializer(webFaultException.DetailType, webFaultException.KnownTypes));
                                break;

                            case WebMessageFormat.Xml:
                                if (isXmlSerializerFaultFormat)
                                {
                                    fault = context.CreateXmlResponse(webFaultException.DetailObject, new XmlSerializer(webFaultException.DetailType, webFaultException.KnownTypes));
                                }
                                else
                                {
                                    fault = context.CreateXmlResponse(webFaultException.DetailObject, new DataContractSerializer(webFaultException.DetailType, webFaultException.KnownTypes));
                                }
                                break;
                            }
                        }
                        else
                        {
                            HttpResponseMessageProperty property;
                            if (OperationContext.Current.OutgoingMessageProperties.TryGetValue <HttpResponseMessageProperty>(HttpResponseMessageProperty.Name, out property) &&
                                property != null)
                            {
                                property.SuppressEntityBody = true;
                            }
                            if (format == WebMessageFormat.Json)
                            {
                                fault.Properties.Add(WebBodyFormatMessageProperty.Name, WebBodyFormatMessageProperty.JsonProperty);
                            }
                        }
                    }
                    else
                    {
                        throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR2.OperationNameNotFound));
                    }
                }
                else
                {
                    fault = CreateHtmlResponse(error);
                }
            }
            catch (Exception ex)
            {
                if (Fx.IsFatal(ex))
                {
                    throw;
                }
                if (System.ServiceModel.DiagnosticUtility.ShouldTraceWarning)
                {
                    System.ServiceModel.DiagnosticUtility.TraceHandledException(new InvalidOperationException(SR2.GetString(SR2.HelpPageFailedToCreateErrorMessage)), TraceEventType.Warning);
                }

                WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.BadRequest;
                fault = CreateHtmlResponse(ex);
            }
        }
        public override Message CreateResponse(object response)
        {
            var serializer = new DataContractJsonSerializer(response.GetType());

            return(_webOperationContext.CreateJsonResponse(response, serializer));
        }