mirror of
https://github.com/danieldemus/openhab-addons
synced 2026-07-29 12:34:21 +02:00
Reduce SAT and build warnings (#20549)
Signed-off-by: Leo Siepel <leosiepel@gmail.com>
This commit is contained in:
+10
-11
@@ -22,7 +22,7 @@ import org.apache.ftpserver.FtpServerConfigurationException;
|
||||
import org.apache.ftpserver.ftplet.FtpException;
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.eclipse.jdt.annotation.Nullable;
|
||||
import org.openhab.binding.ftpupload.internal.ftp.FtpServer;
|
||||
import org.openhab.binding.ftpupload.internal.ftp.FtpServerManager;
|
||||
import org.openhab.binding.ftpupload.internal.handler.FtpUploadHandler;
|
||||
import org.openhab.core.thing.Thing;
|
||||
import org.openhab.core.thing.ThingStatus;
|
||||
@@ -50,10 +50,9 @@ public class FtpUploadHandlerFactory extends BaseThingHandlerFactory {
|
||||
|
||||
private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(THING_TYPE_IMAGERECEIVER);
|
||||
|
||||
private final int DEFAULT_PORT = 2121;
|
||||
private final int DEFAULT_IDLE_TIMEOUT = 60;
|
||||
private static final int DEFAULT_IDLE_TIMEOUT = 60;
|
||||
|
||||
private @Nullable FtpServer ftpServer;
|
||||
private @NonNullByDefault({}) FtpServerManager ftpServer;
|
||||
|
||||
@Override
|
||||
public boolean supportsThingType(ThingTypeUID thingTypeUID) {
|
||||
@@ -65,7 +64,7 @@ public class FtpUploadHandlerFactory extends BaseThingHandlerFactory {
|
||||
ThingTypeUID thingTypeUID = thing.getThingTypeUID();
|
||||
|
||||
if (thingTypeUID.equals(THING_TYPE_IMAGERECEIVER)) {
|
||||
if (ftpServer.getStartUpErrorReason() != null) {
|
||||
if (!ftpServer.getStartUpErrorReason().isBlank()) {
|
||||
thing.setStatusInfo(new ThingStatusInfo(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
|
||||
ftpServer.getStartUpErrorReason()));
|
||||
}
|
||||
@@ -78,7 +77,7 @@ public class FtpUploadHandlerFactory extends BaseThingHandlerFactory {
|
||||
@Override
|
||||
protected synchronized void activate(ComponentContext componentContext) {
|
||||
super.activate(componentContext);
|
||||
ftpServer = new FtpServer();
|
||||
ftpServer = new FtpServerManager();
|
||||
modified(componentContext);
|
||||
}
|
||||
|
||||
@@ -94,7 +93,7 @@ public class FtpUploadHandlerFactory extends BaseThingHandlerFactory {
|
||||
Dictionary<String, @Nullable Object> properties = componentContext.getProperties();
|
||||
DataConnectionConfigurationFactory dataConnectionConfigurationFactory = new DataConnectionConfigurationFactory();
|
||||
|
||||
int port = DEFAULT_PORT;
|
||||
int port = FtpServerManager.DEFAULT_PORT;
|
||||
int idleTimeout = DEFAULT_IDLE_TIMEOUT;
|
||||
|
||||
Object objPort = properties.get("port");
|
||||
@@ -110,7 +109,7 @@ public class FtpUploadHandlerFactory extends BaseThingHandlerFactory {
|
||||
}
|
||||
|
||||
Object objIdleTimeout = properties.get("idleTimeout");
|
||||
if (objIdleTimeout != null) {
|
||||
if (objIdleTimeout != null && objPort != null) {
|
||||
String strIdleTimeout = objPort.toString();
|
||||
if (strIdleTimeout != null && !strIdleTimeout.isEmpty()) {
|
||||
try {
|
||||
@@ -120,9 +119,9 @@ public class FtpUploadHandlerFactory extends BaseThingHandlerFactory {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (properties.get("passivePorts") != null) {
|
||||
String strPassivePorts = properties.get("passivePorts").toString();
|
||||
Object objPassivePorts = properties.get("passivePorts");
|
||||
if (objPassivePorts != null) {
|
||||
String strPassivePorts = objPassivePorts.toString();
|
||||
if (!strPassivePorts.isEmpty()) {
|
||||
try {
|
||||
dataConnectionConfigurationFactory.setPassivePorts(strPassivePorts);
|
||||
|
||||
+8
-5
@@ -17,6 +17,8 @@ import java.util.List;
|
||||
import org.apache.ftpserver.ftplet.Authority;
|
||||
import org.apache.ftpserver.ftplet.AuthorizationRequest;
|
||||
import org.apache.ftpserver.ftplet.User;
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.eclipse.jdt.annotation.Nullable;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -26,8 +28,9 @@ import org.slf4j.LoggerFactory;
|
||||
*
|
||||
* @author Pauli Anttila - Initial contribution
|
||||
*/
|
||||
@NonNullByDefault
|
||||
public class FTPUser implements User {
|
||||
private static Logger logger = LoggerFactory.getLogger(FTPUser.class);
|
||||
private final Logger logger = LoggerFactory.getLogger(FTPUser.class);
|
||||
|
||||
private final String login;
|
||||
private final int idleTimeout;
|
||||
@@ -38,7 +41,7 @@ public class FTPUser implements User {
|
||||
}
|
||||
|
||||
@Override
|
||||
public AuthorizationRequest authorize(final AuthorizationRequest authRequest) {
|
||||
public AuthorizationRequest authorize(@NonNullByDefault({}) final AuthorizationRequest authRequest) {
|
||||
logger.trace("authorize: {}", authRequest);
|
||||
return authRequest;
|
||||
}
|
||||
@@ -68,19 +71,19 @@ public class FTPUser implements User {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Authority> getAuthorities() {
|
||||
public @Nullable List<Authority> getAuthorities() {
|
||||
logger.trace("getAuthorities");
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Authority> getAuthorities(Class<? extends Authority> arg0) {
|
||||
public @Nullable List<Authority> getAuthorities(@NonNullByDefault({}) Class<? extends Authority> arg0) {
|
||||
logger.trace("getAuthorities: {}", arg0);
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPassword() {
|
||||
public @Nullable String getPassword() {
|
||||
logger.trace("getPassword");
|
||||
return null;
|
||||
}
|
||||
|
||||
+5
-2
@@ -43,7 +43,10 @@ public class FTPUserManager implements UserManager {
|
||||
public User authenticate(final @Nullable Authentication inAuth) throws AuthenticationFailedException {
|
||||
logger.trace("authenticate: {}", inAuth);
|
||||
|
||||
UsernamePasswordAuthentication upa = (UsernamePasswordAuthentication) inAuth;
|
||||
if (!(inAuth instanceof UsernamePasswordAuthentication upa)) {
|
||||
throw new AuthenticationFailedException("Only username/password authentication is supported!");
|
||||
}
|
||||
|
||||
String login = upa.getUsername();
|
||||
String password = upa.getPassword();
|
||||
|
||||
@@ -85,7 +88,7 @@ public class FTPUserManager implements UserManager {
|
||||
}
|
||||
|
||||
@Override
|
||||
public User getUserByName(final @Nullable String login) throws FtpException {
|
||||
public User getUserByName(@NonNullByDefault({}) final String login) throws FtpException {
|
||||
logger.trace("getUserByName: {}", login);
|
||||
return new FTPUser(login, idleTimeout);
|
||||
}
|
||||
|
||||
+29
-19
@@ -20,6 +20,7 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.ftpserver.DataConnectionConfiguration;
|
||||
import org.apache.ftpserver.FtpServer;
|
||||
import org.apache.ftpserver.FtpServerConfigurationException;
|
||||
import org.apache.ftpserver.FtpServerFactory;
|
||||
import org.apache.ftpserver.ftplet.DefaultFtplet;
|
||||
@@ -35,6 +36,8 @@ import org.apache.ftpserver.ftplet.FtpletResult;
|
||||
import org.apache.ftpserver.ftplet.User;
|
||||
import org.apache.ftpserver.listener.Listener;
|
||||
import org.apache.ftpserver.listener.ListenerFactory;
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.eclipse.jdt.annotation.Nullable;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -44,23 +47,26 @@ import org.slf4j.LoggerFactory;
|
||||
*
|
||||
* @author Pauli Anttila - Initial contribution
|
||||
*/
|
||||
public class FtpServer {
|
||||
@NonNullByDefault
|
||||
public class FtpServerManager {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(FtpServer.class);
|
||||
public static final int DEFAULT_PORT = 2121;
|
||||
|
||||
private int port;
|
||||
private DataConnectionConfiguration dataConnectionConfiguration;
|
||||
private final Logger logger = LoggerFactory.getLogger(FtpServerManager.class);
|
||||
|
||||
private int port = DEFAULT_PORT;
|
||||
private @Nullable DataConnectionConfiguration dataConnectionConfiguration;
|
||||
int idleTimeout;
|
||||
|
||||
private org.apache.ftpserver.FtpServer server;
|
||||
private @Nullable FtpServer server;
|
||||
private List<FtpServerEventListener> listeners;
|
||||
private MyFTPLet myFTPLet;
|
||||
private FTPUserManager FTPUserManager;
|
||||
private String ftpStartUpErrorReason;
|
||||
private MyFTPLet myFTPLet = new MyFTPLet();
|
||||
private FTPUserManager ftpUserManager;
|
||||
private String ftpStartUpErrorReason = "";
|
||||
|
||||
public FtpServer() {
|
||||
public FtpServerManager() {
|
||||
listeners = new ArrayList<>();
|
||||
FTPUserManager = new FTPUserManager();
|
||||
ftpUserManager = new FTPUserManager();
|
||||
}
|
||||
|
||||
public void startServer(int port, int idleTimeout, DataConnectionConfiguration dataConnectionConfiguration)
|
||||
@@ -69,11 +75,12 @@ public class FtpServer {
|
||||
this.port = port;
|
||||
this.idleTimeout = idleTimeout;
|
||||
this.dataConnectionConfiguration = dataConnectionConfiguration;
|
||||
FTPUserManager.setIdleTimeout(idleTimeout);
|
||||
ftpUserManager.setIdleTimeout(idleTimeout);
|
||||
initServer();
|
||||
}
|
||||
|
||||
public void stopServer() {
|
||||
FtpServer server = this.server;
|
||||
if (server != null) {
|
||||
server.stop();
|
||||
}
|
||||
@@ -91,11 +98,11 @@ public class FtpServer {
|
||||
|
||||
public synchronized void addAuthenticationCredentials(String username, String password)
|
||||
throws IllegalArgumentException {
|
||||
FTPUserManager.addAuthenticationCredentials(username, password);
|
||||
ftpUserManager.addAuthenticationCredentials(username, password);
|
||||
}
|
||||
|
||||
public synchronized void removeAuthenticationCredentials(String username) {
|
||||
FTPUserManager.removeAuthenticationCredentials(username);
|
||||
ftpUserManager.removeAuthenticationCredentials(username);
|
||||
}
|
||||
|
||||
public synchronized void removeEventListener(FtpServerEventListener listener) {
|
||||
@@ -148,28 +155,31 @@ public class FtpServer {
|
||||
serverFactory.setFtplets(ftplets);
|
||||
serverFactory.setFileSystem(new FileSystemFactory() {
|
||||
@Override
|
||||
public FileSystemView createFileSystemView(User user) throws FtpException {
|
||||
public FileSystemView createFileSystemView(@NonNullByDefault({}) User user) throws FtpException {
|
||||
logger.debug("createFileSystemView: {}", user.getName());
|
||||
return new SimpleFileSystemView();
|
||||
}
|
||||
});
|
||||
|
||||
// set the user manager
|
||||
serverFactory.setUserManager(FTPUserManager);
|
||||
server = serverFactory.createServer();
|
||||
serverFactory.setUserManager(ftpUserManager);
|
||||
FtpServer server = serverFactory.createServer();
|
||||
|
||||
try {
|
||||
server.start();
|
||||
ftpStartUpErrorReason = null;
|
||||
this.server = server;
|
||||
ftpStartUpErrorReason = "";
|
||||
} catch (FtpException | FtpServerConfigurationException e) {
|
||||
ftpStartUpErrorReason = "Failed to start FTP server";
|
||||
if (!e.getMessage().isEmpty()) {
|
||||
ftpStartUpErrorReason += ": " + e.getMessage();
|
||||
String message = e.getMessage();
|
||||
if (message != null && !message.isEmpty()) {
|
||||
ftpStartUpErrorReason += ": " + message;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
@NonNullByDefault({})
|
||||
private class MyFTPLet extends DefaultFtplet {
|
||||
FtpletContext ftpletContext;
|
||||
|
||||
+6
-2
@@ -38,6 +38,11 @@ public class SimpleFtpFile implements FtpFile {
|
||||
MyOutputStream file;
|
||||
|
||||
public byte[] getData() {
|
||||
MyOutputStream file = this.file;
|
||||
if (file == null) {
|
||||
logger.debug("No data");
|
||||
return new byte[0];
|
||||
}
|
||||
return file.getData();
|
||||
}
|
||||
|
||||
@@ -50,8 +55,7 @@ public class SimpleFtpFile implements FtpFile {
|
||||
@Override
|
||||
public OutputStream createOutputStream(long arg0) throws IOException {
|
||||
logger.trace("createOutputStream: {}", arg0);
|
||||
MyOutputStream file = this.file = new MyOutputStream();
|
||||
return file;
|
||||
return this.file = new MyOutputStream();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+6
-4
@@ -17,9 +17,10 @@ import static org.openhab.binding.ftpupload.internal.FtpUploadBindingConstants.*
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.regex.PatternSyntaxException;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.openhab.binding.ftpupload.internal.config.FtpUploadConfig;
|
||||
import org.openhab.binding.ftpupload.internal.ftp.FtpServer;
|
||||
import org.openhab.binding.ftpupload.internal.ftp.FtpServerEventListener;
|
||||
import org.openhab.binding.ftpupload.internal.ftp.FtpServerManager;
|
||||
import org.openhab.core.io.net.http.HttpUtil;
|
||||
import org.openhab.core.library.types.RawType;
|
||||
import org.openhab.core.thing.Channel;
|
||||
@@ -39,14 +40,15 @@ import org.slf4j.LoggerFactory;
|
||||
*
|
||||
* @author Pauli Anttila - Initial contribution
|
||||
*/
|
||||
@NonNullByDefault
|
||||
public class FtpUploadHandler extends BaseThingHandler implements FtpServerEventListener {
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(FtpUploadHandler.class);
|
||||
|
||||
private FtpUploadConfig configuration;
|
||||
private FtpServer ftpServer;
|
||||
private @NonNullByDefault({}) FtpUploadConfig configuration;
|
||||
private final FtpServerManager ftpServer;
|
||||
|
||||
public FtpUploadHandler(Thing thing, FtpServer ftpServer) {
|
||||
public FtpUploadHandler(Thing thing, FtpServerManager ftpServer) {
|
||||
super(thing);
|
||||
this.ftpServer = ftpServer;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user