Package org.apache.wink.client

Examples of org.apache.wink.client.Resource


        for (Map.Entry<String, Object> p : matrixParams.entrySet()) {
            uriBuilder.replaceMatrixParam(p.getKey(), p.getValue());
        }

        uri = uriBuilder.buildFromMap(pathParams);
        Resource resource = restClient.resource(uri);

        for (Map.Entry<String, Object> p : headerParams.entrySet()) {
            resource.header(p.getKey(), String.valueOf(p.getValue()));
        }

        for (Map.Entry<String, Object> p : cookieParams.entrySet()) {
            Cookie cookie = new Cookie(p.getKey(), String.valueOf(p.getValue()));
            resource.cookie(cookie);
        }

        resource.contentType(getContentType());
        resource.accept(getAccepts());

        //handles declarative headers configured on the composite
        for (HTTPHeader header : binding.getHttpHeaders()) {
            //treat special headers that need to be calculated
            if (header.getName().equalsIgnoreCase("Expires")) {
                GregorianCalendar calendar = new GregorianCalendar();
                calendar.setTime(new Date());

                calendar.add(Calendar.HOUR, Integer.parseInt(header.getValue()));

                resource.header("Expires", HTTPCacheContext.RFC822DateFormat.format(calendar.getTime()));
            } else {
                //default behaviour to pass the header value to HTTP response
                resource.header(header.getName(), header.getValue());
            }
        }

        Object result = resource.invoke(httpMethod, responseType, entity);
        msg.setBody(result);
        return msg;
    }
View Full Code Here


        Date d = new Date(System.currentTimeMillis() - 60000);
        String date = DateFormat.getDateTimeInstance().format(d);
        /*
         * sets a last modified date
         */
        Resource dateResource = client.resource(getBaseURI() + "/context/request/date");
        ClientResponse response = dateResource.contentType("text/string").put(date);
        assertEquals(204, response.getStatusCode());

        response = dateResource.header(HttpHeaders.IF_MODIFIED_SINCE, formatter.format(d)).get();
        /*
         * verifies that if the exact date is sent in and used in
         * If-Modified-Since header, then the server will be ok and that it will
         * return 304
         */
        assertEquals(304, response.getStatusCode());

        /*
         * verifies that if no If-Modified-Since header is sent, then the server
         * will be ok and the Request instance won't build a response.
         */
        dateResource = client.resource(getBaseURI() + "/context/request/date");
        response = dateResource.get();
        assertEquals(200, response.getStatusCode());
        assertEquals("the date: " + rfc1123Format.format(d), response.getEntity(String.class));

        rfc1123Format.setTimeZone(TimeZone.getTimeZone("GMT"));
        assertEquals(rfc1123Format.format(d), response.getHeaders()
            .getFirst(HttpHeaders.LAST_MODIFIED));
        rfc1123Format.setTimeZone(TimeZone.getDefault());

        /*
         * verifies that using Last-Modified response header sent by server as
         * If-Modified-Since request header, then the server will return a 304
         */
        String lastModified = response.getHeaders().getFirst(HttpHeaders.LAST_MODIFIED);
        dateResource = client.resource(getBaseURI() + "/context/request/date");
        response = dateResource.header(HttpHeaders.IF_MODIFIED_SINCE, lastModified).get();
        assertEquals(304, response.getStatusCode());

        /*
         * verifies that using a If-Modified-Since earlier than the
         * Last-Modified response header sent by server then the server will
         * return a 200 with entity
         */
        dateResource = client.resource(getBaseURI() + "/context/request/date");
        response = dateResource.header(HttpHeaders.IF_MODIFIED_SINCE, formatter.format(d2)).get();
        assertEquals(200, response.getStatusCode());
        assertEquals("the date: " + rfc1123Format.format(d), response.getEntity(String.class));

        rfc1123Format.setTimeZone(TimeZone.getTimeZone("GMT"));
        assertEquals(rfc1123Format.format(d), response.getHeaders()
            .getFirst(HttpHeaders.LAST_MODIFIED));
        rfc1123Format.setTimeZone(TimeZone.getDefault());

        /*
         * verifies that using a If-Modified-Since later than the Last-Modified
         * response header sent by server, then the server will return a 304
         */
        dateResource = client.resource(getBaseURI() + "/context/request/date");
        response =
            dateResource.header(HttpHeaders.IF_MODIFIED_SINCE, formatter.format(new Date())).get();
        assertEquals(304, response.getStatusCode());
    }
View Full Code Here

    }

    public void testResourceGet() {
        server.setMockResponseCode(200);
        RestClient client = getRestClient();
        Resource resource = client.resource(serviceURL);

        String string = resource.get(String.class);
        assertEquals(RECEIVED_MESSAGE, string);

        // do get with response
        ClientResponse clientResponse = resource.get();
        assertEquals(RECEIVED_MESSAGE, clientResponse.getEntity(String.class));

        // test generic entity
        TestGenerics<String> tg = resource.get(new EntityType<TestGenerics<String>>() {
        });
        assertEquals(RECEIVED_MESSAGE, tg.getT());
    }
View Full Code Here

    }

    public void testResourcePut() throws IOException {
        server.setMockResponseCode(200);
        RestClient client = getRestClient();
        Resource resource = client.resource(serviceURL + "/testResourcePut");
        String response =
            resource.contentType("text/plain").accept("text/plain").put(String.class, SENT_MESSAGE);
        assertEquals(RECEIVED_MESSAGE, response);
        assertEquals(SENT_MESSAGE, server.getRequestContentAsString());

        // do put with response
        ClientResponse clientResponse = resource.put(SENT_MESSAGE);
        assertEquals(RECEIVED_MESSAGE, clientResponse.getEntity(String.class));

        // test generic entity
        TestGenerics<String> tg = resource.put(new EntityType<TestGenerics<String>>() {
        }, SENT_MESSAGE);
        assertEquals(RECEIVED_MESSAGE, tg.getT());

    }
View Full Code Here

    }

    public void testResourcePost() throws IOException {
        server.setMockResponseCode(200);
        RestClient client = getRestClient();
        Resource resource = client.resource(serviceURL + "/testResourcePost");
        String response =
            resource.contentType("text/plain").accept("text/plain")
                .post(String.class, SENT_MESSAGE);
        assertEquals(RECEIVED_MESSAGE, response);
        assertEquals(SENT_MESSAGE, server.getRequestContentAsString());

        // do post with response
        ClientResponse clientResponse = resource.post(SENT_MESSAGE);
        assertEquals(RECEIVED_MESSAGE, clientResponse.getEntity(String.class));

        // test generic entity
        TestGenerics<String> tg = resource.post(new EntityType<TestGenerics<String>>() {
        }, SENT_MESSAGE);
        assertEquals(RECEIVED_MESSAGE, tg.getT());
    }
View Full Code Here

    }

    public void testResourceDelete() {
        server.setMockResponseCode(200);
        RestClient client = getRestClient();
        Resource resource = client.resource(serviceURL);
        String response = resource.accept(MediaType.TEXT_PLAIN_TYPE).delete(String.class);
        assertEquals(RECEIVED_MESSAGE, response);

        // do delete with response
        ClientResponse clientResponse = resource.delete();
        assertEquals(RECEIVED_MESSAGE, clientResponse.getEntity(String.class));

        // test generic entity
        TestGenerics<String> tg = resource.delete(new EntityType<TestGenerics<String>>() {
        });
        assertEquals(RECEIVED_MESSAGE, tg.getT());
    }
View Full Code Here

    }

    public void testInvoke() {
        server.setMockResponseCode(200);
        RestClient client = getRestClient();
        Resource resource = client.resource(serviceURL);

        String string = resource.invoke("GET", String.class, null);
        assertEquals(RECEIVED_MESSAGE, string);

        // test generic entity
        TestGenerics<String> tg = resource.invoke("GET", new EntityType<TestGenerics<String>>() {
        }, null);
        assertEquals(RECEIVED_MESSAGE, tg.getT());
    }
View Full Code Here

    }

    public void testHttpErrorNoResponse() throws IOException {
        server.setMockResponseCode(400);
        RestClient client = getRestClient();
        Resource resource = client.resource(serviceURL);
        try {
            resource.accept("text/plain").invoke("GET", String.class, null);
            fail("ClientWebException must be thrown");
        } catch (ClientWebException e) {
            assertTrue(e.getResponse().getStatusCode() == 400);
        }
    }
View Full Code Here

    }

    public void testHttpErrorWithResponse() throws IOException {
        server.setMockResponseCode(400);
        RestClient client = getRestClient();
        Resource resource = client.resource(serviceURL);
        try {
            ClientResponse res = resource.accept("text/plain").get();
            assertTrue(res.getStatusCode() == 400);
        } catch (Exception e) {
            fail("Exception must not be thrown");
        }
    }
View Full Code Here

        server.setMockResponseContentType("text/plain; charset=UTF-16");

        server.startServer();
        try {
            RestClient client = getRestClient();
            Resource resource =
                client.resource(MessageFormat.format(SERVICE_URL, String.valueOf(server
                    .getServerPort())));
            String response = resource.accept(MediaType.TEXT_PLAIN_TYPE).get(String.class);
            assertEquals("REQUEST", response);

        } finally {
            server.stopServer();
        }
View Full Code Here

TOP

Related Classes of org.apache.wink.client.Resource

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.