432640 (1) [Avatar] Offline
#1
I loved the book by the way.

Sorry i cant get the following basic example to work with post get is ok, restlet 2.3.12 java 1.7
firefox or ie

this example simply displays a page via get which prompts for passwd and validates passwd ok.
but with post/put it prompts for passwd but does not validate passwd. gives 401 unauth

the example consists of 3 files.
namespace app
4 libs required
org.freemarker.jar
org.restlet.exr.crtypo.jar
org.restlet.ext.freemarker.jar
org.restlet.jar

MyApp.java
package app;



import java.util.logging.Level;

import org.restlet.Application;
import org.restlet.Component;
import org.restlet.Restlet;

import org.restlet.data.Protocol;
import org.restlet.engine.Engine;
import org.restlet.ext.crypto.DigestAuthenticator;

import org.restlet.routing.Router;
import org.restlet.security.MapVerifier;




public class MyApp extends Application {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

Component c = new Component();
c.getClients().add(Protocol.CLAP);
c.getClients().add(Protocol.HTTP);
c.getServers().add(Protocol.HTTP, 8111);
c.getServers().add(Protocol.CLAP, 8111);
c.getDefaultHost().attach(new MyApp());
try {
c.start();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public MyApp() {
Engine.setLogLevel(Level.ALL);
Engine.setRestletLogLevel(Level.FINEST);
}
@Override
public Restlet createInboundRoot() {
Router router = new Router(getContext());

router.attach("/", SimpleResource.class);
router.attach("/pig", SimpleResource.class);
DigestAuthenticator authenticator = new DigestAuthenticator ( getContext(),"My Realm","My Server Key");
MapVerifier verifier = new MapVerifier();
verifier.getLocalSecrets().put("me", "none".toCharArray());
authenticator.setWrappedVerifier(verifier);
authenticator.setNext(router);
return authenticator;
}
}


simple.ftl

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title> Example </title>
</head>
<body>

<form action="?method=PUT" method="POST" >
<input type="hidden" name="csrf-token" value="nc98P987bcpncYhoadjoiydc9ajDlcn">
<input type="submit" value="Update/Save item to database"></td>

</form>

</body>
</html>



SimpleResource.java
package app;
import java.util.HashMap;
import java.util.Map;

import org.restlet.data.LocalReference;
import org.restlet.data.MediaType;
import org.restlet.ext.freemarker.TemplateRepresentation;
import org.restlet.representation.Representation;
import org.restlet.representation.Variant;
import org.restlet.resource.ClientResource;
import org.restlet.resource.Get;
import org.restlet.resource.Put;
import org.restlet.resource.ResourceException;
import org.restlet.resource.ServerResource;


public class SimpleResource extends ServerResource{
@Override
protected void doInit() throws ResourceException {
// Declares the two variants supported
getVariants().add(new Variant(MediaType.TEXT_HTML));
// getVariants().add(new Variant(MediaType.APPLICATION_XML));
// getVariants().add(new Variant(MediaType.APPLICATION_JSON));
}



//@Override
@Get
public Representation get(Variant variant) throws ResourceException {
Representation result = null;
if( MediaType.TEXT_HTML.isCompatible(variant.getMediaType())) {
System.out.println(getClass().getPackage());
Representation resFtl =


new ClientResource ( LocalReference.createClapReference(getClass().getPackage()) + "/simple.ftl").get();

boolean pig = false;
Map<String, Object> dataModel = new HashMap<String,Object>();
result = new TemplateRepresentation( resFtl,dataModel , MediaType.TEXT_HTML);

}
return result;
}
//@Override
@Put("html")
public Representation put(Representation input) {
Representation result = null;
if( MediaType.TEXT_HTML.isCompatible(input.getMediaType())) {
Representation resFtl =

new ClientResource ( LocalReference.createClapReference(getClass().getPackage()) + "/simple.ftl").get();

boolean pig = false;
Map<String, Object> dataModel = new HashMap<String,Object>();
result = new TemplateRepresentation( resFtl,dataModel , MediaType.TEXT_HTML);

}
return result;
}
}