diff --git a/java/org/slub/rosetta/dps/repository/plugin/SLUBVirusCheckClamAVPlugin.java b/java/org/slub/rosetta/dps/repository/plugin/SLUBVirusCheckClamAVPlugin.java index 18aa467eedd7ddf1c86e3d39873b9c1cf8476a9d..759cef5e01fd8bc96253fd016ce82c225991a29d 100644 --- a/java/org/slub/rosetta/dps/repository/plugin/SLUBVirusCheckClamAVPlugin.java +++ b/java/org/slub/rosetta/dps/repository/plugin/SLUBVirusCheckClamAVPlugin.java @@ -62,7 +62,6 @@ public class SLUBVirusCheckClamAVPlugin implements VirusCheckPluginV2 { private int timeout; private String host; private int port; - private String response; /* Status: * @return 0 if last scan passed (means: virus free) -> PASSED * @return 1 if last scan found a virus -> FAILED @@ -186,28 +185,14 @@ public class SLUBVirusCheckClamAVPlugin implements VirusCheckPluginV2 { Socket socket = new Socket(); try { socket.setSoTimeout(getTimeOut()); - } catch (SocketException e) { - //System.out.println("Could not set socket timeout to " + getTimeOut() + "ms " + e); - log.error( "Could not set socket timeout to " + getTimeOut() + "ms", e); - } - try { socket.setKeepAlive(true); - } catch (SocketException e) { - log.error( "Could not enable KeepAlive", e); - } - try { socket.setReuseAddress( true ); - } catch (SocketException e) { - log.error( "Could not set ReUseAddress", e); - } - //socket.connect( new InetSocketAddress(getHost())); - socket.connect(new InetSocketAddress(getHost(), getPort())); - try { + socket.connect(new InetSocketAddress(getHost(), getPort())); log.debug( "socket has timeout of: " + socket.getSoTimeout() + " ms"); log.debug( "socket has keepalive set: " + socket.getKeepAlive()); log.debug( "socket has reuse adress set: " + socket.getReuseAddress()); } catch (SocketException e) { - log.warn( "Problems to get socket parameters after connect", e); + log.warn( "Problems to open socket", e); } return socket; } @@ -232,49 +217,57 @@ public class SLUBVirusCheckClamAVPlugin implements VirusCheckPluginV2 { } } + private String read_socket_to_buffer(Socket socket, byte[] buffer) throws IOException { + int read = socket.getInputStream().read(buffer); + String response = ""; + if (read > 0) response = new String(buffer, 0, read); + return response; + } - /** calls a simple clamd command via socket + /** + * calls a simple clamd command via socket * * @param socket opened socket - * @param command clamd command * @throws IOException if something goes wrong */ - private void callSocketCommand(Socket socket, byte[] command) throws IOException { + private String callSocketCommand_Version(Socket socket) throws IOException { DataOutputStream dos = null; + String response = ""; try { dos = new DataOutputStream(socket.getOutputStream()); - dos.write(command); - int read; + dos.write(SLUBVirusCheckClamAVPlugin.VERSION); byte[] buffer = new byte[DEFAULT_CHUNK_SIZE]; dos.flush(); - read = socket.getInputStream().read(buffer); - if (read > 0) response = new String(buffer, 0, read); + response = read_socket_to_buffer(socket, buffer).trim(); } finally { closeSocket(socket, dos); } + return response; } - /** calls an extended clamd command via socket, which expects an additional data inputstream which should be sent + + + /** + * calls an extended clamd command via socket, which expects an additional data inputstream which should be sent * * @param socket opened socket - * @param command clamd command - * @param in input stream which should be sent to clamd + * @param in input stream which should be sent to clamd * @throws IOException if something goes wrong */ - private void callSocketCommandStream(Socket socket, byte[] command, InputStream in) throws IOException { + private String callSocketCommand_Stream(Socket socket, InputStream in) throws IOException { DataOutputStream dos = null; + String response = ""; try { dos = new DataOutputStream(socket.getOutputStream()); - dos.write(command); - int read; + dos.write(SLUBVirusCheckClamAVPlugin.INSTREAM); byte[] buffer = new byte[DEFAULT_CHUNK_SIZE]; writeStreamToStream(in, dos, buffer); dos.flush(); - read = socket.getInputStream().read(buffer); - if (read > 0) response = new String(buffer, 0, read); + response = read_socket_to_buffer(socket, buffer).trim(); } finally { closeSocket(socket, dos); } + return response; } /** scans a given file for viruses @@ -287,11 +280,8 @@ public class SLUBVirusCheckClamAVPlugin implements VirusCheckPluginV2 { InputStream in = new FileInputStream(fileFullPath); // send stream - response = ""; - byte[] command = INSTREAM; - callSocketCommandStream(socket, command, in); + String result = callSocketCommand_Stream(socket, in); in.close(); - String result = response.trim(); log.debug( "Response: " + result); //System.out.println("Response: " + result); // parse return code @@ -340,10 +330,7 @@ public class SLUBVirusCheckClamAVPlugin implements VirusCheckPluginV2 { try { // create a socket Socket socket = openSocket(); - byte[] command = VERSION; - response = ""; - callSocketCommand(socket, command); - return response.trim(); + return callSocketCommand_Version(socket); } catch (IOException e) { log.error("exception creation socket in getAgent(), clamd not available at host=" + host + "port=" + port, e); //System.out.println("exception creation socket, clamd not available at host=" + host + "port=" + port + " " + e);