Books
in black and white
Main menu
Share a book About us Home
Books
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics
Ads

More Java Pitfalls Share Reactor - Daconta M,C.

Daconta M,C. More Java Pitfalls Share Reactor - Wiley publishing, 2003. - 476 p.
ISBN: 0-471-23751-5
Download (direct link): morejavapitfallssharereactor2003.pdf
Previous << 1 .. 59 60 61 62 63 64 < 65 > 66 67 68 69 70 71 .. 166 >> Next

Now let's examine Listing 22.2 that presents the utility class, AmazonHttpGet, that performs the networking operations of the application.
001 package org.javapitfalls.item22;
002
003 import java.net.*;
004 import java.io.*;
005
006 class AmazonHttpGet
007 {
008 public static final boolean debug;
009
// - removed static block that sets debug variable
025
026 public static final String DEVTAG = "YOUR-DEV-TAG-HERE";
027 public static final String [] legalOps = { "KeywordSearch", 2
// - removed other keywords listed In Table 22.1 2
"SimilaritySearch", };
031
032 public static final String OP_KEYWORD_SEARCH = "KeywordSearch" ;
033 public static final String OP_BROWSE_NODE_SEARCH = 2
"BrowseNodeSearch";
// - removed remaining "operation" constants
043
044 public static final String [] legalModes = { "baby", "books",
// - removed other keywords listed In Table 22.1
047 "videogames", };
048
049 public static final String MODE_BABY = "baby";
050 public static final String MODE_BOOKS = "Books";
// - removed remaining "mode" constants
065
066 public static final String KEYWORD_MODE = "mode";
// - removed other KEYWORD constants
069
070 public static final String TYPE_LIGHT = "lite";
071 public static final String TYPE_HEAVY = "heavy";
072
// - removed stringExists() utility method
092
093 private StringBuffer urlBuf;
Listing 22.2 AmazonHttpGet.java (continued)
172 Item 22
This class was designed with modularity and reusability in mind. The class data members consist mostly of static constants (though many have been removed for brevity). The key data members are the DEVTAG provided when you register at Amazon.com and the urlBuf StringBuffer. In fact, this class does not go far enough in terms of composability of the various queries and additional checking of parameter combinations. Unfortunately, as you will see, most of this flexibility will be eliminated in the J2ME port to reduce the number of method invocations. Throughout this pitfall, you should notice the recurring theme of a mind set shift required to program for small devices. The StringBuffer contains a string representation of the URL we will GET:
// removed urlBuf accessor method
097
098 public AmazonHttpGet()
099 {
100 newBaseURL();
101 }
102
103 public void newBaseURL()
104 {
105 urlBuf = new StringBuffer("http://xml.amazon.com/ 2
onca/xml?v=1. 0&t=webservices-20&dev-t=" + DEVTAG);
106 }
107
108 public boolean validOp(String op)
109 {
110 if (stringExists(op, legalOps, false))
111 return true;
112 else
113 return false;
114 }
115
// removed validMode() as it is similar to validOp()
// removed validType() as it is similar to validOp()
// removed validPage() as it is similar to validOp()
145
146 public void addOperation(String operation, String target) 2
throws MalformedURLException
147 {
148 // validate the operation
149 if (validOp(operation))
150 {
151: urlBuf.append('&');
152 urlBuf.append(operation);
153 urlBuf.append('=');
154 if (target != null)
155 {
156 target.trim();
157 target = replaceString(target, " ", "%20", 0);
158
159 urlBuf.append(target);
Listing 22.2 (continued)
J2ME Performance and Pitfalls 173
160: }
161: else
162: throw new MalformedURLException("Invalid target");
163: }
164: else
165: throw new MalformedURLException("Invalid operation.");
166: }
167:
// - removed addMode() as it is similar to addOperation()
// - removed addType() as it is similar to addOperation()
// - removed addPage() as it is similar to addOperation()
206:
207: public void addFormat()
208: {
209: urlBuf.append("&f=xml"); // TBD: allow XSLT stylesheet
210: }
211:
// - removed replaceString() utility method
Listing 22.2 (continued)
The formatting of the URL involves validating and then appending name/value pairs to the urlBuf StringBuf f er. The addOperation() (line 146), addMode(), and addType() methods add the parameters discussed in Table 22.1 to the StringBuffer. Two utility methods were removed for brevity: stringExists() checked for the existence of a String in an array of Strings and replaceString() replaces a portion of a String with supplied replacement text:
233 public String httpGet() throws IOException
234 {
235 if (debug) System.out.println("URL: " + urlBuf.toString());
236
237 // Create a URL object
238 URL url = new URL(urlBuf.toString());
239 // get the connection object
240 URLConnection con = url.openConnection();
241
242 con.setDoOutput(true);
243 con.setUseCaches(false);
244
245 InputStream in = con.getInputStream();
246 BufferedReader br = new BufferedReader(new 2
InputStreamReader(in));
247
248 // read response
249 String line = null;
250 StringWriter sw2 = new StringWriter();
251 PrintWriter pw2 = new PrintWriter(sw2);
252 while ( (line = br.readLine()) != null)
Listing 22.2 (continued)
Previous << 1 .. 59 60 61 62 63 64 < 65 > 66 67 68 69 70 71 .. 166 >> Next