Search in sources :

Example 6 with Range

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

use of io.milton.http.Range in project lobcder by skoulouzis.

the class PutHelper method parseContentRange.

/**
 * Largly copied from tomcat
 *
 * See the spec
 * http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
 *
 * @param r
 * @param request
 * @return
 * @throws IOException
 * @throws BadRequestException - if the range header is invalid
 */
public Range parseContentRange(Resource r, Request request) throws IOException, BadRequestException {
    // Retrieving the content-range header (if any is specified
    String rangeHeader = request.getContentRangeHeader();
    if (rangeHeader == null) {
        return null;
    }
    // bytes is the only range unit supported
    if (!rangeHeader.startsWith("bytes")) {
        log.warn("Invalid range header, does not start with 'bytes': " + rangeHeader);
        throw new BadRequestException(r);
    }
    rangeHeader = rangeHeader.substring(6).trim();
    int dashPos = rangeHeader.indexOf('-');
    int slashPos = rangeHeader.indexOf('/');
    if (dashPos == -1) {
        log.warn("Invalid range header, dash not found: " + rangeHeader);
        throw new BadRequestException(r);
    }
    if (slashPos == -1) {
        log.warn("Invalid range header, slash not found: " + rangeHeader);
        throw new BadRequestException(r);
    }
    String s;
    long start;
    s = rangeHeader.substring(0, dashPos);
    try {
        start = Long.parseLong(s);
    } catch (NumberFormatException e) {
        log.warn("Invalid range header, start is not a valid number: " + s + " Raw header:" + rangeHeader);
        throw new BadRequestException(r);
    }
    long finish;
    s = rangeHeader.substring(dashPos + 1, slashPos);
    try {
        finish = Long.parseLong(s);
    } catch (NumberFormatException e) {
        log.warn("Invalid range header, finish is not a valid number: " + s + " Raw header:" + rangeHeader);
        throw new BadRequestException(r);
    }
    Range range = new Range(start, finish);
    if (!validate(range)) {
        throw new BadRequestException(r);
    }
    return range;
}
Also used : BadRequestException(io.milton.http.exceptions.BadRequestException) Range(io.milton.http.Range)

Example 8 with Range

use of io.milton.http.Range in project lobcder by skoulouzis.

the class PartialGetHelper method getRanges.

public List<Range> getRanges(String rangeHeader) {
    if (rangeHeader == null || rangeHeader.length() == 0) {
        log.trace("getRanges: no range header");
        return null;
    }
    if (rangeHeader.startsWith("bytes=")) {
        rangeHeader = rangeHeader.substring(6);
        String[] arr = rangeHeader.split(",");
        List<Range> list = new ArrayList<Range>();
        for (String s : arr) {
            Range r = Range.parse(s);
            list.add(r);
        }
        if (log.isTraceEnabled()) {
            log.trace("getRanges: header: " + rangeHeader + " parsed ranges: " + list.size());
        }
        return list;
    } else {
        return null;
    }
}
Also used : ArrayList(java.util.ArrayList) Range(io.milton.http.Range)

Aggregations

Range (io.milton.http.Range)8 BadRequestException (io.milton.http.exceptions.BadRequestException)3 File (java.io.File)2 IOException (java.io.IOException)2 RandomFileOutputStream (io.milton.common.RandomFileOutputStream)1 PartialEntity (io.milton.http.entity.PartialEntity)1 NotFoundException (io.milton.http.exceptions.NotFoundException)1 GetableResource (io.milton.resource.GetableResource)1 BufferedInputStream (java.io.BufferedInputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 RandomAccessFile (java.io.RandomAccessFile)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 PDRI (nl.uva.cs.lobcder.resources.PDRI)1 PDRIDescr (nl.uva.cs.lobcder.resources.PDRIDescr)1 Stats (nl.uva.cs.lobcder.rest.wrappers.Stats)1