using System; using ServiceStack.Common.Web; using ServiceStack.ServiceHost; namespace ServiceStack.ServiceInterface { /// /// Base class for services that support HTTP verbs. /// /// The request class that the descendent class /// is responsible for processing. [Obsolete("Use the New API (ServiceStack.ServiceInterface.Service) for future services. See: https://github.com/ServiceStack/ServiceStack/wiki/New-Api")] public abstract class RestServiceBase : ServiceBase, IRestGetService, IRestPutService, IRestPostService, IRestDeleteService, IRestPatchService { /// /// What gets run when the request is sent to AsyncOneWay endpoint. /// For a REST service the OnPost() method is called. /// protected override object Run(TRequest request) { return OnPost(request); } /// /// The method may overriden by the descendent class to provide support for the /// GET verb on objects. /// /// The request object containing parameters for the GET /// operation. /// /// A response object that the client expects, if a response /// object is not applicable, or an object, to indicate an /// error condition to the client. The object allows the /// implementation to control the exact HTTP status code returned to the client. /// Any return value other than causes the HTTP status code /// of 200 to be returned to the client. /// public virtual object OnGet(TRequest request) { throw new NotImplementedException("This base method should be overridden but not called"); } public object Get(TRequest request) { try { BeforeEachRequest(request); return AfterEachRequest(request, OnGet(request)); } catch (Exception ex) { var result = HandleException(request, ex); if (result == null) throw; return result; } } /// /// The method may overriden by the descendent class to provide support for the /// PUT verb on objects. /// /// The request object containing parameters for the PUT /// operation. /// /// A response object that the client expects, if a response /// object is not applicable, or an object, to indicate an /// error condition to the client. The object allows the /// implementation to control the exact HTTP status code returned to the client. /// Any return value other than causes the HTTP status code /// of 200 to be returned to the client. /// public virtual object OnPut(TRequest request) { throw new NotImplementedException("This base method should be overridden but not called"); } public object Put(TRequest request) { try { BeforeEachRequest(request); return AfterEachRequest(request, OnPut(request)); } catch (Exception ex) { var result = HandleException(request, ex); if (result == null) throw; return result; } } /// /// The method may overriden by the descendent class to provide support for the /// POST verb on objects. /// /// The request object containing parameters for the POST /// operation. /// /// A response object that the client expects, if a response /// object is not applicable, or an object, to indicate an /// error condition to the client. The object allows the /// implementation to control the exact HTTP status code returned to the client. /// Any return value other than causes the HTTP status code /// of 200 to be returned to the client. /// public virtual object OnPost(TRequest request) { throw new NotImplementedException("This base method should be overridden but not called"); } public object Post(TRequest request) { try { BeforeEachRequest(request); return AfterEachRequest(request, OnPost(request)); } catch (Exception ex) { var result = HandleException(request, ex); if (result == null) throw; return result; } } /// /// The method may overriden by the descendent class to provide support for the /// DELETE verb on objects. /// /// The request object containing parameters for the DELETE /// operation. /// /// A response object that the client expects, if a response /// object is not applicable, or an object, to indicate an /// error condition to the client. The object allows the /// implementation to control the exact HTTP status code returned to the client. /// Any return value other than causes the HTTP status code /// of 200 to be returned to the client. /// public virtual object OnDelete(TRequest request) { throw new NotImplementedException("This base method should be overridden but not called"); } public object Delete(TRequest request) { try { BeforeEachRequest(request); return AfterEachRequest(request, OnDelete(request)); } catch (Exception ex) { var result = HandleException(request, ex); if (result == null) throw; return result; } } /// /// The method may overriden by the descendent class to provide support for the /// PATCH verb on objects. /// /// The request object containing parameters for the PATCH /// operation. /// /// A response object that the client expects, if a response /// object is not applicable, or an object, to indicate an /// error condition to the client. The object allows the /// implementation to control the exact HTTP status code returned to the client. /// Any return value other than causes the HTTP status code /// of 200 to be returned to the client. /// public virtual object OnPatch(TRequest request) { throw new NotImplementedException("This base method should be overridden but not called"); } public object Patch(TRequest request) { try { BeforeEachRequest(request); return AfterEachRequest(request, OnPatch(request)); } catch (Exception ex) { var result = HandleException(request, ex); if (result == null) throw; return result; } } } }