Smack/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/internal/JingleIncomingFileOffer.java

139 lines
4.7 KiB
Java
Raw Normal View History

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-07-26 16:17:33 +02:00
package org.jivesoftware.smackx.jft.internal;
2017-07-28 14:02:32 +02:00
import java.io.File;
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;
import java.io.OutputStream;
2017-07-28 14:02:32 +02:00
import java.util.concurrent.Future;
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-07-27 15:18:18 +02:00
import org.jivesoftware.smackx.jft.controller.IncomingFileOfferController;
2017-07-26 16:17:33 +02:00
import org.jivesoftware.smackx.jft.element.JingleFileTransferChildElement;
2017-07-27 15:18:18 +02:00
import org.jivesoftware.smackx.jft.internal.file.RemoteFile;
2017-07-28 14:02:32 +02:00
import org.jivesoftware.smackx.jingle.components.JingleSession;
import org.jivesoftware.smackx.jingle.element.JingleContentDescriptionInfoElement;
import org.jivesoftware.smackx.jingle.element.JingleElement;
2017-07-26 16:17:33 +02:00
/**
* Created by vanitas on 26.07.17.
*/
2017-07-27 15:18:18 +02:00
public class JingleIncomingFileOffer extends AbstractJingleFileOffer<RemoteFile> implements IncomingFileOfferController {
2017-07-26 16:17:33 +02:00
private static final Logger LOGGER = Logger.getLogger(JingleIncomingFileOffer.class.getName());
private File target;
2017-07-26 16:17:33 +02:00
public JingleIncomingFileOffer(JingleFileTransferChildElement offer) {
super(new RemoteFile(offer));
}
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
public void onTransportReady(BytestreamSession bytestreamSession) {
LOGGER.log(Level.INFO, "Receive file to " + target.getAbsolutePath());
File mFile = target;
if (!mFile.exists()) {
try {
mFile.createNewFile();
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Could not create new File!");
}
}
InputStream inputStream = null;
OutputStream outputStream = null;
2017-07-26 16:17:33 +02:00
try {
2017-07-27 00:12:42 +02:00
inputStream = bytestreamSession.getInputStream();
outputStream = new FileOutputStream(mFile);
byte[] filebuf = new byte[(int) file.getSize()];
int read = 0;
byte[] bufbuf = new byte[4096];
LOGGER.log(Level.INFO, "Begin receiving bytes.");
while (read < filebuf.length) {
int r = inputStream.read(bufbuf);
if (r >= 0) {
System.arraycopy(bufbuf, 0, filebuf, read, r);
read += r;
LOGGER.log(Level.INFO, "Read " + r + " (" + read + " of " + filebuf.length + ") bytes.");
} else {
break;
}
}
outputStream.write(filebuf);
outputStream.flush();
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);
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Could not close InputStream: " + e, e);
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Could not close OutputStream: " + e, e);
}
}
2017-07-26 16:17:33 +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-07-29 19:31:16 +02:00
public Future<Void> accept(XMPPConnection connection, File target)
throws InterruptedException, XMPPException.XMPPErrorException, SmackException.NotConnectedException,
SmackException.NoResponseException {
this.target = target;
2017-07-28 14:02:32 +02:00
JingleSession session = getParent().getParent();
if (session.getSessionState() == JingleSession.SessionState.pending) {
2017-07-29 19:31:16 +02:00
session.accept(connection);
2017-07-28 14:02:32 +02:00
}
return null;
}
2017-07-26 16:17:33 +02:00
}