| | |
| | | package com.genersoft.iot.vmp.gb28181.transmit.event.request; |
| | | |
| | | import com.genersoft.iot.vmp.gb28181.bean.ParentPlatform; |
| | | import gov.nist.javax.sip.SipProviderImpl; |
| | | import gov.nist.javax.sip.SipStackImpl; |
| | | import gov.nist.javax.sip.message.SIPRequest; |
| | | import gov.nist.javax.sip.stack.SIPServerTransaction; |
| | | import org.apache.commons.lang3.ArrayUtils; |
| | | import org.dom4j.Document; |
| | | import org.dom4j.DocumentException; |
| | | import org.dom4j.Element; |
| | |
| | | import javax.sip.message.Request; |
| | | import javax.sip.message.Response; |
| | | import java.io.ByteArrayInputStream; |
| | | import java.nio.ByteBuffer; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.text.ParseException; |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * @description:处理接收IPCamera发来的SIP协议请求消息 |
| | |
| | | serverTransaction.sendResponse(response); |
| | | if (statusCode >= 200 && !"NOTIFY".equals(evt.getRequest().getMethod())) { |
| | | |
| | | if (serverTransaction.getDialog() != null) serverTransaction.getDialog().delete(); |
| | | if (serverTransaction.getDialog() != null) { |
| | | serverTransaction.getDialog().delete(); |
| | | } |
| | | } |
| | | } |
| | | |
| | |
| | | ServerTransaction serverTransaction = getServerTransaction(evt); |
| | | serverTransaction.sendResponse(response); |
| | | if (statusCode >= 200 && !"NOTIFY".equals(evt.getRequest().getMethod())) { |
| | | if (serverTransaction.getDialog() != null) serverTransaction.getDialog().delete(); |
| | | if (serverTransaction.getDialog() != null) { |
| | | serverTransaction.getDialog().delete(); |
| | | } |
| | | } |
| | | } |
| | | |
| | |
| | | * @throws InvalidArgumentException |
| | | * @throws ParseException |
| | | */ |
| | | public void responseSdpAck(RequestEvent evt, String sdp) throws SipException, InvalidArgumentException, ParseException { |
| | | public void responseSdpAck(RequestEvent evt, String sdp, ParentPlatform platform) throws SipException, InvalidArgumentException, ParseException { |
| | | Response response = getMessageFactory().createResponse(Response.OK, evt.getRequest()); |
| | | SipFactory sipFactory = SipFactory.getInstance(); |
| | | ContentTypeHeader contentTypeHeader = sipFactory.createHeaderFactory().createContentTypeHeader("APPLICATION", "SDP"); |
| | | response.setContent(sdp, contentTypeHeader); |
| | | |
| | | // 兼容国标中的使用编码@域名作为RequestURI的情况 |
| | | SipURI sipURI = (SipURI)evt.getRequest().getRequestURI(); |
| | | if (sipURI.getPort() == -1) { |
| | | sipURI = sipFactory.createAddressFactory().createSipURI(platform.getServerGBId(), platform.getServerIP()+":"+platform.getServerPort()); |
| | | } |
| | | logger.debug("responseSdpAck SipURI: {}:{}", sipURI.getHost(), sipURI.getPort()); |
| | | |
| | | Address concatAddress = sipFactory.createAddressFactory().createAddress( |
| | | sipFactory.createAddressFactory().createSipURI(sipURI.getUser(), sipURI.getHost()+":"+sipURI.getPort() |
| | |
| | | * @throws InvalidArgumentException |
| | | * @throws ParseException |
| | | */ |
| | | public Response responseXmlAck(RequestEvent evt, String xml) throws SipException, InvalidArgumentException, ParseException { |
| | | public Response responseXmlAck(RequestEvent evt, String xml, ParentPlatform platform) throws SipException, InvalidArgumentException, ParseException { |
| | | Response response = getMessageFactory().createResponse(Response.OK, evt.getRequest()); |
| | | SipFactory sipFactory = SipFactory.getInstance(); |
| | | ContentTypeHeader contentTypeHeader = sipFactory.createHeaderFactory().createContentTypeHeader("APPLICATION", "MANSCDP+xml"); |
| | | ContentTypeHeader contentTypeHeader = sipFactory.createHeaderFactory().createContentTypeHeader("Application", "MANSCDP+xml"); |
| | | response.setContent(xml, contentTypeHeader); |
| | | |
| | | // 兼容国标中的使用编码@域名作为RequestURI的情况 |
| | | SipURI sipURI = (SipURI)evt.getRequest().getRequestURI(); |
| | | if (sipURI.getPort() == -1) { |
| | | sipURI = sipFactory.createAddressFactory().createSipURI(platform.getServerGBId(), platform.getServerIP()+":"+platform.getServerPort()); |
| | | } |
| | | logger.debug("responseXmlAck SipURI: {}:{}", sipURI.getHost(), sipURI.getPort()); |
| | | |
| | | Address concatAddress = sipFactory.createAddressFactory().createAddress( |
| | | sipFactory.createAddressFactory().createSipURI(sipURI.getUser(), sipURI.getHost()+":"+sipURI.getPort() |
| | |
| | | return getRootElement(evt, "gb2312"); |
| | | } |
| | | public Element getRootElement(RequestEvent evt, String charset) throws DocumentException { |
| | | if (charset == null) charset = "gb2312"; |
| | | if (charset == null) { |
| | | charset = "gb2312"; |
| | | } |
| | | Request request = evt.getRequest(); |
| | | SAXReader reader = new SAXReader(); |
| | | reader.setEncoding(charset); |
| | | Document xml = reader.read(new ByteArrayInputStream(request.getRawContent())); |
| | | // 对海康出现的未转义字符做处理。 |
| | | String[] destStrArray = new String[]{"<",">","&","'","""}; |
| | | char despChar = '&'; // 或许可扩展兼容其他字符 |
| | | byte destBye = (byte) despChar; |
| | | List<Byte> result = new ArrayList<>(); |
| | | byte[] rawContent = request.getRawContent(); |
| | | for (int i = 0; i < rawContent.length; i++) { |
| | | if (rawContent[i] == destBye) { |
| | | boolean resul = false; |
| | | for (String destStr : destStrArray) { |
| | | if (i + destStr.length() <= rawContent.length) { |
| | | byte[] bytes = Arrays.copyOfRange(rawContent, i, i + destStr.length()); |
| | | resul = resul || (Arrays.equals(bytes,destStr.getBytes())); |
| | | } |
| | | } |
| | | if (resul) { |
| | | result.add(rawContent[i]); |
| | | } |
| | | }else { |
| | | result.add(rawContent[i]); |
| | | } |
| | | } |
| | | Byte[] bytes = new Byte[0]; |
| | | byte[] bytesResult = ArrayUtils.toPrimitive(result.toArray(bytes)); |
| | | |
| | | Document xml = reader.read(new ByteArrayInputStream(bytesResult)); |
| | | return xml.getRootElement(); |
| | | } |
| | | |