mirror of
https://github.com/vanitasvitae/Smack.git
synced 2024-11-23 20:42:06 +01:00
Add some listeners and callbacks
This commit is contained in:
parent
9e1f777332
commit
1a16565697
9 changed files with 268 additions and 75 deletions
|
@ -1,72 +0,0 @@
|
|||
/**
|
||||
*
|
||||
* Copyright © 2017 Paul Schaub
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jivesoftware.smackx.jingle_filetransfer;
|
||||
|
||||
import org.jivesoftware.smackx.hash.HashManager;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.security.DigestInputStream;
|
||||
|
||||
/**
|
||||
* Dirty.
|
||||
*/
|
||||
public final class DirtyHelper {
|
||||
|
||||
public static BytesAndDigest readFile(File file, HashManager.ALGORITHM algorithm) throws IOException {
|
||||
byte[] bytes = null;
|
||||
byte[] digest = null;
|
||||
int read;
|
||||
FileInputStream fin = null;
|
||||
try {
|
||||
fin = new FileInputStream(file);
|
||||
DigestInputStream din = new DigestInputStream(fin, HashManager.getMessageDigest(algorithm));
|
||||
bytes = new byte[(int) file.length()];
|
||||
read = din.read(bytes);
|
||||
din.close();
|
||||
digest = din.getMessageDigest().digest();
|
||||
} finally {
|
||||
if (fin != null) {
|
||||
fin.close();
|
||||
}
|
||||
}
|
||||
if (read == -1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new BytesAndDigest(bytes, digest);
|
||||
}
|
||||
|
||||
public static class BytesAndDigest {
|
||||
|
||||
private final byte[] bytes, digest;
|
||||
|
||||
public BytesAndDigest(byte[] bytes, byte[] digest) {
|
||||
this.bytes = bytes;
|
||||
this.digest = digest;
|
||||
}
|
||||
|
||||
public byte[] getBytes() {
|
||||
return bytes;
|
||||
}
|
||||
|
||||
public byte[] getDigest() {
|
||||
return digest;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,83 @@
|
|||
/**
|
||||
*
|
||||
* Copyright © 2017 Paul Schaub
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jivesoftware.smackx.jingle_filetransfer;
|
||||
|
||||
import org.jivesoftware.smackx.hash.HashManager;
|
||||
import org.jivesoftware.smackx.hash.element.HashElement;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.security.DigestInputStream;
|
||||
|
||||
/**
|
||||
* FileReader that reads a file into a byte array while returning the hash of the file.
|
||||
*/
|
||||
public final class FileAndHashReader {
|
||||
|
||||
/**
|
||||
* Read a file into a byte array and calculate the hash sum of the bytes that were read on the fly.
|
||||
* @param file file to read.
|
||||
* @param destination byte array where the file is read into.
|
||||
* @param algorithm algorithm used to calculate the hash.
|
||||
* @return hashElement containing the hash sum of the read bytes.
|
||||
* @throws IOException
|
||||
*/
|
||||
public static HashElement readAndCalculateHash(File file, final byte[] destination, HashManager.ALGORITHM algorithm) throws IOException {
|
||||
return readAndCalculateHash(file, destination, 0, algorithm);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read a file into a byte array and calculate the hash sum of the bytes that were read on the fly.
|
||||
* @param file file to read.
|
||||
* @param destination byte array where the file is read into.
|
||||
* @param offset offset from the beginning of the file.
|
||||
* @param algorithm algorithm used to calculate the hash.
|
||||
* @return hashElement containing the hash sum of the read bytes.
|
||||
* @throws IOException
|
||||
*/
|
||||
public static HashElement readAndCalculateHash(File file, final byte[] destination, int offset, HashManager.ALGORITHM algorithm) throws IOException {
|
||||
byte[] hash;
|
||||
int read;
|
||||
FileInputStream fin = null;
|
||||
DigestInputStream din = null;
|
||||
try {
|
||||
fin = new FileInputStream(file);
|
||||
try {
|
||||
din = new DigestInputStream(fin, HashManager.getMessageDigest(algorithm));
|
||||
read = din.read(destination, offset, destination.length);
|
||||
hash = din.getMessageDigest().digest();
|
||||
}
|
||||
finally {
|
||||
if (din != null) {
|
||||
din.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
finally {
|
||||
if (fin != null) {
|
||||
fin.close();
|
||||
}
|
||||
}
|
||||
|
||||
if (read == -1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return HashManager.assembleHashElement(algorithm, hash);
|
||||
}
|
||||
}
|
|
@ -25,6 +25,7 @@ import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamManager;
|
|||
import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamSession;
|
||||
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
|
||||
import org.jivesoftware.smackx.hash.HashManager;
|
||||
import org.jivesoftware.smackx.hash.element.HashElement;
|
||||
import org.jivesoftware.smackx.jingle.JingleManager;
|
||||
import org.jivesoftware.smackx.jingle.JingleSessionHandler;
|
||||
import org.jivesoftware.smackx.jingle.element.Jingle;
|
||||
|
@ -88,15 +89,22 @@ public final class JingleFileTransferManager extends Manager {
|
|||
return manager;
|
||||
}
|
||||
|
||||
public JingleFileTransferPayload createPayloadFromFile(File file) {
|
||||
JingleFileTransferPayload.Builder payloadBuilder = JingleFileTransferPayload.getBuilder();
|
||||
|
||||
return payloadBuilder.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* QnD method.
|
||||
* @param file
|
||||
*/
|
||||
public void sendFile(File file, final FullJid recipient) throws IOException, SmackException.NotConnectedException, InterruptedException {
|
||||
final DirtyHelper.BytesAndDigest bd = DirtyHelper.readFile(file, HashManager.ALGORITHM.SHA_256);
|
||||
final byte[] bytes = new byte[(int) file.length()];
|
||||
HashElement hashElement = FileAndHashReader.readAndCalculateHash(file, bytes, HashManager.ALGORITHM.SHA_256);
|
||||
Date lastModified = new Date(file.lastModified());
|
||||
JingleFileTransferPayload payload = new JingleFileTransferPayload(
|
||||
lastModified, "A file", HashManager.assembleHashElement(HashManager.ALGORITHM.SHA_256, bd.getDigest()),
|
||||
lastModified, "A file", hashElement,
|
||||
"application/octet-stream", file.getName(), (int) file.length(), null);
|
||||
ArrayList<JingleContentDescriptionPayloadType> payloadTypes = new ArrayList<>();
|
||||
payloadTypes.add(payload);
|
||||
|
@ -137,7 +145,7 @@ public final class JingleFileTransferManager extends Manager {
|
|||
}
|
||||
|
||||
try {
|
||||
session.getOutputStream().write(bd.getBytes());
|
||||
session.getOutputStream().write(bytes);
|
||||
} catch (IOException e) {
|
||||
LOGGER.log(Level.SEVERE, "Fail while writing: " + e, e);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,51 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2017 Paul Schaub
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jivesoftware.smackx.jingle_filetransfer;
|
||||
|
||||
import org.jivesoftware.smackx.jingle.JingleSession;
|
||||
import org.jxmpp.jid.Jid;
|
||||
|
||||
/**
|
||||
* Class that represents a jingle session in the context of Jingle File Transfer
|
||||
*/
|
||||
public class JingleFileTransferSession extends JingleSession {
|
||||
|
||||
public JingleFileTransferSession(Jid initiator, Jid responder, String sid) {
|
||||
super(initiator, responder, sid);
|
||||
}
|
||||
|
||||
/**
|
||||
* A user might choose to abort all active transfers.
|
||||
*/
|
||||
public void abortAllActiveFileTransfers() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* A user might want to abort the transfer of a single file.
|
||||
*/
|
||||
public void abortSingleFileTransfer() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Successfully end session after all files have been transferred.
|
||||
*/
|
||||
void endSession() {
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2017 Paul Schaub
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jivesoftware.smackx.jingle_filetransfer.callback;
|
||||
|
||||
/**
|
||||
* Callback that allows the user to accept or cancel file transfers.
|
||||
*/
|
||||
public interface IncomingJingleFileTransferCallback {
|
||||
|
||||
void acceptFileTransfer();
|
||||
|
||||
void cancelFileTransfer();
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2017 Paul Schaub
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Smack's API for <a href="https://xmpp.org/extensions/xep-0234.html">XEP-0234: Jingle File Transfer</a>.
|
||||
* Callback classes.
|
||||
*/
|
||||
package org.jivesoftware.smackx.jingle_filetransfer.callback;
|
|
@ -0,0 +1,24 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2017 Paul Schaub
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jivesoftware.smackx.jingle_filetransfer.listener;
|
||||
|
||||
/**
|
||||
* Listener for content-add actions.
|
||||
*/
|
||||
public interface IncomingFileTransferContentAddListener extends IncomingJingleFileTransferListener {
|
||||
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2017 Paul Schaub
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jivesoftware.smackx.jingle_filetransfer.listener;
|
||||
|
||||
import org.jivesoftware.smackx.jingle_filetransfer.callback.IncomingJingleFileTransferCallback;
|
||||
import org.jivesoftware.smackx.jingle_filetransfer.element.JingleFileTransferPayload;
|
||||
|
||||
/**
|
||||
* Listener for incoming file transfers.
|
||||
*/
|
||||
public interface IncomingJingleFileTransferListener {
|
||||
|
||||
void onIncomingJingleFileTransfer(JingleFileTransferPayload file, IncomingJingleFileTransferCallback callback);
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2017 Paul Schaub
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Smack's API for <a href="https://xmpp.org/extensions/xep-0234.html">XEP-0234: Jingle File Transfer</a>.
|
||||
* Listener classes.
|
||||
*/
|
||||
package org.jivesoftware.smackx.jingle_filetransfer.listener;
|
Loading…
Reference in a new issue