atlassian.net-sdk-v2.0 /Atlassian.Jira/Remote/RemoteWorklogPatchBehavior.cs

Language C# Lines 93
MD5 Hash 8c0332ae9413dc9e905512f3886b853e Estimated Cost $1,855 (why?)
Repository https://bitbucket.org/yyo/atlassian.net-sdk-v2.0.git View Raw File View Project SPDX
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
using System.IO;
using System.Xml;
using System.ServiceModel.Channels;
using System.ServiceModel;

namespace Atlassian.Jira.Remote
{
    internal class RemoteWorklogPatchBehavior : IEndpointBehavior
    {
        public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
        {
        }

        public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
        {
            clientRuntime.MessageInspectors.Add(new RemoteWorklogMessageInspector());
        }

        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
        {
        }

        public void Validate(ServiceEndpoint endpoint)
        {
        }

        private class RemoteWorklogMessageInspector : IClientMessageInspector
        {
            private static string _correlationState = "worklog";

            public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
            {
                if(correlationState != null 
                    && String.Equals(correlationState, _correlationState)
                    && !reply.ToString().Contains("<soapenv:Fault>"))
                {
                    var memoryStream = new MemoryStream();
                    var writer = XmlWriter.Create(memoryStream);
                    reply.WriteMessage(writer);
                    writer.Flush();

                    memoryStream.Position = 0;
                    var doc = new XmlDocument();
                    doc.Load(memoryStream);

                    UpdateMessage(doc);

                    memoryStream.SetLength(0);
                    writer = XmlWriter.Create(memoryStream);
                    doc.WriteTo(writer);
                    writer.Flush();

                    memoryStream.Position = 0;
                    var reader = XmlReader.Create(memoryStream);
                    reply = Message.CreateMessage(reader, int.MaxValue, reply.Version);
                }
            }

            private void UpdateMessage(XmlDocument doc)
            {
                var ns = new XmlNamespaceManager(doc.NameTable);
                ns.AddNamespace("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
                foreach(XmlElement multiRefElement in doc.SelectNodes("//soapenv:Body/multiRef", ns))
                {
                    multiRefElement.SetAttribute("xsi:type", "ns2:RemoteWorklog");
                    multiRefElement.SetAttribute("xmlns:ns2", "http://beans.soap.rpc.jira.atlassian.com");
                }
            }

            public object BeforeSendRequest(ref Message request, IClientChannel channel)
            {
                var requestContent = request.ToString();

                if (requestContent.Contains("addWorklogAndAutoAdjustRemainingEstimate")
                    || requestContent.Contains("addWorklogAndRetainRemainingEstimate")
                    || requestContent.Contains("addWorklogWithNewRemainingEstimate")
                    || requestContent.Contains("getWorklogs"))
                {
                    return _correlationState;
                }
                return null;
            }
        }
    }

}
Back to Top