2010-08-11

Google Sites API

  1. get XHTML text
    ((XhtmlTextConstruct) entry.getTextContent().getContent()).getXhtml().getBlob()
  2. get plain text (no xml tags)
    entry.getTextContent().getContent().getPlainText().
  3. update xhtml
    XmlBlob newBlob = new XmlBlob();
    newBlob.setBlob(updatedXml);
    entry.setContent(new XhtmlTextConstruct(newBlob));
    entry.update();
    System.out.println("Updated " + getEntryId(entry.getId()));
  4. get all entries (possible failure in getting all entries if there is a write action to the server during these code)
    List<ContentFeed> feeds = new LinkedList<ContentFeed>();
    
    int startIndex = 1;
    while (true) {
    q.setStringCustomParameter("start-index",
    String.valueOf(startIndex));
    ContentFeed contentFeed = client.query(q, ContentFeed.class);
    int entrySize = contentFeed.getEntries().size();
    if (entrySize > 0) {
    feeds.add(contentFeed);
    startIndex += entrySize;
    System.out.println(getEntryId(contentFeed.getEntries().get(0).getId()));
    } else {
    break;
    }
    }
  5. Download a file using browser-accessible URL:
    private byte[] getData(String url) {
    
    MediaContent mc = new MediaContent();
    mc.setUri(url);
    MediaSource msrc;
    try {
    msrc = client.getMedia(mc);
    InputStream in = msrc.getInputStream();
    return org.apache.commons.io.IOUtils.toByteArray(in);
    } catch (Exception e) {
    return null;
    }
    }
  6. Alternative to download a file using browser-accessible URL:
    private byte[] getData(String url) {
    
    try {
    ILink link = new com.google.gdata.model.atom.Link("alternate", null, url);
    GDataRequest req = client.createLinkQueryRequest(link);
    req.execute();
    InputStream in = req.getResponseStream();
    return org.apache.commons.io.IOUtils.toByteArray(in));
    } catch (Exception e) {
    return null;
    }
    }
  7. Retrieve the entry corresponding to a browser-accessible URL(http://sites.google.com/site/siteName/path/to/the/page):
    ContentQuery query = new ContentQuery(new URL(buildContentFeedUrl()));
    
    query.setPath("/path/to/the/page");
    ContentFeed contentFeed = client.getFeed(query, ContentFeed.class);
    for (BaseContentEntry entry : contentFeed.getEntries()) {
    // do something with the entry
    }