use of io.milton.http.Range in project lobcder by skoulouzis.
the class PartialGetHelperTest method testGetRange_single.
public void testGetRange_single() {
List<Range> ranges = partialGetHelper.getRanges("bytes=0-499");
assertNotNull(ranges);
assertEquals(1, ranges.size());
Range r = ranges.get(0);
assertEquals(0l, r.getStart().longValue());
assertEquals(499l, r.getFinish().longValue());
}
use of io.milton.http.Range in project lobcder by skoulouzis.
the class PartialGetHelperTest method testGetRange_second_half.
public void testGetRange_second_half() {
List<Range> ranges = partialGetHelper.getRanges("bytes=-100");
assertNotNull(ranges);
assertEquals(1, ranges.size());
Range r = ranges.get(0);
assertNull(r.getStart());
assertEquals(100, r.getFinish().intValue());
}
use of io.milton.http.Range in project lobcder by skoulouzis.
the class PutHandler method processReplace.
/**
* "If an existing resource is modified, either the 200 (OK) or 204 (No
* Content) response codes SHOULD be sent to indicate successful completion
* of the request."
*
* @param request
* @param response
* @param replacee
*/
private void processReplace(HttpManager manager, Request request, Response response, ReplaceableResource replacee) throws BadRequestException, NotAuthorizedException, ConflictException, NotFoundException {
if (!handlerHelper.checkAuthorisation(manager, replacee, request)) {
responseHandler.respondUnauthorised(replacee, response, request);
return;
}
try {
Range range = putHelper.parseContentRange(replacee, request);
if (range != null) {
log.debug("partial put: " + range);
if (replacee instanceof PartialllyUpdateableResource) {
log.debug("doing partial put on a PartialllyUpdateableResource");
PartialllyUpdateableResource partialllyUpdateableResource = (PartialllyUpdateableResource) replacee;
partialllyUpdateableResource.replacePartialContent(range, request.getInputStream());
} else if (replacee instanceof GetableResource) {
log.debug("doing partial put on a GetableResource");
File tempFile = File.createTempFile("milton-partial", null);
RandomAccessFile randomAccessFile = null;
// The new length of the resource
long length;
try {
randomAccessFile = new RandomAccessFile(tempFile, "rw");
RandomFileOutputStream tempOut = new RandomFileOutputStream(tempFile);
GetableResource gr = (GetableResource) replacee;
// Update the content with the supplied partial content, and get the result as an inputstream
gr.sendContent(tempOut, null, null, null);
// Calculate new length, if the partial put is extending it
length = randomAccessFile.length();
if (range.getFinish() + 1 > length) {
length = range.getFinish() + 1;
}
randomAccessFile.setLength(length);
randomAccessFile.seek(range.getStart());
int numBytesRead;
byte[] copyBuffer = new byte[1024];
InputStream newContent = request.getInputStream();
while ((numBytesRead = newContent.read(copyBuffer)) != -1) {
randomAccessFile.write(copyBuffer, 0, numBytesRead);
}
} finally {
FileUtils.close(randomAccessFile);
}
InputStream updatedContent = new FileInputStream(tempFile);
BufferedInputStream bufin = new BufferedInputStream(updatedContent);
// Now, finally, we can just do a normal update
replacee.replaceContent(bufin, length);
} else {
throw new BadRequestException(replacee, "Cant apply partial update. Resource does not support PartialllyUpdateableResource or GetableResource");
}
} else {
// Not a partial update, but resource implements Replaceable, so give it the new data
Long l = request.getContentLengthHeader();
replacee.replaceContent(request.getInputStream(), l);
}
} catch (IOException ex) {
log.warn("IOException reading input stream. Probably interrupted upload: " + ex.getMessage());
return;
}
// Respond with a 204
responseHandler.respondNoContent(replacee, response, request);
log.debug("process: finished");
}
use of io.milton.http.Range in project lobcder by skoulouzis.
the class PartialGetHelper method sendPartialContent.
public void sendPartialContent(GetableResource resource, Request request, Response response, List<Range> ranges, Map<String, String> params) throws NotAuthorizedException, BadRequestException, IOException, NotFoundException {
log.trace("sendPartialContent");
if (ranges.size() == 1) {
log.trace("partial get, single range");
Range r = ranges.get(0);
responseHandler.respondPartialContent(resource, response, request, params, r);
} else {
log.trace("partial get, multiple ranges");
File temp = File.createTempFile("milton_partial_get", null);
FileOutputStream fout = null;
try {
fout = new FileOutputStream(temp);
BufferedOutputStream bufOut = new BufferedOutputStream(fout);
resource.sendContent(bufOut, null, params, request.getContentTypeHeader());
bufOut.flush();
fout.flush();
} finally {
StreamUtils.close(fout);
}
response.setEntity(new PartialEntity(ranges, temp));
}
}
use of io.milton.http.Range in project lobcder by skoulouzis.
the class PartialGetHelperTest method testGetRange_first_half.
public void testGetRange_first_half() {
List<Range> ranges = partialGetHelper.getRanges("bytes=100-");
assertNotNull(ranges);
assertEquals(1, ranges.size());
Range r = ranges.get(0);
assertEquals(100, r.getStart().intValue());
assertNull(r.getFinish());
}
Aggregations