Monday, 10 November 2008

[Restlet with Spring DM]

How you can create restlet with Spring DM

1. need update 6 files
- web.xml
- applicationContext-osgi.xml
- applicationContext.xml
- MANIFEST.MF
- TestApplication.java
- TestDefaultResource.java

2. main point

use "org.restlet.component" as context-param

in web.xml add below line


3.
[context-param]
[param-name]org.restlet.component[/param-name]
[param-value]component[/param-value]
[/context-param]

4. "component is from "applicationContext.xml" file

[bean id="component" class="org.restlet.ext.spring.SpringComponent" ]
[property name="defaultTarget" ref="application" /]
[/bean]

5. "application" is from same file "applicationContext.xml" file

[bean id="application" class="applicationPackage.TestApplication"][/bean]

6. TestApplication.java
import TestDefaultResouce;
import org.restlet.Application;
import org.restlet.Context;
import org.restlet.Restlet;
import org.restlet.Router;

public class EamApplication extends Application {

public EamApplication() {
super();
// TODO Auto-generated constructor stub
}

public EamApplication(Context context) {
super(context);
}

@Override
public Restlet createRoot() {
Router router = new Router(getContext());
router.attachDefault(TestDefaultResouce.class);
return router;
}
}

7. TestDefaultResouce.java

import org.restlet.Context;
import org.restlet.data.MediaType;
import org.restlet.data.Request;
import org.restlet.data.Response;
import org.restlet.resource.Representation;
import org.restlet.resource.Resource;
import org.restlet.resource.StringRepresentation;
import org.restlet.resource.Variant;

public class EamDefaultResouce extends Resource {

public EamDefaultResouce() {
super();
// TODO Auto-generated constructor stub
}

public EamDefaultResouce(Context context, Request request, Response response) {
super(context, request, response);
getVariants().add(new Variant(MediaType.TEXT_PLAIN));
}

@Override
public Representation getRepresentation(Variant variant){
Representation representation = new StringRepresentation("index page", MediaType.TEXT_PLAIN);
return representation;
}
}

No comments: