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