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);
}
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;
}
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;
}
}
Aggregations