2
2
using System . Globalization ;
3
3
using System . Linq ;
4
4
using System . Net ;
5
+ using System . Net . Http . Headers ;
5
6
using System . Text ;
6
7
using System . Xml . Linq ;
7
8
using PushSharp . Core ;
@@ -17,90 +18,122 @@ public class BlackberryPushChannel : IPushChannel
17
18
public BlackberryPushChannel ( BlackberryPushChannelSettings channelSettings )
18
19
{
19
20
bisChannelSettings = channelSettings ;
21
+
22
+ http = new BlackberryHttpClient ( bisChannelSettings ) ;
20
23
}
21
24
22
25
public class BlackberryHttpClient : HttpClient
23
26
{
24
- public BlackberryHttpClient ( ) : base ( )
27
+ private BlackberryPushChannelSettings channelSettings ;
28
+
29
+ public BlackberryHttpClient ( BlackberryPushChannelSettings channelSettings ) : base ( )
25
30
{
31
+ this . channelSettings = channelSettings ;
32
+
33
+ var authInfo = channelSettings . ApplicationId + ":" + channelSettings . Password ;
34
+ authInfo = Convert . ToBase64String ( Encoding . Default . GetBytes ( authInfo ) ) ;
35
+
36
+ this . DefaultRequestHeaders . Authorization = new AuthenticationHeaderValue ( "Basic" , authInfo ) ;
37
+ this . DefaultRequestHeaders . ConnectionClose = true ;
38
+
39
+ this . DefaultRequestHeaders . Remove ( "connection" ) ;
26
40
}
27
41
28
42
public Task < HttpResponseMessage > PostNotification ( BlackberryPushChannelSettings channelSettings , BlackberryNotification n )
29
43
{
30
- var authInfo = channelSettings . ApplicationId + ":" + channelSettings . Password ;
31
- authInfo = Convert . ToBase64String ( Encoding . Default . GetBytes ( authInfo ) ) ;
32
-
33
44
var c = new MultipartContent ( "related" , channelSettings . Boundary ) ;
34
- c . Headers . TryAddWithoutValidation ( "Authorization" , "Basic " + authInfo ) ;
35
- c . Headers . TryAddWithoutValidation ( "Content-Type" , "type: application/xml" ) ;
45
+ c . Headers . Remove ( "Content-Type" ) ;
46
+ c . Headers . TryAddWithoutValidation ( "Content-Type" , "multipart/related; boundary=" + channelSettings . Boundary + "; type=application/xml" ) ;
47
+
36
48
var xml = n . ToPapXml ( ) ;
37
49
38
- c . Add ( new StringContent ( xml . ToString ( ) , Encoding . UTF8 , "application/xml" ) ) ;
39
50
40
- c . Add ( new ByteArrayContent ( n . Message ) ) ;
51
+ c . Add ( new StringContent ( xml , Encoding . UTF8 , "application/xml" ) ) ;
52
+
53
+ var bc = new ByteArrayContent ( n . Content . Content ) ;
54
+ bc . Headers . Add ( "Content-Type" , n . Content . ContentType ) ;
55
+
56
+ foreach ( var header in n . Content . Headers )
57
+ bc . Headers . Add ( header . Key , header . Value ) ;
58
+
59
+ c . Add ( bc ) ;
41
60
42
61
return PostAsync ( channelSettings . SendUrl , c ) ;
43
62
}
44
63
}
45
64
46
- BlackberryHttpClient http = new BlackberryHttpClient ( ) ;
65
+ private BlackberryHttpClient http ;
47
66
48
67
public void SendNotification ( INotification notification , SendNotificationCallbackDelegate callback )
49
68
{
50
69
var n = notification as BlackberryNotification ;
51
70
52
- var response = http . PostNotification ( bisChannelSettings , n ) . Result ;
53
- var description = string . Empty ;
54
-
55
- var status = new BlackberryMessageStatus
56
- {
57
- Notification = n ,
58
- HttpStatus = HttpStatusCode . ServiceUnavailable
59
- } ;
60
-
61
- var bbNotStatus = string . Empty ;
62
- status . HttpStatus = response . StatusCode ;
63
-
64
- var doc = XDocument . Load ( response . Content . ReadAsStreamAsync ( ) . Result ) ;
65
-
66
- var result = doc . Descendants ( "response-result" ) . SingleOrDefault ( ) ;
67
- if ( result != null )
68
- {
69
- bbNotStatus = result . Attribute ( "code" ) . Value ;
70
- description = result . Attribute ( "desc" ) . Value ;
71
- }
72
- else
73
- {
74
- result = doc . Descendants ( "badmessage-response" ) . SingleOrDefault ( ) ;
75
- if ( result != null )
76
- {
77
- bbNotStatus = result . Attribute ( "code" ) . Value ;
78
- description = result . Attribute ( "desc" ) . Value ;
79
- }
80
- }
81
-
82
- BlackberryNotificationStatus notStatus ;
83
- Enum . TryParse ( bbNotStatus , true , out notStatus ) ;
84
- status . NotificationStatus = notStatus ;
85
-
86
- if ( status . NotificationStatus == BlackberryNotificationStatus . NoAppReceivePush )
87
- {
88
- if ( callback != null )
89
- callback ( this , new SendNotificationResult ( notification , false , new Exception ( "Device Subscription Expired" ) ) { IsSubscriptionExpired = true } ) ;
90
-
91
- return ;
92
- }
93
-
94
- if ( status . HttpStatus == HttpStatusCode . OK
95
- && status . NotificationStatus == BlackberryNotificationStatus . RequestAcceptedForProcessing )
96
- {
97
- if ( callback != null )
98
- callback ( this , new SendNotificationResult ( notification ) ) ;
99
- return ;
100
- }
101
-
102
- if ( callback != null )
103
- callback ( this , new SendNotificationResult ( status . Notification , false , new BisNotificationSendFailureException ( status , description ) ) ) ;
71
+ try
72
+ {
73
+ var response = http . PostNotification ( bisChannelSettings , n ) . Result ;
74
+ var description = string . Empty ;
75
+
76
+ var status = new BlackberryMessageStatus
77
+ {
78
+ Notification = n ,
79
+ HttpStatus = HttpStatusCode . ServiceUnavailable
80
+ } ;
81
+
82
+ var bbNotStatus = string . Empty ;
83
+ status . HttpStatus = response . StatusCode ;
84
+
85
+ var doc = XDocument . Load ( response . Content . ReadAsStreamAsync ( ) . Result ) ;
86
+
87
+ var result = doc . Descendants ( "response-result" ) . SingleOrDefault ( ) ;
88
+ if ( result != null )
89
+ {
90
+ bbNotStatus = result . Attribute ( "code" ) . Value ;
91
+ description = result . Attribute ( "desc" ) . Value ;
92
+ }
93
+ else
94
+ {
95
+ result = doc . Descendants ( "badmessage-response" ) . SingleOrDefault ( ) ;
96
+ if ( result != null )
97
+ {
98
+ bbNotStatus = result . Attribute ( "code" ) . Value ;
99
+ description = result . Attribute ( "desc" ) . Value ;
100
+ }
101
+ }
102
+
103
+ BlackberryNotificationStatus notStatus ;
104
+ Enum . TryParse ( bbNotStatus , true , out notStatus ) ;
105
+ status . NotificationStatus = notStatus ;
106
+
107
+ if ( status . NotificationStatus == BlackberryNotificationStatus . NoAppReceivePush )
108
+ {
109
+ if ( callback != null )
110
+ callback ( this ,
111
+ new SendNotificationResult ( notification , false , new Exception ( "Device Subscription Expired" ) )
112
+ {
113
+ IsSubscriptionExpired = true
114
+ } ) ;
115
+
116
+ return ;
117
+ }
118
+
119
+ if ( status . HttpStatus == HttpStatusCode . OK
120
+ && status . NotificationStatus == BlackberryNotificationStatus . RequestAcceptedForProcessing )
121
+ {
122
+ if ( callback != null )
123
+ callback ( this , new SendNotificationResult ( notification ) ) ;
124
+ return ;
125
+ }
126
+
127
+ if ( callback != null )
128
+ callback ( this ,
129
+ new SendNotificationResult ( status . Notification , false ,
130
+ new BisNotificationSendFailureException ( status , description ) ) ) ;
131
+ }
132
+ catch ( Exception ex )
133
+ {
134
+ if ( callback != null )
135
+ callback ( this , new SendNotificationResult ( notification , false , ex ) ) ;
136
+ }
104
137
}
105
138
106
139
public void Dispose ( )
0 commit comments