Skip to content

Commit

Permalink
Merge pull request #5390 from ant-media/fix-issue-5246
Browse files Browse the repository at this point in the history
Keep https for onvif protocol
  • Loading branch information
mekya authored Aug 16, 2023
2 parents 1f2a7c5 + 5e7ca1e commit fe240e8
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/main/java/io/antmedia/ipcamera/IOnvifCamera.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
public interface IOnvifCamera {
/**
*
* @param address
* @param address it can be direct ip address with port or it can start with http:// or https://
* @param username
* @param password
* @return true if connected
Expand Down
35 changes: 27 additions & 8 deletions src/main/java/io/antmedia/ipcamera/OnvifCamera.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ public class OnvifCamera implements IOnvifCamera {
public static final int CONNECT_ERROR = -1;
public static final int AUTHENTICATION_ERROR = -2;
String profileToken;
private static final String HTTP = "http://";
private static final String HTTP_PREFIX = "http://";
private static final String HTTPS_PREFIX = "https://";



protected static Logger logger = LoggerFactory.getLogger(OnvifCamera.class);
Expand All @@ -37,9 +39,12 @@ public int connect(String address, String username, String password) {
String camIP = "";
try {

camIP = getURL(address);
//address may include http:// or https:// so get the IPAddress directly
camIP = getIPAddress(address);

nvt = new OnvifDevice(camIP, username, password);
String protocol = getProtocol(address);

nvt = new OnvifDevice(camIP, protocol, username, password);
nvt.getSoap().setLogging(false);
nvt.getDevices().getCapabilities().getDevice();
nvt.getDevices().getServices(false);
Expand Down Expand Up @@ -75,6 +80,19 @@ public int connect(String address, String username, String password) {
return result;
}

public String getProtocol(String address) {
String protocol = null;
if (address.startsWith(HTTP_PREFIX))
{
protocol = "http";
}
else if (address.startsWith(HTTPS_PREFIX))
{
protocol = "https";
}
return protocol;
}

@Override
public String[] getProfiles() {
String profilesStr[] = null;
Expand Down Expand Up @@ -257,22 +275,23 @@ public boolean setDateTime(java.sql.Date date, java.sql.Time time) {
return false;
}

public String getURL (String url) {
public String getIPAddress (String url) {

String[] ipAddrParts = null;
String ipAddr = url;

if(url != null && (url.startsWith(HTTP) ||
if(url != null && (url.startsWith(HTTP_PREFIX) ||
url.startsWith("https://") ||
url.startsWith("rtmp://") ||
url.startsWith("rtmps://") ||
url.startsWith("rtsp://"))) {
url.startsWith("rtsp://")))
{

ipAddrParts = url.split("//");
ipAddr = ipAddrParts[1];
}
if (ipAddr != null) {

if (ipAddr != null)
{
if (ipAddr.contains("/")){
ipAddrParts = ipAddr.split("/");
ipAddr = ipAddrParts[0];
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/io/antmedia/test/StreamFetcherUnitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -441,10 +441,21 @@ public void testOnvifError() {

//it should be -1 because there is a connection error
assertEquals(-1, connResult);


//Test with protocol
newCam.setIpAddr("http://127.0.0.1:8080");
connResult = onvif.connect(newCam.getIpAddr(), newCam.getUsername(), newCam.getPassword());
logger.info("connResult {}", connResult);

//it should be 0 because URL and credentials are correct
assertEquals(0, connResult);

stopCameraEmulator();

}





Expand Down
44 changes: 44 additions & 0 deletions src/test/java/io/antmedia/test/ipcamera/OnvifCameraTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package io.antmedia.test.ipcamera;

import io.antmedia.ipcamera.OnvifCamera;
import io.antmedia.ipcamera.onvif.soap.OnvifDevice;
import junit.framework.TestCase;
import org.junit.Assert;

public class OnvifCameraTest extends TestCase {

public void testGetIPAddress() {

OnvifCamera onvifCamera = new OnvifCamera();

Assert.assertEquals("192.168.1.1:8080", onvifCamera.getIPAddress("http://192.168.1.1:8080"));

Assert.assertEquals("192.168.1.1:8080", onvifCamera.getIPAddress("192.168.1.1:8080/test"));

Assert.assertEquals("192.168.1.1:8080", onvifCamera.getIPAddress("192.168.1.1:8080/test/test2"));

Assert.assertEquals("192.168.1.1:8080", onvifCamera.getIPAddress("rtmp://192.168.1.1:8080/test"));

Assert.assertEquals("192.168.1.1:8080", onvifCamera.getIPAddress("rtmps://192.168.1.1:8080/test"));

Assert.assertEquals("192.168.1.1:8080", onvifCamera.getIPAddress("http://192.168.1.1:8080/test"));

Assert.assertEquals("192.168.1.1:8080", onvifCamera.getIPAddress("https://192.168.1.1:8080/test"));
}


public void testGetPortAddress() {

OnvifCamera onvifCamera = new OnvifCamera();

Assert.assertEquals("http", onvifCamera.getProtocol("http://192.168.1.1:8080"));

Assert.assertEquals("https", onvifCamera.getProtocol("https://192.168.1.1:8080/test"));

Assert.assertNull(onvifCamera.getProtocol("192.168.1.1:8080/test/test2"));

}



}

0 comments on commit fe240e8

Please sign in to comment.