All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sun.syndication.propono.blogclient.metaweblog.MetaWeblogBlog Maven / Gradle / Ivy

Go to download

The ROME Propono subproject is a Java class library that supports publishing protocols, specifically the Atom Publishing Protocol and the legacy MetaWeblog API. Propono includes an Atom client library, Atom server framework and a Blog client that supports both Atom protocol and the MetaWeblog API.

The newest version!
/*   
 *  Copyright 2007 Dave Johnson (Blogapps project)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */ 
package com.sun.syndication.propono.blogclient.metaweblog;

import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Iterator;
import com.sun.syndication.propono.blogclient.BlogEntry;
import com.sun.syndication.propono.blogclient.Blog;
import com.sun.syndication.propono.blogclient.BlogClientException;
import com.sun.syndication.propono.blogclient.BlogResource;
import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
import java.util.TreeMap;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;

/**
 * Blog implementation that uses a mix of Blogger and MetaWeblog API methods.
 */
public class MetaWeblogBlog implements Blog {
    private String blogid;
    private String name;
    private URL    url;
    private String userName;
    private String password;
    private String appkey = "dummy";
    private Map    collections;
    
    private XmlRpcClient xmlRpcClient = null;

    /**
     * {@inheritDoc}
     */
    public String getName() { return name; }
    
    /**
     * {@inheritDoc}
     */
    public String getToken() { return blogid; }
    
    /**
     * String representation of blog, returns the name.
     */
    public String toString() { return getName(); }
    
    private XmlRpcClient getXmlRpcClient() { 
        
        if (xmlRpcClient == null) {
            XmlRpcClientConfigImpl xmlrpcConfig = new XmlRpcClientConfigImpl();
            xmlrpcConfig.setServerURL(url);
            xmlRpcClient = new XmlRpcClient();
            xmlRpcClient.setConfig(xmlrpcConfig);
        }
        return xmlRpcClient; 
    }
    
    MetaWeblogBlog(String blogid, String name, 
            URL url, String userName, String password) {
        this.blogid = blogid;
        this.name = name;
        this.url = url;
        this.userName = userName;
        this.password = password;
        this.collections = new TreeMap();
        collections.put("entries", 
            new MetaWeblogBlogCollection(this, "entries", "Entries", "entry"));
        collections.put("resources", 
            new MetaWeblogBlogCollection(this, "resources", "Resources", "*"));
    }

    MetaWeblogBlog(String blogId, String name, 
            URL url, String userName, String password, String appkey) {
        this(blogId, name, url, userName, password);
        this.appkey = appkey;
    }

    /**
     * {@inheritDoc}
     */
    public BlogEntry newEntry() {
        return new MetaWeblogEntry(this, new HashMap());
    }

    String saveEntry(BlogEntry entry) throws BlogClientException {
        Blog.Collection col = (Blog.Collection)collections.get("entries");
        return col.saveEntry(entry);
    }

    /**
     * {@inheritDoc}
     */
    public BlogEntry getEntry(String id) throws BlogClientException {
        try {
            Map result = (Map)
                getXmlRpcClient().execute("metaWeblog.getPost", new Object[] {id, userName, password});
            return new MetaWeblogEntry(this, result);
        } catch (Exception e) {
            throw new BlogClientException("ERROR: XML-RPC error getting entry", e);
        }
    }

    void deleteEntry(String id) throws BlogClientException {
        try {
            getXmlRpcClient().execute("blogger.deletePost", 
                new Object[] {appkey, id, userName, password, Boolean.FALSE});
        } catch (Exception e) {
            throw new BlogClientException("ERROR: XML-RPC error getting entry", e);
        }
    }

    /**
     * {@inheritDoc}
     */
    public Iterator getEntries() throws BlogClientException {
        return new EntryIterator();
    }

    /**
     * {@inheritDoc}
     */
    public BlogResource newResource(String name, String contentType, byte[] bytes) throws BlogClientException {
        return new MetaWeblogResource(this, name, contentType, bytes);
    }
    
    String saveResource(MetaWeblogResource resource) throws BlogClientException {  
        Blog.Collection col = (Blog.Collection)collections.get("resources");
        return col.saveResource(resource);
    }
    
    BlogResource getResource(String token) throws BlogClientException {
        return null;
    }
    
    /**
     * {@inheritDoc}
     */
    public Iterator getResources() throws BlogClientException {
        return new NoOpIterator();
    }
     
    void deleteResource(BlogResource resource) throws BlogClientException {
        // no-op
    }
    
    /**
     * {@inheritDoc}
     */
    public List getCategories() throws BlogClientException {
 
        ArrayList ret = new ArrayList();        
        try {
            Object result = 
                getXmlRpcClient().execute ("metaWeblog.getCategories", 
                new Object[] {blogid, userName, password});
            if (result != null && result instanceof HashMap) {
                // Standard MetaWeblog API style: struct of struts
                Map catsmap = (Map)result;
                Iterator keys = catsmap.keySet().iterator();
                while (keys.hasNext()) {
                    String key = (String)keys.next();
                    Map catmap = (Map)catsmap.get(key);
                    BlogEntry.Category category = new BlogEntry.Category(key);
                    category.setName((String)catmap.get("description"));
                    // catmap.get("htmlUrl"); 
                    // catmap.get("rssUrl");
                    ret.add(category);
                } 
            } else if (result != null && result instanceof Object[]) {
                // Wordpress style: array of structs
                Object[] resultArray = (Object[])result;
                for (int i=0; i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy