CHAPTER 7
204
Integration Based on HTTP Requests
HTTP calls are simpler than web services, so let’s start with them. Begin by creating a really simple Java
application that uses a few simple classes and JSP to generate XML content. We’ll consume this XML
content from within a Flex application.
This elementary Java application produces a collection of all-time top ten NBA scorers. Each entry in the
list includes the scorer’s first name and last name, the total score, and his position in the top ten list. Each
entry maps to a simple JavaBean class, appropriately called Scorer.java. Here’s a listing of the
Scorer.java source:
/* Scorer.java */
package org.shanky.flex.examples.nbascorers;
import java.io.Serializable;
public class Scorer implements Serializable {
private static final long serialVersionUID = 5529587875471400663L;
private int scorerId;
private String firstName;
private String lastName;
private int totalScore;
private int position;
public Scorer() {
super();
}
public Scorer(int scorerId, String firstName, String lastName,
int totalScore, int position) {
super();
this.scorerId = scorerId;
this.firstName = firstName;
this.lastName = lastName;
this.totalScore = totalScore;
this.position = position;
}
public int getScorerId() {
return scorerId;
}
public void setScorerId(int scorerId) {
this.scorerId = scorerId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
…// getters and setters for the lastName, totalScore and position.
INTEGRATING FLEX WITH JAVA USING SERVICES
205
}
The getter and setter methods for a few attributes are not included in the listing as they are implicitly
understood to be similar to the ones included. The complete source code is available in the book’s code
download bundle.
A collection of the top scorers is returned by a Java service class named TopScorersService.java. The
TopScorersService class has a method, named getTopScorers, that returns a collection of top NBA
scorers. The simple example here needs to return a list of just the all-time top ten scorers. Therefore,
getTopScorers ends up calling a private method called getAllTimeTop10Scorers, which creates and
returns an array list of the top ten scorers. To keep things flexible and allow alternative ways to access and
return the top scorers list, the getTopScorers method is parameterized, with time interval and collection
size as the parameters. Time interval is defined as an enumeration type with ALL_TIME and CURRENT the
two possible values. You can extend this enumeration class to include other time horizons, such as last
five years, last twenty years, or any such period. As for the collection size, top ten suffices for now. Ideally,
you may want to allow this to be equal to any arbitrary integer, say 20, 100, 350, or 10,000. The
implementation of the methods is simple and all the logic resides inline, but the example can be easily
extended to include an external data store and a data access intermediation layer. The
TopScorersService.java class is as follows:
/* TopScorersService.java */
package org.shanky.flex.examples.nbascorers;
import java.util.List;
import java.util.ArrayList;
public class TopScorersService {
public List<Scorer> getTopScorers(TimeInterval timeInterval, int collectionSize) {
if(timeInterval == TimeInterval.ALL_TIME && collectionSize == 10) {
return getAllTimeTop10Scorers();
} else {
return null;
}
}
private List<Scorer> getAllTimeTop10Scorers() {
List<Scorer> topScorersList = new ArrayList<Scorer>();
topScorersList.add(new
Scorer(1, "Kareem", "Abdul-Jabbar", 38387, 1));
topScorersList.add (new
Scorer(2, "Wilt", "Chamberlain", 31419, 2));
topScorersList.add (new
Scorer(3, "Karl", "Malone", 30599, 3));
topScorersList.add (new
Scorer(4, "Michael", "Jordan", 29277, 4));
Get AdvancED Flex 4 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.