Search in sources :

Example 6 with NotFoundException

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

the class WebDataFileResource method sendContent.

@Override
public void sendContent(OutputStream out, Range range, Map<String, String> params, String contentType) throws IOException, NotAuthorizedException, BadRequestException, NotFoundException {
    double start = System.currentTimeMillis();
    PDRI pdri;
    Iterator<PDRIDescr> it;
    try {
        List<PDRIDescr> pdris = getCatalogue().getPdriDescrByGroupId(getLogicalData().getPdriGroupId());
        if (pdris.size() <= 0) {
            throw new NotFoundException("File inconsistency! Could not find physical file!");
        }
        // it = getCatalogue().getPdriDescrByGroupId(getLogicalData().getPdriGroupId()).iterator();
        if (range != null) {
            if (range.getFinish() == null) {
                range = new Range(range.getStart(), (getLogicalData().getLength() - 1));
            }
            it = pdris.iterator();
            Logger.getLogger(WebDataFileResource.class.getName()).log(Level.FINEST, "Start: {0} end: {1} range: {2}", new Object[] { range.getStart(), range.getFinish(), range.getRange() });
            pdri = transfererRange(it, out, 0, null, range);
        } else {
            // pdri = transfer(it, out, 0, null, false);
            pdri = transfer(pdris, out, 0, false);
        }
    } catch (SQLException ex) {
        throw new BadRequestException(this, ex.getMessage());
    } catch (IOException ex) {
        if (ex.getMessage().contains("Resource not found") || ex.getMessage().contains("Couldn't locate path")) {
            throw new NotFoundException(ex.getMessage());
        } else {
            throw new BadRequestException(this, ex.getMessage());
        }
    } finally {
    // Don't close the output, we need it to send back the response
    // if (out != null) {
    // out.flush();
    // out.close();
    // }
    }
    double elapsed = System.currentTimeMillis() - start;
    long len;
    if (range != null) {
        len = range.getFinish() - range.getStart() + 1;
    } else {
        len = this.getLogicalData().getLength();
    }
    double speed = ((len * 8.0) * 1000.0) / (elapsed * 1000.0);
    Double oldSpeed = weightPDRIMap.get(pdri.getHost());
    if (oldSpeed == null) {
        oldSpeed = speed;
    }
    Integer numOfGets = numOfGetsMap.get(pdri.getHost());
    if (numOfGets == null) {
        numOfGets = 1;
    }
    double averagre = (speed + oldSpeed) / (double) numOfGets;
    numOfGetsMap.put(pdri.getHost(), numOfGets++);
    weightPDRIMap.put(pdri.getHost(), averagre);
    getCatalogue().addViewForRes(getLogicalData().getUid());
    Stats stats = new Stats();
    stats.setSource(pdri.getHost());
    stats.setDestination(fromAddress);
    stats.setSpeed(speed);
    stats.setSize(getLogicalData().getLength());
    String msg = "Source: " + stats.getSource() + " Destination: " + stats.getDestination() + " Tx_Speed: " + speed + " Kbites/sec Tx_Size: " + getLogicalData().getLength() + " bytes Elapsed_Time: " + elapsed + " ms";
    try {
        if (!pdri.isCahce()) {
            getCatalogue().setSpeed(stats);
        }
    } catch (SQLException ex) {
        Logger.getLogger(WebDataFileResource.class.getName()).log(Level.SEVERE, null, ex);
    }
    Logger.getLogger(WebDataFileResource.class.getName()).log(Level.INFO, msg);
}
Also used : SQLException(java.sql.SQLException) PDRIDescr(nl.uva.cs.lobcder.resources.PDRIDescr) NotFoundException(io.milton.http.exceptions.NotFoundException) IOException(java.io.IOException) Range(io.milton.http.Range) PDRI(nl.uva.cs.lobcder.resources.PDRI) Stats(nl.uva.cs.lobcder.rest.wrappers.Stats) BadRequestException(io.milton.http.exceptions.BadRequestException)

Example 7 with NotFoundException

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

the class LoginResponseHandler method attemptRespondLoginPage.

private void attemptRespondLoginPage(Request request, Resource resource, Response response) throws RuntimeException {
    Resource rLogin;
    try {
        rLogin = resourceFactory.getResource(request.getHostHeader(), loginPage);
    } catch (NotAuthorizedException e) {
        throw new RuntimeException(e);
    } catch (BadRequestException ex) {
        throw new RuntimeException(ex);
    }
    if (rLogin == null || !(rLogin instanceof GetableResource)) {
        log.info("Couldnt find login resource: " + request.getHostHeader() + loginPage + " with resource factory: " + resourceFactory.getClass());
        wrapped.respondUnauthorised(resource, response, request);
    } else {
        log.trace("respond with 200 to suppress login prompt, using resource: " + rLogin.getName() + " - " + rLogin.getClass());
        try {
            // set request attribute so rendering knows it authorisation failed, or authentication is required
            Auth auth = request.getAuthorization();
            if (auth != null && auth.getTag() != null) {
                // no authentication was attempted,
                request.getAttributes().put("authReason", "notPermitted");
            } else {
                request.getAttributes().put("authReason", "required");
            }
            GetableResource gr = (GetableResource) rLogin;
            wrapped.respondContent(gr, response, request, null);
        } catch (NotAuthorizedException ex) {
            throw new RuntimeException(ex);
        } catch (BadRequestException ex) {
            throw new RuntimeException(ex);
        } catch (NotFoundException ex) {
            throw new RuntimeException(ex);
        }
    }
}
Also used : GetableResource(io.milton.resource.GetableResource) Resource(io.milton.resource.Resource) BadRequestException(io.milton.http.exceptions.BadRequestException) GetableResource(io.milton.resource.GetableResource) NotFoundException(io.milton.http.exceptions.NotFoundException) NotAuthorizedException(io.milton.http.exceptions.NotAuthorizedException)

Example 8 with NotFoundException

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

the class FsFileResource method sendContent.

@Override
public void sendContent(OutputStream out, Range range, Map<String, String> params, String contentType) throws IOException, NotFoundException {
    InputStream in = null;
    try {
        in = contentService.getFileContent(file);
        if (range != null) {
            log.debug("sendContent: ranged content: " + file.getAbsolutePath());
            RangeUtils.writeRange(in, range, out);
        } else {
            log.debug("sendContent: send whole file " + file.getAbsolutePath());
            IOUtils.copy(in, out);
        }
        out.flush();
    } catch (FileNotFoundException e) {
        throw new NotFoundException("Couldnt locate content");
    } catch (ReadingException e) {
        throw new IOException(e);
    } catch (WritingException e) {
        throw new IOException(e);
    } finally {
        IOUtils.closeQuietly(in);
    }
}
Also used : ReadingException(io.milton.common.ReadingException) NotFoundException(io.milton.http.exceptions.NotFoundException) WritingException(io.milton.common.WritingException)

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