Merge pull request #1045 from gaudryc/master

Logout request and Shutdown hook merge to target_5_2_next
This commit is contained in:
BWS Systems
2019-05-21 09:56:57 -05:00
committed by GitHub
16 changed files with 134 additions and 40 deletions

View File

@@ -355,7 +355,7 @@ public class BridgeSecurity {
public void removeAuthenticatedUser(Request request) {
request.session().removeAttribute(USER_SESSION_ID);
request.session().invalidate();
}
public User getAuthenticatedUser(Request request) {

View File

@@ -12,8 +12,8 @@ import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.PosixFilePermission;
import java.security.GeneralSecurityException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;
@@ -32,7 +32,7 @@ public class BridgeSettings extends BackupHandler {
private BridgeSettingsDescriptor theBridgeSettings;
private BridgeControlDescriptor bridgeControl;
private BridgeSecurity bridgeSecurity;
private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
private static final DateTimeFormatter dateTimeFormat = DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmss");
public BridgeSettings() {
super();
@@ -59,10 +59,10 @@ public class BridgeSettings extends BackupHandler {
public BridgeSecurity getBridgeSecurity() {
return bridgeSecurity;
}
public static String getCurrentDate() {
return dateFormat.format(new Date());
}
public String getCurrentDate() {
return LocalDateTime.now().format(dateTimeFormat);
}
public void buildSettings() {
String addressString = null;
String theVeraAddress = null;

View File

@@ -44,7 +44,8 @@ public class HABridge {
Version theVersion;
@SuppressWarnings("unused")
HttpClientPool thePool;
ShutdownHook shutdownHook = null;
log.info("HA Bridge startup sequence...");
theVersion = new Version();
// Singleton initialization
@@ -68,6 +69,14 @@ public class HABridge {
// setup system control api first
theSystem = new SystemControl(bridgeSettings, theVersion);
theSystem.setupServer();
// Add shutdown hook to be able to properly stop server
if (shutdownHook != null) {
Runtime.getRuntime().removeShutdownHook(shutdownHook);
}
shutdownHook = new ShutdownHook(bridgeSettings, theSystem);
Runtime.getRuntime().addShutdownHook(shutdownHook);
// setup the UDP Datagram socket to be used by the HueMulator and the upnpListener
udpSender = UDPDatagramSender.createUDPDatagramSender(bridgeSettings.getBridgeSettingsDescriptor().getUpnpResponsePort());
if(udpSender == null) {

View File

@@ -0,0 +1,48 @@
package com.bwssystems.HABridge;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* This class implements the shutdown hook used to properly stop server from the
* command line (sending SIGTERM), or while shutting down the host machine.
*
* @author gaudryc
*/
public class ShutdownHook extends Thread {
private final BridgeSettings bridgeSettings;
private final SystemControl theSystem;
/**
* Constructor
*
* @param bridgeSettings
* bridge settings
* @param theSystem
*/
public ShutdownHook(final BridgeSettings bridgeSettings, final SystemControl theSystem) {
this.bridgeSettings = bridgeSettings;
this.theSystem = theSystem;
}
@Override
public void run() {
Logger log = LoggerFactory.getLogger(ShutdownHook.class);
log.info("Shutdown requested...");
if (bridgeSettings != null) {
if (!bridgeSettings.getBridgeControl().isStop() && (theSystem != null)) {
log.info("Forcing system stop...");
theSystem.stop();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
log.error("Sleep error: " + e.getMessage());
}
} else {
log.info("Already stopped");
}
}
}
}

View File

@@ -243,6 +243,24 @@ public class SystemControl {
return result;
}, new JsonTransformer());
// http://ip_address:port/system/logout CORS request
options(SYSTEM_CONTEXT + "/logout", (request, response) -> {
response.status(HttpStatus.SC_OK);
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.header("Access-Control-Allow-Methods", "GET, POST, PUT");
response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
response.header("Content-Type", "text/html; charset=utf-8");
return "";
});
// http://ip_address:port/system/logout invalidates user session
put(SYSTEM_CONTEXT + "/logout", (request, response) -> {
log.debug("logout....");
bridgeSettings.getBridgeSecurity().removeAuthenticatedUser(request);
response.status(HttpStatus.SC_OK);
response.type("application/json");
return "";
});
// http://ip_address:port/system/presslinkbutton CORS request
options(SYSTEM_CONTEXT + "/presslinkbutton", (request, response) -> {
response.status(HttpStatus.SC_OK);
@@ -558,4 +576,5 @@ public class SystemControl {
pingListener();
return "{\"control\":\"stopping\"}";
}
}

View File

@@ -1,14 +1,14 @@
package com.bwssystems.HABridge.api.hue;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class WhitelistEntry
{
private String lastUseDate;
private String createDate;
private String name;
private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
private static final DateTimeFormatter dateTimeFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
public static WhitelistEntry createEntry(String devicetype) {
WhitelistEntry anEntry = new WhitelistEntry();
@@ -18,9 +18,9 @@ public class WhitelistEntry
return anEntry;
}
public static String getCurrentDate() {
return dateFormat.format(new Date());
}
public static String getCurrentDate() {
return LocalDateTime.now().format(dateTimeFormat);
}
public String getLastUseDate() {
return lastUseDate;

View File

@@ -597,10 +597,10 @@ public class HueMulator {
notFirstChange = true;
}
if(deviceState.isOn() && deviceState.getBri() <= 0)
if((deviceState != null) && deviceState.isOn() && deviceState.getBri() <= 0)
deviceState.setBri(254);
if(!deviceState.isOn() && (targetBri != null || targetBriInc != null))
if((deviceState != null) && !deviceState.isOn() && (targetBri != null || targetBriInc != null))
deviceState.setOn(true);
responseString = responseString + "]";
@@ -1480,10 +1480,11 @@ public class HueMulator {
else
log.warn("Call Items type is null <<<" + callItems[i] + ">>>");
}
if(callItems.length == 0)
if ((callItems == null) || (callItems.length == 0)) {
log.warn("No call items were available: <<<" + url + ">>>");
}
return responseString;
}
}

View File

@@ -142,9 +142,12 @@ public class BroadlinkHome implements Home {
if(broadlinkMap == null)
broadlinkMap = new HashMap<String, BLDevice>();
String newId = theDevice.getHost() + "-" + String.format("%04x", theDevice.getDeviceType());
if(broadlinkMap.get(newId) == null)
broadlinkMap.put(newId, theDevice);
if (theDevice != null) {
String newId = theDevice.getHost() + "-" + String.format("%04x", theDevice.getDeviceType());
if (broadlinkMap.get(newId) == null) {
broadlinkMap.put(newId, theDevice);
}
}
}
}
if (theDevice == null) {

View File

@@ -73,7 +73,7 @@ public class DomoticzHome implements Home {
private Boolean addDomoticzDevices(List<DomoticzDevice> theDeviceList, List<DomoticzDevice> theSourceList, String theKey) {
if(!validDomoticz)
return null;
return false;
Iterator<DomoticzDevice> devices = theSourceList.iterator();
while(devices.hasNext()) {
DomoticzDevice theDevice = devices.next();

View File

@@ -104,7 +104,7 @@ public class HalHome implements Home {
private Boolean addHalDevices(List<HalDevice> theDeviceList, List<HalDevice> theSourceList, String theKey) {
if(!validHal)
return null;
return false;
Iterator<HalDevice> devices = theSourceList.iterator();
while(devices.hasNext()) {
HalDevice theDevice = devices.next();

View File

@@ -98,7 +98,7 @@ public class HassHome implements Home {
private Boolean addHassDevices(List<HassDevice> theDeviceList, List<State> theSourceList, String theKey) {
if(!validHass)
return null;
return false;
Iterator<State> devices = theSourceList.iterator();
while(devices.hasNext()) {
State theDevice = devices.next();

View File

@@ -115,7 +115,7 @@ public class HTTPHandler {
theContent = "";
log.debug("Successfull response - The http response is <<<" + theContent + ">>>");
retryCount = 2;
} else if (callType != null && callType == DeviceMapTypes.FHEM_DEVICE[DeviceMapTypes.typeIndex] && response.getStatusLine().getStatusCode() == 302) {
} else if (DeviceMapTypes.FHEM_DEVICE[DeviceMapTypes.typeIndex].equals(callType) && response.getStatusLine().getStatusCode() == 302) {
if(theContent == null)
theContent = "";
log.debug("Successfull response - The http response is <<<" + theContent + ">>>");

View File

@@ -32,7 +32,7 @@ public class HttpTestHandler extends HTTPHandler {
}
else {
for(NameValue aTest:this.theData) {
if(aTest.getName().equals(compareValue));
if(aTest.getName().equals(compareValue))
aTest.setValue(testData);
}
}

View File

@@ -161,8 +161,8 @@ public class HueInfo {
+ "\",\"description\": \"Error on calling HUE to change device state\", \"parameter\": \"/lights/"
+ lightId + "state\"}}]";
} else if (responseString.contains("[{\"error\":")) {
if(responseString.contains("unauthorized user")) {
}
// if(responseString.contains("unauthorized user")) {
// }
log.warn("Error occurred when calling Hue Passthru: " + responseString);
}
} else {

View File

@@ -1,5 +1,7 @@
package com.bwssystems.logservices;
import java.io.Serializable;
import com.bwssystems.logservices.LoggingUtil.LogLevels;
/**
@@ -7,7 +9,10 @@ import com.bwssystems.logservices.LoggingUtil.LogLevels;
*
*
*/
public class LoggerInfo {
public class LoggerInfo implements Serializable {
/** serialVersionUID. */
private static final long serialVersionUID = 1085935297588739585L;
private String loggerName;
private LogLevels logLevel;

View File

@@ -4813,15 +4813,24 @@ app.factory('Auth', function($resource, $rootScope, $sessionStorage, $http, $bas
bridgeService.displayWarn("Login Error: ", error);
});
};
auth.logout = function() {
delete $sessionStorage.user;
delete $rootScope.user;
delete bridgeService.state.loggedInUser;
};
auth.logout = function() {
delete $sessionStorage.user;
delete $rootScope.user;
delete bridgeService.state.loggedInUser;
// Logout on server side to destroy current session (fire and forget it)
$http.put(bridgeService.state.systemsbase + "/logout").then(
function (response) {
// nothing more to do
},
function (error) {
bridgeService.displayWarn("Logout Error: ", error);
}
);
};
auth.checkPermissionForView = function(view) {
if (!view.requiresAuthentication) {
return true;