Package org.apache.abdera.protocol.client

Examples of org.apache.abdera.protocol.client.ClientResponse


        IRI colUri = new IRI(providerURI).resolve("customer");
        // res = client.post(colUri.toString() + "?test=foo", entry, opts);
        String id = eTag.substring( 1, eTag.length()-1);
        // Warning. AbderaClient.put(String uri,Base base,RequestOptions options) caches on the client side.
        // ClientResponse res = client.put(colUri.toString() + id, entry, opts);
        ClientResponse res = client.get(colUri.toString() + "/" + id, opts);

        // Atom server response (item was up to date)
        // >      HTTP/1.1 200 OK
        //        Date: Sat, 24 Feb 2007 13:17:11 GMT
        // >      ETag: "bb4f5e86e92ddb8549604a0df0763581"
        // >      Last-Modified: Mon, 28 Jul 2008 10:25:37 -0500

        // Assert response status code is 200 OK.
        // Assert header Content-Type: application/atom+xml;type=entry
        // Assert header Location: http://example.org/edit/first-post.atom
        // Assert header Content-Location: http://example.org/edit/first-post.atom
        // Assert header ETag: "e180ee84f0671b1"
        // Assert header Last-Modified: Less than If-Unmod
        Assert.assertEquals(200, res.getStatus());
        res.release();
    }
View Full Code Here


        IRI colUri = new IRI(providerURI).resolve("customer");
        // res = client.post(colUri.toString() + "?test=foo", entry, opts);
        String id = eTag.substring( 1, eTag.length()-1);
        // Warning. AbderaClient.put(String uri,Base base,RequestOptions options) caches on the client side.
        // ClientResponse res = client.put(colUri.toString() + id, entry, opts);
        ClientResponse res = client.get(colUri.toString() + "/" + id, opts);

        // Atom server response (item was up to date)
        // >      HTTP/1.1 304 Not Modified
        //       Date: Sat, 24 Feb 2007 13:17:11 GMT

        // Assert response status code is 304 Not Modified.
        Assert.assertEquals(304, res.getStatus());
        res.release();
    }
View Full Code Here

    Entry entry = getFactory().newEntry();
    RequestOptions options = client.getDefaultRequestOptions();
    options.setHeader("Connection", "close");

    // do the introspection step
    ClientResponse response = client.get("http://localhost:8080/service",
                                         options);
    String col_uri;

    try {
      assertEquals(200, response.getStatus());

      Document<Service> service_doc = response.getDocument();
      assertNotNull(service_doc);
      assertEquals(1, service_doc.getRoot().getWorkspaces().size());

      Workspace workspace = service_doc.getRoot().getWorkspace("Test");
      assertNotNull(workspace);

      for (Collection c: workspace.getCollections()) {
        assertNotNull(c.getTitle());
        assertNotNull(c.getHref());
      }
   
      col_uri = getBase() + "/collections/entries";
    } finally {
      response.release();
    }

    // post a new entry
    response = client.post(col_uri, entry, options);

    String self_uri;

    try {
      assertEquals(201, response.getStatus());
      assertNotNull(response.getLocation());
      assertNotNull(response.getContentLocation());
   
      self_uri = response.getLocation().toString();
    } finally {
      response.release();
    }
    // get the collection to see if our entry is there
    response = client.get(col_uri, options);

    try {
      assertEquals(200, response.getStatus());
      Document<Feed> feed_doc = response.getDocument();
      assertEquals(1, feed_doc.getRoot().getEntries().size());
    } finally {
      response.release();
    }

    // get the entry to see if we can get it
    response = client.get(self_uri, options);

    String edit_uri;

    try {
      assertEquals(200, response.getStatus());
      Document<Entry> entry_doc = response.getDocument();

      // this isn't always true, but for our tests they are the same
      assertEquals(self_uri, entry_doc.getRoot().getId().toString());
   
      // get the edit uri from the entry
      edit_uri = entry_doc.getRoot().getEditLink().getHref().toString();
   
      // change the entry
      Document<Entry> doc = response.getDocument();
      entry = (Entry) doc.getRoot().clone();
      entry.setTitle("New title");
    } finally
      response.release();
    }

    // submit the changed entry back to the server
    response = client.put(edit_uri, entry, options);

    try {
      assertEquals(204, response.getStatus());
    } finally {
      response.release();
    }

    // check to see if the entry was modified properly
    response = client.get(self_uri, options);

    try {
      assertEquals(200, response.getStatus());

      Document<Entry> entry_doc = response.getDocument();
      assertEquals("New title", entry_doc.getRoot().getTitle());
    } finally {
      response.release();
    }

    // delete the entry
    response = client.delete(edit_uri, options);

    try {
      assertEquals(204, response.getStatus());
    } finally {
      response.release();
    }

    // is it gone?
    response = client.get(self_uri, options);

    try {
      assertEquals(404, response.getStatus());
    } finally {
      response.release();
    }

    // YAY! We're a working APP client
   
    // Now let's try to do a media post
   
    // Post the media resource
    options = client.getDefaultRequestOptions();
    options.setContentType("text/plain");
    options.setHeader("Connection", "close");

    response = client.post(col_uri,
                           new ByteArrayInputStream("test".getBytes()),
                           options);

    try {
      assertEquals(201, response.getStatus());
      assertNotNull(response.getLocation());
      assertNotNull(response.getContentLocation());
   
      self_uri = response.getLocation().toString();
    } finally {
      response.release();
    }

    // was an entry created?
    options = client.getDefaultRequestOptions();
    options.setHeader("Connection", "close");
    response = client.get(self_uri, options);

    String edit_media, media;

    try {
      assertEquals(200, response.getStatus());
      Document<Entry> entry_doc = response.getDocument();

      // this isn't always true, but for our tests they are the same
      assertEquals(self_uri, entry_doc.getRoot().getId().toString());
   
      // get the right links from the entry
      edit_uri = entry_doc.getRoot().getEditLink().getHref().toString();
      edit_media = entry_doc.getRoot()
                            .getLink("edit-media")
                            .getHref().toString();
      media = entry_doc.getRoot().getContentElement().getSrc().toString();
   
      // edit the entry
      Document doc = response.getDocument();
      entry = (Entry) doc.getRoot().clone();
      entry.setTitle("New title");
    } finally {
      response.release();
    }

    // submit the changes
    options = client.getDefaultRequestOptions();
    options.setContentType("application/atom+xml");
    options.setHeader("Connection", "close");

    response = client.put(edit_uri, entry, options);

    try {
      assertEquals(204, response.getStatus());
    } finally {
      response.release();
    }

    // get the media resource
    response = client.get(media);

    try {
      assertEquals(200, response.getStatus());

      String mediavalue = read(response.getInputStream());
      assertEquals("test", mediavalue);
    } finally {
      response.release();
    }

    // edit the media resource
    options = client.getDefaultRequestOptions();
    options.setHeader("Connection", "close");
    options.setContentType("text/plain");

    response = client.put(edit_media,
                          new ByteArrayInputStream("TEST".getBytes()),
                          options);

    try {
      assertEquals(204, response.getStatus());
    } finally {
      response.release();
    }

    // was the resource changed?
    response = client.get(media, options);

    try {
      assertEquals(200, response.getStatus());

      String mediavalue = read(response.getInputStream());
      assertEquals("TEST", mediavalue);
    } finally {
      response.release();
    }

    // delete the entry
    response = client.delete(edit_uri, options);

    try {
      assertEquals(204, response.getStatus());
    } finally {
      response.release();
    }

    // is the entry gone?
    response = client.get(self_uri, options);

    try {
      assertEquals(404, response.getStatus());
    } finally {
      response.release();
    }

    // is the media resource gone?
    options.setNoCache(true); // need to force revalidation to check

    response = client.get(media, options);

    try {
      assertEquals(404, response.getStatus());
    } finally {
      response.release();
    }

    // YAY! We can handle media link entries
  }
View Full Code Here

            client.addCredentials(baseURL.toExternalForm(),
                    null,
                    null,
                    new org.apache.commons.httpclient.UsernamePasswordCredentials("admin",
                            "admin"));
            ClientResponse deleteResponse = client.delete(
                    new URL(baseURL, "rest/packages/scanForChangeInRepository/assets/ruleB2").toExternalForm());
            assertEquals(204, deleteResponse.getStatus());
            ClientResponse binaryResponse = client.get(
                    new URL(baseURL, "rest/packages/scanForChangeInRepository/binary").toExternalForm());
            assertEquals(200, binaryResponse.getStatus());


            // detect the change
            Thread.sleep(6000);
            kbase = kagent.getKnowledgeBase();
View Full Code Here

      AbderaClient client = new AbderaClient(abdera);
      Entry entry = abdera.newEntry();   
      entry.setTitle("testCreatePackageFromAtom");
      entry.setSummary("desc for testCreatePackageFromAtom");
     
      ClientResponse resp = client.post(generateBaseUrl() + "/packages", entry);
        //System.out.println(GetContent(resp.getInputStream()));

    assertEquals(ResponseType.SUCCESS, resp.getType());

    Document<Entry> doc = resp.getDocument();
    Entry returnedEntry = doc.getRoot();
    assertEquals("/packages/testCreatePackageFromAtom", returnedEntry.getBaseUri().getPath());
    assertEquals("testCreatePackageFromAtom", returnedEntry.getTitle());
    assertEquals("desc for testCreatePackageFromAtom", returnedEntry.getSummary());
   
    //Test update package
        Entry e = abdera.newEntry();
        e.setTitle("testUpdatePackageFromAtom");
        org.apache.abdera.model.Link l = abdera.getNewFactory().newLink();
        l.setHref(generateBaseUrl() + "/packages/" + "testCreatePackageFromAtom");
        l.setRel("self");
        e.addLink(l);
        e.setSummary("updated desc for testCreatePackageFromAtom");
        e.addAuthor("Test McTesty");   
        resp = client.put(generateBaseUrl() + "/packages/testCreatePackageFromAtom", e);
        assertEquals(ResponseType.SUCCESS, resp.getType());

        //NOTE: could not figure out why the code below always returns -1 as the ResponseCode.
/*        URL url = new URL(generateBaseUrl() + "/packages/testCreatePackageFromAtom");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("PUT");
        conn.setRequestProperty("Content-type", MediaType.APPLICATION_ATOM_XML);
        conn.setRequestProperty("Content-Length", Integer.toString(e.toString().getBytes().length));
        conn.setDoOutput(true);
        e.writeTo(conn.getOutputStream());
        assertEquals(204, conn.getResponseCode());
        conn.disconnect(); */
        URL url1 = new URL(generateBaseUrl() + "/packages/testCreatePackageFromAtom");
        HttpURLConnection conn1 = (HttpURLConnection)url1.openConnection();
        conn1.setRequestMethod("GET");
        conn1.setRequestProperty("Accept", MediaType.APPLICATION_ATOM_XML);
        conn1.connect();
        //System.out.println(GetContent(conn));
        assertEquals (200, conn1.getResponseCode());
        assertEquals(MediaType.APPLICATION_ATOM_XML, conn1.getContentType());
       
        InputStream in = conn1.getInputStream();
        assertNotNull(in);
    doc = abdera.getParser().parse(in);
    entry = doc.getRoot();
    assertEquals("/packages/testCreatePackageFromAtom", entry.getBaseUri().getPath());   
    assertEquals("testCreatePackageFromAtom", entry.getTitle());
    assertTrue(entry.getPublished() != null);
    assertEquals("updated desc for testCreatePackageFromAtom", entry.getSummary());

       
    //Roll back changes.
    resp = client.delete(generateBaseUrl() + "/packages/testCreatePackageFromAtom");
    assertEquals(ResponseType.SUCCESS, resp.getType());

    //Verify the package is indeed deleted
    URL url2 = new URL(generateBaseUrl() + "/packages/testCreatePackageFromAtom");
    HttpURLConnection conn2 = (HttpURLConnection)url2.openConnection();
        conn2.setRequestMethod("GET");
View Full Code Here

    @Test
    public void testFeedBasics() throws Exception {   
        // System.out.println(">>>ProviderFeedEntityTagsTestCase.testFeedBasics");
        // Normal feed request
        ClientResponse res = client.get(providerURI);
        Assert.assertNotNull(res);
        try {
            // Assert feed provided since no predicates
            Assert.assertEquals(200, res.getStatus());
            Assert.assertEquals(ResponseType.SUCCESS, res.getType());
            // AtomTestCaseUtils.printResponseHeaders( "Feed response headers:", "   ", res );
            // System.out.println("Feed response content:");
            // AtomTestCaseUtils.prettyPrint(abdera, res.getDocument());

            // Perform other tests on feed.
            // Warning. AbderaClient.getEntityTag is very particular on tag pattern.
            // Document<Feed> doc = res.getDocument();
            String body = read( res.getInputStream() );
            // RFC 4287 requires non-null id, title, updated elements
            Assert.assertTrue( -1 != body.indexOf( "</id>" ));
            Assert.assertTrue( -1 != body.indexOf( "</title>" ));
            Assert.assertTrue( -1 != body.indexOf( "</updated>" ));

            eTag = res.getHeader("ETag");
            Assert.assertNotNull( eTag );
            lastModified = res.getLastModified();
            Assert.assertNotNull( lastModified );
        } finally {
            res.release();
        }
    }   
View Full Code Here

        RequestOptions opts = new RequestOptions();
        final String contentType = "application/atom+xml";
        opts.setContentType(contentType);
        opts.setHeader( "If-Match", eTag);

        ClientResponse res = client.get(providerURI, opts);
        Assert.assertNotNull(res);
        try {
            String thisETag = res.getHeader("ETag");
            Assert.assertNotNull( thisETag );
            Date thisLastModified = res.getLastModified();
            Assert.assertNotNull( thisLastModified );

            // Should return 200 - Feed provided since it matches etag.
            Assert.assertEquals(200, res.getStatus());
            Assert.assertEquals(ResponseType.SUCCESS, res.getType());
            // AtomTestCaseUtils.printResponseHeaders( "Feed response headers:", "   ", res );
            // System.out.println("Feed response content:");
            // AtomTestCaseUtils.prettyPrint(abdera, res.getDocument());
        } finally {
            res.release();
        }
    }   
View Full Code Here

        RequestOptions opts = new RequestOptions();
        final String contentType = "application/atom+xml";
        opts.setContentType(contentType);
        opts.setHeader( "If-None-Match", eTag);

        ClientResponse res = client.get(providerURI, opts);
        Assert.assertNotNull(res);
        try {
            // Should return 304 - Feed not provided since it matches ETag.
            Assert.assertEquals(304, res.getStatus());
        } finally {
            res.release();
        }
    }   
View Full Code Here

        RequestOptions opts = new RequestOptions();
        final String contentType = "application/atom+xml";
        opts.setContentType(contentType);
        opts.setHeader( "If-Unmodified-Since", dateFormat.format( new Date() ));

        ClientResponse res = client.get(providerURI, opts);
        Assert.assertNotNull(res);
        try {
            // Should return 304 - Feed not provided since feed is modified since.
            Assert.assertEquals(304, res.getStatus());
        } finally {
            res.release();
        }
    }   
View Full Code Here

        RequestOptions opts = new RequestOptions();
        final String contentType = "application/atom+xml";
        opts.setContentType(contentType);
        opts.setHeader( "If-Modified-Since", dateFormat.format( new Date( 0 ) ));

        ClientResponse res = client.get(providerURI, opts);
        Assert.assertNotNull(res);
        try {
            // Should return 200 - Feed provided since feed is changed.
            Assert.assertEquals(200, res.getStatus());
            Assert.assertEquals(ResponseType.SUCCESS, res.getType());

            String thisETag = res.getHeader("ETag");
            Assert.assertNotNull( thisETag );
            Date thisLastModified = res.getLastModified();
            Assert.assertNotNull( thisLastModified );                       
        } finally {
            res.release();
        }
    }   
View Full Code Here

TOP

Related Classes of org.apache.abdera.protocol.client.ClientResponse

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.