001/** 002 * Copyright (C) 2014 Universidade de Aveiro, DETI/IEETA, Bioinformatics Group - http://bioinformatics.ua.pt/ 003 * 004 * This file is part of Dicoogle/dicoogle. 005 * 006 * Dicoogle/dicoogle is free software: you can redistribute it and/or modify 007 * it under the terms of the GNU General Public License as published by 008 * the Free Software Foundation, either version 3 of the License, or 009 * (at your option) any later version. 010 * 011 * Dicoogle/dicoogle is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with Dicoogle. If not, see <http://www.gnu.org/licenses/>. 018 */ 019 020package pt.ua.dicoogle.rGUI.fileTransfer; 021 022import java.io.File; 023import java.io.FileInputStream; 024import java.io.IOException; 025import java.io.OutputStream; 026import java.net.InetAddress; 027import java.net.ServerSocket; 028import java.net.Socket; 029import java.net.SocketTimeoutException; 030import org.slf4j.Logger; 031import org.slf4j.LoggerFactory; 032import org.slf4j.Logger; 033import org.slf4j.LoggerFactory; 034import javax.net.ServerSocketFactory; 035import javax.net.ssl.SSLServerSocketFactory; 036 037/** 038 * 039 * @author Samuel Campos <samuelcampos@ua.pt> 040 */ 041@Deprecated 042public class FileSender extends Thread { 043 private File file; 044 private InetAddress client; 045 private int timeout = 2000; //2 seconds of timeout 046 047 private ServerSocket ssocket; 048 private Socket socket; 049 050 public FileSender(File file, InetAddress client) throws IOException{ 051 ServerSocketFactory ssocketFactory = SSLServerSocketFactory.getDefault(); 052 ssocket = ssocketFactory.createServerSocket(); 053 ssocket.bind(null); 054 ssocket.setSoTimeout(timeout); 055 056 this.file = file; 057 this.client = client; 058 } 059 060 @Override 061 public void run(){ 062 long sizeTransfered; 063 064 try { 065 sizeTransfered = sender(); 066 067 //if(sizeTransfered != -1) 068 // DebugManager.getInstance().debug("Transfer complete! File: " + file.getName()); 069 //else 070 //(DebugManager.getInstance().debug("There was an error transfering the file: " + file.getName()); 071 072 } catch (IllegalAccessException ex) { 073 LoggerFactory.getLogger(FileSender.class).error(ex.getMessage(), ex); 074 } 075 076 return; 077 } 078 079 /** 080 * 081 * @return the listener port 082 */ 083 public int getListenerPort(){ 084 return ssocket.getLocalPort(); 085 } 086 087 088 /** 089 * 090 * @return the number of bytes transfered or -1 in error case 091 * @throws IllegalAccessException 092 */ 093 private long sender() throws IllegalAccessException{ 094 long transferedBytes = 0; 095 byte data[] = new byte[1024]; 096 097 try { 098 try{ 099 socket = ssocket.accept(); 100 } catch(SocketTimeoutException ex){ 101 //DebugManager.getInstance().debug("Timeout Transfering File: " + file.getName()); 102 103 return -1; 104 } 105 106 /* 107 boolean illegalAccess = true; 108 109 InetAddress[] all = InetAddress.getAllByName(client.getHostName()); 110 111 for (int i = 0; i < all.length; i++){ 112 if (socket.getInetAddress().equals(all[i])){ 113 illegalAccess = false; 114 break; 115 } 116 } 117 118 if(illegalAccess && socket.getInetAddress().equals(InetAddress.getByName("0.0.0.0"))) 119 illegalAccess = false; 120 121 if(illegalAccess){ 122 ssocket.close(); 123 socket.close(); 124 125 throw new IllegalAccessException("The IP that tried to establish the connection is not the client!\n"); 126 } 127 */ 128 129 ssocket.close(); // close the listening server socket 130 131 FileInputStream in = new FileInputStream(file); 132 OutputStream out = socket.getOutputStream(); 133 134 // Transfer the file 135 int size; 136 while ((size = in.read(data)) != -1) { 137 transferedBytes += size; 138 139 out.write(data, 0, size); 140 out.flush(); 141 } 142 143 // Freeing resources 144 out.close(); 145 in.close(); 146 socket.close(); 147 148 } catch (Exception ex) { 149 // LoggerFactory.getLogger(FileSender.class).error(ex.getMessage(), ex); 150 151 return -1; 152 } 153 154 return transferedBytes; 155 } 156}