Search in sources :

Example 1 with NotFoundException

use of io.milton.http.exceptions.NotFoundException 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 NotFoundException

use of io.milton.http.exceptions.NotFoundException in project lobcder by skoulouzis.

the class WebDataFileResource method transfererRange.

// private PDRI transfer(Iterator<PDRIDescr> it, OutputStream out, int tryCount, PDRI pdri, boolean doCircularStreamBufferTransferer) throws IOException, NotFoundException {
// InputStream in;
// try {
// boolean reconnect;
// if (pdri == null && it.hasNext()) {
// pdri = PDRIFactory.getFactory().createInstance(it.next(), false);
// reconnect = false;
// } else {
// reconnect = true;
// }
// if (pdri != null) {
// if (reconnect) {
// pdri.reconnect();
// }
// in = pdri.getData();
// Logger.getLogger(WebDataFileResource.class.getName()).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;
// }
// try {
// sleepTime = sleepTime + 20;
// Thread.sleep(sleepTime);
// if (ex instanceof nl.uva.vlet.exception.VlInterruptedException && ++tryCount < Constants.RECONNECT_NTRY) {
// transfer(it, out, tryCount, pdri, false);
// } else if (++tryCount < Constants.RECONNECT_NTRY) {
// transfer(it, out, tryCount, pdri, false);
// } else {
// transfer(it, out, 0, null, true);
// }
// } catch (InterruptedException ex1) {
// sleepTime = 5;
// throw new IOException(ex1);
// }
// }
// sleepTime = 5;
// return pdri;
// }
private PDRI transfererRange(Iterator<PDRIDescr> it, OutputStream out, int tryCount, PDRI pdri, Range range) throws IOException, NotFoundException {
    try {
        boolean reconnect;
        if (pdri == null && it.hasNext()) {
            pdri = PDRIFactory.getFactory().createInstance(it.next(), false);
            reconnect = false;
        } else {
            reconnect = true;
        }
        if (pdri != null) {
            if (reconnect) {
                pdri.reconnect();
            }
            pdri.copyRange(range, out);
        // if (!) {
        // 
        // } else {
        // 
        // DesEncrypter encrypter = new DesEncrypter(pdri.getKeyInt());
        // encrypter.decrypt(pdri.getData(), out);
        // }
        } else {
            sleepTime = 5;
            throw new NotFoundException("Physical resource not found");
        }
    } catch (IOException | java.lang.IllegalStateException ex) {
        if (!ex.getMessage().contains("does not support random reads")) {
            try {
                sleepTime = sleepTime + 20;
                Thread.sleep(sleepTime);
                if (++tryCount < Constants.RECONNECT_NTRY) {
                    transfererRange(it, out, tryCount, pdri, range);
                } else {
                    transfererRange(it, out, 0, null, range);
                }
            } catch (InterruptedException ex1) {
                sleepTime = 5;
                throw new IOException(ex1);
            }
        } else {
            sleepTime = 5;
            throw new IOException(ex);
        }
    }
    sleepTime = 5;
    return pdri;
}
Also used : NotFoundException(io.milton.http.exceptions.NotFoundException) IOException(java.io.IOException)

Example 3 with NotFoundException

use of io.milton.http.exceptions.NotFoundException in project lobcder by skoulouzis.

the class CompressingResponseHandler method respondContent.

@Override
public void respondContent(Resource resource, Response response, Request request, Map<String, String> params) throws NotAuthorizedException, BadRequestException, NotFoundException {
    if (resource instanceof GetableResource) {
        GetableResource r = (GetableResource) resource;
        String acceptableContentTypes = request.getAcceptHeader();
        String contentType = r.getContentType(acceptableContentTypes);
        // Experimental support for already compressed content...
        String acceptableEncodings = request.getAcceptEncodingHeader();
        if (r instanceof CompressedResource) {
            CompressedResource compressedResource = (CompressedResource) r;
            String acceptableEncoding = compressedResource.getSupportedEncoding(acceptableEncodings);
            if (acceptableEncoding != null) {
                response.setContentTypeHeader(contentType);
                cacheControlHelper.setCacheControl(r, response, request.getAuthorization());
                Long contentLength = compressedResource.getCompressedContentLength(acceptableEncoding);
                response.setContentLengthHeader(contentLength);
                response.setContentEncodingHeader(Response.ContentEncoding.GZIP);
                response.setVaryHeader("Accept-Encoding");
                response.setEntity(new CompressedResourceEntity(compressedResource, params, contentType, acceptableEncoding));
                return;
            }
        }
        if (canCompress(r, contentType, acceptableEncodings)) {
            log.trace("respondContent: compressable");
            // get the zipped content before sending so we can determine its
            // compressed size
            BufferingOutputStream tempOut = new BufferingOutputStream(maxMemorySize);
            try {
                OutputStream gzipOut = new GZIPOutputStream(tempOut);
                r.sendContent(gzipOut, null, params, contentType);
                gzipOut.flush();
                gzipOut.close();
                tempOut.flush();
            } catch (NotFoundException e) {
                throw e;
            } catch (Exception ex) {
                tempOut.deleteTempFileIfExists();
                throw new RuntimeException(ex);
            } finally {
                FileUtils.close(tempOut);
            }
            log.trace("respondContent-compressed: " + resource.getClass());
            setRespondContentCommonHeaders(response, resource, Response.Status.SC_OK, request.getAuthorization());
            response.setContentEncodingHeader(Response.ContentEncoding.GZIP);
            response.setVaryHeader("Accept-Encoding");
            Long contentLength = tempOut.getSize();
            if (contentLength != null) {
                response.setContentLengthHeader(contentLength);
            }
            response.setContentTypeHeader(contentType);
            cacheControlHelper.setCacheControl(r, response, request.getAuthorization());
            response.setEntity(new InputStreamEntity(tempOut.getInputStream()));
        } else {
            log.trace("respondContent: not compressable");
            // We really should set this header, but it causes IE to not cache files (eg images)
            // response.setVaryHeader( "Accept-Encoding" );
            wrapped.respondContent(resource, response, request, params);
        }
    } else {
        throw new RuntimeException("Cant generate content for non-Getable resource: " + resource.getClass());
    }
}
Also used : CompressedResourceEntity(io.milton.http.entity.CompressedResourceEntity) GZIPOutputStream(java.util.zip.GZIPOutputStream) OutputStream(java.io.OutputStream) BufferingOutputStream(io.milton.common.BufferingOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) BufferingOutputStream(io.milton.common.BufferingOutputStream) GetableResource(io.milton.resource.GetableResource) NotFoundException(io.milton.http.exceptions.NotFoundException) BadRequestException(io.milton.http.exceptions.BadRequestException) NotAuthorizedException(io.milton.http.exceptions.NotAuthorizedException) NotFoundException(io.milton.http.exceptions.NotFoundException) InputStreamEntity(io.milton.http.entity.InputStreamEntity)

Example 4 with NotFoundException

use of io.milton.http.exceptions.NotFoundException in project lobcder by skoulouzis.

the class MiltonFtpFile method createInputStream.

@Override
public InputStream createInputStream(long offset) throws IOException {
    if (r instanceof GetableResource) {
        GetableResource gr = (GetableResource) r;
        String ct = gr.getContentType(null);
        BufferingOutputStream out = new BufferingOutputStream(50000);
        try {
            gr.sendContent(out, null, null, ct);
            out.close();
            return out.getInputStream();
        } catch (NotFoundException ex) {
            log.warn("Not found exception", ex);
            return null;
        } catch (BadRequestException ex) {
            log.warn("bad request", ex);
            return null;
        } catch (NotAuthorizedException ex) {
            log.warn("not authorising", ex);
            return null;
        }
    } else {
        return null;
    }
}
Also used : BufferingOutputStream(io.milton.common.BufferingOutputStream) NotFoundException(io.milton.http.exceptions.NotFoundException) BadRequestException(io.milton.http.exceptions.BadRequestException) NotAuthorizedException(io.milton.http.exceptions.NotAuthorizedException)

Example 5 with NotFoundException

use of io.milton.http.exceptions.NotFoundException 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)

Aggregations

NotFoundException (io.milton.http.exceptions.NotFoundException)8 BadRequestException (io.milton.http.exceptions.BadRequestException)6 NotAuthorizedException (io.milton.http.exceptions.NotAuthorizedException)5 IOException (java.io.IOException)4 SQLException (java.sql.SQLException)3 PDRI (nl.uva.cs.lobcder.resources.PDRI)3 PDRIDescr (nl.uva.cs.lobcder.resources.PDRIDescr)3 BufferingOutputStream (io.milton.common.BufferingOutputStream)2 ConflictException (io.milton.http.exceptions.ConflictException)2 GetableResource (io.milton.resource.GetableResource)2 InputStream (java.io.InputStream)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 URISyntaxException (java.net.URISyntaxException)2 UnknownHostException (java.net.UnknownHostException)2 DesEncrypter (nl.uva.cs.lobcder.util.DesEncrypter)2 CircularStreamBufferTransferer (nl.uva.vlet.io.CircularStreamBufferTransferer)2 ReadingException (io.milton.common.ReadingException)1 WritingException (io.milton.common.WritingException)1 Range (io.milton.http.Range)1 CompressedResourceEntity (io.milton.http.entity.CompressedResourceEntity)1