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.Hashtable;
037: import java.util.Map;
038: import java.util.Vector;
039:
040: import org.apache.xmlrpc.XmlRpcClient;
041: import org.methodize.nntprss.rss.Item;
042:
043: /**
044: * @author Jason Brome <jason@methodize.org>
045: * @version $Id: MetaWeblogPublisher.java,v 1.1 2003/03/22 16:33:26 jasonbrome Exp $
046: */
047:
048: public class MetaWeblogPublisher implements Publisher {
049:
050: private static final String METHOD_NEWPOST = "metaWeblog.newPost";
051:
052: private static final String BLOGGER_APP_KEY = "0B18094ACF9546D113015FD8376930FA62A827";
053:
054: private static final String METHOD_GETUSERINFO = "blogger.getUserInfo";
055:
056: private static final String METHOD_GETCATEGORIES = "metaWeblog.getCategories";
057:
058: private static final String STRUCT_TITLE = "title";
059: private static final String STRUCT_DESCRIPTION = "description";
060: private static final String STRUCT_LINK = "link";
061:
062: /**
063: * @see org.methodize.nntprss.rss.publish.Publisher#publish(Map, String)
064: */
065: public void publish(Map profile, Item content)
066: throws PublisherException {
067:
068: // blogger.newPost takes the following parameters. All are required:
069: //
070: try {
071: XmlRpcClient xmlrpc = new XmlRpcClient((String) profile
072: .get(PROP_PUBLISHER_URL));
073: Vector params = new Vector();
074:
075: // blogid (string): Unique identifier of the blog the post will be added to.
076: params.addElement(profile.get(PROP_BLOG_ID));
077:
078: // username (string): Login for a Blogger user who has permission to post to the blog.
079: params.addElement(profile.get(PROP_USERNAME));
080:
081: // password (string): Password for said username.
082: params.addElement(profile.get(PROP_PASSWORD));
083:
084: // struct - title, link, description
085: Map struct = new Hashtable();
086:
087: if (content.getTitle() != null) {
088: struct.put(STRUCT_TITLE, content.getTitle());
089: }
090:
091: if (content.getDescription() != null) {
092: struct
093: .put(STRUCT_DESCRIPTION, content
094: .getDescription());
095: }
096:
097: if (content.getLink() != null) {
098: struct.put(STRUCT_LINK, content.getLink());
099: }
100:
101: // content (string): Contents of the post.
102: params.addElement(struct);
103:
104: // publish (boolean): If true, the blog will be published immediately after the post is made.
105: params.addElement(Boolean.valueOf((String) profile
106: .get(PROP_PUBLISH)));
107:
108: String postId = (String) xmlrpc.execute(METHOD_NEWPOST,
109: params);
110:
111: } catch (Exception e) {
112: throw new PublisherException(e);
113: }
114: }
115:
116: /**
117: * @see org.methodize.nntprss.rss.publish.Publisher#validate(Map)
118: */
119: public void validate(Map profile) throws PublisherException {
120: // MetaWebLog API does not currently expose an endpoint
121: // that can be used for URL / username / password validation
122: // Can't use blogger.getUserInfo, as not supported by Radio
123: // Use metaWeblog.getCategories
124:
125: try {
126: XmlRpcClient xmlrpc = new XmlRpcClient((String) profile
127: .get(PROP_PUBLISHER_URL));
128: Vector params = new Vector();
129: // blogid (string): Unique identifier of the blog the post will be added to.
130: params.addElement(profile.get(PROP_BLOG_ID));
131:
132: // username (string): Login for a Blogger user who has permission to post to the blog.
133: params.addElement(profile.get(PROP_USERNAME));
134:
135: // password (string): Password for said username.
136: params.addElement(profile.get(PROP_PASSWORD));
137:
138: Map userInfo = (Map) xmlrpc.execute(METHOD_GETCATEGORIES,
139: params);
140:
141: } catch (Exception e) {
142: throw new PublisherException(e);
143: }
144:
145: }
146:
147: public static void main(String args[]) {
148: // Tester...
149: MetaWeblogPublisher pub = new MetaWeblogPublisher();
150:
151: Map profile = new HashMap();
152:
153: try {
154: BufferedReader reader = new BufferedReader(
155: new InputStreamReader(System.in));
156: System.out.println("Blog id?");
157: String blogId = reader.readLine();
158:
159: System.out.println("Username?");
160: String userName = reader.readLine();
161:
162: System.out.println("Password?");
163: String password = reader.readLine();
164:
165: System.out.println("Description?");
166: String description = reader.readLine();
167:
168: System.out.println("Publish (true/false)?");
169: String publishStr = reader.readLine();
170: boolean publish = publishStr.equalsIgnoreCase("true");
171:
172: profile.put(PROP_BLOG_ID, blogId);
173: profile.put(PROP_PASSWORD, password);
174: profile.put(PROP_PUBLISH, new Boolean(publish));
175: profile.put(PROP_USERNAME, userName);
176:
177: profile.put(Publisher.PROP_PUBLISHER_URL,
178: "http://192.168.1.103:5335/RPC2");
179:
180: pub.validate(profile);
181:
182: Item content = new Item();
183: content.setDescription(description);
184:
185: pub.publish(profile, content);
186: } catch (Exception e) {
187: e.printStackTrace();
188:
189: }
190:
191: }
192:
193: }
|