Package org.apache.wink.client

Examples of org.apache.wink.client.Resource


        Date d = new Date(System.currentTimeMillis() - 60000);

        /*
         * get the time zone for the server
         */
        Resource dateResource = client.resource(getBaseURI() + "/context/request/timezone");
        ClientResponse response = dateResource.get();
        assertEquals(200, response.getStatusCode());

        /*
         * sets a last modified date
         */
        dateResource = client.resource(getBaseURI() + "/context/request/date");
        DateFormat dateFormat =
            new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.ENGLISH); // DateFormat.getDateTimeInstance();
        response = dateResource.contentType("text/string").put(dateFormat.format(d));
        assertEquals(204, response.getStatusCode());

        formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
        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());
        rfc1123Format.setTimeZone(TimeZone.getTimeZone("GMT"));
        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
         */
        formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
        dateResource = client.resource(getBaseURI() + "/context/request/date");
        response = dateResource.header(HttpHeaders.IF_MODIFIED_SINCE, formatter.format(d2)).get();
        assertEquals(200, response.getStatusCode());
        rfc1123Format.setTimeZone(TimeZone.getTimeZone("GMT"));
        assertEquals("the date: " + rfc1123Format.format(d), response.getEntity(String.class));
        rfc1123Format.setTimeZone(TimeZone.getDefault());

        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
         */
        formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
        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


        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

        assert uri != null;
        // params could be null
        // type could be null
        // accept could be null

        Resource resource = getClient().resource(uri);

        if (params != null) {
            resource = resource.queryParams(params);
        }

        // Default content and accept types to XML if not configured
        resource = (type != null) ? resource.contentType(type) : resource.contentType(MediaType.APPLICATION_XML_TYPE);
        resource = (accept != null) ? resource.accept(type) : resource.accept(MediaType.APPLICATION_XML_TYPE);

        if (authCookie != null) {
            resource = resource.cookie(authCookie);
        }

        return resource;
    }
View Full Code Here

   
    final private static String BASE_URI = ServerEnvironmentInfo.getBaseURI() + "/optionalproviders/blogservice";

    public void testAtomGETBlogs() throws Exception {
        RestClient client = new RestClient();
        Resource resource = client.resource(BASE_URI);
        AtomFeed feed = resource.accept("application/atom+xml").get(AtomFeed.class);
        assertEquals(BlogService.ID, feed.getId());
        assertEquals(BlogService.ID, feed.getTitle().getValue());
       
        List<AtomLink> expectedLinks = new ArrayList<AtomLink>();
        AtomLink link = new AtomLink();
View Full Code Here

            assertEquals(expectedLinks.get(i).getHref(), actual.get(i).getHref());
    }
   
    public void testAtomGETBlog() throws Exception {
        RestClient client = new RestClient();
        Resource resource = client.resource(BASE_URI+"/blogs/0");
        AtomFeed feed = resource.accept("application/atom+xml").get(AtomFeed.class);
        assertEquals("0", feed.getId());
        assertEquals("wink-developer-blog", feed.getTitle().getValue());
       
        List<AtomLink> expectedLinks = new ArrayList<AtomLink>();
        AtomLink link = new AtomLink();
        link.setHref(BASE_URI+"/blogs/0/entries/0");
        expectedLinks.add(link);
        link = new AtomLink();
        link.setHref(BASE_URI+"/blogs/0/entries/1");
        expectedLinks.add(link);
        List<AtomLink> actual = feed.getLinks();
        assertEquals(expectedLinks.size(), actual.size());
        for(int i = 0; i < actual.size(); ++i)
            assertEquals(expectedLinks.get(i).getHref(), actual.get(i).getHref());
       
        client = new RestClient();
        resource = client.resource(BASE_URI+"/blogs/1");
        feed = resource.accept("application/atom+xml").get(AtomFeed.class);
        assertEquals("1", feed.getId());
        assertEquals("wink-user-blog", feed.getTitle().getValue());
       
        expectedLinks = new ArrayList<AtomLink>();
        link = new AtomLink();
View Full Code Here

            assertEquals(expectedLinks.get(i).getHref(), actual.get(i).getHref());
    }
   
    public void testAtomGETBlogEntry() throws Exception {
        RestClient client = new RestClient();
        Resource resource = client.resource(BASE_URI+"/blogs/0/entries/0");
        AtomEntry entry = resource.accept("application/atom+xml").get(AtomEntry.class);
        assertNotNull(entry.getAuthors());
        assertEquals(1, entry.getAuthors().size());
        AtomPerson author = entry.getAuthors().get(0);
        assertNotNull(author);
        assertEquals("Blog Admin", author.getName());
        assertEquals("winkblogadmin@wink.blog.com", author.getEmail());
        assertEquals("0", entry.getId());
        assertEquals("Welcome to the wink developer blog!!", entry.getContent().getValue());
        assertEquals("welcomePosting", entry.getTitle().getValue());
        assertEquals(0, entry.getLinks().size());
       
        resource = client.resource(BASE_URI+"/blogs/0/entries/1");
        entry = resource.accept("application/atom+xml").get(AtomEntry.class);
        assertNotNull(entry.getAuthors());
        assertEquals(1, entry.getAuthors().size());
        author = entry.getAuthors().get(0);
        assertNotNull(author);
        assertEquals("Blog Admin", author.getName());
        assertEquals("winkblogadmin@wink.blog.com", author.getEmail());
        assertEquals("1", entry.getId());
        assertEquals("Wink developers,\n\nInstructions on how to set up the wink development have been posted to the wink wiki. Happy wink development!\n\nw--inkblogadmin", entry.getContent().getValue());
        assertEquals("Wink Development Env", entry.getTitle().getValue());
        assertEquals(1, entry.getLinks().size());
        List<AtomLink> comments = entry.getLinks();
        assertEquals(BASE_URI+"/blogs/0/entries/1/comments/0", comments.get(0).getHref());
       
        resource = client.resource(BASE_URI+"/blogs/1/entries/2");
        entry = resource.accept("application/atom+xml").get(AtomEntry.class);
        assertNotNull(entry.getAuthors());
        assertEquals(1, entry.getAuthors().size());
        author = entry.getAuthors().get(0);
        assertNotNull(author);
        assertEquals("Eager User", author.getName());
View Full Code Here

        assertEquals(BASE_URI+"/blogs/1/entries/2/comments/0", comments.get(0).getHref());
    }
   
    public void testAtomGETBlogComments() throws Exception {
        RestClient client = new RestClient();
        Resource resource = client.resource(BASE_URI+"/blogs/0/entries/1/comments/0");
        AtomEntry entry = resource.accept("application/atom+xml").get(AtomEntry.class);
        assertNotNull(entry.getAuthors());
        assertEquals(1, entry.getAuthors().size());
        AtomPerson author = entry.getAuthors().get(0);
        assertNotNull(author);
        assertEquals("Wink Coder", author.getName());
        assertEquals("winkcoder@mybusiness.com", author.getEmail());
        assertEquals("Great!", entry.getTitle().getValue());
        assertEquals("Instructions look great! Now I can begin Wink development!", entry.getContent().getValue());
       
        resource = client.resource(BASE_URI+"/blogs/1/entries/2/comments/0");
        entry = resource.accept("application/atom+xml").get(AtomEntry.class);
        assertNotNull(entry.getAuthors());
        assertEquals(1, entry.getAuthors().size());
        author = entry.getAuthors().get(0);
        assertNotNull(author);
        assertEquals("Blog Reader", author.getName());
View Full Code Here

        assertEquals("This is good news. I'll be sure to try it out.", entry.getContent().getValue());
    }
   
    public void testAtomPOSTBlogEntry() throws Exception {
        RestClient client = new RestClient();
        Resource resource = client.resource(BASE_URI+"/blogs/0/entries");
        AtomEntry entry = new AtomEntry();
        AtomPerson author = new AtomPerson();
        author.setName("Blog Admin");
        author.setEmail("winkblogadmin@wink.blog.com");
        entry.getAuthors().add(author);
        AtomContent content = new AtomContent();
        content.setType("String");
        content.setValue("This is a new entry in the blog");
        entry.setContent(content);
        entry.setTitle(new AtomText(("New blog entry")));
       
        ClientResponse uri = resource.accept("application/atom+xml").contentType("application/atom+xml").post(entry);
        String location = uri.getHeaders().getFirst("Location");
        assertEquals(BASE_URI+"/blogs/0/entries/3", location);
       
        resource = client.resource(location);
        AtomEntry postedEntry = resource.accept("application/atom+xml").get(AtomEntry.class);
        author = postedEntry.getAuthors().get(0);
        assertNotNull(author);
        assertEquals("Blog Admin", author.getName());
        assertEquals("winkblogadmin@wink.blog.com", author.getEmail());
        assertEquals("3", postedEntry.getId());
        assertEquals("This is a new entry in the blog", entry.getContent().getValue());
        assertEquals("New blog entry", entry.getTitle().getValue());
        assertEquals(0, entry.getLinks().size());
       
        resource = client.resource(BASE_URI+"/blogs/0");
        AtomFeed feed = resource.accept("application/atom+xml").get(AtomFeed.class);
        List<AtomLink> expectedLinks = new ArrayList<AtomLink>();
        AtomLink link = new AtomLink();
        link.setHref(BASE_URI+"/blogs/0/entries/0");
        expectedLinks.add(link);
        link = new AtomLink();
View Full Code Here

            assertEquals(expectedLinks.get(i).getHref(), actual.get(i).getHref());
    }
   
    public void testAtomPostBlogComment() throws Exception {
        RestClient client = new RestClient();
        Resource resource = client.resource(BASE_URI+"/blogs/0/entries/1/comments");
        AtomEntry entry = new AtomEntry();
        AtomPerson author = new AtomPerson();
        author.setName("Wink Coder");
        author.setEmail("winkcoder@mybusiness.com");
        entry.getAuthors().add(author);
        AtomContent content = new AtomContent();
        content.setType("String");
        content.setValue("I was able to set up the Wink development environment!");
        entry.setContent(content);
        entry.setTitle(new AtomText(("Success")));
       
        ClientResponse uri = resource.accept("application/atom+xml").contentType("application/atom+xml").post(entry);
        String location = uri.getHeaders().getFirst("Location");
        assertEquals(BASE_URI+"/blogs/0/entries/1/comments/1", location);
       
        resource = client.resource(location);
        AtomEntry postedEntry = resource.accept("application/atom+xml").get(AtomEntry.class);
        author = postedEntry.getAuthors().get(0);
        assertNotNull(author);
        assertEquals("Wink Coder", author.getName());
        assertEquals("winkcoder@mybusiness.com", author.getEmail());
        assertEquals("I was able to set up the Wink development environment!", entry.getContent().getValue());
        assertEquals("Success", entry.getTitle().getValue());
       
        resource = client.resource(BASE_URI+"/blogs/0/entries/1");
        entry = resource.accept("application/atom+xml").get(AtomEntry.class);
        List<AtomLink> expectedLinks = new ArrayList<AtomLink>();
        AtomLink link = new AtomLink();
        link.setHref(BASE_URI+"/blogs/0/entries/1/comments/0");
        expectedLinks.add(link);
        link = new AtomLink();
View Full Code Here

            assertEquals(expectedLinks.get(i).getHref(), actual.get(i).getHref());
    }
   
    public void testAtomPUTBlogEntry() throws Exception {
        RestClient client = new RestClient();
        Resource resource = client.resource(BASE_URI+"/blogs/0/entries/0");
        AtomEntry entry = resource.accept("application/atom+xml").get(AtomEntry.class);
        AtomPerson author = entry.getAuthors().get(0);
        author.setName(author.getName()+"Updated");
        author.setEmail(author.getEmail()+"Updated");
        AtomText title = entry.getTitle();
        title.setValue(title.getValue()+"Updated");
        AtomContent content = entry.getContent();
        content.setValue(content.getValue()+"Updated");
       
        resource = client.resource(BASE_URI+"/blogs/0/entries/0");
        AtomEntry updated = resource.accept("application/atom+xml").contentType("application/atom+xml").put(AtomEntry.class, entry);
        assertNotNull(updated.getAuthors());
        assertEquals(1, updated.getAuthors().size());
        author = updated.getAuthors().get(0);
        assertNotNull(author);
        assertEquals("Blog AdminUpdated", author.getName());
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.