|
|
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 4 of 7
The DictionaryRestlet, shown in Listing 4, is responsible for handling requests for manipulating the /dictionary information space.
DictionaryRestletpackage net.bosatsu.restlet.spell;
import org.restlet.Restlet;
import org.restlet.data.*;
import com.swabunga.spell.event.SpellChecker;
public class DictionaryRestlet extends Restlet {
private SpellChecker spellChecker;
public DictionaryRestlet(SpellChecker spellChecker) {
this.spellChecker = spellChecker;
}
@Override
public void handle(Request request, Response response) {
String word = (String) request.getAttributes().get("word");
if(request.getMethod().equals(Method.PUT)) {
spellChecker.addToDictionary(word);
response.setStatus(Status.SUCCESS_NO_CONTENT);
} if(request.getMethod().equals(Method.GET)) {
// Left as an exercise. Consider fetching the word definition
// from http://dictionary.reference.com/browse/<word> or redirecting
// to it.
if(spellChecker.isCorrect(word)) {
response.setEntity("Not yet implemented", MediaType.TEXT_PLAIN);
} else {
response.setEntity("Word not found: " + word, MediaType.TEXT_PLAIN);
response.setStatus(Status.CLIENT_ERROR_NOT_FOUND);
}
} else {
response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
}
}
}
At present, the DictionaryRestlet handles only PUT and GET requests, but you can imagine issuing DELETE requests to remove words from the dictionary if you wanted to. If a word is successfully added to the dictionary, the client
is told that things are happy but there is nothing else to say by using a Status.SUCCESS_NO_CONTENT response. Doing something meaningful with GET is left as an exercise for the reader, although Listing 4 contains a hint. It is important to take advantage of the Status response code to indicate when something succeeds, fails, the request is malformed, the client is not authorized, and so
on.
The /spellchecker information space works a little differently from its /dictionary friend. On success, what should be returned? If the client has just passed in a properly spelled word, do you really need
to send it back? The SpellCheckerRestlet, shown in Listing 5, reuses Status.SUCCESS_NO_CONTENT, meaning the request was valid and successful, but there is really nothing to say about it.
package net.bosatsu.restlet.spell;
import org.restlet.Restlet;
import org.restlet.resource.Representation;
import org.restlet.resource.StringRepresentation;
import org.restlet.data.*;
import com.swabunga.spell.event.SpellChecker;
import com.swabunga.spell.engine.Word;
import java.util.StringTokenizer;
import java.util.List;
import java.util.Iterator;
public class SpellCheckerRestlet extends Restlet {
private SpellChecker spellChecker;
public SpellCheckerRestlet(SpellChecker spellChecker) {
this.spellChecker = spellChecker;
}
@Override
public void handle(Request request, Response response) {
String word = (String) request.getAttributes().get("word");
if(request.getMethod().equals(Method.GET)) {
if(spellChecker.isCorrect(word)) {
response.setStatus(Status.SUCCESS_NO_CONTENT);
} else {
String resp = null;
StringBuffer sb = new StringBuffer();
List l = spellChecker.getSuggestions(word, 5);
Iterator itor = l.iterator();
while(itor.hasNext()) {
Word w = (Word) itor.next();
// Obviously hard-coding the URL like this is bad. Use the API to discover
// the actual HTTP context and build off of that.
sb.append("http://localhost:8182/dictionary/");
sb.append(w.getWord());
sb.append("\n");
}
resp = sb.toString();
response.setEntity(resp, MediaType.TEXT_PLAIN);
}
} else {
response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
}
}
}
If an incorrectly spelled word is passed in, you do not want to return "Not Found" as was done with the /dictionary information space. You want instead to return a list of suggestions. Because, as Roy Fielding says, "hypermedia is the engine
of application state," you want to return links back to the actual information resources. This way, clients can iterate over
the results and discover new word information resources to resolve if they choose to do so. In this way, REST is not just
about data, but also about discovering links to other resources.