001: package org.methodize.nntprss.rss.publish;
002:
003: /* -----------------------------------------------------------
004: * nntp//rss - a bridge between the RSS world and NNTP clients
005: * Copyright (c) 2002, 2003 Jason Brome. All Rights Reserved.
006: *
007: * email: nntprss@methodize.org
008: * mail: Methodize Solutions
009: * PO Box 3865
010: * Grand Central Station
011: * New York NY 10163
012: *
013: * This file is part of nntp//rss
014: *
015: * nntp//rss is free software; you can redistribute it
016: * and/or modify it under the terms of the GNU General
017: * Public License as published by the Free Software Foundation;
018: * either version 2 of the License, or (at your option) any
019: * later version.
020: *
021: * This program is distributed in the hope that it will be
022: * useful, but WITHOUT ANY WARRANTY; without even the implied
023: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
024: * PURPOSE. See the GNU General Public License for more
025: * details.
026: *
027: * You should have received a copy of the GNU General Public
028: * License along with this program; if not, write to the
029: * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
030: * Boston, MA 02111-1307 USA
031: * ----------------------------------------------------- */
032:
033: import java.io.BufferedReader;
034: import java.io.InputStreamReader;
035: import java.util.HashMap;
036: import java.util.Map;
037: import java.util.Vector;
038:
039: import org.apache.xmlrpc.XmlRpcClient;
040: import org.methodize.nntprss.rss.Item;
041:
042: /**
043: * @author Jason Brome <jason@methodize.org>
044: * @version $Id: BloggerPublisher.java,v 1.1 2003/03/17 04:45:22 jasonbrome Exp $
045: */
046:
047: public class BloggerPublisher implements Publisher {
048:
049: private static final String BLOGGER_APP_KEY = "0B18094ACF9546D113015FD8376930FA62A827";
050:
051: private static final String METHOD_NEWPOST = "blogger.newPost";
052:
053: private static final String METHOD_GETUSERINFO = "blogger.getUserInfo";
054:
055: /**
056: * @see org.methodize.nntprss.rss.publish.Publisher#publish(Map, String)
057: */
058: public void publish(Map profile, Item content)
059: throws PublisherException {
060:
061: // blogger.newPost takes the following parameters. All are required:
062: //
063: try {
064: XmlRpcClient xmlrpc = new XmlRpcClient((String) profile
065: .get(PROP_PUBLISHER_URL));
066: Vector params = new Vector();
067: // appkey (string): Unique identifier/passcode of the application sending the post. (See access info.)
068: params.addElement(BLOGGER_APP_KEY);
069:
070: // blogid (string): Unique identifier of the blog the post will be added to.
071: params.addElement(profile.get(PROP_BLOG_ID));
072:
073: // username (string): Login for a Blogger user who has permission to post to the blog.
074: params.addElement(profile.get(PROP_USERNAME));
075:
076: // password (string): Password for said username.
077: params.addElement(profile.get(PROP_PASSWORD));
078:
079: // content (string): Contents of the post.
080: params.addElement(content.getDescription());
081:
082: // publish (boolean): If true, the blog will be published immediately after the post is made.
083: params.addElement(Boolean.valueOf((String) profile
084: .get(PROP_PUBLISH)));
085:
086: String postId = (String) xmlrpc.execute(METHOD_NEWPOST,
087: params);
088:
089: } catch (Exception e) {
090: throw new PublisherException(e);
091: }
092: }
093:
094: /**
095: * @see org.methodize.nntprss.rss.publish.Publisher#validate(Map)
096: */
097: public void validate(Map profile) throws PublisherException {
098: // blogger.newPost takes the following parameters. All are required:
099: //
100: try {
101: XmlRpcClient xmlrpc = new XmlRpcClient((String) profile
102: .get(PROP_PUBLISHER_URL));
103: Vector params = new Vector();
104: // appkey (string): Unique identifier/passcode of the application sending the post. (See access info.)
105: params.addElement(BLOGGER_APP_KEY);
106:
107: // username (string): Login for a Blogger user who has permission to post to the blog.
108: params.addElement(profile.get(PROP_USERNAME));
109:
110: // password (string): Password for said username.
111: params.addElement(profile.get(PROP_PASSWORD));
112:
113: Map userInfo = (Map) xmlrpc.execute(METHOD_GETUSERINFO,
114: params);
115:
116: } catch (Exception e) {
117: throw new PublisherException(e);
118: }
119:
120: }
121:
122: public static void main(String args[]) {
123: // Tester...
124: BloggerPublisher pub = new BloggerPublisher();
125:
126: Map profile = new HashMap();
127:
128: try {
129: BufferedReader reader = new BufferedReader(
130: new InputStreamReader(System.in));
131: System.out.println("Blog id?");
132: String blogId = reader.readLine();
133:
134: System.out.println("Username?");
135: String userName = reader.readLine();
136:
137: System.out.println("Password?");
138: String password = reader.readLine();
139:
140: System.out.println("Content?");
141: String content = reader.readLine();
142:
143: System.out.println("Publish (true/false)?");
144: String publishStr = reader.readLine();
145: boolean publish = publishStr.equalsIgnoreCase("true");
146:
147: profile.put(PROP_BLOG_ID, blogId);
148: profile.put(PROP_PASSWORD, password);
149: profile.put(PROP_PUBLISH, new Boolean(publish));
150: profile.put(PROP_USERNAME, userName);
151:
152: profile.put(Publisher.PROP_PUBLISHER_URL,
153: "http://plant.blogger.com/api/RPC2");
154:
155: pub.validate(profile);
156:
157: Item newItem = new Item();
158: newItem.setDescription(content);
159:
160: pub.publish(profile, newItem);
161: } catch (Exception e) {
162: e.printStackTrace();
163: }
164:
165: }
166:
167: }
|