/// <summary> /// Returns <c>true</c> if the processor can handle this request. /// </summary> /// <param name="submit"></param> /// <returns></returns> public override Priority CanSubmit(ModelRequest submit) { if (submit.ResourceUri.Scheme == Uri.UriSchemeFile) return Priority.Default; else return Priority.Ignore; }
/// <summary> /// Gets the appropriate Web Request method type for the given <see cref="ModelRequest"/>. /// </summary> /// <param name="request"></param> /// <returns></returns> protected virtual IOMethod GetMethod(ModelRequest request) { Contract.Requires <ArgumentNullException>(request != null); if (request.Method == ModelMethod.Get) { return(IOMethod.Get); } if (request.Method == ModelMethod.Put) { return(IOMethod.Put); } if (request.Method == ModelMethod.Post || request.Method == ModelMethod.UrlEncodedPost || request.Method == ModelMethod.MultipartPost || request.Method == ModelMethod.FormDataPost) { return(IOMethod.Post); } if (request.Method == ModelMethod.Delete) { return(IOMethod.Delete); } return(IOMethod.None); }
/// <summary> /// Submits the request and returns the response. /// </summary> /// <param name="request"></param> /// <returns></returns> public override ModelResponse Submit(ModelRequest request) { // write request var ioRequest = WriteIORequest(request); if (ioRequest == null) { throw new InvalidOperationException(); } // submit request and retrieve response var ioResponse = ioService.Send(ioRequest); if (ioResponse == null) { throw new InvalidOperationException(); } // read response var response = ReadIOResponse(ioResponse, request); if (response == null) { throw new InvalidOperationException(); } return(response); }
/// <summary> /// Gets the <see cref="MediaType"/> to be used on outgoing data. /// </summary> /// <param name="request"></param> /// <returns></returns> protected override MediaRange GetMediaType(ModelRequest request) { if (request.Method == ModelMethod.Post || request.Method == ModelMethod.Put) { return("application/xml"); } if (request.Method == ModelMethod.Get || request.Method == ModelMethod.Delete || request.Method == ModelMethod.UrlEncodedPost) { return("application/x-www-form-urlencoded"); } if (request.Method == ModelMethod.MultipartPost) { return("multipart/related"); } if (request.Method == ModelMethod.FormDataPost) { return("multipart/form-data"); } return(null); }
/// <summary> /// Extracts an <see cref="ModelResponse"/> from the given <see cref="IOResponse"/> content. /// </summary> /// <param name="ioResponse"></param> /// <param name="request"></param> /// <returns></returns> ModelResponse ReadIOResponseFromContent(IOResponse ioResponse, ModelRequest request) { Contract.Requires <ArgumentNullException>(ioResponse != null); Contract.Requires <ArgumentNullException>(request != null); // deserialize if possible if (ioResponse.Content != null && ioResponse.ContentType != null) { // obtain serializer var deserializer = GetDeserializer(ioResponse.ContentType); if (deserializer == null) { throw new UnsupportedMediaTypeException(); } // generate new response return(new ModelResponse( request, ReadRequestStatus(ioResponse), deserializer.Deserialize( new StreamReader(ioResponse.Content), ioResponse.ContentType))); } else { return(new ModelResponse( request, ReadRequestStatus(ioResponse), null)); } }
/// <summary> /// Submits the given request. /// </summary> /// <param name="request"></param> /// <returns></returns> public ModelResponse Submit(ModelRequest request) { var handler = GetHandler(request); if (handler == null) return new ModelResponse(request, ModelResponseStatus.Error, null); return handler.Submit(request); }
/// <summary> /// Initializes a new instance. /// </summary> /// <param name="request"></param> public ModelResponse(ModelRequest request, ModelResponseStatus status, XDocument body) { Contract.Requires <ArgumentNullException>(request != null); this.request = request; this.status = status; this.body = body; this.headers = new Headers(); }
/// <summary> /// Initializes a new instance. /// </summary> /// <param name="request"></param> public ModelResponse(ModelRequest request, ModelResponseStatus status, XDocument body) { Contract.Requires<ArgumentNullException>(request != null); this.request = request; this.status = status; this.body = body; this.headers = new Headers(); }
/// <summary> /// Deserializes the <see cref="WebResponse"/> into a <see cref="ModelResponse"/>. /// </summary> /// <param name="ioResponse"></param> /// <param name="request"></param> /// <returns></returns> protected virtual ModelResponse ReadIOResponse(IOResponse ioResponse, ModelRequest request) { Contract.Requires <ArgumentNullException>(ioResponse != null); Contract.Requires <ArgumentNullException>(request != null); var response = ReadIOResponseFromContent(ioResponse, request); response.Headers.Add(ioResponse.Headers); return(response); }
protected override IOMethod GetMethod(ModelRequest request) { if (request.Method == ModelMethod.Get) return IOMethod.Get; if (request.Method == ModelMethod.Put) return IOMethod.Put; if (request.Method == ModelMethod.Post) return IOMethod.Put; return IOMethod.None; }
/// <summary> /// Gets the <see cref="IModelRequestHandler"/> to handle the given request. /// </summary> /// <param name="request"></param> /// <returns></returns> IModelRequestHandler GetHandler(ModelRequest request) { Contract.Requires<ArgumentNullException>(request != null); return handlers .Select(i => new { Priority = i.CanSubmit(request), Processor = i }) .Where(i => i.Priority != Priority.Ignore) .OrderByDescending(i => i.Priority) .Select(i => i.Processor) .FirstOrDefault(); }
/// <summary> /// Gets the <see cref="IModelRequestHandler"/> to handle the given request. /// </summary> /// <param name="request"></param> /// <returns></returns> IModelRequestHandler GetHandler(ModelRequest request) { Contract.Requires <ArgumentNullException>(request != null); return(handlers .Select(i => new { Priority = i.CanSubmit(request), Processor = i }) .Where(i => i.Priority != Priority.Ignore) .OrderByDescending(i => i.Priority) .Select(i => i.Processor) .FirstOrDefault()); }
/// <summary> /// Submits the given request. /// </summary> /// <param name="request"></param> /// <returns></returns> public ModelResponse Submit(ModelRequest request) { var handler = GetHandler(request); if (handler == null) { return(new ModelResponse(request, ModelResponseStatus.Error, null)); } return(handler.Submit(request)); }
/// <summary> /// Returns <c>true</c> if the processor can handle this request. /// </summary> /// <param name="submit"></param> /// <returns></returns> public override Priority CanSubmit(ModelRequest submit) { if (submit.ResourceUri.Scheme == Uri.UriSchemeFile) { return(Priority.Default); } else { return(Priority.Ignore); } }
/// <summary> /// Retrieves a <see cref="WebRequest"/> for the given <see cref="ModelRequest"/>. /// </summary> /// <param name="request"></param> /// <returns></returns> protected virtual IORequest WriteIORequest(ModelRequest request) { Contract.Requires <ArgumentNullException>(request != null); var mediaType = GetMediaType(request); if (mediaType == null) { return(null); } // obtain and build request parts var cnt = (string)null; var stm = new MemoryStream(); var uri = request.ResourceUri; if (IsQuery(request) && request.Body != null) { // serialize body to string var u = new UriBuilder(uri); var s = new StringBuilder(u.Query); using (var w = new StringWriter(s)) Serialize(w, request.Body, mediaType); u.Query = s.ToString(); // replace uri uri = u.Uri; } else if (request.Body != null) { // serialize data to body using (var w = new StreamWriter(stm, request.Encoding, 1024, true)) Serialize(w, request.Body, mediaType); // configure body cnt = mediaType; stm.Position = 0; } // generate new web request var ioRequest = new IORequest(uri, GetMethod(request)); ioRequest.ContentType = cnt; ioRequest.Headers.Add(request.Headers); ioRequest.Content = stm; return(ioRequest); }
/// <summary> /// Returns <c>true</c> if the request should append serialized data to the URI query path. /// </summary> /// <param name="request"></param> /// <returns></returns> protected virtual bool IsQuery(ModelRequest request) { Contract.Requires <ArgumentNullException>(request != null); // get http method var method = GetMethod(request); if (method == null) { throw new InvalidOperationException(); } return (method == IOMethod.Get || method == IOMethod.Delete); }
protected override IOMethod GetMethod(ModelRequest request) { if (request.Method == ModelMethod.Get) { return(IOMethod.Get); } if (request.Method == ModelMethod.Put) { return(IOMethod.Put); } if (request.Method == ModelMethod.Post) { return(IOMethod.Put); } return(IOMethod.None); }
/// <summary> /// Submits the request and returns the response. /// </summary> /// <param name="request"></param> /// <returns></returns> public override ModelResponse Submit(ModelRequest request) { // write request var ioRequest = WriteIORequest(request); if (ioRequest == null) throw new InvalidOperationException(); // submit request and retrieve response var ioResponse = ioService.Send(ioRequest); if (ioResponse == null) throw new InvalidOperationException(); // read response var response = ReadIOResponse(ioResponse, request); if (response == null) throw new InvalidOperationException(); return response; }
/// <summary> /// Gets the appropriate Web Request method type for the given <see cref="ModelRequest"/>. /// </summary> /// <param name="request"></param> /// <returns></returns> protected virtual IOMethod GetMethod(ModelRequest request) { Contract.Requires<ArgumentNullException>(request != null); if (request.Method == ModelMethod.Get) return IOMethod.Get; if (request.Method == ModelMethod.Put) return IOMethod.Put; if (request.Method == ModelMethod.Post || request.Method == ModelMethod.UrlEncodedPost || request.Method == ModelMethod.MultipartPost || request.Method == ModelMethod.FormDataPost) return IOMethod.Post; if (request.Method == ModelMethod.Delete) return IOMethod.Delete; return IOMethod.None; }
/// <summary> /// Gets the <see cref="MediaRange"/> to determine the format of the outgoing data. /// </summary> /// <param name="request"></param> /// <returns></returns> protected abstract MediaRange GetMediaType(ModelRequest request);
/// <summary> /// Return <c>true</c> if your <see cref="IModelRequestHandler"/> supports the given <see cref="ModelRequest"/>. /// </summary> /// <param name="request"></param> /// <returns></returns> public abstract Priority CanSubmit(ModelRequest request);
void OnSubmitImpl() { // The data model is updated based on some of the flags defined for deferred updates. Specifically, if the // deferred update rebuild flag is set for the model containing this submission, then the rebuild operation // is performed without dispatching an event to invoke the operation. Then, if the deferred update // recalculate flag is set for the model containing this submission, then the recalculate operation is // performed without dispatching an event to invoke the operation. This sequence of operations affects the // deferred update behavior by clearing the deferred update flags associated with the operations performed. var model = Element.Ancestors(Constants.XForms_1_0 + "model").First().Interface<Model>(); if (model.State.Rebuild) model.OnRebuild(); if (model.State.Recalculate) model.OnRecalculate(); // If the binding attributes of submission indicate an empty sequence or an item other than an element or // an instance document root node, then submission fails with no-data. Otherwise, the binding attributes of // submission indicate a node of instance data. var modelItems = new Binding(Element, context.Value.Context, properties.Ref).ModelItems; if (modelItems == null || modelItems.Length != 1 || modelItems.Any(i => i.Xml.NodeType != XmlNodeType.Document && i.Xml.NodeType != XmlNodeType.Element)) throw new DOMTargetEventException(Element, Events.SubmitError, new SubmitErrorContextInfo( SubmitErrorErrorType.NoData )); // The indicated node and all nodes for which it is an ancestor are selected. If the attribute relevant is // true, whether by default or declaration, then any selected node which is not relevant as defined in The // relevant Property is deselected (pruned). If all instance nodes are deselected, then submission fails // with no-data. var node = (XNode)new SubmitTransformer(properties.Relevant) .Visit(modelItems[0].Xml); if (node == null) throw new DOMTargetEventException(Element, Events.SubmitError, new SubmitErrorContextInfo( SubmitErrorErrorType.NoData )); // If the attribute validate is true, whether by default or declaration, then all selected instance data // nodes are checked for validity according to the definition in The xforms-revalidate Event (no // notification events are marked for dispatching due to this operation). If any selected instance data // node is found to be invalid, submission fails with validation-error. if (properties.Validate && new ValidationVisitor().Validate(node) == false) throw new DOMTargetEventException(Element, Events.SubmitError, new SubmitErrorContextInfo( SubmitErrorErrorType.ValidationError )); // The submission method is determined. // The submission method may be specified by the method attribute. The submission element can have a child // element named method, which overrides the submission method setting obtained from the method attribute // if both are specified. If more than one method element is given, the first occurrence in document order // must be selected for use. Individually, the method element and the method attribute are not required. // However, one of the two is mandatory as there is no default submission method. var method = GetMethod(); if (method == null) throw new DOMTargetEventException(Element, Events.SubmitError, "Unknown ModelMethod."); // The resource element provides the submission URI, overriding the resource attribute and the action // attribute. If a submission has more than one resource child element, the first resource element child // must be selected for use. Individually, the resource element, the resource attribute and the action // attribute are not required. However, one of the three is mandatory as there is no default submission // resource. var resource = GetResource(); if (resource == null) throw new DOMTargetEventException(Element, Events.SubmitError, new SubmitErrorContextInfo( SubmitErrorErrorType.ResourceError)); // If the serialization attribute value is "none", then the submission data serialization is the empty // string. Otherwise, the event xforms-submit-serialize is dispatched; if the submission-body property // of the event is changed from the initial value of empty string, then the content of the submission-body // property string is used as the submission data serialization. Otherwise, the submission data // serialization consists of a serialization of the selected instance data according to the rules stated // in Serialization. if (properties.Serialization.None) node = null; else { var evt = Element.Interface<EventTarget>().Dispatch(Events.SubmitSerialize, new SubmitSerializeContextInfo()); var ctx = evt.Context as SubmitSerializeContextInfo; if (ctx != null && ctx.SubmissionBody != "") // wrap in XText node = new XText(ctx.SubmissionBody); } // The submission is performed based on the submission headers, submission method, submission resource, and // submission data serialization. The exact rules of submission are based on the URI scheme and the // submission method, as defined in Submission Options. var request = new ModelRequest(resource, (ModelMethod)method); request.MediaType = properties.MediaType; request.Body = node; request.Encoding = properties.Encoding; request.Headers.Add(GetHeaders()); // submit and check for response var response = requestService.Submit(request); if (response == null) throw new DOMTargetEventException(Element, Events.SubmitError, new SubmitErrorContextInfo( SubmitErrorErrorType.ResourceError)); // For error responses, processing depends on the value of the replace attribute on element submission: // all: either the document is replaced with an implementation-specific indication of an error or submission fails with resource-error. // any other value: nothing in the document is replaced, and submission fails with resource-error. if (response.Status == ModelResponseStatus.Error) throw new DOMTargetEventException(Element, Events.SubmitError, new SubmitErrorContextInfo( SubmitErrorErrorType.ResourceError)); // For success responses, if the response does not include a body, submission succeeds. if (response.Body == null) { Element.DispatchEvent(Events.SubmitDone); return; } // handle result based on 'replace' property switch (properties.Replace) { // none: submission succeeds. case SubmissionReplace.None: Element.DispatchEvent(Events.SubmitDone); break; // all: the event xforms-submit-done may be dispatched with appropriate context information, and submit // processing concludes with the entire containing document being replaced with the returned body. case SubmissionReplace.All: throw new NotImplementedException(); // instance: If the body is not of type accepted by the processor, as specified in Creating instance data // from external resources, nothing in the document is replaced and submission fails with resource-error. // Otherwise the body is parsed to give an XPath Data Model according to Creating instance data from // external resources. If the parse fails, then submission fails with parse-error. If the parse succeeds, // then instance data replacement is performed according to Replacing Data with the Submission Response. // If this operation fails, submission fails with target-error. Otherwise, submission succeeds. case SubmissionReplace.Instance: FinishWithReplaceInstance(response, modelItems[0]); break; // text: If the body is neither an XML media type (i.e. with a content type not matching any of the // specifiers in [RFC 3023]) nor a text type (i.e. with a content type not matching text/*), nothing in the // document is replaced and submission fails with resource-error. Otherwise the content replacement is // performed according to Replacing Data with the Submission Response. If this operation fails, then the // submission fails with target-error. Otherwise, submission succeeds. case SubmissionReplace.Text: throw new NotImplementedException(); } }
public ModelResponse Submit(ModelRequest request) { Contract.Requires<ArgumentNullException>(request != null); Contract.Ensures(Contract.Result<ModelResponse>() != null); throw new NotImplementedException(); }
public ModelResponse Submit(ModelRequest request) { Contract.Requires <ArgumentNullException>(request != null); Contract.Ensures(Contract.Result <ModelResponse>() != null); throw new NotImplementedException(); }
/// <summary> /// Retrieves a <see cref="WebRequest"/> for the given <see cref="ModelRequest"/>. /// </summary> /// <param name="request"></param> /// <returns></returns> protected virtual IORequest WriteIORequest(ModelRequest request) { Contract.Requires<ArgumentNullException>(request != null); var mediaType = GetMediaType(request); if (mediaType == null) return null; // obtain and build request parts var cnt = (string)null; var stm = new MemoryStream(); var uri = request.ResourceUri; if (IsQuery(request) && request.Body != null) { // serialize body to string var u = new UriBuilder(uri); var s = new StringBuilder(u.Query); using (var w = new StringWriter(s)) Serialize(w, request.Body, mediaType); u.Query = s.ToString(); // replace uri uri = u.Uri; } else if (request.Body != null) { // serialize data to body using (var w = new StreamWriter(stm, request.Encoding, 1024, true)) Serialize(w, request.Body, mediaType); // configure body cnt = mediaType; stm.Position = 0; } // generate new web request var ioRequest = new IORequest(uri, GetMethod(request)); ioRequest.ContentType = cnt; ioRequest.Headers.Add(request.Headers); ioRequest.Content = stm; return ioRequest; }
/// <summary> /// Extracts an <see cref="ModelResponse"/> from the given <see cref="IOResponse"/> content. /// </summary> /// <param name="ioResponse"></param> /// <param name="request"></param> /// <returns></returns> ModelResponse ReadIOResponseFromContent(IOResponse ioResponse, ModelRequest request) { Contract.Requires<ArgumentNullException>(ioResponse != null); Contract.Requires<ArgumentNullException>(request != null); // deserialize if possible if (ioResponse.Content != null && ioResponse.ContentType != null) { // obtain serializer var deserializer = GetDeserializer(ioResponse.ContentType); if (deserializer == null) throw new UnsupportedMediaTypeException(); // generate new response return new ModelResponse( request, ReadRequestStatus(ioResponse), deserializer.Deserialize( new StreamReader(ioResponse.Content), ioResponse.ContentType)); } else { return new ModelResponse( request, ReadRequestStatus(ioResponse), null); } }
protected override bool IsQuery(ModelRequest request) { return(false); }
public override Priority CanSubmit(ModelRequest request) { return Priority.Low; }
public override Priority CanSubmit(ModelRequest request) { return(Priority.Low); }
protected override bool IsQuery(ModelRequest request) { return false; }
public Priority CanSubmit(ModelRequest request) { Contract.Requires <ArgumentNullException>(request != null); throw new NotImplementedException(); }
/// <summary> /// Deserializes the <see cref="WebResponse"/> into a <see cref="ModelResponse"/>. /// </summary> /// <param name="ioResponse"></param> /// <param name="request"></param> /// <returns></returns> protected virtual ModelResponse ReadIOResponse(IOResponse ioResponse, ModelRequest request) { Contract.Requires<ArgumentNullException>(ioResponse != null); Contract.Requires<ArgumentNullException>(request != null); var response = ReadIOResponseFromContent(ioResponse, request); response.Headers.Add(ioResponse.Headers); return response; }
public Priority CanSubmit(ModelRequest request) { Contract.Requires<ArgumentNullException>(request != null); throw new NotImplementedException(); }
/// <summary> /// Returns <c>true</c> if the request should append serialized data to the URI query path. /// </summary> /// <param name="request"></param> /// <returns></returns> protected virtual bool IsQuery(ModelRequest request) { Contract.Requires<ArgumentNullException>(request != null); // get http method var method = GetMethod(request); if (method == null) throw new InvalidOperationException(); return method == IOMethod.Get || method == IOMethod.Delete; }
/// <summary> /// Handles a submitted request. /// </summary> /// <param name="request"></param> /// <returns></returns> public abstract ModelResponse Submit(ModelRequest request);
/// <summary> /// Gets the <see cref="MediaType"/> to be used on outgoing data. /// </summary> /// <param name="request"></param> /// <returns></returns> protected override MediaRange GetMediaType(ModelRequest request) { if (request.Method == ModelMethod.Post || request.Method == ModelMethod.Put) return "application/xml"; if (request.Method == ModelMethod.Get || request.Method == ModelMethod.Delete || request.Method == ModelMethod.UrlEncodedPost) return "application/x-www-form-urlencoded"; if (request.Method == ModelMethod.MultipartPost) return "multipart/related"; if (request.Method == ModelMethod.FormDataPost) return "multipart/form-data"; return null; }