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