Rework ThingActions (#8732)
* Remove proxy workarounds * Move ThingActions and a few other classes into the internal package * Use more consistent action labels/descriptions Related to: * openhab/openhab-core#1714 * openhab/openhab-core#1639 Signed-off-by: Wouter Born <github@maindrain.net>
This commit is contained in:
@@ -1,32 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2010-2020 Contributors to the openHAB project
|
||||
*
|
||||
* See the NOTICE file(s) distributed with this work for additional
|
||||
* information.
|
||||
*
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.openhab.binding.mail.action;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.eclipse.jdt.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* The {@link ISendMailActions} interface defines rule actions for sending mail
|
||||
*
|
||||
* @author Jan N. Klug - Initial contribution
|
||||
*/
|
||||
@NonNullByDefault
|
||||
public interface ISendMailActions {
|
||||
Boolean sendMail(@Nullable String recipient, @Nullable String subject, @Nullable String text,
|
||||
@Nullable List<String> urlStringList);
|
||||
|
||||
Boolean sendHtmlMail(@Nullable String recipient, @Nullable String subject, @Nullable String html,
|
||||
@Nullable List<String> urlStringList);
|
||||
}
|
||||
@@ -19,7 +19,7 @@ import org.apache.commons.mail.DefaultAuthenticator;
|
||||
import org.apache.commons.mail.Email;
|
||||
import org.apache.commons.mail.EmailException;
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.openhab.binding.mail.action.SendMailActions;
|
||||
import org.openhab.binding.mail.internal.action.SendMailActions;
|
||||
import org.openhab.binding.mail.internal.config.SMTPConfig;
|
||||
import org.openhab.core.thing.ChannelUID;
|
||||
import org.openhab.core.thing.Thing;
|
||||
|
||||
@@ -10,10 +10,8 @@
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.openhab.binding.mail.action;
|
||||
package org.openhab.binding.mail.internal.action;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.net.MalformedURLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -41,13 +39,13 @@ import org.slf4j.LoggerFactory;
|
||||
*/
|
||||
@ThingActionsScope(name = "mail")
|
||||
@NonNullByDefault
|
||||
public class SendMailActions implements ThingActions, ISendMailActions {
|
||||
public class SendMailActions implements ThingActions {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(SendMailActions.class);
|
||||
|
||||
private @Nullable SMTPHandler handler;
|
||||
|
||||
@RuleAction(label = "Send Text Mail", description = "sends a text mail")
|
||||
@RuleAction(label = "send a text mail", description = "Sends a text mail.")
|
||||
public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendMail(
|
||||
@ActionInput(name = "recipient") @Nullable String recipient,
|
||||
@ActionInput(name = "subject") @Nullable String subject,
|
||||
@@ -55,7 +53,7 @@ public class SendMailActions implements ThingActions, ISendMailActions {
|
||||
return sendMail(recipient, subject, text, new ArrayList<>());
|
||||
}
|
||||
|
||||
@RuleAction(label = "Send Text Mail", description = "sends a text mail with URL attachment")
|
||||
@RuleAction(label = "send a text mail", description = "Sends a text mail with URL attachment.")
|
||||
public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendMail(
|
||||
@ActionInput(name = "recipient") @Nullable String recipient,
|
||||
@ActionInput(name = "subject") @Nullable String subject, @ActionInput(name = "text") @Nullable String text,
|
||||
@@ -67,8 +65,7 @@ public class SendMailActions implements ThingActions, ISendMailActions {
|
||||
return sendMail(recipient, subject, text, urlList);
|
||||
}
|
||||
|
||||
@Override
|
||||
@RuleAction(label = "Send Text Mail", description = "sends a text mail with several URL attachments")
|
||||
@RuleAction(label = "send a text mail", description = "Sends a text mail with several URL attachments.")
|
||||
public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendMail(
|
||||
@ActionInput(name = "recipient") @Nullable String recipient,
|
||||
@ActionInput(name = "subject") @Nullable String subject, @ActionInput(name = "text") @Nullable String text,
|
||||
@@ -122,10 +119,14 @@ public class SendMailActions implements ThingActions, ISendMailActions {
|
||||
|
||||
public static boolean sendMail(@Nullable ThingActions actions, @Nullable String recipient, @Nullable String subject,
|
||||
@Nullable String text, @Nullable List<String> urlStringList) {
|
||||
return invokeMethodOf(actions).sendMail(recipient, subject, text, urlStringList);
|
||||
if (actions instanceof SendMailActions) {
|
||||
return ((SendMailActions) actions).sendMail(recipient, subject, text, urlStringList);
|
||||
} else {
|
||||
throw new IllegalArgumentException("Actions is not an instance of SendMailActions");
|
||||
}
|
||||
}
|
||||
|
||||
@RuleAction(label = "Send HTML Mail", description = "sends a HTML mail")
|
||||
@RuleAction(label = "send a HTML mail", description = "Sends a HTML mail.")
|
||||
public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendHtmlMail(
|
||||
@ActionInput(name = "recipient") @Nullable String recipient,
|
||||
@ActionInput(name = "subject") @Nullable String subject,
|
||||
@@ -133,7 +134,7 @@ public class SendMailActions implements ThingActions, ISendMailActions {
|
||||
return sendHtmlMail(recipient, subject, html, new ArrayList<>());
|
||||
}
|
||||
|
||||
@RuleAction(label = "Send HTML Mail", description = "sends a HTML mail with URL attachment")
|
||||
@RuleAction(label = "send a HTML mail", description = "Sends a HTML mail with URL attachment.")
|
||||
public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendHtmlMail(
|
||||
@ActionInput(name = "recipient") @Nullable String recipient,
|
||||
@ActionInput(name = "subject") @Nullable String subject, @ActionInput(name = "html") @Nullable String html,
|
||||
@@ -145,8 +146,7 @@ public class SendMailActions implements ThingActions, ISendMailActions {
|
||||
return sendHtmlMail(recipient, subject, html, urlList);
|
||||
}
|
||||
|
||||
@Override
|
||||
@RuleAction(label = "Send HTML Mail", description = "sends a HTML mail with several URL attachments")
|
||||
@RuleAction(label = "send a HTML mail", description = "Sends a HTML mail with several URL attachments.")
|
||||
public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendHtmlMail(
|
||||
@ActionInput(name = "recipient") @Nullable String recipient,
|
||||
@ActionInput(name = "subject") @Nullable String subject, @ActionInput(name = "html") @Nullable String html,
|
||||
@@ -200,7 +200,11 @@ public class SendMailActions implements ThingActions, ISendMailActions {
|
||||
|
||||
public static boolean sendHtmlMail(@Nullable ThingActions actions, @Nullable String recipient,
|
||||
@Nullable String subject, @Nullable String html, @Nullable List<String> urlStringList) {
|
||||
return invokeMethodOf(actions).sendHtmlMail(recipient, subject, html, urlStringList);
|
||||
if (actions instanceof SendMailActions) {
|
||||
return ((SendMailActions) actions).sendHtmlMail(recipient, subject, html, urlStringList);
|
||||
} else {
|
||||
throw new IllegalArgumentException("Actions is not an instance of SendMailActions");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -212,25 +216,6 @@ public class SendMailActions implements ThingActions, ISendMailActions {
|
||||
|
||||
@Override
|
||||
public @Nullable ThingHandler getThingHandler() {
|
||||
return this.handler;
|
||||
}
|
||||
|
||||
private static ISendMailActions invokeMethodOf(@Nullable ThingActions actions) {
|
||||
if (actions == null) {
|
||||
throw new IllegalArgumentException("actions cannot be null");
|
||||
}
|
||||
if (actions.getClass().getName().equals(SendMailActions.class.getName())) {
|
||||
if (actions instanceof ISendMailActions) {
|
||||
return (ISendMailActions) actions;
|
||||
} else {
|
||||
return (ISendMailActions) Proxy.newProxyInstance(ISendMailActions.class.getClassLoader(),
|
||||
new Class[] { ISendMailActions.class }, (Object proxy, Method method, Object[] args) -> {
|
||||
Method m = actions.getClass().getDeclaredMethod(method.getName(),
|
||||
method.getParameterTypes());
|
||||
return m.invoke(actions, args);
|
||||
});
|
||||
}
|
||||
}
|
||||
throw new IllegalArgumentException("Actions is not an instance of SendMailActions");
|
||||
return handler;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user