Source code: com/opencloud/slee/services/sip/common/MessageHandler.java
1 package com.opencloud.slee.services.sip.common;
2
3 import java.io.*;
4 import java.util.*;
5
6 import javax.naming.*;
7 import javax.sip.address.URI;
8 import javax.sip.address.Address;
9 import javax.sip.header.HeaderAddress;
10 import javax.sip.message.Response;
11 import javax.sip.message.Request;
12 import javax.sip.ServerTransaction;
13 /*
14 import com.opencloud.slee.resources.jainsiplocal.JainSipFactoryProvider;
15 import com.opencloud.util.sip.*;
16 import com.opencloud.rhino.testapps.sipratestapps.profile.SipConfigProfileCMP;
17 */
18 /**
19 * Abstract class for handling SIP requests/responses in SBBs. Provides access
20 * to JAIN SIP resources (SipProvider, Address/Header/Message Factories,
21 * Location Service) via the SipServerConfig interface and various utility methods.
22 * Subclass to handle particular types of messages.
23 */
24 public abstract class MessageHandler {
25
26 protected SipServerConfig config;
27
28 public MessageHandler(SipServerConfig config) {
29 this.config = config;
30 }
31
32 // bean methods
33 public abstract void processRequest(ServerTransaction txn, Request request);
34 public abstract void processResponse(ServerTransaction txn, Response response);
35
36 // utility methods
37
38 /**
39 * returns string in form "sip:user@domain"
40 */
41 public static String getCanonicalAddress(HeaderAddress header) {
42 Address na = header.getAddress();
43
44 URI uri = na.getURI();
45
46 String addr = uri.toString();
47
48 // the URI may contain sip:user@host:port;transport=etc etc
49 // we want everything upto and including the host
50 int index = addr.indexOf(':');
51 index = addr.indexOf(':', index+1);
52 if (index != -1) {
53 // Strip off port and any optional tags following the host part of the URI
54 addr = addr.substring(0, index);
55 }
56
57 return addr;
58 }
59
60 public boolean isLocalDomain(URI uri) {
61 //System.out.println("isLocalDomain");
62 // Is the request-uri for this domain
63 // eg. sip:opencloud.com
64
65 boolean belongs = false;
66 String uriDomain = getDomain(uri);
67 String[] localDomainNames = config.getLocalDomainNames();
68 for (int i = 0; i < localDomainNames.length; i++ ) {
69 belongs = belongsToDomain(uriDomain, localDomainNames[i]);
70 if (belongs) break;
71 }
72
73 return belongs;
74 }
75
76 private boolean belongsToDomain(String uriDomain, String domainSpec) {
77 switch (domainSpec.indexOf("*.")) {
78 // Case 1, the configured local domain contains no wildcards, e.g. simply 'opencloud.com'
79 case -1: return domainSpec.equalsIgnoreCase(uriDomain);
80 // Case 2, the configured local domain contains a wildcard, e.g. '*.opencloud.com'
81 // only allow this at the start of the domain spec and only in the form *.domain
82 case 0: return uriDomain.toLowerCase().endsWith(domainSpec.substring(2).toLowerCase());
83 // Ignore local domains specified in any other format
84 default:
85 System.err.println("I don't understand the format of local domain "+domainSpec);
86 return false;
87 }
88 }
89
90 public String getDomain(URI uri) {
91 String address = uri.toString();
92
93 // strip off user part, if any. eg. ben@opencloud.com -> opencloud.com
94 int index = address.indexOf('@');
95 if (index != -1) address = address.substring(index+1);
96
97 // Strip off protocol part (NIST SIP 1.1 getSchemeData() no longer available and toString() returns _whole_ URI!)
98 // If there was a user part stripped above it will have been after the protocol part so we wouldn't expect to see a protocol part here
99 index = address.indexOf(':');
100 if (index != -1) address = address.substring(index+1);
101
102 // Lastly possibly strip off the port and any subsequent tags
103 // This is the fix for bugs caused by the REGISTER sip:host:5060;transport=UDP line seen with some SIP UA'a
104 index = address.indexOf(':');
105 if (index != -1) address = address.substring(0, index);
106
107 return address;
108 }
109
110 public boolean isSupportedURIScheme(URI uri) {
111
112 String uriScheme = uri.getScheme();
113 String[] supportedURISchemes = config.getSupportedURISchemes();
114
115 for (int i = 0; i < supportedURISchemes.length; i++ ) {
116 if (supportedURISchemes[i].equalsIgnoreCase(uriScheme)) return true;
117 }
118
119 return false;
120 }
121
122 }