Package org.restlet

Examples of org.restlet.Client


        if (!SystemUtils.isWindows()) {
            Request request;
            Response response;

            BioUtils.delete(testDir, true);
            Client client = new Client(new Context(), Protocol.HTTP);
            client.getContext().getParameters().add("tracing", "true");

            // 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);
            client.stop();
        }
    }
View Full Code Here


    }

    @Override
    protected void call(String uri) throws Exception {
        final Request request = new Request(Method.GET, uri);
        final Client client = new Client(Protocol.HTTPS);
        if (client.getContext() == null) {
            client.setContext(new Context());
        }
        configureSslServerParameters(client.getContext());
        final Response r = client.handle(request);

        assertEquals(r.getStatus().getDescription(), Status.SUCCESS_OK,
                r.getStatus());
        assertEquals("Hello world", r.getEntity().getText());

        Thread.sleep(200);
        client.stop();
    }
View Full Code Here

    }

    private void sendPut(String uri, int size) throws Exception {
        Request request = new Request(Method.PUT, uri,
                createChunkedRepresentation(size));
        Client c = new Client(Protocol.HTTP);
        Response r = c.handle(request);

        try {
            if (!r.getStatus().isSuccess()) {
                System.out.println(r.getStatus());
            }

            assertNotNull(r.getEntity());
            assertEquals(createChunkedRepresentation(size).getText(), r
                    .getEntity().getText());
        } finally {
            r.release();
            c.stop();
        }
    }
View Full Code Here

     * Tests ranges.
     *
     * @throws Exception
     */
    public void testRanges() throws Exception {
        Client client = new Client(Protocol.HTTP);
        Request request;
        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();

        // Multiple ranges are not supported yet.
        // request = new Request(Method.GET, "http://localhost:" + TEST_PORT
View Full Code Here

                WRONG_USERNAME, SHORT_PASSWORD.toCharArray()));
    }

    public void HTTPBasicLong() throws Exception {
        final Request request = new Request(Method.GET, this.uri);
        final Client client = new Client(Protocol.HTTP);

        final ChallengeResponse authentication = new ChallengeResponse(
                ChallengeScheme.HTTP_BASIC, LONG_USERNAME, LONG_PASSWORD);
        request.setChallengeResponse(authentication);

        final Response response = client.handle(request);
        assertEquals("Long username did not return 200 OK", Status.SUCCESS_OK,
                response.getStatus());
        assertEquals(AUTHENTICATED_MSG, response.getEntity().getText());
       
        client.stop();
    }
View Full Code Here

        client.stop();
    }

    public void HTTPBasicLongWrong() throws Exception {
        final Request request = new Request(Method.GET, this.uri);
        final Client client = new Client(Protocol.HTTP);

        final ChallengeResponse authentication = new ChallengeResponse(
                ChallengeScheme.HTTP_BASIC, LONG_USERNAME, SHORT_PASSWORD);
        request.setChallengeResponse(authentication);

        final Response response = client.handle(request);

        assertEquals("Long username w/wrong pw did not throw 403",
                Status.CLIENT_ERROR_UNAUTHORIZED, response.getStatus());
       
        client.stop();
    }
View Full Code Here

    }

    // Test various HTTP Basic auth connections
    public void HTTPBasicNone() throws Exception {
        final Request request = new Request(Method.GET, this.uri);
        final Client client = new Client(Protocol.HTTP);
        final Response response = client.handle(request);
        assertEquals("No user did not throw 401",
                Status.CLIENT_ERROR_UNAUTHORIZED, response.getStatus());
        client.stop();
    }
View Full Code Here

        client.stop();
    }

    public void HTTPBasicShort() throws Exception {
        final Request request = new Request(Method.GET, this.uri);
        final Client client = new Client(Protocol.HTTP);

        final ChallengeResponse authentication = new ChallengeResponse(
                ChallengeScheme.HTTP_BASIC, SHORT_USERNAME, SHORT_PASSWORD);
        request.setChallengeResponse(authentication);

        final Response response = client.handle(request);
        assertEquals("Short username did not return 200 OK", Status.SUCCESS_OK,
                response.getStatus());
        assertEquals(AUTHENTICATED_MSG, response.getEntity().getText());
       
        client.stop();
    }
View Full Code Here

        client.stop();
    }

    public void HTTPBasicShortWrong() throws Exception {
        final Request request = new Request(Method.GET, this.uri);
        final Client client = new Client(Protocol.HTTP);

        final ChallengeResponse authentication = new ChallengeResponse(
                ChallengeScheme.HTTP_BASIC, SHORT_USERNAME, LONG_PASSWORD);
        request.setChallengeResponse(authentication);

        final Response response = client.handle(request);

        assertEquals("Short username did not throw 401",
                Status.CLIENT_ERROR_UNAUTHORIZED, response.getStatus());
       
        client.stop();
    }
View Full Code Here

        client.stop();
    }

    public void HTTPBasicWrongUser() throws Exception {
        final Request request = new Request(Method.GET, this.uri);
        final Client client = new Client(Protocol.HTTP);

        final ChallengeResponse authentication = new ChallengeResponse(
                ChallengeScheme.HTTP_BASIC, WRONG_USERNAME, SHORT_PASSWORD);
        request.setChallengeResponse(authentication);

        final Response response = client.handle(request);

        assertEquals("Wrong username did not throw 401",
                Status.CLIENT_ERROR_UNAUTHORIZED, response.getStatus());
       
        client.stop();
    }
View Full Code Here

TOP

Related Classes of org.restlet.Client

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.