1 | using System; |
---|
2 | using System.Collections.Generic; |
---|
3 | using System.Linq; |
---|
4 | using System.Text; |
---|
5 | using System.ServiceModel.Description; |
---|
6 | using System.ServiceModel.Dispatcher; |
---|
7 | using System.ServiceModel; |
---|
8 | using System.Windows.Forms; |
---|
9 | using System.ServiceModel.Channels; |
---|
10 | |
---|
11 | public static class CustomHeaderNames |
---|
12 | { |
---|
13 | public const String CustomHeaderName = "UsernameToken"; |
---|
14 | |
---|
15 | public const String UserName = "user"; |
---|
16 | public const String PassName = "pass"; |
---|
17 | |
---|
18 | public const String CustomHeaderNamespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"; |
---|
19 | |
---|
20 | } |
---|
21 | |
---|
22 | public class CustomHeader : MessageHeader |
---|
23 | { |
---|
24 | private String _user, _pass; |
---|
25 | |
---|
26 | public String user |
---|
27 | { |
---|
28 | get |
---|
29 | { |
---|
30 | return (this._user); |
---|
31 | } |
---|
32 | } |
---|
33 | |
---|
34 | public String pass |
---|
35 | { |
---|
36 | get |
---|
37 | { |
---|
38 | return (this._pass); |
---|
39 | } |
---|
40 | } |
---|
41 | |
---|
42 | public CustomHeader(String user, String pass) |
---|
43 | { |
---|
44 | this._user = user; |
---|
45 | this._pass = pass; |
---|
46 | } |
---|
47 | |
---|
48 | public override string Name |
---|
49 | { |
---|
50 | get { return (CustomHeaderNames.CustomHeaderName); } |
---|
51 | } |
---|
52 | |
---|
53 | public override string Namespace |
---|
54 | { |
---|
55 | get { return (CustomHeaderNames.CustomHeaderNamespace); } |
---|
56 | } |
---|
57 | |
---|
58 | protected override void OnWriteHeaderContents(System.Xml.XmlDictionaryWriter writer, MessageVersion messageVersion) |
---|
59 | { |
---|
60 | // Write the content of the header directly using the XmlDictionaryWriter |
---|
61 | writer.WriteElementString(CustomHeaderNames.UserName, this.user); |
---|
62 | writer.WriteElementString(CustomHeaderNames.PassName, this.pass); |
---|
63 | } |
---|
64 | |
---|
65 | public static CustomHeader ReadHeader(System.Xml.XmlDictionaryReader reader) |
---|
66 | { |
---|
67 | // Read the header content (key) using the XmlDictionaryReader |
---|
68 | if (reader.ReadToDescendant(CustomHeaderNames.UserName, CustomHeaderNames.CustomHeaderNamespace)) |
---|
69 | { |
---|
70 | String user = reader.ReadElementString("user"); |
---|
71 | String pass = reader.ReadElementString("pass"); |
---|
72 | return (new CustomHeader(user, pass)); |
---|
73 | } |
---|
74 | else |
---|
75 | { |
---|
76 | return null; |
---|
77 | } |
---|
78 | } |
---|
79 | } |
---|
80 | |
---|
81 | public class MyMessageInspector : IClientMessageInspector |
---|
82 | { |
---|
83 | //static string response; |
---|
84 | public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState) |
---|
85 | { |
---|
86 | try |
---|
87 | { |
---|
88 | //Console.WriteLine(reply.ToString()); |
---|
89 | //MessageBox.Show(reply.ToString()); |
---|
90 | string ret = reply.ToString().Substring(reply.ToString().IndexOf("<return ")); |
---|
91 | ret = ret.Substring(ret.ToString().IndexOf(">") + 1); |
---|
92 | ret = ret.Substring(0, ret.ToString().IndexOf("</ret")); |
---|
93 | BS_SOAP_API.SOAPWorker.response = ret; |
---|
94 | } |
---|
95 | catch (Exception e) { BS_SOAP_API.SOAPWorker.response = e.Message; } |
---|
96 | } |
---|
97 | public static string CalculateMD5Hash(string input) |
---|
98 | { |
---|
99 | // step 1, calculate MD5 hash from input |
---|
100 | System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create(); |
---|
101 | byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input); |
---|
102 | byte[] hash = md5.ComputeHash(inputBytes); |
---|
103 | |
---|
104 | // step 2, convert byte array to hex string |
---|
105 | StringBuilder sb = new StringBuilder(); |
---|
106 | for (int i = 0; i < hash.Length; i++) |
---|
107 | { |
---|
108 | sb.Append(hash[i].ToString("X2")); |
---|
109 | } |
---|
110 | return sb.ToString(); |
---|
111 | } |
---|
112 | public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel) |
---|
113 | { |
---|
114 | |
---|
115 | |
---|
116 | string pass = BS_SOAP_API.SOAPWorker.Password; |
---|
117 | request.Headers.Add(new CustomHeader(BS_SOAP_API.SOAPWorker.Username, pass)); |
---|
118 | //Console.WriteLine(request.ToString()); |
---|
119 | return request; |
---|
120 | } |
---|
121 | } |
---|
122 | |
---|
123 | public class InspectorBehavior : IEndpointBehavior |
---|
124 | { |
---|
125 | public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime) |
---|
126 | { clientRuntime.MessageInspectors.Add(new MyMessageInspector()); } |
---|
127 | public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters) { } |
---|
128 | public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) { } |
---|
129 | public void Validate(ServiceEndpoint endpoint) { } |
---|
130 | } |
---|
131 | |
---|
132 | namespace BS_SOAP_API |
---|
133 | { |
---|
134 | |
---|
135 | public class SOAPWorker |
---|
136 | { |
---|
137 | public static string Username; |
---|
138 | public static string Password; |
---|
139 | public static string response; |
---|
140 | public static APIService.EDokApiPortTypeClient call; |
---|
141 | static System.ServiceModel.BasicHttpBinding binding = new System.ServiceModel.BasicHttpBinding(); |
---|
142 | public static string CalculateMD5Hash(string input) |
---|
143 | { |
---|
144 | // step 1, calculate MD5 hash from input |
---|
145 | System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create(); |
---|
146 | byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input); |
---|
147 | byte[] hash = md5.ComputeHash(inputBytes); |
---|
148 | |
---|
149 | // step 2, convert byte array to hex string |
---|
150 | StringBuilder sb = new StringBuilder(); |
---|
151 | for (int i = 0; i < hash.Length; i++) |
---|
152 | { |
---|
153 | sb.Append(hash[i].ToString("X2")); |
---|
154 | } |
---|
155 | return sb.ToString(); |
---|
156 | } |
---|
157 | public static Boolean init(string address, string username, string password) |
---|
158 | { |
---|
159 | address += "eDokumentyApi.php"; |
---|
160 | try |
---|
161 | { |
---|
162 | Username = username; |
---|
163 | Password = password; |
---|
164 | call = new APIService.EDokApiPortTypeClient( |
---|
165 | (System.ServiceModel.Channels.Binding)binding, |
---|
166 | new EndpointAddress(new Uri(address), |
---|
167 | EndpointIdentity.CreateDnsIdentity("eDokumentyAPI"))); |
---|
168 | call.Endpoint.Behaviors.Add(new InspectorBehavior()); |
---|
169 | call.Open(); |
---|
170 | if (call.State == System.ServiceModel.CommunicationState.Opened) return true; else return false; |
---|
171 | } |
---|
172 | catch (Exception e) |
---|
173 | { |
---|
174 | MessageBox.Show("Wystąpił błąd podczas inicjalizacji połączenia"); |
---|
175 | e.GetType(); |
---|
176 | return false; |
---|
177 | //throw e; |
---|
178 | } |
---|
179 | } |
---|
180 | |
---|
181 | public static int CreateDocument(Dictionary<string, string> data) |
---|
182 | { |
---|
183 | string json = Newtonsoft.Json.JsonConvert.SerializeObject(data); |
---|
184 | |
---|
185 | try |
---|
186 | { |
---|
187 | SOAPWorker.call.createDocument( |
---|
188 | new BS_SOAP_API.APIService.createDocumentRequest((object)json) |
---|
189 | ); |
---|
190 | return int.Parse(response.Replace("\"", "")); |
---|
191 | } |
---|
192 | catch (Exception e) { e.GetType(); return -1; } |
---|
193 | } |
---|
194 | |
---|
195 | public static int SearchContacts(Dictionary<string, string> data) |
---|
196 | { |
---|
197 | string json = Newtonsoft.Json.JsonConvert.SerializeObject(data); |
---|
198 | SOAPWorker.call.searchContacts( |
---|
199 | new BS_SOAP_API.APIService.searchContactsRequest((object)json) |
---|
200 | ); |
---|
201 | try |
---|
202 | { |
---|
203 | |
---|
204 | //Console.WriteLine(response); |
---|
205 | return Newtonsoft.Json.JsonConvert.DeserializeObject<int[]>(response)[0]; |
---|
206 | //return int.Parse(response); |
---|
207 | } |
---|
208 | catch (Exception e) { e.GetType(); return -1; } |
---|
209 | } |
---|
210 | |
---|
211 | public static Dictionary<string, string> getUserAccount(string name) |
---|
212 | { |
---|
213 | Dictionary<string, string> data = new Dictionary<string, string>(); |
---|
214 | data["usrnam"] = name; |
---|
215 | string json = Newtonsoft.Json.JsonConvert.SerializeObject(data); |
---|
216 | SOAPWorker.call.getUserAccount( |
---|
217 | new BS_SOAP_API.APIService.getUserAccountRequest((object)json) |
---|
218 | ); |
---|
219 | try |
---|
220 | { |
---|
221 | return Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(response); |
---|
222 | } |
---|
223 | catch (Exception e) { e.GetType(); return null; } |
---|
224 | } |
---|
225 | |
---|
226 | |
---|
227 | public static Dictionary<string, string> GetContactData(int id) |
---|
228 | { |
---|
229 | //string json = Newtonsoft.Json.JsonConvert.SerializeObject(data); |
---|
230 | SOAPWorker.call.getContact( |
---|
231 | new BS_SOAP_API.APIService.getContactRequest((object)id) |
---|
232 | ); |
---|
233 | try |
---|
234 | { |
---|
235 | |
---|
236 | //Console.WriteLine(response); |
---|
237 | return Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(response); |
---|
238 | // return int.Parse(response); |
---|
239 | } |
---|
240 | catch (Exception e) { e.GetType(); return null; } |
---|
241 | } |
---|
242 | |
---|
243 | public static int GetTypeOfDocumentData(string name) |
---|
244 | { |
---|
245 | Dictionary<string, string> data = new Dictionary<string, string>(); |
---|
246 | data["dctptp"] = name; |
---|
247 | string json = Newtonsoft.Json.JsonConvert.SerializeObject(data); |
---|
248 | |
---|
249 | try |
---|
250 | { |
---|
251 | SOAPWorker.call.getDocumentTypeData( |
---|
252 | new BS_SOAP_API.APIService.getDocumentTypeDataRequest((object)json) |
---|
253 | ); |
---|
254 | Dictionary<object, object> resp = |
---|
255 | Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<object, object>>(response.Trim(new char[]{'[',']'})); |
---|
256 | return int.Parse((string)resp["dctpid"]); |
---|
257 | } |
---|
258 | catch (Exception e) { e.GetType(); return -1; } |
---|
259 | } |
---|
260 | |
---|
261 | } |
---|
262 | |
---|
263 | } |
---|