dfischer.femtowebserver.test
Class BasicAuthGenericTestWeblet
java.lang.Object
|
+--dfischer.femtowebserver.test.BasicAuthGenericTestWeblet
- All Implemented Interfaces:
- BasicAuthGenericTestInterface, HttpdWebletInterface
- Direct Known Subclasses:
- BasicAuthTestWeblet
- public abstract class BasicAuthGenericTestWeblet
- extends java.lang.Object
- implements HttpdWebletInterface, BasicAuthGenericTestInterface
This example of an abstract Weblet super class checks the username/password access for a protected page.
After the authorization has been successful, the real protected Weblet will be called by the
BasicAuthGenericTestInterface.
Sourcecode
package dfischer.femtowebserver.test;
import dfischer.femtowebserver.httpd.*;
public abstract class BasicAuthGenericTestWeblet implements HttpdWebletInterface, BasicAuthGenericTestInterface
{
private final static String USERNAME = "femto";
private final static String PASSWORD = "access";
public void execute(HttpRequest req, HttpResponse res, HttpdProperties httpdProperties) throws Exception
{
boolean authorized = false;
String username = null;
String password = null;
if (req.hasBasicAuthorization())
{
username = req.getBasicAuthorizationUsername();
password = req.getBasicAuthorizationPassword();
if ((username != null) && (password != null))
authorized = ((username.compareTo(USERNAME) == 0) && (password.compareTo(PASSWORD) == 0));
}
// unauthorized
if (!authorized)
{
res.setAdditionalHeaderField("WWW-Authenticate", "Basic realm=\"Protected Access to " + this.getClass().getName() + "\"");
res.setStatus(401, "Unauthorized");
return;
}
// all ok: authorized - call real instance with BasicAuthGenericTestInterface and with valid username
execute(req, res, httpdProperties, username);
}
}
- See Also:
BasicAuthGenericTestInterface,
BasicAuthTestWeblet
| Methods inherited from class java.lang.Object |
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
BasicAuthGenericTestWeblet
public BasicAuthGenericTestWeblet()
execute
public void execute(HttpRequest req,
HttpResponse res,
HttpdProperties httpdProperties)
throws java.lang.Exception
- Description copied from interface:
HttpdWebletInterface
- Executes the Weblet.
- Specified by:
execute in interface HttpdWebletInterface
- Following copied from interface:
dfischer.femtowebserver.httpd.HttpdWebletInterface
- Parameters:
httpRequest - access to the HTTP request data (browser request)httpResponse - Weblet HTTP response data (server response)httpdProperties - read/only access to the Femto Web Server configuration