Search in sources :

Example 1 with DesEncrypter

use of nl.uva.cs.lobcder.util.DesEncrypter in project lobcder by skoulouzis.

the class WebDataFileResource method transferer.

private PDRI transferer(List<PDRIDescr> pdris, OutputStream out, int tryCount, boolean doCircularStreamBufferTransferer) throws IOException, NotFoundException {
    InputStream in = null;
    PDRI pdri = null;
    try {
        PDRIDescr pdriDescr = selectBestPDRI(pdris);
        pdri = PDRIFactory.getFactory().createInstance(pdriDescr, false);
        if (pdri != null) {
            in = pdri.getData();
            WebDataFileResource.log.log(Level.FINE, "sendContent() for {0}--------- {1}", new Object[] { getPath(), pdri.getFileName() });
            if (!pdri.getEncrypted()) {
                if (doCircularStreamBufferTransferer) {
                    CircularStreamBufferTransferer cBuff = new CircularStreamBufferTransferer((Constants.BUF_SIZE), in, out);
                    cBuff.startTransfer((long) -1);
                } else {
                    int read;
                    byte[] copyBuffer = new byte[Constants.BUF_SIZE];
                    while ((read = in.read(copyBuffer, 0, copyBuffer.length)) != -1) {
                        out.write(copyBuffer, 0, read);
                    }
                }
            } else {
                DesEncrypter encrypter = new DesEncrypter(pdri.getKeyInt());
                encrypter.decrypt(in, out);
            }
        } else {
            sleepTime = 5;
            throw new NotFoundException("Physical resource not found");
        }
    } catch (Exception ex) {
        if (ex instanceof NotFoundException) {
            throw (NotFoundException) ex;
        }
        if (ex.getMessage().contains("Resource not found")) {
            throw new NotFoundException(ex.getMessage());
        }
        try {
            sleepTime = sleepTime + 20;
            Thread.sleep(sleepTime);
            if (ex instanceof nl.uva.vlet.exception.VlInterruptedException && ++tryCount < Constants.RECONNECT_NTRY) {
                transferer(pdris, out, tryCount, false);
            } else if (++tryCount < Constants.RECONNECT_NTRY) {
                transferer(pdris, out, tryCount, true);
            } else {
                transferer(pdris, out, 0, true);
            }
        } catch (InterruptedException ex1) {
            sleepTime = 5;
            throw new IOException(ex1);
        }
    } finally {
        if (in != null) {
            in.close();
        }
    }
    sleepTime = 5;
    return pdri;
}
Also used : CircularStreamBufferTransferer(nl.uva.vlet.io.CircularStreamBufferTransferer) InputStream(java.io.InputStream) PDRIDescr(nl.uva.cs.lobcder.resources.PDRIDescr) NotFoundException(io.milton.http.exceptions.NotFoundException) DesEncrypter(nl.uva.cs.lobcder.util.DesEncrypter) IOException(java.io.IOException) PDRI(nl.uva.cs.lobcder.resources.PDRI) ConflictException(io.milton.http.exceptions.ConflictException) URISyntaxException(java.net.URISyntaxException) SQLException(java.sql.SQLException) BadRequestException(io.milton.http.exceptions.BadRequestException) IOException(java.io.IOException) NotAuthorizedException(io.milton.http.exceptions.NotAuthorizedException) UnknownHostException(java.net.UnknownHostException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NotFoundException(io.milton.http.exceptions.NotFoundException)

Example 2 with DesEncrypter

use of nl.uva.cs.lobcder.util.DesEncrypter in project lobcder by skoulouzis.

the class VPDRI method doCopy.

private void doCopy(VFile file, Range range, OutputStream out, boolean decript) throws VlException, IOException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
    long len = range.getFinish() - range.getStart() + 1;
    InputStream in = null;
    int buffSize;
    Long start = range.getStart();
    if (len <= Constants.BUF_SIZE) {
        buffSize = (int) len;
    } else {
        buffSize = Constants.BUF_SIZE;
    }
    DesEncrypter en = null;
    if (decript) {
        en = new DesEncrypter(getKeyInt());
    }
    int read = 0;
    try {
        if (file instanceof VRandomReadable) {
            VRandomReadable ra = (VRandomReadable) file;
            byte[] buff = new byte[buffSize];
            int totalBytesRead = 0;
            while (totalBytesRead < len || read != -1) {
                long startT = System.currentTimeMillis();
                read = ra.readBytes(start, buff, 0, buff.length);
                Logger.getLogger(VPDRI.class.getName()).log(Level.FINEST, "speed: {0} kb/s", (read / 1024.0) / ((System.currentTimeMillis() - startT) / 1000.0));
                if (read == -1 || totalBytesRead == len) {
                    break;
                }
                totalBytesRead += read;
                start += buff.length;
                if (decript) {
                    byte[] tmp = en.decrypt(buff);
                    buff = tmp;
                }
                out.write(buff, 0, read);
            }
        } else {
            if (start > 0) {
                throw new IOException("Backend at " + vrl.getScheme() + "://" + vrl.getHostname() + "does not support random reads");
            // long skiped = in.skip(start);
            // if (skiped != start) {
            // long n = start;
            // int buflen = (int) Math.min(Constants.BUF_SIZE, n);
            // byte data[] = new byte[buflen];
            // while (n > 0) {
            // int r = in.read(data, 0, (int) Math.min((long) buflen, n));
            // if (r < 0) {
            // break;
            // }
            // n -= r;
            // }
            // }
            }
            // int totalBytesRead = 0;
            // byte[] buff = new byte[buffSize];
            // while (totalBytesRead < len || read != -1) {
            // if (read == -1 || totalBytesRead == len) {
            // break;
            // }
            // read = in.read(buff, 0, buff.length);
            // totalBytesRead += read;
            // start += buff.length;
            // out.write(buff, 0, read);
            // }
            in = getData();
            if (decript) {
                InputStream tmp = en.wrapInputStream(in);
                in = tmp;
            }
            CircularStreamBufferTransferer cBuff = new CircularStreamBufferTransferer(buffSize, in, out);
            cBuff.startTransfer(len);
        }
    } finally {
        if (in != null) {
            in.close();
        }
    }
}
Also used : CircularStreamBufferTransferer(nl.uva.vlet.io.CircularStreamBufferTransferer) DesEncrypter(nl.uva.cs.lobcder.util.DesEncrypter) VRandomReadable(nl.uva.vlet.vrs.io.VRandomReadable)

Example 3 with DesEncrypter

use of nl.uva.cs.lobcder.util.DesEncrypter in project lobcder by skoulouzis.

the class WebDataFileResource method transfer.

private PDRI transfer(List<PDRIDescr> pdris, OutputStream out, int tryCount, boolean doCircularStreamBufferTransferer) throws IOException, NotFoundException {
    InputStream in = null;
    PDRI pdri = null;
    double start = 0;
    Logger.getLogger(WebDataFileResource.class.getName()).log(Level.FINEST, "Start for {0}", getLogicalData().getName());
    try {
        PDRIDescr pdriDescr = selectBestPDRI(pdris);
        pdri = PDRIFactory.getFactory().createInstance(pdriDescr, false);
        Logger.getLogger(WebDataFileResource.class.getName()).log(Level.FINEST, "pdri: {0}", pdri.getURI());
        if (pdri != null) {
            start = System.currentTimeMillis();
            in = pdri.getData();
            if (!pdri.getEncrypted()) {
                if (doCircularStreamBufferTransferer) {
                    CircularStreamBufferTransferer cBuff = new CircularStreamBufferTransferer((Constants.BUF_SIZE), in, out);
                    cBuff.startTransfer((long) -1);
                } else {
                    int read;
                    byte[] copyBuffer = new byte[Constants.BUF_SIZE];
                    while ((read = in.read(copyBuffer, 0, copyBuffer.length)) != -1) {
                        out.write(copyBuffer, 0, read);
                    // tos.write(copyBuffer, 0, read);
                    }
                }
            } else {
                DesEncrypter encrypter = new DesEncrypter(pdri.getKeyInt());
                encrypter.decrypt(in, out);
            }
        } else {
            sleepTime = 5;
            throw new NotFoundException("Physical resource not found");
        }
    } catch (Exception ex) {
        if (ex instanceof NotFoundException) {
            throw (NotFoundException) ex;
        }
        if (ex.getMessage().contains("Resource not found")) {
            throw new NotFoundException(ex.getMessage());
        }
        if (pdri != null) {
            Double speed = weightPDRIMap.get(pdri.getHost());
            if (speed != null) {
                speed = speed - 10;
            } else {
                speed = Double.valueOf(-10);
            }
            weightPDRIMap.put(pdri.getHost(), speed);
        }
        try {
            sleepTime = sleepTime + 5;
            Thread.sleep(sleepTime);
            if (ex instanceof nl.uva.vlet.exception.VlInterruptedException && ++tryCount < Constants.RECONNECT_NTRY) {
                transfer(pdris, out, tryCount, false);
            } else if (++tryCount < Constants.RECONNECT_NTRY) {
                transfer(pdris, out, tryCount, false);
            } else {
                transfer(pdris, out, 0, false);
            }
        } catch (InterruptedException ex1) {
            sleepTime = 5;
            throw new IOException(ex1);
        }
    } finally {
        if (in != null) {
            in.close();
        }
    }
    sleepTime = 5;
    double elapsed = System.currentTimeMillis() - start;
    double speed = ((pdri.getLength() * 8.0) * 1000.0) / (elapsed * 1000.0);
    try {
        String msg = "File: " + pdri.getFileName() + " Destination: " + new URI(pdri.getURI()).getScheme() + "://" + pdri.getHost() + " Rx_Speed: " + speed + " Kbites/sec Rx_Size: " + (pdri.getLength()) + " bytes Elapsed_Time: " + elapsed + " ms";
        Logger.getLogger(WebDataFileResource.class.getName()).log(Level.INFO, msg);
    } catch (URISyntaxException ex) {
    }
    return pdri;
}
Also used : CircularStreamBufferTransferer(nl.uva.vlet.io.CircularStreamBufferTransferer) InputStream(java.io.InputStream) PDRIDescr(nl.uva.cs.lobcder.resources.PDRIDescr) NotFoundException(io.milton.http.exceptions.NotFoundException) DesEncrypter(nl.uva.cs.lobcder.util.DesEncrypter) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) PDRI(nl.uva.cs.lobcder.resources.PDRI) ConflictException(io.milton.http.exceptions.ConflictException) URISyntaxException(java.net.URISyntaxException) SQLException(java.sql.SQLException) BadRequestException(io.milton.http.exceptions.BadRequestException) IOException(java.io.IOException) NotAuthorizedException(io.milton.http.exceptions.NotAuthorizedException) UnknownHostException(java.net.UnknownHostException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NotFoundException(io.milton.http.exceptions.NotFoundException)

Example 4 with DesEncrypter

use of nl.uva.cs.lobcder.util.DesEncrypter in project lobcder by skoulouzis.

the class VPDRI method putData.

@Override
public void putData(InputStream in) throws IOException {
    OutputStream out = null;
    double start = System.currentTimeMillis();
    // VFile tmpFile = null;
    try {
        // upload(in);
        VRL parentVrl = vrl.getParent();
        VDir remoteDir = getVfsClient().mkdirs(parentVrl, true);
        getVfsClient().createFile(vrl, true);
        out = getVfsClient().getFile(vrl).getOutputStream();
        if (!getEncrypted()) {
            // CircularStreamBufferTransferer cBuff = new CircularStreamBufferTransferer((Constants.BUF_SIZE), in, out);
            // cBuff.startTransfer(new Long(-1));
            int read;
            byte[] copyBuffer = new byte[Constants.BUF_SIZE];
            while ((read = in.read(copyBuffer, 0, copyBuffer.length)) != -1) {
                out.write(copyBuffer, 0, read);
            }
        } else {
            DesEncrypter encrypter = new DesEncrypter(getKeyInt());
            encrypter.encrypt(in, out);
        }
        reconnectAttemts = 0;
    } catch (nl.uva.vlet.exception.VlAuthenticationException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException ex) {
        throw new IOException(ex);
    } catch (VlException ex) {
        if (ex.getMessage() != null) {
            Logger.getLogger(VPDRI.class.getName()).log(Level.FINE, "\tVlException {0}", ex.getMessage());
        }
        if (reconnectAttemts <= 2) {
            Logger.getLogger(VPDRI.class.getName()).log(Level.FINE, "\treconnectAttemts {0}", reconnectAttemts);
            reconnect();
            putData(in);
        } else {
            throw new IOException(ex);
        }
    // if (ex instanceof ResourceNotFoundException || ex.getMessage().contains("not found") || ex.getMessage().contains("Couldn open location") || ex.getMessage().contains("not found in container")) {
    // try {
    // vfsClient.mkdirs(vrl.getParent(), true);
    // vfsClient.createFile(vrl, true);
    // putData(in);
    // } catch (VlException ex1) {
    // throw new IOException(ex1);
    // }
    // }
    } finally {
        if (out != null) {
            try {
                out.flush();
                out.close();
            } catch (java.io.IOException ex) {
            }
        }
        if (in != null) {
            in.close();
        }
    }
    double elapsed = System.currentTimeMillis() - start;
    double speed = ((getLength() * 8.0) * 1000.0) / (elapsed * 1000.0);
    try {
        String msg = "File: " + this.getFileName() + " Destination: " + new URI(getURI()).getScheme() + "://" + getHost() + " Rx_Speed: " + speed + " Kbites/sec Rx_Size: " + (getLength()) + " bytes Elapsed_Time: " + elapsed + " ms";
        Logger.getLogger(VPDRI.class.getName()).log(Level.INFO, msg);
    } catch (URISyntaxException ex) {
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) java.io(java.io) VlException(nl.uva.vlet.exception.VlException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) DesEncrypter(nl.uva.cs.lobcder.util.DesEncrypter) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) URISyntaxException(java.net.URISyntaxException) InvalidKeyException(java.security.InvalidKeyException) URI(java.net.URI) VRL(nl.uva.vlet.vrl.VRL)

Example 5 with DesEncrypter

use of nl.uva.cs.lobcder.util.DesEncrypter in project lobcder by skoulouzis.

the class CachePDRI method putData.

@Override
public void putData(InputStream data) throws IOException, FileNotFoundException {
    // asyncPut.run();
    if (!getEncrypted()) {
        try {
            setResourceContent(data);
        } catch (VlException ex) {
            throw new IOException(ex);
        }
    } else {
        try {
            OutputStream out = new FileOutputStream(file);
            DesEncrypter encrypter = new DesEncrypter(getKeyInt());
            encrypter.decrypt(getData(), out);
        } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException ex) {
            throw new IOException(ex);
        }
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) VlException(nl.uva.vlet.exception.VlException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) DesEncrypter(nl.uva.cs.lobcder.util.DesEncrypter) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException)

Aggregations

DesEncrypter (nl.uva.cs.lobcder.util.DesEncrypter)5 URISyntaxException (java.net.URISyntaxException)3 CircularStreamBufferTransferer (nl.uva.vlet.io.CircularStreamBufferTransferer)3 BadRequestException (io.milton.http.exceptions.BadRequestException)2 ConflictException (io.milton.http.exceptions.ConflictException)2 NotAuthorizedException (io.milton.http.exceptions.NotAuthorizedException)2 NotFoundException (io.milton.http.exceptions.NotFoundException)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 URI (java.net.URI)2 UnknownHostException (java.net.UnknownHostException)2 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)2 InvalidKeyException (java.security.InvalidKeyException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 SQLException (java.sql.SQLException)2 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)2 PDRI (nl.uva.cs.lobcder.resources.PDRI)2 PDRIDescr (nl.uva.cs.lobcder.resources.PDRIDescr)2 VlException (nl.uva.vlet.exception.VlException)2