All posts by ajohnson

apache xml-rpc metaWeblog api

Some irish guy wrote up a short blogging APIs mini how-to yesterday. Then some other guy from England (I think) commented that he had written up a quick example of using the apache xml-rpc package to call the metaWeblog api. So I had to try it too. Turns out that a) xml-rpc is really easy to do and b) the Moveable Type xml-rpc api is really easy too… the code below calls the getRecentPosts() method of the metaWeblog api using the apache xml-rpc package:

import org.apache.xmlrpc.*;
import java.util.*;
public class MTTest {
  public static void main(String[] args) throws Exception {

   System.out.println(“Starting Movable type stuff”);
   Vector params=null;

   // Create XML RPC Client object
   XmlRpcClient xmlrpc = new XmlRpcClient(“http://yourhost.com/MT-2.21/mt-xmlrpc.cgi”);

   // blogger.getRecentPosts
   // String appkey, String blogid, String username, String password, int numberOfPosts
   System.out.println(“Adding the parameters”);
   params = new Vector();
   params.add(“your_weblog_id”); // weblog id
   params.add(“yourusername”); // username
   params.add(“yourpassword”); // password
   params.add(“4”); // number of posts to retrieve…

   System.out.println(“Making the call”);
   Vector v = (Vector)xmlrpc.execute(“metaWeblog.getRecentPosts”,params);

   System.out.println(“This call results in ” + v.size() + ” posts.”);
   System.out.println(“These are the results:”);
    for (int i=0; i

3650 better than a PDA?

51 Melcher Street where I work in Boston I mentioned yesterday that the wife let me get the Nokia 3650. AT&T Fedexed it overnight to me, so I got it on Tuesday… I struggled to get mMode mail working (I still haven’t), but I was able to get my personal mail working very quickly… sending pics from the phone is even easier.

Anyway, Russ quotes PDABuyersGuide.com today, which mentions (in no uncertain terms) that the 3650 features were well known by a certain Palm exec. For good reason! The 3650 is a phone and pda and camera and video recorder with bluetooth… but a phone first, unlike anything that will ever come out of Palm (because Palm makes PDA’s first and phones second).

Overriding the equals() method

I was looking for any best practices related to overriding the equals() method in objects and came across javapractices.com, which has a section devoted to the equals() method: Implementing equals. This article doesn’t really answer the question of “when” you override the method, although from other reading I can surmise that you do so whenever you think you or someone else may need to use the object as a key in a hashtable. Any other opinions as to the “when” of overriding the equals() method?

Application settings when using servlets

I posted some notes a couple weeks ago about .NET configuration files, came across the code for doing the same thing in a servlet container tonight. Basically, you get a ServletConfig object using the getServletConfig() method of the Servlet interface and then call getInitParameter(String initParamName) on that. For example, let’s say that I put a global database connection string in my web.xml deployment descriptor:

<web-app>
  <servlet>
   <servlet-name>myservlet</servlet-name>
   <servlet-class>MyServlet</servlet-class>
    <init-param>
     <param-name>connection_string</param-name>
     <param-value>user id=sa;password=mypassword;initial catalog=mydb;data source=mydbserver;Connect Timeout=30</param-value>
    </init-param>
  </servlet>
</web-app>

Then, in my servlet code init() method, I could write this:

ServletConfig sc = getServletConfig();
String connection_string = sc.getInitParameter(“connection_string”);

After retrieving the value from the ServletConfig object, you’ll probably want to store it for later use in the ServletContext object:

ServletContext c = getServletContext();
c.setAttribute(“connection_string”, connection_string);

Now I can get the connection string in a JSP or a servlet later using this short snippet:

String myCS = (String)getServletContext().getAttribute(“connection_string”);

user level security in apache

I needed to lock down a couple directories on this server using Basic Authentication in Apache… in case you’ve ever wanted too, or someday will need too:

1) Add the username to the password file using the htpasswd module:

# htpasswd /etc/httpd/users username

where ‘/etc/httpd/users’ is the name of the file you want your users/passwords stored and ‘username’ is the username you want to add. It’ll ask you for the password, and then again before adding the user to the file. If the file doesn’t exist, usr the -c flag to create the file:

# htpasswd -c /etc/httpd/users username

2) Then add a .htaccess file in the directory you wan to secure that contains this:

AuthName “username”
AuthType Basic
AuthUserFile /etc/httpd/users
require user username

where ‘/etc/httpd/users/’ is the name your passwd file and ‘username’ is the name of the user you want to allow in this directory.