Add an endpoint and extension points

This commit is contained in:
MathewFrancis 2025-04-04 18:28:35 +05:30
parent 721f00bdf8
commit 4e6a39bf4a
26 changed files with 274 additions and 2 deletions

View File

@ -13,7 +13,7 @@
"header": [], "header": [],
"body": { "body": {
"mode": "raw", "mode": "raw",
"raw": "{\n \"id\": \"1001\",\n \"transport\": \"transport-udp\",\n \"aors\": \"1001\",\n \"auth\": \"1001\",\n \"context\": \"from-internal\",\n \"disallow\": \"all\",\n \"allow\": \"ulaw\",\n \"directMedia\": \"no\",\n \"connectedLineMethod\": \"invite\",\n \"callerid\": \"User <1001>\",\n \"dtmfMode\": \"rfc4733\",\n \"mohsuggest\": \"default\",\n \"mailboxes\": \"1001@default\"\n}", "raw": "{\n \"id\": \"1004\",\n \"transport\": \"transport-udp\",\n \"aors\": \"1004\",\n \"auth\": \"1004\",\n \"context\": \"default\",\n \"disallow\": \"all\",\n \"allow\": \"ulaw,alaw\",\n \"directMedia\": \"no\",\n \"connectedLineMethod\": \"invite\",\n \"callerid\": \"User <1004>\",\n \"dtmfMode\": \"rfc4733\",\n \"mohsuggest\": \"default\",\n \"mailboxes\": \"1004@default\"\n}\n\n// {\n// \"id\": \"1004\",\n// \"transport\": \"transport-udp\",\n// \"aors\": \"1004\",\n// \"auth\": \"1004\",\n// \"context\": \"default\",\n// \"disallow\": \"all\",\n// \"allow\": \"ulaw,alaw\",\n// \"directMedia\": \"no\",\n// \"connectedLineMethod\": null,\n// \"callerid\": \"User <1004>\",\n// \"dtmfMode\": null,\n// \"mohsuggest\": \"default\",\n// \"mailboxes\": \"1004@default\"\n// }",
"options": { "options": {
"raw": { "raw": {
"language": "json" "language": "json"
@ -34,6 +34,35 @@
} }
}, },
"response": [] "response": []
},
{
"name": "New Request",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": "{\n \"context\": \"default\",\n \"extension\": \"1004\",\n \"priority\": 2,\n \"app\": \"Dial\",\n \"appdata\": \"PJSIP/1004,20,m(default)\"\n}\n",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "http://localhost:8081/cezen/add_extension",
"protocol": "http",
"host": [
"localhost"
],
"port": "8081",
"path": [
"cezen",
"add_extension"
]
}
},
"response": []
} }
] ]
} }

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,41 @@
USE asterisk_db;
SHOW DATABASES;
SHOW TABLES;
SELECT * FROM `ps_endpoints`;
DELETE FROM `ps_endpoints` WHERE `id` = '1004';
SELECT * FROM `extensions_table`;
DESCRIBE `extensions_table`;
ALTER TABLE `extensions_table`
ADD CONSTRAINT `extension_table_unique_val_two_check` UNIQUE (`exten`, `appdata`);
ALTER TABLE `extensions_table`
DROP INDEX `extension_table_unique_val`;
ALTER TABLE `extensions_table`
ADD CONSTRAINT `extension_table_unique_val` UNIQUE (`exten`, `appdata`, `priority`);
DELETE FROM `extensions_table` WHERE priority = 4 and exten = "1005" ;
DELETE FROM `extensions_table` WHERE exten = "1005" OR exten = "1004";
DELETE FROM `ps_endpoints` WHERE id = "1004" OR id = "1005";
SELECT * FROM `extensions_table` WHERE app = "Dial";
--
SELECT * FROM ps_auths;
SELECT * FROM ps_aors;
DESCRIBE `ps_auths`;
DESCRIBE `ps_aors`;
INSERT INTO `ps_aors`(`id`,`max_contacts`) VALUES ("1004", 1);
INSERT INTO `ps_auths`(`id`, `auth_type`, `username`, `password`, `md5_cred`, `realm`) VALUES("1004", "userpass", "1004", "12345", null, null);

View File

@ -1,7 +1,8 @@
package com.example.cezenPBX.DAO; package com.example.cezenPBX.DAO;
import com.example.cezenPBX.DTO.ReturnStatus;
import com.example.cezenPBX.entity.ExtensionsTable;
import com.example.cezenPBX.entity.PsEndPoints; import com.example.cezenPBX.entity.PsEndPoints;
import jakarta.persistence.Entity;
import jakarta.persistence.EntityManager; import jakarta.persistence.EntityManager;
import jakarta.transaction.Transactional; import jakarta.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -14,6 +15,13 @@ public class BasicAsteriskOpsDAO implements CezenPbxOpsDAO {
@Autowired @Autowired
private EntityManager entityManager; private EntityManager entityManager;
// find end point details by id
@Override
public PsEndPoints findEndPontById(String id) {
return this.entityManager.find(PsEndPoints.class, id);
}
// takes the required data from the service class and persists the entity to the database // takes the required data from the service class and persists the entity to the database
// returns true if operation was carried out without an exception // returns true if operation was carried out without an exception
@Override @Override
@ -28,4 +36,22 @@ public class BasicAsteriskOpsDAO implements CezenPbxOpsDAO {
} }
return true; return true;
} }
@Override
@Transactional
public ReturnStatus saveAnExtension(ExtensionsTable extensionsTable) {
// check if endpoint exists
try{
System.out.println("End point ID is " + findEndPontById(extensionsTable.getExtension()).getId());
}catch (Exception e){
return new ReturnStatus(false, "Create Endpoint First for " + extensionsTable.getExtension(),e.toString());
}
System.out.println("Context in DAO is " + extensionsTable.getContext());
this.entityManager.persist(extensionsTable);
return new ReturnStatus(true, extensionsTable.getExtension() + " Persisted ", "");
}
} }

View File

@ -1,8 +1,14 @@
package com.example.cezenPBX.DAO; package com.example.cezenPBX.DAO;
import com.example.cezenPBX.DTO.ReturnStatus;
import com.example.cezenPBX.entity.ExtensionsTable;
import com.example.cezenPBX.entity.PsEndPoints; import com.example.cezenPBX.entity.PsEndPoints;
public interface CezenPbxOpsDAO { public interface CezenPbxOpsDAO {
PsEndPoints findEndPontById(String id);
boolean addNewEndPoint(PsEndPoints psEndPoints); boolean addNewEndPoint(PsEndPoints psEndPoints);
ReturnStatus saveAnExtension(ExtensionsTable extensionsTable);
} }

View File

@ -0,0 +1,4 @@
package com.example.cezenPBX.DTO;
public record ExtensionsDTO (String context, String extension, int priority, String app, String appdata){
}

View File

@ -0,0 +1,4 @@
package com.example.cezenPBX.DTO;
public record ReturnStatus(Boolean status, String message, String exceptionMessage) {
}

View File

@ -1,6 +1,8 @@
package com.example.cezenPBX.controller; package com.example.cezenPBX.controller;
import com.example.cezenPBX.DTO.ExtensionsDTO;
import com.example.cezenPBX.DTO.PsEndPointsDTO; import com.example.cezenPBX.DTO.PsEndPointsDTO;
import com.example.cezenPBX.DTO.ReturnStatus;
import com.example.cezenPBX.service.BasicPbxServiceInterface; import com.example.cezenPBX.service.BasicPbxServiceInterface;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
@ -40,4 +42,16 @@ public class CezenPbxController {
psEndPointsRecordDTO.mailboxes() psEndPointsRecordDTO.mailboxes()
); );
} }
@PostMapping("/add_extension")
public ReturnStatus addANewExtensionToDatabase(@RequestBody ExtensionsDTO extensionsDTO){
System.out.println(extensionsDTO);
return this.basicPbxServiceInterface.addExtensionForUser(
extensionsDTO.context(),
extensionsDTO.extension(),
extensionsDTO.priority(),
extensionsDTO.app(),
extensionsDTO.appdata());
}
} }

View File

@ -0,0 +1,83 @@
package com.example.cezenPBX.entity;
import jakarta.persistence.*;
import jdk.jfr.Name;
@Entity
@Table(name = "extensions_table")
public class ExtensionsTable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
int id;
@Column(name = "context")
String context;
@Column(name = "exten")
String extension;
@Column(name = "priority")
int priority;
@Column(name = "app")
String app;
@Column(name = "appdata")
String appdata;
public ExtensionsTable(){}
public ExtensionsTable(String context, String extension, int priority, String app, String appdata) {
this.context = context;
this.extension = extension;
this.priority = priority;
this.app = app;
this.appdata = appdata;
}
public int getId() {
return id;
}
public String getContext() {
return context;
}
public void setContext(String context) {
this.context = context;
}
public String getExtension() {
return extension;
}
public void setExtension(String extension) {
this.extension = extension;
}
public int getPriority() {
return priority;
}
public void setPriority(int priority) {
this.priority = priority;
}
public String getApp() {
return app;
}
public void setApp(String app) {
this.app = app;
}
public String getAppdata() {
return appdata;
}
public void setAppdata(String appdata) {
this.appdata = appdata;
}
}

View File

@ -1,6 +1,8 @@
package com.example.cezenPBX.service; package com.example.cezenPBX.service;
import com.example.cezenPBX.DAO.CezenPbxOpsDAO; import com.example.cezenPBX.DAO.CezenPbxOpsDAO;
import com.example.cezenPBX.DTO.ReturnStatus;
import com.example.cezenPBX.entity.ExtensionsTable;
import com.example.cezenPBX.entity.PsEndPoints; import com.example.cezenPBX.entity.PsEndPoints;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -43,4 +45,25 @@ public class BasicPbxService implements BasicPbxServiceInterface {
return false; return false;
} }
} }
@Override
public ReturnStatus addExtensionForUser(String context, String extension, int priority, String app, String appdata) {
System.out.println("Context in SERVICE is " + context);
try{
// create a new entity of extension
this.cezenPbxOpsDAO.saveAnExtension(new ExtensionsTable(
context,
extension,
priority,
app,
appdata
));
return new ReturnStatus(true, extension + " Persisted ", "");
}catch ( Exception e){
// the persisting data already exists or it's a db issue
return new ReturnStatus(false, "Data likely already exists or DB issue",e.getMessage());
}
}
} }

View File

@ -1,9 +1,15 @@
package com.example.cezenPBX.service; package com.example.cezenPBX.service;
import com.example.cezenPBX.DTO.ReturnStatus;
public interface BasicPbxServiceInterface { public interface BasicPbxServiceInterface {
public boolean createANewEndpointService(String id, String transport, String aors, String auth, public boolean createANewEndpointService(String id, String transport, String aors, String auth,
String context, String disallow, String allow, String directMedia, String context, String disallow, String allow, String directMedia,
String connectedLineMethod, String callerid, String dtmfMode, String connectedLineMethod, String callerid, String dtmfMode,
String mohsuggest, String mailboxes); String mohsuggest, String mailboxes);
// provided the user exists returns message of error or status
public ReturnStatus addExtensionForUser(String context, String extension, int priority, String app, String appdata);
} }

View File

@ -0,0 +1,18 @@
;
; Message Information file
;
[message]
origmailbox=1002
context=default
macrocontext=
exten=1002
rdnis=unknown
priority=30
callerchan=PJSIP/1003-00000000
callerid="mat@1003" <1003>
origdate=Fri Apr 4 12:21:21 PM UTC 2025
origtime=1743769281
category=
msg_id=1743769281-00000000
flag=
duration=0

View File

@ -0,0 +1,18 @@
;
; Message Information file
;
[message]
origmailbox=1002
context=default
macrocontext=
exten=1002
rdnis=unknown
priority=30
callerchan=PJSIP/1004-00000005
callerid="User" <1004>
origdate=Fri Apr 4 12:49:20 PM UTC 2025
origtime=1743770960
category=
msg_id=1743770960-00000001
flag=
duration=0