Music Post: Don Felder

It’s almost Thanksgiving and I haven’t posted anything about music in while so here’s one that’s been sitting as a draft for some time now.  I hope you find it interesting.

Don Felder - Heaven and Hell

Don Felder - Heaven and Hell

If you are an Eagles fan, you probably know this already, for those of you that are not familiar with him, Don Felder was one of the driving forces behind the Eagles great success. He and Joe Walsh really were largely responsible for transforming the Eagles from a country band to a rock band in the mid to late seventies.

I been an Eagles fan ever since l spent hours upon hours listening to Eagles Live over my buddy’s house. I was ten or eleven years old and his brother had a decent collection of 70’s rock including Springsteen, Billy Joel, and the Eagles. One of our favorites was Eagles Live, a great album even now. Back then I had no idea who Felder was and even as I grew older I was mostly aware of Henley and Frey as being the driving force behind the Eagles.

Felder’s recently published book, Heaven and Hell: My Life in the Eagles 1974-2001, describes in detail his childhood and especially his involvement in the Eagles. I always suspecting Henley of being a bit of a ass, but after reading that book and hearing him interviewed on Howard Stern’s radio show a couple weeks ago, I realize that both Henley and Frey were complete ego-maniacs, control-freaks, and just plain greedy.  Now I know this is coming from Felder himself and I’m sure everyone has their own side of the story, but Felder really comes across as honest overall, if a little bitter.

One of the more interesting aspects of the book is the many Rock icons that Felder either grew up with or worked with before joing The Eagles.  For instance, he actually taught Tom Petty to play guitar early in his life.

If you are a fan of the Eagles or a fan of rock music in the 70’s, i recommend you take a look at this book, it’s really surprisingly well written and quite interesting. It is a little depressing, however, in that it really sounds like being a member of the band during their heyday wasn’t nearly as much fun as it should have been.

You can check it out at Google Book Search.

Share/Save/Bookmark

Accessing Twitter with Java using HttpClient

Today I am going to post about making authenticated REST calls using Java and the Jakarta Commons HttpClient library.  I am going to use Twitter as an example.  I originally wanted an example that would allow me to post simultaneously to Facebook, LinkedIn and Twitter, but Twitter is the only one that allows such access without going through unnecessary hoops, so here it is.

The HttpClient library is a very useful library that simplifies the process of making HTTP calls in Java.  The project has been around since 2001, originally as a subproject of Jakarta Comons.

The main reason to use the HttpClient library is that it makes calling authenticated HTTP sources easy.  Making HTTP calls in Java is fairly straightforward now anyway, it gets a little more difficult when authentication is involved.  The HttpClient library makes the whole process somewhat trivial, as this example shows.

Since there are two different types of calls to make, but both utilize the same foundation in HttpClient, I found it useful to create a helper method that actually sets up the authentication and executes the call that is passed to it.

Both the Get and the Post methods extend HttpMethodBase, so that will be the input argument.  It is assumed that the user name and password needed for authentication are attributes of the class this method is in and that they have already been set.

Here is the code for the helper method:

/**
 * Executes an Authenticated HTTP method (Get or Post)
 * @param method - the get or post to execute
 * @throws NullPointerException
 * @throws HttpException
 * @throws IOException
 */
private void executeAuthenticatedMethod(HttpMethodBase method)
  throws NullPointerException, HttpException, IOException {
  if ((twitteruser == null)||(twitterpwd == null)) {
    throw new RuntimeException("User and/or password has not been initialized!");
  }
  HttpClient client = new HttpClient();
  Credentials credentials =
    new UsernamePasswordCredentials(this.getTwitteruser(), this.getTwitterpwd());
  client.getState()
    .setCredentials(new AuthScope(twitterhost, 80, AuthScope.ANY_REALM), credentials);
  HostConfiguration host = client.getHostConfiguration();
  host.setHost(new URI("http://"+twitterhost, true));
  client.executeMethod(host, method);
}

Here you can see that all that is necessary to set up an authenticated call is to create some new credentials and assign them to the State of the client.  To configure the call it is necessary to set up the host by setting the HTTP address of the host, in this case “http://www.twitter.com”.  Finally the call that has been passed in to this method is executed.  The results can be access via the call that was passed in so there is no reason to return anything at this point.

The Get is really simple once this helper method is in place.  To demonstrate the get method, I will use the retrieval of a Friends Time LIne in Twitter.  This is an authenticated call as the post will be.

	/**
	 * Gets the 20 most recent statuses by friends (equivalent to /home on the web)
	 * @return a String representing the XML response.
	 */
	private String getFriendsTimeline() {
		String message = "";
		String url = "/statuses/friends_timeline.xml";
		GetMethod get = new GetMethod(url);
	    try {
	    	executeAuthenticatedMethod(get);
		message = message + get.getResponseBodyAsString();
		} catch (URIException e) {
			message = e.getMessage();
		} catch (NullPointerException e) {
			message = e.getMessage();
		} catch (IOException e) {
			message = e.getMessage();
		} finally {
			get.releaseConnection();
		}
		return message;
	}

Here, to use the get, all we need to do now is create the GetMethod with the correct url and pass it to our helper method to be executed. The response is simply returned as a String, which in this case is simply XML. One item to note, is that after the response has been retrieved, the connection is released. This should happen no matter what the status of the call so that the connection to the host is terminated and the resources are freed up.

The post is almost as simple as the get, the only difference in our call is that for the post we are including a parameter that is the Twitter status message to be posted.

	/**
	 * Update your Twitter Status
	 * @param state
	 * @return
	 */
	private String postStateToTwitter(String status) {
		String url = "/statuses/update.xml";
		String message = "";
		PostMethod post = new PostMethod(url);
		NameValuePair params[] = new NameValuePair[1];
		params[0] = new NameValuePair("status", status);
		post.setRequestBody(params);
		message = "Status:"+post.getStatusText() + " " + message;
	    try {
	    	executeAuthenticatedMethod(post);
		} catch (URIException e) {
			message = e.getMessage();
		} catch (NullPointerException e) {
			message = e.getMessage();
		} catch (IOException e) {
			message = e.getMessage();
		} finally {
			post.releaseConnection();
		}

		return message;
	}

To pass the parameter we use the NameValuePair class in the HttpClient library. Create an array of NameValuePair to set and set the names and values accordingly. In this case the name is “status” and the value is the String called status that is passed to the method. Again, remember to release the connection after the call has been made. For this particular method call we are not interesed in the response body, only that what the status of the call is. After running this, you should get a status of “OK” returned from the call.

It is all pulled together in the TwitterClient.java file which you can download here.

Check out the Twitter API Wiki for more information on the Twitter API.

Check out the HttpClient project home for more information about the HttpClient project.

If you are really bored, you can follow me on Twiiter.

Share/Save/Bookmark