KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > blogs > BlogTester


1 /*
2  * ____.
3  * __/\ ______| |__/\. _______
4  * __ .____| | \ | +----+ \
5  * _______| /--| | | - \ _ | : - \_________
6  * \\______: :---| : : | : | \________>
7  * |__\---\_____________:______: :____|____:_____\
8  * /_____|
9  *
10  * . . . i n j a h i a w e t r u s t . . .
11  *
12  *
13  *
14  * ----- BEGIN LICENSE BLOCK -----
15  * Version: JCSL 1.0
16  *
17  * The contents of this file are subject to the Jahia Community Source License
18  * 1.0 or later (the "License"); you may not use this file except in
19  * compliance with the License. You may obtain a copy of the License at
20  * http://www.jahia.org/license
21  *
22  * Software distributed under the License is distributed on an "AS IS" basis,
23  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
24  * for the rights, obligations and limitations governing use of the contents
25  * of the file. The Original and Upgraded Code is the Jahia CMS and Portal
26  * Server. The developer of the Original and Upgraded Code is JAHIA Ltd. JAHIA
27  * Ltd. owns the copyrights in the portions it created. All Rights Reserved.
28  *
29  * The Shared Modifications are Jahia View Helper.
30  *
31  * The Developer of the Shared Modifications is Jahia Solution Sŕrl.
32  * Portions created by the Initial Developer are Copyright (C) 2002 by the
33  * Initial Developer. All Rights Reserved.
34  *
35  * ----- END LICENSE BLOCK -----
36  */

37
38 package org.jahia.blogs;
39
40 import org.apache.xmlrpc.XmlRpcClient;
41 import org.apache.xmlrpc.XmlRpcException;
42
43 import java.io.IOException JavaDoc;
44
45 import java.util.Vector JavaDoc;
46
47 import org.apache.commons.httpclient.HttpClient;
48 import org.apache.commons.httpclient.methods.PostMethod;
49
50 import org.apache.log4j.Logger;
51
52 /**
53  * Simple tester class for testing the XML-RPC methods and the ping servlet.
54  *
55  * @author Xavier Lawrence
56  */

57 public class BlogTester {
58     
59     public static final String JavaDoc SERVER = "http://localhost:8080/jahia/blogs";
60     public static final String JavaDoc TB_PING_SERVLET =
61             "http://localhost:8080/jahia/trackbacks";
62     
63     static Logger log = Logger.getLogger(BlogTester.class);
64     
65     /** Creates a new instance of BlogTester */
66     public BlogTester() {
67     }
68     
69     /**
70      *
71      */

72     private static Object JavaDoc execute(XmlRpcClient xmlrpc, String JavaDoc methodName,
73             Vector JavaDoc params) throws XmlRpcException, IOException JavaDoc {
74         return xmlrpc.execute(methodName, params);
75     }
76     
77     /**
78      *
79      */

80     private static Vector JavaDoc toVector(String JavaDoc[] params) {
81         Vector JavaDoc result = new Vector JavaDoc(params.length);
82         
83         for (int i=0; i<params.length; i++) {
84             result.addElement(params[i]);
85         }
86         return result;
87     }
88     
89     /**
90      *
91      */

92     private static String JavaDoc sendPing(String JavaDoc postID) throws IOException JavaDoc {
93         HttpClient client = new HttpClient();
94         PostMethod post = new PostMethod(TB_PING_SERVLET);
95         
96         post.setRequestHeader("Content-type",
97                 "application/x-www-form-urlencoded; charset=utf-8");
98         
99         post.addParameter("entryID", postID);
100         post.addParameter("url", "http://test.com/12");
101         post.addParameter("title", "Le titre");
102         post.addParameter("excerpt", "Un court résumé");
103         post.addParameter("blog_name", "Le nom du blog");
104         
105         int status = client.executeMethod(post);
106         String JavaDoc body = post.getResponseBodyAsString();
107         
108         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
109         buff.append("StatusCode: ");
110         buff.append(status);
111         buff.append("; ");
112         buff.append(body);
113         
114         return buff.toString();
115     }
116     
117     /**
118      *
119      */

120     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
121         
122         String JavaDoc postID = "27";
123         
124         if (args == null || args.length == 0) {
125             log.info("Need to specify a postID to ping as argument");
126             log.info("Using postID "+postID);
127         } else {
128             postID = args[0];
129         }
130         
131         XmlRpcClient xmlrpc = new XmlRpcClient(SERVER);
132         
133         String JavaDoc[] params = {postID};
134         String JavaDoc name = "mt.getTrackbackPings";
135         
136         log.info("Sending ping...");
137         String JavaDoc result = sendPing(postID);
138         log.info("Result of ping: "+result);
139
140         log.info("Executing "+name);
141         result = execute(xmlrpc, name, toVector(params)).toString();
142         log.info("Result of "+name+": "+result);
143     }
144 }
145
Popular Tags