Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

make REST API distributed

-configure postgresql to be accessed from every node in the cluster
-add posgres Address API
-replicate postgres user and address in sipxrest-config
-change sipXcdr to use replicated address
-fix callcontroller to query mongo from secondary
  • Loading branch information...
commit b642b338663c503d8afeff10984543e85159b7ff 1 parent 15f576d
@mirceac mirceac authored
View
22 sipXcallController/src/org/sipfoundry/callcontroller/CallControllerRestlet.java
@@ -7,6 +7,8 @@
import gov.nist.javax.sip.clientauthutils.UserCredentialHash;
+import java.net.UnknownHostException;
+
import javax.sip.Dialog;
import org.apache.log4j.Logger;
@@ -18,8 +20,8 @@
import org.restlet.data.Response;
import org.restlet.data.Status;
import org.sipfoundry.commons.userdb.User;
+import org.sipfoundry.commons.util.UnfortunateLackOfSpringSupportFactory;
import org.sipfoundry.sipxrest.RestServer;
-import org.sipfoundry.sipxrest.SipHelper;
public class CallControllerRestlet extends Restlet {
@@ -41,6 +43,14 @@ public CallControllerRestlet(Context context) {
@Override
public void handle(Request request, Response response) {
+
+ String configDir = System.getProperties().getProperty("conf.dir", "/etc/sipxpbx");
+ try{
+ UnfortunateLackOfSpringSupportFactory.initialize(configDir + "/mongo-client.ini");
+ } catch (UnknownHostException e) {
+ logger.error("Cannot init mongo", e);
+ }
+
try {
Method httpMethod = request.getMethod();
if (!httpMethod.equals(Method.POST) && !httpMethod.equals(Method.GET)) {
@@ -54,7 +64,7 @@ public void handle(Request request, Response response) {
String agentName = (String) request.getAttributes().get(CallControllerParams.AGENT);
String method = (String) request.getAttributes().get(CallControllerParams.METHOD);
-
+
logger.debug("sipMethod = " + method);
String callingParty = (String) request.getAttributes().get(
@@ -149,13 +159,13 @@ public void handle(Request request, Response response) {
response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
return;
}
-
+
String action = (String) request.getAttributes().get(CallControllerParams.ACTION);
-
+
if ( action == null ) {
action = CallControllerParams.CALL;
}
-
+
if ( action.equals(CallControllerParams.CALL)) {
if (method.equalsIgnoreCase(CallControllerParams.REFER)) {
DialogContext dialogContext = SipUtils.createDialogContext(key, timeout,
@@ -196,7 +206,7 @@ public void handle(Request request, Response response) {
Status.CLIENT_ERROR_BAD_REQUEST.getCode(), "Need a transfer target parameter");
response.setEntity(result, MediaType.TEXT_XML);
response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
- return;
+ return;
}
if ( target.indexOf("@") == -1) {
target = target + "@" + RestServer.getRestServerConfig().getSipxProxyDomain();
View
26 sipXcdrLog/src/org/sipfoundry/sipxrest/cdrlog/CdrLogRestlet.java
@@ -5,47 +5,34 @@
*/
package org.sipfoundry.sipxrest.cdrlog;
-import java.sql.Driver;
import java.sql.Connection;
import java.sql.DriverManager;
-import java.sql.ResultSet;
-import java.sql.ResultSetMetaData;
-import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
+import java.sql.ResultSetMetaData;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
-import java.util.GregorianCalendar;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.transform.*;
-import javax.xml.transform.stream.*;
-import javax.xml.transform.dom.*;
import org.apache.log4j.Logger;
-import org.restlet.Context;
import org.restlet.Restlet;
-import org.restlet.Route;
-import org.restlet.Router;
import org.restlet.data.MediaType;
import org.restlet.data.Method;
import org.restlet.data.Request;
import org.restlet.data.Response;
import org.restlet.data.Status;
import org.restlet.resource.DomRepresentation;
-
-import org.sipfoundry.sipxrest.*;
-
+import org.sipfoundry.commons.restconfig.RestServerConfig;
+import org.sipfoundry.sipxrest.RestServer;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class CdrLogRestlet extends Restlet {
private static Logger logger = Logger.getLogger(CdrLogRestlet.class);
- private static String serviceName = "cdr";
- private String cdrDBUrl = "jdbc:postgresql:SIPXCDR";
private String sqlStmt = "SELECT caller_aor, callee_aor, callee_contact, start_time, (end_time - connect_time) AS duration, termination, callee_route from cdrs";
private String sqlUserWhereStmt = "WHERE (caller_aor LIKE ? OR callee_aor LIKE ? ) ";
private String sqlFromDateStmt = "AND start_time > ? ";
@@ -61,6 +48,11 @@ public void handle(Request request, Response response) {
Connection cdrConnection = null;
ResultSet qResults = null;
PreparedStatement qStatement = null;
+
+ RestServerConfig config = RestServer.getRestServerConfig();
+ String cdrDBUrl = config.getSipxcdrAddress();
+ String dbUser = config.getDbUser();
+
try {
Method httpMethod = request.getMethod();
if (!httpMethod.equals(Method.GET)) {
@@ -113,7 +105,7 @@ public void handle(Request request, Response response) {
System.setProperty("jdbc.drivers", "org.postgresql.Driver");
// Establish a connection to the CDR database.
- cdrConnection = DriverManager.getConnection(cdrDBUrl, "postgres", "");
+ cdrConnection = DriverManager.getConnection(cdrDBUrl, dbUser, "");
String sqlPrepareString;
String userLike = "%:" + userId + "@%";
View
18 sipXcommons/src/main/java/org/sipfoundry/commons/restconfig/RestServerConfig.java
@@ -17,6 +17,8 @@
private String loggingLevel = Level.INFO.toString();
private String logDirectory = "/var/log/sipxpbx/";
private String sipxProxyDomain;
+ private String sipxcdrAddress;
+ private String dbUser;
public void setIpAddress(String ipAddress) {
this.ipAddress = ipAddress;
@@ -74,6 +76,22 @@ public String getSipxProxyDomain() {
return sipxProxyDomain;
}
+ public String getSipxcdrAddress() {
+ return sipxcdrAddress;
+ }
+
+ public void setSipxcdrAddress(String sipxcdrAddress) {
+ this.sipxcdrAddress = sipxcdrAddress;
+ }
+
+ public String getDbUser() {
+ return dbUser;
+ }
+
+ public void setDbUser(String dbUser) {
+ this.dbUser = dbUser;
+ }
+
public int getCacheTimeout() {
return 30;
}
View
2  sipXcommons/src/main/java/org/sipfoundry/commons/restconfig/RestServerConfigFileParser.java
@@ -29,6 +29,8 @@ private static void addRules(Digester digester) {
digester.addCallMethod( String.format("%s/%s", REST_CONFIG,"sipx-proxy-domain"), "setSipxProxyDomain",0);
digester.addCallMethod( String.format("%s/%s", REST_CONFIG,"log-directory"), "setLogDirectory",0);
digester.addCallMethod( String.format("%s/%s", REST_CONFIG,"log-level"), "setLogLevel",0);
+ digester.addCallMethod( String.format("%s/%s", REST_CONFIG,"sipxcdr-address"), "setSipxcdrAddress", 0);
+ digester.addCallMethod( String.format("%s/%s", REST_CONFIG,"db-user"), "setDbUser", 0);
}
public RestServerConfig parse(String url) {
View
2  sipXconfig/etc/sipxpbx/sipxrest/sipxrest-config.vm
@@ -7,4 +7,6 @@
<log-directory>@SIPX_LOGDIR@</log-directory>
<log-level>$settings.getSetting('log.level').Value</log-level>
<sip-port>$settings.getSetting('sipPort').Value</sip-port>
+ <sipxcdr-address>${sipxcdrDbAddress}</sipxcdr-address>
+ <db-user>@POSTGRESQL_USER@</db-user>
</rest-config>
View
1  sipXconfig/neoconf/src/org/sipfoundry/sipxconfig/admin/AdminContext.java
@@ -18,6 +18,7 @@
public static final LocationFeature FEATURE = new LocationFeature("admin");
public static final AddressType HTTP_ADDRESS = new AddressType("adminApi", URL_FORMAT);
public static final AddressType HTTP_ADDRESS_AUTH = new AddressType("adminApiAuth", URL_FORMAT);
+ public static final AddressType SIPXCDR_DB_ADDRESS = new AddressType("sipxcdr", "jdbc:postgresql://%s/SIPXCDR");
public static final AlarmDefinition ALARM_LOGIN_FAILED = new AlarmDefinition("LOGIN_FAILED", 3);
public static final String ARCHIVE = "configuration.tar.gz";
final String CONTEXT_BEAN_NAME = "adminContext";
View
4 sipXconfig/neoconf/src/org/sipfoundry/sipxconfig/admin/AdminContextImpl.java
@@ -43,7 +43,7 @@
public class AdminContextImpl extends HibernateDaoSupport implements AdminContext, AddressProvider, ProcessProvider,
AlarmProvider, FirewallProvider, ArchiveProvider {
private static final Collection<AddressType> ADDRESSES =
- Arrays.asList(new AddressType[] {HTTP_ADDRESS, HTTP_ADDRESS_AUTH});
+ Arrays.asList(new AddressType[] {HTTP_ADDRESS, HTTP_ADDRESS_AUTH, SIPXCDR_DB_ADDRESS});
private LocationsManager m_locationsManager;
private int m_internalPort;
private int m_internalPortAuth;
@@ -60,6 +60,8 @@
address = new Address(HTTP_ADDRESS, location.getAddress(), m_internalPort);
} else if (type.equals(HTTP_ADDRESS_AUTH)) {
address = new Address(HTTP_ADDRESS, location.getAddress(), m_internalPortAuth);
+ } else if (type.equals(SIPXCDR_DB_ADDRESS)) {
+ address = new Address(SIPXCDR_DB_ADDRESS, location.getAddress());
}
return Collections.singleton(address);
}
View
9 sipXconfig/neoconf/src/org/sipfoundry/sipxconfig/restserver/RestConfiguration.java
@@ -25,6 +25,8 @@
import org.apache.commons.io.IOUtils;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
+import org.sipfoundry.sipxconfig.address.Address;
+import org.sipfoundry.sipxconfig.admin.AdminContext;
import org.sipfoundry.sipxconfig.cfgmgt.ConfigManager;
import org.sipfoundry.sipxconfig.cfgmgt.ConfigProvider;
import org.sipfoundry.sipxconfig.cfgmgt.ConfigRequest;
@@ -44,6 +46,7 @@ public void replicate(ConfigManager manager, ConfigRequest request) throws IOExc
}
RestServerSettings settings = m_restServer.getSettings();
+ Address sipxcdrApi = manager.getAddressManager().getSingleAddress(AdminContext.SIPXCDR_DB_ADDRESS);
for (Location location : locations) {
File dir = manager.getLocationDataDirectory(location);
boolean enabled = manager.getFeatureManager().isFeatureEnabled(RestServer.FEATURE);
@@ -53,18 +56,20 @@ public void replicate(ConfigManager manager, ConfigRequest request) throws IOExc
}
Writer wtr = new FileWriter(new File(dir, "sipxrest-config.xml"));
try {
- write(wtr, settings, location, manager.getDomainManager().getDomain());
+ write(wtr, settings, location, manager.getDomainManager().getDomain(), sipxcdrApi);
} finally {
IOUtils.closeQuietly(wtr);
}
}
}
- void write(Writer wtr, RestServerSettings settings, Location location, Domain domain) throws IOException {
+ void write(Writer wtr, RestServerSettings settings, Location location,
+ Domain domain, Address sipxcdrApi) throws IOException {
VelocityContext context = new VelocityContext();
context.put("settings", settings.getSettings().getSetting("rest-config"));
context.put("location", location);
context.put("domainName", domain.getName());
+ context.put("sipxcdrDbAddress", sipxcdrApi.toString());
try {
m_velocityEngine.mergeTemplate("sipxrest/sipxrest-config.vm", context, wtr);
} catch (Exception e) {
View
5 ...config/neoconf/test/org/sipfoundry/sipxconfig/restserver/RestServerConfigurationTest.java
@@ -22,6 +22,8 @@
import org.apache.commons.io.IOUtils;
import org.junit.Test;
+import org.sipfoundry.sipxconfig.address.Address;
+import org.sipfoundry.sipxconfig.admin.AdminContext;
import org.sipfoundry.sipxconfig.commserver.Location;
import org.sipfoundry.sipxconfig.domain.Domain;
import org.sipfoundry.sipxconfig.test.TestHelper;
@@ -38,7 +40,8 @@ public void testConfig() throws Exception {
Domain domain = new Domain("example.org");
Location location = TestHelper.createDefaultLocation();
StringWriter actual = new StringWriter();
- config.write(actual, settings, location, domain);
+ Address cdrAddress = new Address(AdminContext.SIPXCDR_DB_ADDRESS, "master.example.com");
+ config.write(actual, settings, location, domain, cdrAddress);
String expected = IOUtils.toString(getClass().getResourceAsStream("expected-sipxrest-config"));
assertEquals(expected, actual.toString());
}
View
2  sipXconfig/neoconf/test/org/sipfoundry/sipxconfig/restserver/expected-sipxrest-config
@@ -7,4 +7,6 @@
<log-directory>@SIPX_LOGDIR@</log-directory>
<log-level>NOTICE</log-level>
<sip-port>6050</sip-port>
+ <sipxcdr-address>jdbc:postgresql://master.example.com/SIPXCDR</sipxcdr-address>
+ <db-user>@POSTGRESQL_USER@</db-user>
</rest-config>
View
4 sipXpostgres/etc/postgres.cf
@@ -92,6 +92,8 @@ bundle agent postgres_config {
bundle edit_line postgresql_conf {
insert_lines:
any::
+ # make sure any node can connect to posgresql
+ "listen_addresses='*'";
# makes a difference when row counts get higher both
# in users table and cdrs. A database that has not
# been vacuumed will take 20-30 minutes before it will
@@ -123,6 +125,8 @@ bundle edit_line pg_hba_conf {
"local all all trust";
"host all all 127.0.0.1/32 trust";
"host all all ::1/128 trust";
+ # any node in the cluster can connect to postgresql
+ "host all all 0.0.0.0/0 trust";
delete_lines:
"host.*";
"local.*";
Please sign in to comment.
Something went wrong with that request. Please try again.