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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
/*
* Jitsi, the OpenSource Java VoIP and Instant Messaging client.
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package net.java.sip.communicator.impl.protocol.sip;
import gov.nist.javax.sip.header.*;
import net.java.sip.communicator.util.*;
import javax.sip.address.*;
import javax.sip.header.*;
import javax.sip.message.*;
import java.util.*;
/**
* Checks the <tt>protocolProvider</tt>'s account properties for configured
* custom headers and add them to the outgoing requests.
* The header account properties are of form:
* ConfigHeader.N.Name=HeaderName
* ConfigHeader.N.Value=HeaderValue
* ConfigHeader.N.Method=SIP_MethodName (optional)
* Where N is the index of the header, multiple custom headers can be added.
* Name is the header name to use and Value is its value. The optional
* property is whether to use a specific request method to attach headers to
* or if missing we will attach it to all requests.
*
* @author Damian Minkov
*/
public class ConfigHeaders
{
/**
* The logger for this class
*/
public final static Logger logger = Logger.getLogger(ConfigHeaders.class);
/**
* The account property holding all the custom pre-configured headers.
*/
private final static String ACC_PROPERTY_CONFIG_HEADER = "ConfigHeader";
/**
* The config property suffix for custom header name.
*/
private final static String ACC_PROPERTY_CONFIG_HEADER_NAME
= "Name";
/**
* The config property suffix for custom header value.
*/
private final static String ACC_PROPERTY_CONFIG_HEADER_VALUE
= "Value";
/**
* The config property suffix for custom header method.
*/
private final static String ACC_PROPERTY_CONFIG_HEADER_METHOD
= "Method";
/**
* Attach any custom headers pre configured for the account. Added only
* to message Requests.
* The header account properties are of form:
* ConfigHeader.N.Name=HeaderName
* ConfigHeader.N.Value=HeaderValue
* ConfigHeader.N.Method=SIP_MethodName (optional)
* Where N is the index of the header, multiple custom headers can be added.
* Name is the header name to use and Value is its value. The optional
* property is whether to use a specific request method to attach headers to
* or if missing we will attach it to all requests.
*
* @param message the message that we'd like to attach custom headers to.
* @param protocolProvider the protocol provider to check for configured
* custom headers.
*/
static void attachConfigHeaders(
Message message,
ProtocolProviderServiceSipImpl protocolProvider)
{
if(message instanceof Response)
return;
Request request = (Request)message;
Map<String, String> props
= protocolProvider.getAccountID().getAccountProperties();
Map<String,Map<String,String>> headers
= new HashMap<String, Map<String, String>>();
// parse the properties into the map where the index is the key
for(Map.Entry<String, String> entry : props.entrySet())
{
String pName = entry.getKey();
String prefStr = entry.getValue();
String name;
String ix;
if(!pName.startsWith(ACC_PROPERTY_CONFIG_HEADER) || prefStr == null)
continue;
prefStr = prefStr.trim();
if(pName.contains("."))
{
pName = pName.replaceAll(ACC_PROPERTY_CONFIG_HEADER + ".", "");
name = pName.substring(pName.lastIndexOf('.') + 1).trim();
if(!pName.contains("."))
continue;
ix = pName.substring(0, pName.lastIndexOf('.')).trim();
}
else
continue;
Map<String,String> headerValues = headers.get(ix);
if(headerValues == null)
{
headerValues = new HashMap<String, String>();
headers.put(ix, headerValues);
}
headerValues.put(name, prefStr);
}
// process the found custom headers
for(Map<String, String> headerValues : headers.values())
{
String method = headerValues.get(ACC_PROPERTY_CONFIG_HEADER_METHOD);
// if there is a method setting and is different from
// current request method skip this header
// if any of the required properties are missing skip (Name & Value)
if((method != null
&& !request.getMethod().equalsIgnoreCase(method))
|| !headerValues.containsKey(ACC_PROPERTY_CONFIG_HEADER_NAME)
|| !headerValues.containsKey(ACC_PROPERTY_CONFIG_HEADER_VALUE))
continue;
try
{
String name = headerValues.get(ACC_PROPERTY_CONFIG_HEADER_NAME);
String value = processParams(
headerValues.get(ACC_PROPERTY_CONFIG_HEADER_VALUE),
request);
Header customHeader;
// use the custom header for those custom headers that has
// multiple values, as the factory will switch on the header
// parser for standard headers and will produce multiple headers
// that ends with an error creating/sending the request
if(value.contains(","))
{
customHeader = new CustomHeader(name, value);
}
else
{
customHeader = protocolProvider.getHeaderFactory()
.createHeader(name, value);
}
request.setHeader(customHeader);
}
catch(Exception e)
{
logger.error("Cannot create custom header", e);
}
}
}
/**
* Checks for certain params existence in the value, and replace them
* with real values obtained from <tt>request</tt>.
* @param value the value of the header param
* @param request the request we are processing
* @return the value with replaced params
*/
private static String processParams(String value, Request request)
{
if(value.indexOf("${from.address}") != -1)
{
FromHeader fromHeader
= (FromHeader)request.getHeader(FromHeader.NAME);
if(fromHeader != null)
{
value = value.replace(
"${from.address}",
fromHeader.getAddress().getURI().toString());
}
}
if(value.indexOf("${to.address}") != -1)
{
ToHeader toHeader =
(ToHeader)request.getHeader(ToHeader.NAME);
if(toHeader != null)
{
value = value.replace(
"${to.address}",
toHeader.getAddress().getURI().toString());
}
}
if(value.indexOf("${to.userID}") != -1)
{
ToHeader toHeader =
(ToHeader)request.getHeader(ToHeader.NAME);
if(toHeader != null)
{
URI toURI = toHeader.getAddress().getURI();
String toAddr = toURI.toString();
// strips sip: or sips:
if(toURI.isSipURI())
{
toAddr = toAddr.replaceFirst(toURI.getScheme() + ":", "");
}
// take the userID part
int index = toAddr.indexOf('@');
if ( index > -1 )
toAddr = toAddr.substring(0, index);
value = value.replace("${to.userID}", toAddr);
}
}
return value;
}
/**
* Custom header to instert. Custom name and value.
*/
private static class CustomHeader
extends SIPHeader
{
/**
* The header value.
*/
private String value;
/**
* Constructs header.
* @param name header name
* @param value header value
*/
CustomHeader(String name, String value)
{
super(name);
this.value = value;
}
/**
* Just the encoded body of the header.
* @param buffer the insert result
* @return the string encoded header body.
*/
@Override
protected StringBuilder encodeBody(StringBuilder buffer)
{
return value != null ? buffer.append(value) : buffer.append("");
}
}
}
|