[xmppclient] Add send image throught HTTP (#11247)

Signed-off-by: Fabien Carrion <fabien@carrion.mx>
This commit is contained in:
Fabien Carrion 2021-10-19 12:08:48 -05:00 committed by GitHub
parent 59ae706754
commit 8257179e46
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 57 additions and 0 deletions

View File

@ -43,6 +43,12 @@
<version>${smack.version}</version> <version>${smack.version}</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency>
<groupId>org.igniterealtime.smack</groupId>
<artifactId>smack-experimental</artifactId>
<version>${smack.version}</version>
<scope>compile</scope>
</dependency>
<dependency> <dependency>
<groupId>org.minidns</groupId> <groupId>org.minidns</groupId>
<artifactId>minidns-core</artifactId> <artifactId>minidns-core</artifactId>

View File

@ -6,12 +6,14 @@
<feature>openhab-runtime-base</feature> <feature>openhab-runtime-base</feature>
<bundle dependency="true">mvn:org.igniterealtime.smack/smack-extensions/4.3.3</bundle> <bundle dependency="true">mvn:org.igniterealtime.smack/smack-extensions/4.3.3</bundle>
<bundle dependency="true">mvn:org.igniterealtime.smack/smack-experimental/4.3.3</bundle>
<bundle dependency="true">mvn:org.igniterealtime.smack/smack-im/4.3.3</bundle> <bundle dependency="true">mvn:org.igniterealtime.smack/smack-im/4.3.3</bundle>
<bundle dependency="true">mvn:org.igniterealtime.smack/smack-tcp/4.3.3</bundle> <bundle dependency="true">mvn:org.igniterealtime.smack/smack-tcp/4.3.3</bundle>
<bundle dependency="true">mvn:org.jxmpp/jxmpp-core/0.6.3</bundle> <bundle dependency="true">mvn:org.jxmpp/jxmpp-core/0.6.3</bundle>
<bundle dependency="true">mvn:org.jxmpp/jxmpp-jid/0.6.3</bundle> <bundle dependency="true">mvn:org.jxmpp/jxmpp-jid/0.6.3</bundle>
<bundle dependency="true">mvn:org.jxmpp/jxmpp-util-cache/0.6.3</bundle> <bundle dependency="true">mvn:org.jxmpp/jxmpp-util-cache/0.6.3</bundle>
<bundle dependency="true">mvn:org.minidns/minidns-core/0.3.3</bundle> <bundle dependency="true">mvn:org.minidns/minidns-core/0.3.3</bundle>
<bundle dependency="true">mvn:org.bouncycastle/bcprov-jdk15on/1.69</bundle>
<bundle dependency="true">mvn:org.igniterealtime.smack/smack-core/4.3.3</bundle> <bundle dependency="true">mvn:org.igniterealtime.smack/smack-core/4.3.3</bundle>
<bundle dependency="true">mvn:org.igniterealtime.smack/smack-sasl-javax/4.3.3</bundle> <bundle dependency="true">mvn:org.igniterealtime.smack/smack-sasl-javax/4.3.3</bundle>
<bundle dependency="true">mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.xpp3/1.1.4c_7</bundle> <bundle dependency="true">mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.xpp3/1.1.4c_7</bundle>

View File

@ -12,7 +12,9 @@
*/ */
package org.openhab.binding.xmppclient.internal; package org.openhab.binding.xmppclient.internal;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.URL;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
@ -30,6 +32,7 @@ import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration; import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager; import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
import org.jivesoftware.smackx.disco.packet.DiscoverInfo.Identity; import org.jivesoftware.smackx.disco.packet.DiscoverInfo.Identity;
import org.jivesoftware.smackx.httpfileupload.HttpFileUploadManager;
import org.jxmpp.jid.EntityBareJid; import org.jxmpp.jid.EntityBareJid;
import org.jxmpp.jid.impl.JidCreate; import org.jxmpp.jid.impl.JidCreate;
import org.jxmpp.stringprep.XmppStringprepException; import org.jxmpp.stringprep.XmppStringprepException;
@ -46,6 +49,7 @@ public class XMPPClient implements IncomingChatMessageListener, ConnectionListen
private final Logger logger = LoggerFactory.getLogger(XMPPClient.class); private final Logger logger = LoggerFactory.getLogger(XMPPClient.class);
private AbstractXMPPConnection connection; private AbstractXMPPConnection connection;
private ChatManager chatManager; private ChatManager chatManager;
private HttpFileUploadManager httpFileUploadManager;
private Set<XMPPClientMessageSubscriber> subscribers = new HashSet<>(); private Set<XMPPClientMessageSubscriber> subscribers = new HashSet<>();
public void subscribe(XMPPClientMessageSubscriber channel) { public void subscribe(XMPPClientMessageSubscriber channel) {
@ -90,6 +94,7 @@ public class XMPPClient implements IncomingChatMessageListener, ConnectionListen
chatManager = ChatManager.getInstanceFor(connection); chatManager = ChatManager.getInstanceFor(connection);
chatManager.addIncomingListener(this); chatManager.addIncomingListener(this);
httpFileUploadManager = HttpFileUploadManager.getInstanceFor(connection);
} }
public void disconnect() { public void disconnect() {
@ -116,6 +121,24 @@ public class XMPPClient implements IncomingChatMessageListener, ConnectionListen
} }
} }
public void sendImageByHTTP(String to, String filename) {
if (connection == null) {
logger.warn("XMPP connection is null");
return;
}
if (httpFileUploadManager == null) {
logger.warn("XMPP httpFileUploadManager is null");
return;
}
try {
URL u = httpFileUploadManager.uploadFile(new File(filename));
// Use Stanza oob
this.sendMessage(to, u.toString());
} catch (XMPPException.XMPPErrorException | SmackException | InterruptedException | IOException e) {
logger.warn("XMPP HTTP image sending error", e);
}
}
@Override @Override
public void newIncomingMessage(EntityBareJid from, Message message, Chat chat) { public void newIncomingMessage(EntityBareJid from, Message message, Chat chat) {
logger.debug("XMPP {} says {}", from.asBareJid().toString(), message.getBody()); logger.debug("XMPP {} says {}", from.asBareJid().toString(), message.getBody());

View File

@ -66,7 +66,33 @@ public class XMPPActions implements ThingActions {
connection.sendMessage(to, text); connection.sendMessage(to, text);
} }
@RuleAction(label = "publish an image by HTTP", description = "Publish an image by HTTP using XMPP.")
public void publishXMPPImageByHTTP(
@ActionInput(name = "to", label = "To", description = "Send to") @Nullable String to,
@ActionInput(name = "filename", label = "Filename", description = "Image Filename") @Nullable String filename) {
XMPPClientHandler clientHandler = handler;
if (clientHandler == null) {
logger.warn("XMPP ThingHandler is null");
return;
}
XMPPClient connection = clientHandler.getXMPPClient();
if (connection == null) {
logger.warn("XMPP ThingHandler connection is null");
return;
}
if ((to == null) || (filename == null)) {
logger.warn("Skipping XMPP messaging to {} value {}", to, filename);
return;
}
connection.sendImageByHTTP(to, filename);
}
public static void publishXMPP(ThingActions actions, @Nullable String to, @Nullable String text) { public static void publishXMPP(ThingActions actions, @Nullable String to, @Nullable String text) {
((XMPPActions) actions).publishXMPP(to, text); ((XMPPActions) actions).publishXMPP(to, text);
} }
public static void publishXMPPImageByHTTP(ThingActions actions, @Nullable String to, @Nullable String filename) {
((XMPPActions) actions).publishXMPPImageByHTTP(to, filename);
}
} }