处理获取消息体内容为空时造成的空指针异常

This commit is contained in:
648540858 2022-08-17 16:45:06 +08:00
parent aa7ab1e479
commit 46317f896b
13 changed files with 127 additions and 113 deletions

View File

@ -231,6 +231,9 @@ public abstract class SIPRequestProcessorParent {
byte destBye = (byte) despChar;
List<Byte> result = new ArrayList<>();
byte[] rawContent = request.getRawContent();
if (rawContent == null) {
return null;
}
for (int i = 0; i < rawContent.length; i++) {
if (rawContent[i] == destBye) {
boolean resul = false;

View File

@ -41,7 +41,7 @@ import java.util.Iterator;
import java.util.concurrent.ConcurrentLinkedQueue;
/**
* SIP命令类型 NOTIFY请求
* SIP命令类型 NOTIFY请求,这是作为上级发送订阅请求后设备才会响应的
*/
@Component
public class NotifyRequestProcessor extends SIPRequestProcessorParent implements InitializingBean, ISIPRequestProcessor {
@ -198,6 +198,7 @@ public class NotifyRequestProcessor extends SIPRequestProcessorParent implements
}
storager.updateChannelPosition(deviceChannel);
// 发送redis消息 通知位置信息的变化
JSONObject jsonObject = new JSONObject();
jsonObject.put("time", time);
@ -237,6 +238,10 @@ public class NotifyRequestProcessor extends SIPRequestProcessorParent implements
return;
}
rootElement = getRootElement(evt, device.getCharset());
if (rootElement == null) {
logger.warn("[ NotifyAlarm ] content cannot be null");
return;
}
DeviceAlarm deviceAlarm = new DeviceAlarm();
deviceAlarm.setDeviceId(deviceId);
deviceAlarm.setAlarmPriority(XmlUtil.getText(rootElement, "AlarmPriority"));
@ -272,8 +277,6 @@ public class NotifyRequestProcessor extends SIPRequestProcessorParent implements
mobilePosition.setLatitude(deviceAlarm.getLatitude());
mobilePosition.setReportSource("GPS Alarm");
// 更新device channel 的经纬度
DeviceChannel deviceChannel = new DeviceChannel();
deviceChannel.setDeviceId(device.getDeviceId());
@ -294,6 +297,18 @@ public class NotifyRequestProcessor extends SIPRequestProcessorParent implements
}
storager.updateChannelPosition(deviceChannel);
// 发送redis消息 通知位置信息的变化
JSONObject jsonObject = new JSONObject();
jsonObject.put("time", mobilePosition.getTime());
jsonObject.put("serial", deviceChannel.getDeviceId());
jsonObject.put("code", deviceChannel.getChannelId());
jsonObject.put("longitude", mobilePosition.getLongitude());
jsonObject.put("latitude", mobilePosition.getLatitude());
jsonObject.put("altitude", mobilePosition.getAltitude());
jsonObject.put("direction", mobilePosition.getDirection());
jsonObject.put("speed", mobilePosition.getSpeed());
redisCatchStorage.sendMobilePositionMsg(jsonObject);
}
// TODO: 需要实现存储报警信息报警分类
@ -322,6 +337,10 @@ public class NotifyRequestProcessor extends SIPRequestProcessorParent implements
return;
}
Element rootElement = getRootElement(evt, device.getCharset());
if (rootElement == null) {
logger.warn("[ 收到目录订阅 ] content cannot be null");
return;
}
Element deviceListElement = rootElement.element("DeviceList");
if (deviceListElement == null) {
return;

View File

@ -7,7 +7,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* 命令类型 通知命令
* 命令类型 通知命令 参看 A.2.5 通知命令
* 命令类型 状态信息(心跳)报送, 报警通知, 媒体通知, 移动设备位置数据语音广播通知(TODO), 设备预置位(TODO)
* @author lin
*/

View File

@ -1,5 +1,6 @@
package com.genersoft.iot.vmp.gb28181.transmit.event.request.impl.message.notify.cmd;
import com.alibaba.fastjson.JSONObject;
import com.genersoft.iot.vmp.conf.SipConfig;
import com.genersoft.iot.vmp.conf.UserSetting;
import com.genersoft.iot.vmp.gb28181.bean.*;
@ -32,6 +33,9 @@ import java.text.ParseException;
import static com.genersoft.iot.vmp.gb28181.utils.XmlUtil.*;
/**
* 报警事件的处理参考9.4
*/
@Component
public class AlarmNotifyMessageHandler extends SIPRequestProcessorParent implements InitializingBean, IMessageHandler {
@ -73,12 +77,8 @@ public class AlarmNotifyMessageHandler extends SIPRequestProcessorParent impleme
// 回复200 OK
try {
responseAck(evt, Response.OK);
} catch (SipException e) {
throw new RuntimeException(e);
} catch (InvalidArgumentException e) {
throw new RuntimeException(e);
} catch (ParseException e) {
throw new RuntimeException(e);
} catch (SipException | InvalidArgumentException | ParseException e) {
logger.error("[收到报警通知], 回复200OK失败", e);
}
Element deviceIdElement = rootElement.element("DeviceID");
@ -124,7 +124,6 @@ public class AlarmNotifyMessageHandler extends SIPRequestProcessorParent impleme
mobilePosition.setLatitude(deviceAlarm.getLatitude());
mobilePosition.setReportSource("GPS Alarm");
// 更新device channel 的经纬度
DeviceChannel deviceChannel = new DeviceChannel();
deviceChannel.setDeviceId(device.getDeviceId());
@ -144,6 +143,18 @@ public class AlarmNotifyMessageHandler extends SIPRequestProcessorParent impleme
storager.insertMobilePosition(mobilePosition);
}
storager.updateChannelPosition(deviceChannel);
// 发送redis消息 通知位置信息的变化
JSONObject jsonObject = new JSONObject();
jsonObject.put("time", mobilePosition.getTime());
jsonObject.put("serial", deviceChannel.getDeviceId());
jsonObject.put("code", deviceChannel.getChannelId());
jsonObject.put("longitude", mobilePosition.getLongitude());
jsonObject.put("latitude", mobilePosition.getLatitude());
jsonObject.put("altitude", mobilePosition.getAltitude());
jsonObject.put("direction", mobilePosition.getDirection());
jsonObject.put("speed", mobilePosition.getSpeed());
redisCatchStorage.sendMobilePositionMsg(jsonObject);
}
}
if (!StringUtils.isEmpty(deviceAlarm.getDeviceId())) {
@ -159,7 +170,6 @@ public class AlarmNotifyMessageHandler extends SIPRequestProcessorParent impleme
alarmChannelMessage.setAlarmDescription(deviceAlarm.getAlarmDescription());
alarmChannelMessage.setGbId(channelId);
redisCatchStorage.sendAlarmMsg(alarmChannelMessage);
return;
}
@ -169,7 +179,6 @@ public class AlarmNotifyMessageHandler extends SIPRequestProcessorParent impleme
deviceAlarmService.add(deviceAlarm);
}
if (redisCatchStorage.deviceIsOnline(device.getDeviceId())) {
publisher.deviceAlarmEventPublish(deviceAlarm);
}

View File

@ -1,88 +0,0 @@
package com.genersoft.iot.vmp.gb28181.transmit.event.request.impl.message.notify.cmd;
import com.genersoft.iot.vmp.gb28181.bean.*;
import com.genersoft.iot.vmp.gb28181.transmit.callback.DeferredResultHolder;
import com.genersoft.iot.vmp.gb28181.transmit.cmd.impl.SIPCommanderFroPlatform;
import com.genersoft.iot.vmp.gb28181.transmit.event.request.SIPRequestProcessorParent;
import com.genersoft.iot.vmp.gb28181.transmit.event.request.impl.message.IMessageHandler;
import com.genersoft.iot.vmp.gb28181.transmit.event.request.impl.message.notify.NotifyMessageHandler;
import com.genersoft.iot.vmp.storager.IVideoManagerStorage;
import org.dom4j.Element;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.sip.InvalidArgumentException;
import javax.sip.RequestEvent;
import javax.sip.SipException;
import javax.sip.header.FromHeader;
import javax.sip.message.Response;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
@Component
public class CatalogNotifyMessageHandler extends SIPRequestProcessorParent implements InitializingBean, IMessageHandler {
private final String cmdType = "Catalog";
@Autowired
private NotifyMessageHandler notifyMessageHandler;
@Autowired
private IVideoManagerStorage storage;
@Autowired
private SIPCommanderFroPlatform cmderFroPlatform;
@Override
public void afterPropertiesSet() throws Exception {
notifyMessageHandler.addHandler(cmdType, this);
}
@Override
public void handForDevice(RequestEvent evt, Device device, Element element) {
}
@Override
public void handForPlatform(RequestEvent evt, ParentPlatform parentPlatform, Element rootElement) {
String key = DeferredResultHolder.CALLBACK_CMD_CATALOG + parentPlatform.getServerGBId();
FromHeader fromHeader = (FromHeader) evt.getRequest().getHeader(FromHeader.NAME);
try {
// 回复200 OK
responseAck(evt, Response.OK);
Element snElement = rootElement.element("SN");
String sn = snElement.getText();
// 准备回复通道信息
List<DeviceChannelInPlatform> deviceChannels = storage.queryChannelListInParentPlatform(parentPlatform.getServerGBId());
// 查询关联的直播通道
List<DeviceChannel> gbStreams = storage.queryGbStreamListInPlatform(parentPlatform.getServerGBId());
// 回复目录信息
List<DeviceChannel> catalogs = storage.queryCatalogInPlatform(parentPlatform.getServerGBId());
List<DeviceChannel> allChannels = new ArrayList<>();
if (catalogs.size() > 0) {
allChannels.addAll(catalogs);
}
// 回复级联的通道
if (deviceChannels.size() > 0) {
allChannels.addAll(deviceChannels);
}
// 回复直播的通道
if (gbStreams.size() > 0) {
allChannels.addAll(gbStreams);
}
if (allChannels.size() > 0) {
cmderFroPlatform.catalogQuery(allChannels, parentPlatform, sn, fromHeader.getTag());
}else {
// 回复无通道
cmderFroPlatform.catalogQuery(null, parentPlatform, sn, fromHeader.getTag(), 0);
}
} catch (SipException | InvalidArgumentException | ParseException e) {
e.printStackTrace();
}
}
}

View File

@ -25,6 +25,9 @@ import javax.sip.header.ViaHeader;
import javax.sip.message.Response;
import java.text.ParseException;
/**
* 状态信息(心跳)报送
*/
@Component
public class KeepaliveNotifyMessageHandler extends SIPRequestProcessorParent implements InitializingBean, IMessageHandler {

View File

@ -29,6 +29,9 @@ import java.text.ParseException;
import static com.genersoft.iot.vmp.gb28181.utils.XmlUtil.getText;
/**
* 媒体通知
*/
@Component
public class MediaStatusNotifyMessageHandler extends SIPRequestProcessorParent implements InitializingBean, IMessageHandler {

View File

@ -1,16 +1,16 @@
package com.genersoft.iot.vmp.gb28181.transmit.event.request.impl.message.notify.cmd;
import com.alibaba.fastjson.JSONObject;
import com.genersoft.iot.vmp.conf.UserSetting;
import com.genersoft.iot.vmp.gb28181.bean.*;
import com.genersoft.iot.vmp.gb28181.transmit.event.request.SIPRequestProcessorParent;
import com.genersoft.iot.vmp.gb28181.transmit.event.request.impl.message.IMessageHandler;
import com.genersoft.iot.vmp.gb28181.transmit.event.request.impl.message.notify.NotifyMessageHandler;
import com.genersoft.iot.vmp.gb28181.utils.Coordtransform;
import com.genersoft.iot.vmp.gb28181.utils.NumericUtil;
import com.genersoft.iot.vmp.service.IDeviceChannelService;
import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
import com.genersoft.iot.vmp.storager.IVideoManagerStorage;
import com.genersoft.iot.vmp.utils.DateUtil;
import com.genersoft.iot.vmp.utils.GpsUtil;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.slf4j.Logger;
@ -28,6 +28,9 @@ import java.text.ParseException;
import static com.genersoft.iot.vmp.gb28181.utils.XmlUtil.getText;
/**
* 移动设备位置数据通知设备主动发起不需要上级订阅
*/
@Component
public class MobilePositionNotifyMessageHandler extends SIPRequestProcessorParent implements InitializingBean, IMessageHandler {
@ -43,6 +46,9 @@ public class MobilePositionNotifyMessageHandler extends SIPRequestProcessorParen
@Autowired
private IVideoManagerStorage storager;
@Autowired
private IRedisCatchStorage redisCatchStorage;
@Autowired
private IDeviceChannelService deviceChannelService;
@ -56,7 +62,11 @@ public class MobilePositionNotifyMessageHandler extends SIPRequestProcessorParen
try {
rootElement = getRootElement(evt, device.getCharset());
if (rootElement == null) {
logger.warn("[ 移动设备位置数据通知 ] content cannot be null");
responseAck(evt, Response.BAD_REQUEST);
return;
}
MobilePosition mobilePosition = new MobilePosition();
mobilePosition.setCreateTime(DateUtil.getNow());
if (!StringUtils.isEmpty(device.getName())) {
@ -106,6 +116,19 @@ public class MobilePositionNotifyMessageHandler extends SIPRequestProcessorParen
storager.updateChannelPosition(deviceChannel);
//回复 200 OK
responseAck(evt, Response.OK);
// 发送redis消息 通知位置信息的变化
JSONObject jsonObject = new JSONObject();
jsonObject.put("time", mobilePosition.getTime());
jsonObject.put("serial", deviceChannel.getDeviceId());
jsonObject.put("code", deviceChannel.getChannelId());
jsonObject.put("longitude", mobilePosition.getLongitude());
jsonObject.put("latitude", mobilePosition.getLatitude());
jsonObject.put("altitude", mobilePosition.getAltitude());
jsonObject.put("direction", mobilePosition.getDirection());
jsonObject.put("speed", mobilePosition.getSpeed());
redisCatchStorage.sendMobilePositionMsg(jsonObject);
} catch (DocumentException | SipException | InvalidArgumentException | ParseException e) {
e.printStackTrace();
}

View File

@ -95,12 +95,16 @@ public class CatalogResponseMessageHandler extends SIPRequestProcessorParent imp
HandlerCatchData take = taskQueue.poll();
try {
Element rootElement = getRootElement(take.getEvt(), take.getDevice().getCharset());
if (rootElement == null) {
logger.warn("[ 收到通道 ] content cannot be null");
continue;
}
Element deviceListElement = rootElement.element("DeviceList");
Element sumNumElement = rootElement.element("SumNum");
Element snElement = rootElement.element("SN");
if (snElement == null || sumNumElement == null || deviceListElement == null) {
responseAck(take.getEvt(), Response.BAD_REQUEST, "xml error");
return;
continue;
}
int sumNum = Integer.parseInt(sumNumElement.getText());
@ -129,7 +133,8 @@ public class CatalogResponseMessageHandler extends SIPRequestProcessorParent imp
catalogDataCatch.put(take.getDevice().getDeviceId(), sn, sumNum, take.getDevice(), channelList);
logger.info("[收到通道]设备: {} -> {}个,{}/{}", take.getDevice().getDeviceId(), channelList.size(), catalogDataCatch.get(take.getDevice().getDeviceId()) == null ? 0 :catalogDataCatch.get(take.getDevice().getDeviceId()).size(), sumNum);
if (catalogDataCatch.get(take.getDevice().getDeviceId()).size() == sumNum) {
// 数据已经完整接收
// 数据已经完整接收 此时可能存在某个设备离线变上线的情况但是考虑到性能此处不做处理
// 目前支持设备通道上线通知时和设备上线时向上级通知
boolean resetChannelsResult = storager.resetChannels(take.getDevice().getDeviceId(), catalogDataCatch.get(take.getDevice().getDeviceId()));
if (!resetChannelsResult) {
String errorMsg = "接收成功,写入失败,共" + sumNum + "条,已接收" + catalogDataCatch.get(take.getDevice().getDeviceId()).size() + "";

View File

@ -75,6 +75,11 @@ public class DeviceInfoResponseMessageHandler extends SIPRequestProcessorParent
}
try {
rootElement = getRootElement(evt, device.getCharset());
if (rootElement == null) {
logger.warn("[ 接收到DeviceInfo应答消息 ] content cannot be null");
responseAck(evt, Response.BAD_REQUEST);
return;
}
Element deviceIdElement = rootElement.element("DeviceID");
String channelId = deviceIdElement.getTextTrim();
String key = DeferredResultHolder.CALLBACK_CMD_DEVICEINFO + device.getDeviceId() + channelId;

View File

@ -1,5 +1,6 @@
package com.genersoft.iot.vmp.gb28181.transmit.event.request.impl.message.response.cmd;
import com.alibaba.fastjson.JSONObject;
import com.genersoft.iot.vmp.conf.UserSetting;
import com.genersoft.iot.vmp.gb28181.bean.*;
import com.genersoft.iot.vmp.gb28181.transmit.event.request.SIPRequestProcessorParent;
@ -8,6 +9,7 @@ import com.genersoft.iot.vmp.gb28181.transmit.event.request.impl.message.respons
import com.genersoft.iot.vmp.gb28181.utils.Coordtransform;
import com.genersoft.iot.vmp.gb28181.utils.NumericUtil;
import com.genersoft.iot.vmp.service.IDeviceChannelService;
import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
import com.genersoft.iot.vmp.storager.IVideoManagerStorage;
import com.genersoft.iot.vmp.utils.DateUtil;
import com.genersoft.iot.vmp.utils.GpsUtil;
@ -28,6 +30,10 @@ import java.text.ParseException;
import static com.genersoft.iot.vmp.gb28181.utils.XmlUtil.getText;
/**
* 移动设备位置数据查询回复
* @author lin
*/
@Component
public class MobilePositionResponseMessageHandler extends SIPRequestProcessorParent implements InitializingBean, IMessageHandler {
@ -43,6 +49,9 @@ public class MobilePositionResponseMessageHandler extends SIPRequestProcessorPar
@Autowired
private IVideoManagerStorage storager;
@Autowired
private IRedisCatchStorage redisCatchStorage;
@Autowired
private IDeviceChannelService deviceChannelService;
@ -56,7 +65,11 @@ public class MobilePositionResponseMessageHandler extends SIPRequestProcessorPar
try {
rootElement = getRootElement(evt, device.getCharset());
if (rootElement == null) {
logger.warn("[ 移动设备位置数据查询回复 ] content cannot be null");
responseAck(evt, Response.BAD_REQUEST);
return;
}
MobilePosition mobilePosition = new MobilePosition();
mobilePosition.setCreateTime(DateUtil.getNow());
if (!StringUtils.isEmpty(device.getName())) {
@ -103,6 +116,18 @@ public class MobilePositionResponseMessageHandler extends SIPRequestProcessorPar
storager.insertMobilePosition(mobilePosition);
}
storager.updateChannelPosition(deviceChannel);
// 发送redis消息 通知位置信息的变化
JSONObject jsonObject = new JSONObject();
jsonObject.put("time", mobilePosition.getTime());
jsonObject.put("serial", deviceChannel.getDeviceId());
jsonObject.put("code", deviceChannel.getChannelId());
jsonObject.put("longitude", mobilePosition.getLongitude());
jsonObject.put("latitude", mobilePosition.getLatitude());
jsonObject.put("altitude", mobilePosition.getAltitude());
jsonObject.put("direction", mobilePosition.getDirection());
jsonObject.put("speed", mobilePosition.getSpeed());
redisCatchStorage.sendMobilePositionMsg(jsonObject);
//回复 200 OK
responseAck(evt, Response.OK);
} catch (DocumentException | SipException | InvalidArgumentException | ParseException e) {

View File

@ -26,6 +26,9 @@ import java.util.List;
import static com.genersoft.iot.vmp.gb28181.utils.XmlUtil.getText;
/**
* 设备预置位查询应答
*/
@Component
public class PresetQueryResponseMessageHandler extends SIPRequestProcessorParent implements InitializingBean, IMessageHandler {
@ -49,7 +52,11 @@ public class PresetQueryResponseMessageHandler extends SIPRequestProcessorParent
Element rootElement = null;
try {
rootElement = getRootElement(evt, device.getCharset());
if (rootElement == null) {
logger.warn("[ 设备预置位查询应答 ] content cannot be null");
responseAck(evt, Response.BAD_REQUEST);
return;
}
Element presetListNumElement = rootElement.element("PresetList");
Element snElement = rootElement.element("SN");
//该字段可能为通道或则设备的id
@ -61,11 +68,7 @@ public class PresetQueryResponseMessageHandler extends SIPRequestProcessorParent
}
int sumNum = Integer.parseInt(presetListNumElement.attributeValue("Num"));
List<PresetQuerySipReq> presetQuerySipReqList = new ArrayList<>();
if (sumNum == 0) {
// 数据无预置位信息
}else {
if (sumNum > 0) {
for (Iterator<Element> presetIterator = presetListNumElement.elementIterator();presetIterator.hasNext();){
Element itemListElement = presetIterator.next();
PresetQuerySipReq presetQuerySipReq = new PresetQuerySipReq();

View File

@ -80,6 +80,10 @@ public class RecordInfoResponseMessageHandler extends SIPRequestProcessorParent
try {
HandlerCatchData take = taskQueue.poll();
Element rootElementForCharset = getRootElement(take.getEvt(), take.getDevice().getCharset());
if (rootElement == null) {
logger.warn("[ 国标录像 ] content cannot be null");
continue;
}
String sn = getText(rootElementForCharset, "SN");
String channelId = getText(rootElementForCharset, "DeviceID");
RecordInfo recordInfo = new RecordInfo();