Search in sources :

Example 1 with Range

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());
}
Also used : Range(io.milton.http.Range)

Example 2 with Range

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());
}
Also used : Range(io.milton.http.Range)

Example 3 with Range

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");
}
Also used : RandomFileOutputStream(io.milton.common.RandomFileOutputStream) RandomAccessFile(java.io.RandomAccessFile) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) GetableResource(io.milton.resource.GetableResource) BadRequestException(io.milton.http.exceptions.BadRequestException) IOException(java.io.IOException) Range(io.milton.http.Range) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 4 with Range

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));
    }
}
Also used : PartialEntity(io.milton.http.entity.PartialEntity) FileOutputStream(java.io.FileOutputStream) Range(io.milton.http.Range) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 5 with Range

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());
}
Also used : 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