Package org.restlet.data

Examples of org.restlet.data.Range


                    try {
                        // The temporary file used for partial PUT.
                        tmp = new File(file.getCanonicalPath() + "."
                                + getTemporaryExtension());
                        // Support only one range.
                        Range range = request.getRanges().get(0);

                        if (tmp.exists() && !isResumeUpload()) {
                            BioUtils.delete(tmp);
                        }

                        if (!tmp.exists()) {
                            // Copy the target file.
                            InputStream in = new FileInputStream(file);
                            OutputStream out = new FileOutputStream(tmp);
                            BioUtils.copy(in, out);
                            out.flush();
                            out.close();
                        }

                        raf = new RandomAccessFile(tmp, "rwd");

                        // Go to the desired offset.
                        if (range.getIndex() == Range.INDEX_LAST) {
                            if (raf.length() <= range.getSize()) {
                                raf.seek(range.getSize());
                            } else {
                                raf.seek(raf.length() - range.getSize());
                            }
                        } else {
                            raf.seek(range.getIndex());
                        }

                        // Write the entity to the temporary file.
                        if (request.isEntityAvailable()) {
                            BioUtils.copy(request.getEntity().getStream(), raf);
                        }
                    } catch (IOException ioe) {
                        getLogger().log(Level.WARNING,
                                "Unable to create the temporary file", ioe);
                        response.setStatus(new Status(
                                Status.SERVER_ERROR_INTERNAL,
                                "Unable to create a temporary file"));
                        error = true;
                    } finally {
                        try {
                            if (raf != null) {
                                raf.close();
                            }
                        } catch (IOException ioe) {
                            getLogger().log(Level.WARNING,
                                    "Unable to close the temporary file", ioe);
                            response.setStatus(Status.SERVER_ERROR_INTERNAL,
                                    ioe);
                            error = true;
                        }
                    }
                } else {
                    FileOutputStream fos = null;
                    try {
                        tmp = File.createTempFile("restlet-upload", "bin");
                        if (request.isEntityAvailable()) {
                            fos = new FileOutputStream(tmp);
                            BioUtils.copy(request.getEntity().getStream(), fos);
                        }
                    } catch (IOException ioe) {
                        getLogger().log(Level.WARNING,
                                "Unable to create the temporary file", ioe);
                        response.setStatus(new Status(
                                Status.SERVER_ERROR_INTERNAL,
                                "Unable to create a temporary file"));
                        error = true;
                    } finally {
                        try {
                            if (fos != null) {
                                fos.close();
                            }
                        } catch (IOException ioe) {
                            getLogger().log(Level.WARNING,
                                    "Unable to close the temporary file", ioe);
                            response.setStatus(Status.SERVER_ERROR_INTERNAL,
                                    ioe);
                            error = true;
                        }
                    }
                }

                if (error) {
                    if (tmp.exists() && !isResumeUpload()) {
                        BioUtils.delete(tmp);
                    }

                    return;
                }

                // Then delete the existing file
                if (tmp.exists() && BioUtils.delete(file)) {
                    // Finally move the temporary file to the existing file
                    // location
                    boolean renameSuccessful = false;
                    if (tmp.renameTo(file)) {
                        if (request.getEntity() == null) {
                            response.setStatus(Status.SUCCESS_NO_CONTENT);
                        } else {
                            response.setStatus(Status.SUCCESS_OK);
                        }

                        renameSuccessful = true;
                    } else {
                        // Many aspects of the behavior of the method "renameTo"
                        // are inherently platform-dependent: the rename
                        // operation might not be able to move a file from one
                        // file system to another.
                        if (tmp.exists()) {
                            try {
                                InputStream in = new FileInputStream(tmp);
                                OutputStream out = new FileOutputStream(file);
                                BioUtils.copy(in, out);
                                out.close();
                                renameSuccessful = true;
                                BioUtils.delete(tmp);
                            } catch (Exception e) {
                                renameSuccessful = false;
                            }
                        }
                        if (!renameSuccessful) {
                            getLogger()
                                    .log(Level.WARNING,
                                            "Unable to move the temporary file to replace the existing file");
                            response.setStatus(new Status(
                                    Status.SERVER_ERROR_INTERNAL,
                                    "Unable to move the temporary file to replace the existing file"));
                        }
                    }
                } else {
                    getLogger().log(Level.WARNING,
                            "Unable to delete the existing file");
                    response.setStatus(new Status(Status.SERVER_ERROR_INTERNAL,
                            "Unable to delete the existing file"));

                    if (tmp.exists() && !isResumeUpload()) {
                        BioUtils.delete(tmp);
                    }
                }
            } else {
                // The file does not exist yet.
                File parent = file.getParentFile();

                if ((parent != null) && !parent.exists()) {
                    // Create the parent directories then the new file
                    if (!parent.mkdirs()) {
                        getLogger().log(Level.WARNING,
                                "Unable to create the parent directory");
                        response.setStatus(new Status(
                                Status.SERVER_ERROR_INTERNAL,
                                "Unable to create the parent directory"));
                    }
                }

                // Create the new file
                if (partialPut) {
                    // This is a partial PUT
                    RandomAccessFile raf = null;
                    try {
                        raf = new RandomAccessFile(file, "rwd");
                        // Support only one range.
                        Range range = request.getRanges().get(0);
                        // Go to the desired offset.
                        if (range.getIndex() == Range.INDEX_LAST) {
                            if (raf.length() <= range.getSize()) {
                                raf.seek(range.getSize());
                            } else {
                                raf.seek(raf.length() - range.getSize());
                            }
                        } else {
                            raf.seek(range.getIndex());
                        }
                        // Write the entity to the file.
                        if (request.isEntityAvailable()) {
                            BioUtils.copy(request.getEntity().getStream(), raf);
                        }
View Full Code Here


                long startIndex = (index == 0) ? Range.INDEX_LAST : Long
                        .parseLong(value.substring(0, index));
                long endIndex = Long.parseLong(value.substring(index + 1,
                        index1));

                representation.setRange(new Range(startIndex, endIndex
                        - startIndex + 1));
            }

            String strLength = value.substring(index1 + 1, value.length());
            if (!("*".equals(strLength))) {
View Full Code Here

                    if (tab.length == 2) {
                        index = Long.parseLong(tab[0]);
                        length = Long.parseLong(tab[1]) - index + 1;
                    }
                }
                result.add(new Range(index, length));
            }
        }

        return result;
    }
View Full Code Here

                        if (request.getRanges().size() == 1
                                && (!request.getConditions().hasSomeRange() || request
                                        .getConditions()
                                        .getRangeStatus(response.getEntity())
                                        .isSuccess())) {
                            Range requestedRange = request.getRanges().get(0);

                            if (response.getEntity().getSize() == Representation.UNKNOWN_SIZE) {
                                if ((requestedRange.getIndex() == Range.INDEX_LAST || requestedRange
                                        .getSize() == Range.SIZE_MAX)
                                        && !(requestedRange.getIndex() == Range.INDEX_LAST && requestedRange
                                                .getSize() == Range.SIZE_MAX)) {
                                    // The end index cannot be properly computed
                                    response.setStatus(Status.SERVER_ERROR_INTERNAL);
                                    getLogger()
                                            .warning(
                                                    "Unable to serve this range since at least the end index of the range cannot be computed.");
                                    response.setEntity(null);
                                }
                            } else if (!requestedRange.equals(response
                                    .getEntity().getRange())) {
                                if (rangedEntity) {
                                    getLogger()
                                            .info("The range of the response entity is not equal to the requested one.");
                                }
View Full Code Here

        assertEquals(10, response.getEntity().getSize());
        assertEquals(10, response.getEntity().getAvailableSize());

        request = new Request(Method.GET, "http://localhost:" + TEST_PORT
                + "/testGet");
        request.setRanges(Arrays.asList(new Range(0, 10)));
        response = client.handle(request);
        assertEquals(Status.SUCCESS_PARTIAL_CONTENT, response.getStatus());
        assertEquals("1234567890", response.getEntity().getText());
        assertEquals(10, response.getEntity().getSize());
        assertEquals(10, response.getEntity().getAvailableSize());
        assertEquals(0, response.getEntity().getRange().getIndex());
        assertEquals(10, response.getEntity().getRange().getSize());

        request.setRanges(Arrays.asList(new Range(Range.INDEX_FIRST, 2)));
        response = client.handle(request);
        assertEquals(Status.SUCCESS_PARTIAL_CONTENT, response.getStatus());
        assertEquals("12", response.getEntity().getText());
        assertEquals(10, response.getEntity().getSize());
        assertEquals(2, response.getEntity().getAvailableSize());
        assertEquals(0, response.getEntity().getRange().getIndex());
        assertEquals(2, response.getEntity().getRange().getSize());

        request.setRanges(Arrays.asList(new Range(2, 2)));
        response = client.handle(request);
        assertEquals(Status.SUCCESS_PARTIAL_CONTENT, response.getStatus());
        assertEquals("34", response.getEntity().getText());
        assertEquals(10, response.getEntity().getSize());
        assertEquals(2, response.getEntity().getAvailableSize());
        assertEquals(2, response.getEntity().getRange().getIndex());
        assertEquals(2, response.getEntity().getRange().getSize());

        request.setRanges(Arrays.asList(new Range(2, 7)));
        response = client.handle(request);
        assertEquals(Status.SUCCESS_PARTIAL_CONTENT, response.getStatus());
        assertEquals("3456789", response.getEntity().getText());
        assertEquals(10, response.getEntity().getSize());
        assertEquals(7, response.getEntity().getAvailableSize());
        assertEquals(2, response.getEntity().getRange().getIndex());
        assertEquals(7, response.getEntity().getRange().getSize());

        request.setRanges(Arrays.asList(new Range(Range.INDEX_LAST, 7)));
        response = client.handle(request);
        assertEquals(Status.SUCCESS_PARTIAL_CONTENT, response.getStatus());
        assertEquals("4567890", response.getEntity().getText());
        assertEquals(10, response.getEntity().getSize());
        assertEquals(7, response.getEntity().getAvailableSize());
        assertEquals(3, response.getEntity().getRange().getIndex());
        assertEquals(7, response.getEntity().getRange().getSize());

        request.setRanges(Arrays.asList(new Range(2, Range.SIZE_MAX)));
        response = client.handle(request);
        assertEquals(Status.SUCCESS_PARTIAL_CONTENT, response.getStatus());
        assertEquals("34567890", response.getEntity().getText());
        assertEquals(10, response.getEntity().getSize());
        assertEquals(8, response.getEntity().getAvailableSize());
View Full Code Here

        Request request = new Request(Method.GET, "http://localhost:"
                + TEST_PORT + "/testGet");
        Response response = client.handle(request);
        Tag entityTag = response.getEntity().getTag();

        request.setRanges(Arrays.asList(new Range(1, Range.SIZE_MAX)));
        request.getConditions().setRangeTag(entityTag);
        response = client.handle(request);
        assertEquals(Status.SUCCESS_PARTIAL_CONTENT, response.getStatus());
        assertEquals("234567890", response.getEntity().getText());
        assertEquals(10, response.getEntity().getSize());
        assertEquals(9, response.getEntity().getAvailableSize());
        assertEquals(1, response.getEntity().getRange().getIndex());
        assertEquals(9, response.getEntity().getRange().getSize());

        entityTag = new Tag(entityTag.getName() + "-test");
        request.setRanges(Arrays.asList(new Range(1, Range.SIZE_MAX)));
        request.getConditions().setRangeTag(entityTag);
        response = client.handle(request);
        assertEquals(Status.SUCCESS_OK, response.getStatus());
        assertEquals("1234567890", response.getEntity().getText());
        client.stop();
View Full Code Here

            // PUT on a file that does not exist
            request = new Request(Method.PUT, "http://localhost:" + TEST_PORT
                    + "/testPut/essai.txt");
            request.setEntity(new StringRepresentation("1234567890"));
            request.setRanges(Arrays.asList(new Range(0, 10)));
            response = client.handle(request);
            assertEquals(Status.SUCCESS_OK, response.getStatus());
            response = client.handle(new Request(Method.GET, request
                    .getResourceRef()));
            assertEquals(Status.SUCCESS_OK, response.getStatus());
            assertEquals("1234567890", response.getEntity().getText());

            // Partial PUT on a file, the provided representation overflowed the
            // existing file
            request = new Request(Method.PUT, "http://localhost:" + TEST_PORT
                    + "/testPut/essai.txt");
            request.setEntity(new StringRepresentation("0000000000"));
            request.setRanges(Arrays.asList(new Range(1, 10)));
            response = client.handle(request);
            System.out.println(response.getStatus() + " / "
                    + response.getStatus().getThrowable());
            assertEquals(Status.SUCCESS_OK, response.getStatus());
            response = client.handle(new Request(Method.GET, request
                    .getResourceRef()));
            assertEquals(Status.SUCCESS_OK, response.getStatus());
            assertEquals("10000000000", response.getEntity().getText());

            // Partial PUT on a file that does not exists, the provided range
            // does not start at the 0 index.
            request = new Request(Method.PUT, "http://localhost:" + TEST_PORT
                    + "/testPut/essai2.txt");
            request.setEntity(new StringRepresentation("0000000000"));
            request.setRanges(Arrays.asList(new Range(1, 10)));
            response = client.handle(request);
            assertEquals(Status.SUCCESS_OK, response.getStatus());
            request.setMethod(Method.GET);
            response = client.handle(request);
            assertEquals(Status.SUCCESS_PARTIAL_CONTENT, response.getStatus());
            assertEquals("0000000000", response.getEntity().getText());

            // Partial PUT on a file, simple range
            request = new Request(Method.PUT, "http://localhost:" + TEST_PORT
                    + "/testPut/essai.txt");
            request.setEntity(new StringRepresentation("22"));
            request.setRanges(Arrays.asList(new Range(2, 2)));
            response = client.handle(request);
            assertEquals(Status.SUCCESS_OK, response.getStatus());
            response = client.handle(new Request(Method.GET, request
                    .getResourceRef()));
            assertEquals(Status.SUCCESS_OK, response.getStatus());
            assertEquals("10220000000", response.getEntity().getText());

            // Partial PUT on a file, the provided representation will be padded
            // at the very end of the file.
            request = new Request(Method.PUT, "http://localhost:" + TEST_PORT
                    + "/testPut/essai.txt");
            request.setEntity(new StringRepresentation("888"));
            request.setRanges(Arrays.asList(new Range(8, Range.SIZE_MAX)));
            response = client.handle(request);
            assertEquals(Status.SUCCESS_OK, response.getStatus());
            response = client.handle(new Request(Method.GET, request
                    .getResourceRef()));
            assertEquals(Status.SUCCESS_OK, response.getStatus());
            assertEquals("10220000888", response.getEntity().getText());

            // Partial PUT on a file that does not exist, the range does not
            // specify the range size.
            request = new Request(Method.PUT, "http://localhost:" + TEST_PORT
                    + "/testPut/essai3.txt");
            request.setEntity(new StringRepresentation("888"));
            request.setRanges(Arrays.asList(new Range(8, Range.SIZE_MAX)));
            response = client.handle(request);
            assertEquals(Status.SUCCESS_OK, response.getStatus());
            request.setMethod(Method.GET);
            response = client.handle(request);
            assertEquals(Status.SUCCESS_PARTIAL_CONTENT, response.getStatus());
            assertEquals("888", response.getEntity().getText());

            // Partial PUT on a file, the provided representation will be padded
            // just before the end of the file.
            request = new Request(Method.PUT, "http://localhost:" + TEST_PORT
                    + "/testPut/essai.txt");
            request.setEntity(new StringRepresentation("99"));
            request.setRanges(Arrays.asList(new Range(8, Range.SIZE_MAX)));
            response = client.handle(request);
            assertEquals(Status.SUCCESS_OK, response.getStatus());
            response = client.handle(new Request(Method.GET, request
                    .getResourceRef()));
            assertEquals(Status.SUCCESS_OK, response.getStatus());
            assertEquals("10220000998", response.getEntity().getText());

            request = new Request(Method.GET, "http://localhost:" + TEST_PORT
                    + "/testPut/essai.txt");
            request.setRanges(Arrays.asList(new Range(3, Range.SIZE_MAX)));
            response = client.handle(request);
            assertEquals(Status.SUCCESS_PARTIAL_CONTENT, response.getStatus());
            assertEquals("20000998", response.getEntity().getText());

            BioUtils.delete(testDir, true);
View Full Code Here

        Response response;

        // Test "range" header.
        request = new Request(Method.GET, "http://localhost:" + TEST_PORT
                + "/test?range=0-500");
        request.setRanges(Arrays.asList(new Range(0, 500)));
        response = client.handle(request);
        assertTrue(response.getStatus().isSuccess());
        response.getEntity().exhaust();

        request = new Request(Method.GET, "http://localhost:" + TEST_PORT
                + "/test?range=-500");
        request.setRanges(Arrays.asList(new Range(Range.INDEX_LAST, 500)));
        response = client.handle(request);
        assertTrue(response.getStatus().isSuccess());
        response.getEntity().exhaust();

        request = new Request(Method.GET, "http://localhost:" + TEST_PORT
                + "/test?range=500-");
        request.setRanges(Arrays.asList(new Range(500, Range.SIZE_MAX)));
        response = client.handle(request);
        assertTrue(response.getStatus().isSuccess());
        response.getEntity().exhaust();

        request = new Request(Method.GET, "http://localhost:" + TEST_PORT
                + "/test?range=500-1000");
        request.setRanges(Arrays.asList(new Range(500, 500)));

        response = client.handle(request);
        assertTrue(response.getStatus().isSuccess());
        response.getEntity().exhaust();
View Full Code Here

public class RangeRepresentationTestCase extends RestletTestCase {

    public void testAppendable() throws Exception {
        StringRepresentation sr = new StringRepresentation("1234567890");
        RangeRepresentation rr = new RangeRepresentation(sr);
        rr.setRange(new Range(2, 5));
        assertNull(sr.getRange());
        assertEquals(10, sr.getAvailableSize());
        assertEquals(5, rr.getAvailableSize());
        assertEquals("1234567890", sr.getText());
        assertEquals("34567", rr.getText());
View Full Code Here

                            try {
                                // The temporary file used for partial PUT.
                                tmp = new File(file.getCanonicalPath() + "."
                                        + getTemporaryExtension());
                                // Support only one range.
                                Range range = request.getRanges().get(0);

                                if (tmp.exists() && !isResumeUpload()) {
                                    tmp.delete();
                                }

                                if (!tmp.exists()) {
                                    // Copy the target file.
                                    InputStream in = new FileInputStream(file);
                                    OutputStream out = new FileOutputStream(tmp);
                                    ByteUtils.write(in, out);
                                    out.flush();
                                    out.close();
                                }
                                raf = new RandomAccessFile(tmp, "rwd");

                                // Go to the desired offset.
                                if (range.getIndex() == Range.INDEX_LAST) {
                                    if (raf.length() <= range.getSize()) {
                                        raf.seek(range.getSize());
                                    } else {
                                        raf
                                                .seek(raf.length()
                                                        - range.getSize());
                                    }
                                } else {
                                    raf.seek(range.getIndex());
                                }

                                // Write the entity to the temporary file.
                                if (request.isEntityAvailable()) {
                                    ByteUtils.write(request.getEntity()
                                            .getStream(), raf);
                                }
                            } catch (IOException ioe) {
                                getLogger().log(Level.WARNING,
                                        "Unable to create the temporary file",
                                        ioe);
                                response.setStatus(new Status(
                                        Status.SERVER_ERROR_INTERNAL,
                                        "Unable to create a temporary file"));
                                error = true;
                            } finally {
                                try {
                                    if (raf != null) {
                                        raf.close();

                                        // Calling the garbage collector helps
                                        // to workaround lock issues on Windows
                                        System.gc();
                                    }
                                } catch (IOException ioe) {
                                    getLogger()
                                            .log(
                                                    Level.WARNING,
                                                    "Unable to close the temporary file",
                                                    ioe);
                                    response.setStatus(
                                            Status.SERVER_ERROR_INTERNAL, ioe);
                                    error = true;
                                }
                            }
                        } else {
                            FileOutputStream fos = null;
                            try {
                                tmp = File.createTempFile("restlet-upload",
                                        "bin");
                                if (request.isEntityAvailable()) {
                                    fos = new FileOutputStream(tmp);
                                    ByteUtils.write(request.getEntity()
                                            .getStream(), fos);
                                }
                            } catch (IOException ioe) {
                                getLogger().log(Level.WARNING,
                                        "Unable to create the temporary file",
                                        ioe);
                                response.setStatus(new Status(
                                        Status.SERVER_ERROR_INTERNAL,
                                        "Unable to create a temporary file"));
                                error = true;
                            } finally {
                                try {
                                    if (fos != null) {
                                        fos.close();
                                    }
                                } catch (IOException ioe) {
                                    getLogger()
                                            .log(
                                                    Level.WARNING,
                                                    "Unable to close the temporary file",
                                                    ioe);
                                    response.setStatus(
                                            Status.SERVER_ERROR_INTERNAL, ioe);
                                    error = true;
                                }
                            }
                        }

                        if (error) {
                            if (tmp.exists() && !isResumeUpload()) {
                                tmp.delete();
                            }
                            return;
                        }

                        // Then delete the existing file
                        if (tmp.exists() && file.delete()) {
                            // Finally move the temporary file to the
                            // existing file location
                            boolean renameSuccessfull = false;
                            if (tmp.renameTo(file)) {
                                if (request.getEntity() == null) {
                                    response
                                            .setStatus(Status.SUCCESS_NO_CONTENT);
                                } else {
                                    response.setStatus(Status.SUCCESS_OK);
                                }
                                renameSuccessfull = true;
                            } else {
                                // Many aspects of the behavior of the method
                                // "renameTo" are inherently platform-dependent:
                                // the rename operation might not be able to
                                // move a file from one filesystem to another.
                                if (tmp.exists()) {
                                    try {
                                        InputStream in = new FileInputStream(
                                                tmp);
                                        OutputStream out = new FileOutputStream(
                                                file);
                                        ByteUtils.write(in, out);
                                        out.flush();
                                        out.close();
                                        renameSuccessfull = true;
                                        tmp.delete();
                                    } catch (Exception e) {
                                        renameSuccessfull = false;
                                    }
                                }
                                if (!renameSuccessfull) {
                                    getLogger()
                                            .log(Level.WARNING,
                                                    "Unable to move the temporary file to replace the existing file");
                                    response
                                            .setStatus(new Status(
                                                    Status.SERVER_ERROR_INTERNAL,
                                                    "Unable to move the temporary file to replace the existing file"));
                                }
                            }
                        } else {
                            getLogger().log(Level.WARNING,
                                    "Unable to delete the existing file");
                            response.setStatus(new Status(
                                    Status.SERVER_ERROR_INTERNAL,
                                    "Unable to delete the existing file"));
                            if (tmp.exists() && !isResumeUpload()) {
                                tmp.delete();
                            }
                        }
                    } else {
                        // The file does not exist yet.
                        final File parent = file.getParentFile();
                        if ((parent != null) && !parent.exists()) {
                            // Create the parent directories then the new file
                            if (!parent.mkdirs()) {
                                getLogger()
                                        .log(Level.WARNING,
                                                "Unable to create the parent directory");
                                response
                                        .setStatus(new Status(
                                                Status.SERVER_ERROR_INTERNAL,
                                                "Unable to create the parent directory"));
                            }
                        }
                        // Create the new file
                        if (partialPut) {
                            // This is a partial PUT
                            RandomAccessFile raf = null;
                            try {
                                raf = new RandomAccessFile(file, "rwd");
                                // Support only one range.
                                Range range = request.getRanges().get(0);
                                // Go to the desired offset.
                                if (range.getIndex() == Range.INDEX_LAST) {
                                    if (raf.length() <= range.getSize()) {
                                        raf.seek(range.getSize());
                                    } else {
                                        raf
                                                .seek(raf.length()
                                                        - range.getSize());
                                    }
                                } else {
                                    raf.seek(range.getIndex());
                                }
                                // Write the entity to the file.
                                if (request.isEntityAvailable()) {
                                    ByteUtils.write(request.getEntity()
                                            .getStream(), raf);
View Full Code Here

TOP

Related Classes of org.restlet.data.Range

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.