5. Security Policy for Network Connections
The connections discussed
above are part of the Net Access function group (see the RSP addendum to
the MIDP specification). On the Nokia N95, for example, an untrusted
MIDlet can access the Net Access function group with User permission
(explicit confirmation required from the user). Figure 1
shows an example of an unsigned MIDlet, Google's Gmail, and the
available permission options on a Nokia N95. This policy varies from
licensee to licensee so you must check with the manufacturer of your
target devices which settings apply for existing security domains.
6. NetworkDemo MIDlet
We finish this section with a simple example using MIDP's javax.microedition.io.HttpConnection to interrogate a web server. The NetworkDemo MIDlet connects to a web server via HttpConnection,
and reads and displays all the headers and the first 256 characters of
the response itself. Let's take a look at the core network functionality
exploited in this MIDlet. The connection work is all done in the ClientConnection class:
public class ClientConnection extends Thread {
private static final int TEXT_FIELD_SIZE = 256;
private NetworkDemoMIDlet midlet = null;
private String url = null;
public ClientConnection(NetworkDemoMIDlet midlet) {
this.midlet = midlet;
}
public void sendMessage(String url) {
this.url = url;
start();
}
public void run() {
try{
HttpConnection conn = (HttpConnection)Connector.open(url);
int responseCode = conn.getResponseCode();
midlet.append("Response code","" + responseCode);
int i = 0;
String headerKey = null;
while((headerKey = conn.getHeaderFieldKey(i++)) != null) {
midlet.append(headerKey,conn.getHeaderField(headerKey));
}
InputStream in = conn.openInputStream();
StringBuffer buffer = new StringBuffer(TEXT_FIELD_SIZE);
int ch;
int read = 0;
while ( (ch = in.read()) != −1 && read < TEXT_FIELD_SIZE) {
buffer.append((char)ch);
read++;
}
midlet.append("HTML Response" ,buffer.toString());
conn.close();
conn = null;
}
catch(Exception e){
e.printStackTrace();
}
}
}
The url parameter of the sendMessage() method has the following form:
http://www.symbian.com:80
The sendMessage() method creates a request and then starts a new Thread to create the connection, send the request and read the response. Let us look at the contents of the thread's run() method in more detail:
HttpConnection conn = (HttpConnection)Connector.open(url);
int responseCode = conn.getResponseCode();
midlet.append("Response code","" + responseCode);
int i = 0;
String headerKey = null;
while((headerKey = conn.getHeaderFieldKey(i++)) != null) {
midlet.append(headerKey,conn.getHeaderField(headerKey));
}
An HttpConnection is
opened using the URI given by the user and is used first to retrieve
the HTTP response code (200, in case of success). We also loop through
the HTTP response methods until they are all read and append them to the
MIDlet's Form object as a StringItem.
Having read the HTTP readers, it is time to read the actual HTML content. We open an InputStream from the HttpConnection, start reading the input and save it to a StringBuffer
of TEXT_FIELD_SIZE length. Once we reach this value in number of bytes
read, we leave the loop, as we don't want to pollute the user's screen
with too much raw HTML.
InputStream in = conn.openInputStream();
StringBuffer buffer = new StringBuffer(TEXT_FIELD_SIZE);
int ch;
int read = 0;
while ( (ch = in.read()) != −1 && read < TEXT_FIELD_SIZE) {
buffer.append((char)ch);
read++;
}
After reading TEXT_FIELD_SIZE bytes of HTML, we append them to the MIDlet's Form so they can be shown on the screen along with all the other information we retrieved. Figure 2 shows the NetworkDemo MIDlet running on a Nokia N95.