Hi Jerome,
So here are my findings...
1. I had erroneously used "id" instead of "name" for form input
elements that is why things didn't turn up as expected.
2. A form with enctype="multipart/form-data" isn't parsed properly.
Attached restlet shows it. Everything works fine if you remove the
enctype.
3. Reference.getQueryAsForm() declares throwing an IOException while
call.getInputAsForm() doesn't and I'd expect the other way around.
4. This way of handling login requires a lot of redirects (okay 2 to
be more specific). So I use code like
RedirectRestlet redirect = new RedirectRestlet(this.getContext(),
redirectReference, RedirectRestlet.MODE_CLIENT_FOUND);
try {
redirect.start();
} catch (Exception ex) {
//log it!
}
redirect.handle(call);
try {
redirect.stop();
} catch (Exception ex) {
//log it!
}
In my authentication filter I use
RedirectRestlet.MODE_CLIENT_TEMPORARY to redirect to the login restlet
and in the login restlet I use RedirectRestlet.MODE_CLIENT_FOUND to
reach the expected reference. I hope that is fine.
Can't that exception (Exception ex) be a little more specific as it
also encompasses all runtime exceptions. Introduce a checked
RestletException?
5. What about session support? I had to create my own.
Cheers
Piyush
import com.noelios.restlet.data.StringRepresentation;
import java.io.IOException;
import java.util.Map;
import java.util.logging.Logger;
import org.restlet.Call;
import org.restlet.data.Method;
import java.util.List;
import org.restlet.Restlet;
import org.restlet.data.Form;
import org.restlet.data.MediaType;
import org.restlet.data.Parameter;
import org.restlet.data.Representation;
import org.restlet.data.Status;
public class LoginDebugRestlet extends Restlet {
private static final Logger logger = Logger.getLogger(LoginRestlet.class.getName());
/** Creates a new instance of LoginDebugRestlet */
public LoginDebugRestlet() {
}
public void handleGet(Call call) {
String html = String.format(HTML, call.getResourceRef().toString());
call.setOutput(new StringRepresentation(html, MediaType.TEXT_HTML));
}
protected void handlePost(Call call) {
Form inputForm = call.getInputAsForm();
StringBuilder output = new StringBuilder(256);
output.append(TABLE_BEGIN);
for(Parameter param: inputForm) {
output.append(String.format(TABLE_ROW, param.getName(), param.getValue()));
}
output.append(TABLE_END);
call.setOutput(new StringRepresentation(output.toString(), MediaType.TEXT_HTML));
}
public static final String TABLE_BEGIN = "<table><tr><td>Key</td><td>Value</td></tr>";
public static final String TABLE_ROW = "<tr><td>%s</td><td>%s</td></tr>";
public static final String TABLE_END = "</table>";
public static final String HTML = "<html><head><title>login form</title></head><body><form action=\"%s\" enctype=\"multipart/form-data\" method=\"post\" >" +
"<p><label for=\"username\">user name: </label><input type=\"text\" name=\"username\" id=\"username\"/><br/>" +
"<label for=\"password\">password : </label><input type=\"password\" name=\"password\" id=\"password\"/><br/>" +
"<input type=\"submit\" value=\"Send\"/></p></form></body></html>";
}