Package org.apache.abdera.protocol.client

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


    return getAuth(id, pwd, service);
  }
 
  protected String getAuth(String id, String pwd, String service) {
    try {
      Client client = new CommonsClient();
      Formatter f = new Formatter();
      f.format(
        "Email=%s&Passwd=%s&service=%s&source=%s",
        URLEncoder.encode(id, "utf-8"),
        URLEncoder.encode(pwd, "utf-8"),
        (service != null) ? URLEncoder.encode(service, "utf-8") : "",
        URLEncoder.encode(Version.APP_NAME, "utf-8"));
      StringRequestEntity stringreq = new StringRequestEntity(f.toString());
      String uri = "https://www.google.com/accounts/ClientLogin";
      RequestOptions options = client.getDefaultRequestOptions();
      options.setContentType("application/x-www-form-urlencoded");
      ClientResponse response = client.post(uri, stringreq, options);
      InputStream in = response.getInputStream();
      ByteArrayOutputStream out = new ByteArrayOutputStream();
      int n = -1;
      while ((n = in.read()) != -1) { out.write(n); }
      out.flush();
View Full Code Here


        new QName(
          "http://www.w3.org/2000/09/xmldsig#",
          "Signature")));

    // Verify the signature with Verisign's "Signed Ping" interop endpoint
    Client client = new CommonsClient();
    RequestOptions reqoptions = client.getDefaultRequestOptions();
    reqoptions.setContentType("application/xml");
    BaseRequestEntity bre = new BaseRequestEntity(entry,false);
    ClientResponse response = client.post(
      "http://verisignlabs.com/tg/verify",
      bre, reqoptions);
    assertEquals(response.getStatus(),200);
    Document<Element> result = response.getDocument();
   
View Full Code Here

        CollectionServlet.class.getName(), "/collections/*"
      );
 
 
  public void testAppClient() throws Exception {
    Client client = new CommonsClient();
    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();
View Full Code Here

//    assertEquals(response.getStatus(), HttpServletResponse.SC_OK);
//    assertEquals(resp1, "5");
  }
 
  public void testResponseMustRevalidate() throws Exception {
    Client client = new CommonsClient();
    RequestOptions options = client.getDefaultRequestOptions();
    options.setHeader("Connection", "close");
    options.setHeader("x-reqnum", "1");
    ClientResponse response = client.get(CHECK_MUST_REVALIDATE, options);
 
    String resp1 = getResponse(response);
    assertEquals(resp1, "1");
   
    // Should be revalidated and use the cache
    options.setHeader("x-reqnum", "2");
    response = client.get(CHECK_MUST_REVALIDATE, options);
    assertTrue(response instanceof CachedResponse);
   
    String resp2 = getResponse(response);
    assertEquals(resp2, "1");
   
    // Should be revalidated and return a 404
    options.setHeader("x-reqnum", "3");
    response = client.get(CHECK_MUST_REVALIDATE, options)
    assertEquals(response.getStatus(), 404);
    response.release();

  }
View Full Code Here

  }
 
  private void _methodInvalidates(int type) throws Exception {
   
    Client client = new CommonsClient();
    RequestOptions options = client.getDefaultRequestOptions();
    options.setHeader("Connection", "close");
    options.setHeader("x-reqnum", "1");
    ClientResponse response = client.get(CHECK_CACHE_INVALIDATE, options);
 
    String resp1 = getResponse(response);
    response.release();
    assertEquals(resp1, "1");
   
    // calling a method that could change state on the server should invalidate the cache
    options.setHeader("x-reqnum", "2");
    switch(type) {
      case POST: 
        response = client.post(
          CHECK_CACHE_INVALIDATE,
          new ByteArrayInputStream("".getBytes()),
          options);
        break;
      case PUT:
        response = client.put(
          CHECK_CACHE_INVALIDATE,
          new ByteArrayInputStream("".getBytes()),
          options);
        break;
      case DELETE:
        response = client.delete(
          CHECK_CACHE_INVALIDATE,
          options);
        break;
    }
    response.release();
   
    options.setHeader("x-reqnum", "3");
    response = client.get(CHECK_CACHE_INVALIDATE, options);
 
    resp1 = getResponse(response);
    response.release();
    assertEquals(resp1, "3");
  }
View Full Code Here

    assertEquals(resp1, "3");
  }
 
  private void _requestCacheInvalidation(int type) throws Exception {
   
    Client client = new CommonsClient();
    RequestOptions options = client.getDefaultRequestOptions();
    options.setHeader("Connection", "close");
    options.setHeader("x-reqnum", "1");
    ClientResponse response = client.get(CHECK_CACHE_INVALIDATE, options)
    String resp1 = getResponse(response);
    assertEquals(resp1, "1");
   
    // Should not use the cache
    options.setHeader("x-reqnum", "2");
    switch(type) {
      case NOCACHE: options.setNoCache(true); break;
      case NOSTORE: options.setNoStore(true); break;
      case MAXAGE0: options.setMaxAge(0); break;
    }
    response = client.get(CHECK_CACHE_INVALIDATE, options);
 
    String resp2 = getResponse(response);
    assertEquals(resp2, "2");
   
    // Should use the cache
    options.setHeader("x-reqnum", "3");
    switch(type) {
      case NOCACHE: options.setNoCache(false); break;
      case NOSTORE: options.setNoStore(false); break;
      case MAXAGE0: options.setMaxAge(60); break;
    }
    response = client.get(CHECK_CACHE_INVALIDATE, options);
 
    String resp3 = getResponse(response);
    assertEquals(resp3, "2");
  }
View Full Code Here

    assertEquals(resp3, "2");
  }
 
  private void _responseNoCache(int type) throws Exception {
   
    Client client = new CommonsClient();
    RequestOptions options = client.getDefaultRequestOptions();
    options.setHeader("Connection", "close");
    options.setHeader("x-reqnum", "1");
    options.setHeader("x-reqtest", String.valueOf(type));
    ClientResponse response = client.get(CHECK_NO_CACHE, options);
 
    String resp1 = getResponse(response);
    assertEquals(resp1, "1");
   
    // Should not use the cache
    options.setHeader("x-reqnum", "2");
    response = client.get(CHECK_NO_CACHE, options);
 
    String resp2 = getResponse(response);
    assertEquals(resp2, "2");
   
    // Should use the cache
    options.setHeader("x-reqnum", "3");
    response = client.get(CHECK_NO_CACHE, options);
 
    String resp3 = getResponse(response);
    assertEquals(resp3, "3");
  }
View Full Code Here

        CollectionServlet.class.getName(), "/collections/*"
      );
 
 
  public void testRequestOptions() throws Exception {
    Client client = new CommonsClient();
    RequestOptions options = client.getDefaultRequestOptions();
    options.setIfModifiedSince(new Date());
    assertNotNull(options.getIfModifiedSince());
  }
View Full Code Here

    options.setIfModifiedSince(new Date());
    assertNotNull(options.getIfModifiedSince());
  }
 
  public void testAppClient() throws Exception {
    Client client = new CommonsClient();
    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();
View Full Code Here

public class Main {

  public static void main(String[] args) throws Exception {
   
    Abdera abdera = new Abdera();
    Client client = new CommonsClient(abdera);
    Factory factory = abdera.getFactory();
   
    // Perform introspection.  This is an optional step.  If you already
    // know the URI of the APP collection to POST to, you can skip it.
    Document<Service> introspection =
      client.get(
        args[0]).getDocument();
    Service service =
      introspection.getRoot();
    Collection collection =
      service.getCollection(
        args[1],
        args[2]);
    report("The Collection Element", collection.toString());
   
    // Create the entry to post to the collection
    Entry entry = factory.newEntry();
    entry.setId("tag:example.org,2006:foo", false);
    entry.setTitle("This is the title");
    entry.setUpdated(new Date());
    entry.addAuthor("James");
    entry.setContent("This is the content");
    report("The Entry to Post", entry.toString());
   
    // Post the entry. Be sure to grab the resolved HREF of the collection
    Document<Entry> doc = client.post(
      collection.getResolvedHref().toString(),
      entry).getDocument();
   
    // In some implementations (such as Google's GData API, the entry URI is
    // distinct from it's edit URI.  To be safe, we should assume it may be
    // different
    IRI entryUri = doc.getBaseUri();
    report("The Created Entry", doc.getRoot().toString());
   
    // Grab the Edit URI from the entry.  The entry MAY have more than one
    // edit link.  We need to make sure we grab the right one.
    IRI editUri = getEditUri(doc.getRoot());
   
    // If there is an Edit Link, we can edit the entry
    if (editUri != null) {
      // Before we can edit, we need to grab an "editable" representation
      doc = client.get(editUri.toString()).getDocument();   
     
      // Change whatever you want in the retrieved entry
      doc.getRoot().getTitleElement().setValue("This is the changed title");
     
      // Put it back to the server
      client.put(editUri.toString(), doc.getRoot());
     
      // This is just to show that the entry has been modified
      doc = client.get(entryUri.toString()).getDocument();
      report("The Modified Entry", doc.getRoot().toString());
    } else {
      // Otherwise, the entry cannot be modified (no suitable edit link was found)
      report("The Entry cannot be modified", null);
    }

    // Delete the entry.  Again, we need to make sure that we have the current
    // edit link for the entry
    doc = client.get(entryUri.toString()).getDocument();
    editUri = getEditUri(doc.getRoot());
    if (editUri != null) {
      client.delete(editUri.toString());
      report("The Enry has been deleted", null);
    } else {
      report("The Entry cannot be deleted", null);
    }
  }
View Full Code Here

TOP

Related Classes of org.apache.abdera.protocol.client.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.