forked from opentelecoms-org/MjSIP-fork
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MessageFactory.java
164 lines (152 loc) · 6.64 KB
/
MessageFactory.java
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
* Copyright (C) 2005 Luca Veltri - University of Parma - Italy
*
* This file is part of MjSip (http://www.mjsip.org)
*
* MjSip is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* MjSip is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MjSip; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author(s):
* Luca Veltri (luca.veltri@unipr.it)
*/
package org.zoolu.sip.message;
import org.zoolu.sip.address.*;
import org.zoolu.sip.header.SubjectHeader;
import org.zoolu.sip.header.ReferToHeader;
import org.zoolu.sip.header.ReferredByHeader;
import org.zoolu.sip.header.EventHeader;
import org.zoolu.sip.dialog.Dialog;
import org.zoolu.sip.provider.SipProvider;
/**
* Class sipx.message.MessageFactory extends class
* sip.message.BaseMessageFactory.
* <p />
* MessageFactory is used to create SIP messages (requests and responses).
* <br />
* A valid SIP request sent by a UAC MUST, at least, contain the following
* header fields: To, From, CSeq, Call-ID, Max-Forwards, and Via; all of these
* header fields are mandatory in all SIP requests. These sip header fields are
* the fundamental building blocks of a SIP message, as they jointly provide for
* most of the critical message routing services including the addressing of
* messages, the routing of responses, limiting message propagation, ordering of
* messages, and the unique identification of transactions. These header fields
* are in addition to the mandatory request line, which contains the method,
* Request-URI, and SIP version.
*/
public class MessageFactory extends org.zoolu.sip.message.BaseMessageFactory {
/** Creates a new MESSAGE request (RFC3428) */
public static Message createMessageRequest(SipProvider sip_provider,
NameAddress recipient, NameAddress from, String subject,
String type, String body) {
SipURL request_uri = recipient.getAddress();
String callid = sip_provider.pickCallId();
int cseq = SipProvider.pickInitialCSeq();
String localtag = SipProvider.pickTag();
// String branch=SipStack.pickBranch();
Message req = createRequest(sip_provider, SipMethods.MESSAGE,
request_uri, recipient, from, null, callid, cseq, localtag,
null, null, null, null); // modified by mandrajg
if (subject != null)
req.setSubjectHeader(new SubjectHeader(subject));
req.setBody(type, body);
return req;
}
/** Creates a new REFER request (RFC3515) */
public static Message createReferRequest(SipProvider sip_provider,
NameAddress recipient, NameAddress from, NameAddress contact,
NameAddress refer_to/* , NameAddress referred_by */) {
SipURL request_uri = recipient.getAddress();
String callid = sip_provider.pickCallId();
int cseq = SipProvider.pickInitialCSeq();
String localtag = SipProvider.pickTag();
// String branch=SipStack.pickBranch();
Message req = createRequest(sip_provider, SipMethods.REFER,
request_uri, recipient, from, contact, callid, cseq, localtag,
null, null, null, null); // modified by mandrajg
req.setReferToHeader(new ReferToHeader(refer_to));
// if (referred_by!=null) req.setReferredByHeader(new
// ReferredByHeader(referred_by));
req.setReferredByHeader(new ReferredByHeader(from));
return req;
}
/**
* Creates a new REFER request (RFC3515) within a dialog
* <p>
* parameters: <br> - <i>refer_to</i> mandatory <br> - <i>referred_by</i>
* optional
*/
public static Message createReferRequest(Dialog dialog,
NameAddress refer_to, NameAddress referred_by) {
Message req = createRequest(dialog, SipMethods.REFER, null);
req.setReferToHeader(new ReferToHeader(refer_to));
if (referred_by != null)
req.setReferredByHeader(new ReferredByHeader(referred_by));
else
req
.setReferredByHeader(new ReferredByHeader(dialog
.getLocalName()));
return req;
}
/**
* Creates a new SUBSCRIBE request (RFC3265) out of any pre-existing
* dialogs.
*/
public static Message createSubscribeRequest(SipProvider sip_provider,
SipURL recipient, NameAddress to, NameAddress from,
NameAddress contact, String event, String id, String content_type,
String body) {
Message req = createRequest(sip_provider, SipMethods.SUBSCRIBE,
recipient, to, from, contact, null);
req.setEventHeader(new EventHeader(event, id));
req.setBody(content_type, body);
return req;
}
/** Creates a new SUBSCRIBE request (RFC3265) within a dialog (re-subscribe). */
public static Message createSubscribeRequest(Dialog dialog, String event,
String id, String content_type, String body) {
Message req = createRequest(dialog, SipMethods.SUBSCRIBE, null);
req.setEventHeader(new EventHeader(event, id));
req.setBody(content_type, body);
return req;
}
/** Creates a new NOTIFY request (RFC3265) within a dialog */
public static Message createNotifyRequest(Dialog dialog, String event,
String id, String content_type, String body) {
Message req = createRequest(dialog, SipMethods.NOTIFY, null);
req.removeExpiresHeader();
req.setEventHeader(new EventHeader(event, id));
req.setBody(content_type, body);
return req;
}
/** Creates a new NOTIFY request (RFC3265) within a dialog */
public static Message createNotifyRequest(Dialog dialog, String event,
String id, String sipfragment) {
Message req = createRequest(dialog, SipMethods.NOTIFY, null);
req.removeExpiresHeader();
req.setEventHeader(new EventHeader(event, id));
req.setBody("message/sipfrag;version=2.0", sipfragment);
return req;
}
public Message createPublishRequest(SipProvider sip_provider, NameAddress from, String event, int expires, String contentType, String body )
{ SipURL request_uri=from.getAddress();
String callid=sip_provider.pickCallId();
int cseq=SipProvider.pickInitialCSeq();
String localtag=SipProvider.pickTag();
Message req=createRequest(sip_provider,SipMethods.PUBLISH,request_uri,from,from,null,callid,cseq,localtag,null,null,body);
req.setEventHeader(new EventHeader(event));
req.setBody(contentType, body);
req.setExpiresHeader(new ExpiresHeader(expires));
return req;
}
}