2017-07-27 17:35:16 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2017-08-14 13:58:48 +02:00
|
|
|
package org.jivesoftware.smackx.jingle_filetransfer.component;
|
2017-07-26 16:17:33 +02:00
|
|
|
|
2017-07-28 14:02:32 +02:00
|
|
|
import java.io.File;
|
2017-07-29 22:21:36 +02:00
|
|
|
import java.io.FileOutputStream;
|
2017-07-26 16:17:33 +02:00
|
|
|
import java.io.IOException;
|
2017-07-27 00:12:42 +02:00
|
|
|
import java.io.InputStream;
|
2017-07-29 22:21:36 +02:00
|
|
|
import java.io.OutputStream;
|
2017-07-26 16:17:33 +02:00
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.logging.Logger;
|
|
|
|
|
2017-07-29 19:31:16 +02:00
|
|
|
import org.jivesoftware.smack.SmackException;
|
|
|
|
import org.jivesoftware.smack.XMPPConnection;
|
|
|
|
import org.jivesoftware.smack.XMPPException;
|
2017-07-26 16:17:33 +02:00
|
|
|
import org.jivesoftware.smackx.bytestreams.BytestreamSession;
|
2017-08-04 23:05:59 +02:00
|
|
|
import org.jivesoftware.smackx.jingle.component.JingleSession;
|
2017-07-28 14:02:32 +02:00
|
|
|
import org.jivesoftware.smackx.jingle.element.JingleContentDescriptionInfoElement;
|
|
|
|
import org.jivesoftware.smackx.jingle.element.JingleElement;
|
2017-08-14 22:15:23 +02:00
|
|
|
import org.jivesoftware.smackx.jingle_filetransfer.controller.IncomingFileOfferController;
|
|
|
|
import org.jivesoftware.smackx.jingle_filetransfer.element.JingleFileTransferChildElement;
|
2017-07-26 16:17:33 +02:00
|
|
|
|
|
|
|
/**
|
2017-08-13 14:38:53 +02:00
|
|
|
* Behind the scenes logic of an incoming Jingle file offer.
|
2017-07-26 16:17:33 +02:00
|
|
|
* Created by vanitas on 26.07.17.
|
|
|
|
*/
|
2017-08-13 19:44:02 +02:00
|
|
|
public class JingleIncomingFileOffer extends AbstractJingleFileOffer<JingleFileTransferFile.RemoteFile> implements IncomingFileOfferController {
|
2017-07-26 16:17:33 +02:00
|
|
|
|
|
|
|
private static final Logger LOGGER = Logger.getLogger(JingleIncomingFileOffer.class.getName());
|
2017-08-15 17:43:06 +02:00
|
|
|
private OutputStream target;
|
2017-07-29 22:21:36 +02:00
|
|
|
|
2017-07-26 16:17:33 +02:00
|
|
|
public JingleIncomingFileOffer(JingleFileTransferChildElement offer) {
|
2017-08-13 19:44:02 +02:00
|
|
|
super(new JingleFileTransferFile.RemoteFile(offer));
|
2017-07-26 16:17:33 +02:00
|
|
|
}
|
|
|
|
|
2017-07-28 14:02:32 +02:00
|
|
|
@Override
|
|
|
|
public JingleElement handleDescriptionInfo(JingleContentDescriptionInfoElement info) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-07-26 16:17:33 +02:00
|
|
|
@Override
|
2017-07-30 15:40:04 +02:00
|
|
|
public void onBytestreamReady(BytestreamSession bytestreamSession) {
|
2017-08-15 17:43:06 +02:00
|
|
|
if (target == null) {
|
|
|
|
throw new IllegalStateException("Target OutputStream is null");
|
2017-07-29 22:21:36 +02:00
|
|
|
}
|
|
|
|
|
2017-08-15 17:43:06 +02:00
|
|
|
LOGGER.log(Level.INFO, "Receive file");
|
|
|
|
|
2017-07-29 22:21:36 +02:00
|
|
|
InputStream inputStream = null;
|
2017-07-26 16:17:33 +02:00
|
|
|
try {
|
2017-07-27 00:12:42 +02:00
|
|
|
inputStream = bytestreamSession.getInputStream();
|
2017-07-29 22:21:36 +02:00
|
|
|
|
2017-07-31 14:21:49 +02:00
|
|
|
int length = 0;
|
2017-07-29 22:21:36 +02:00
|
|
|
int read = 0;
|
|
|
|
byte[] bufbuf = new byte[4096];
|
2017-07-31 14:21:49 +02:00
|
|
|
while ((length = inputStream.read(bufbuf)) >= 0) {
|
2017-08-15 17:43:06 +02:00
|
|
|
target.write(bufbuf, 0, length);
|
2017-07-31 14:21:49 +02:00
|
|
|
read += length;
|
|
|
|
LOGGER.log(Level.INFO, "Read " + read + " (" + length + ") of " + file.getSize() + " bytes.");
|
|
|
|
if (read == (int) file.getSize()) {
|
2017-07-29 22:21:36 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-07-31 14:21:49 +02:00
|
|
|
LOGGER.log(Level.INFO, "Reading/Writing finished.");
|
2017-07-26 16:17:33 +02:00
|
|
|
} catch (IOException e) {
|
2017-07-27 00:12:42 +02:00
|
|
|
LOGGER.log(Level.SEVERE, "Cannot get InputStream from BytestreamSession: " + e, e);
|
2017-07-29 22:21:36 +02:00
|
|
|
} finally {
|
|
|
|
if (inputStream != null) {
|
|
|
|
try {
|
|
|
|
inputStream.close();
|
2017-07-31 14:21:49 +02:00
|
|
|
LOGGER.log(Level.INFO, "CipherInputStream closed.");
|
2017-07-29 22:21:36 +02:00
|
|
|
} catch (IOException e) {
|
|
|
|
LOGGER.log(Level.SEVERE, "Could not close InputStream: " + e, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-15 17:43:06 +02:00
|
|
|
if (target != null) {
|
2017-07-29 22:21:36 +02:00
|
|
|
try {
|
2017-08-15 17:43:06 +02:00
|
|
|
target.close();
|
2017-07-31 14:21:49 +02:00
|
|
|
LOGGER.log(Level.INFO, "FileOutputStream closed.");
|
2017-07-29 22:21:36 +02:00
|
|
|
} catch (IOException e) {
|
|
|
|
LOGGER.log(Level.SEVERE, "Could not close OutputStream: " + e, e);
|
|
|
|
}
|
|
|
|
}
|
2017-07-26 16:17:33 +02:00
|
|
|
}
|
2017-07-29 22:21:36 +02:00
|
|
|
notifyProgressListenersFinished();
|
2017-07-26 16:17:33 +02:00
|
|
|
}
|
2017-07-27 16:43:09 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isOffer() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isRequest() {
|
|
|
|
return false;
|
|
|
|
}
|
2017-07-28 14:02:32 +02:00
|
|
|
|
|
|
|
@Override
|
2017-08-15 17:43:06 +02:00
|
|
|
public void accept(XMPPConnection connection, File target)
|
2017-07-29 19:31:16 +02:00
|
|
|
throws InterruptedException, XMPPException.XMPPErrorException, SmackException.NotConnectedException,
|
2017-08-15 17:43:06 +02:00
|
|
|
SmackException.NoResponseException, IOException {
|
|
|
|
|
|
|
|
if (!target.exists()) {
|
|
|
|
target.createNewFile();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.target = new FileOutputStream(target);
|
|
|
|
|
2017-07-28 14:02:32 +02:00
|
|
|
JingleSession session = getParent().getParent();
|
|
|
|
if (session.getSessionState() == JingleSession.SessionState.pending) {
|
2017-08-07 23:55:32 +02:00
|
|
|
session.sendAccept(connection);
|
2017-07-28 14:02:32 +02:00
|
|
|
}
|
2017-08-15 17:43:06 +02:00
|
|
|
}
|
2017-07-28 14:02:32 +02:00
|
|
|
|
2017-08-15 17:43:06 +02:00
|
|
|
@Override
|
|
|
|
public void accept(XMPPConnection connection, OutputStream stream)
|
|
|
|
throws InterruptedException, XMPPException.XMPPErrorException, SmackException.NotConnectedException,
|
|
|
|
SmackException.NoResponseException {
|
|
|
|
target = stream;
|
|
|
|
|
|
|
|
JingleSession session = getParent().getParent();
|
|
|
|
if (session.getSessionState() == JingleSession.SessionState.pending) {
|
|
|
|
session.sendAccept(connection);
|
|
|
|
}
|
2017-07-28 14:02:32 +02:00
|
|
|
}
|
2017-08-13 14:38:53 +02:00
|
|
|
|
|
|
|
@Override
|
2017-08-13 19:44:02 +02:00
|
|
|
public JingleFileTransferFile.RemoteFile getFile() {
|
|
|
|
return (JingleFileTransferFile.RemoteFile) this.file;
|
2017-08-13 14:38:53 +02:00
|
|
|
}
|
2017-07-26 16:17:33 +02:00
|
|
|
}
|