0

I am completely newbie for java & restlet. I am using eclipse LUNA Java SE. I try to use html/css/xml/bootstrap etc. in my web application. I searched around and all examples which I can find is based on Java EE. so I am wondering that I should use Java EE instead of Java SE if I want to use html(files)/css/xml/bootstrap/json etc.. to contain more rich contexts in my web service. I did read Restlet user guide/tutorials in their homepage, even go through the book, Restlet in Action. but I can't find any answers myself. Maybe there is answers in those materials but I haven't figured it out. so, In java SE restlet, for now, I am doing hardcoding html in my java code... but that is not enough.

This is probably very basic to the others. but please understand, this also can be confused to the beginners. Examples will be very helpful. thx.

user1915570
  • 386
  • 2
  • 7
  • 27

1 Answers1

2

Don't worry! There is no silly question ;-)

Sure, you can use Restlet with only JavaSE for the server side. You can define a Restlet server within a standalone Java application. This can be done using the class Component. After having instantiated it, you can add a server to it. Following code describes this:

public static void main(String[] args) {
    Component component = new Component();
    component.getServers().add(Protocol.HTTP, 8080);
    (...)
    component.start();
}

Before starting the component, you can attach your Restlet application on it, as described below:

component.getDefaultHost().attach(new SampleApplication());

A Restlet application corresponds to à class that extends the class Application. The next step consists in overriding the method createInboundRoot. This method defines the REST routes of your application. Following code describes the skelekon of a Restlet application:

public class SampleApplication extends Application {
    @Override
    public Restlet createInboundRoot() {
        Router router = new Router(getContext());

        router.attach("/myresource", MyServerResource.class);

        (...)

        return router;
    }
}

Since you want to serve static files (JS, CSS, ...), you can leverage the class Directory. It allows to attach automatically and recursively all folders and files within a root folder to a path. This can be configured as described below:

String rootUri = "file:///(...)/static-content";
Directory directory = new Directory(getContext(), rootUri);
directory.setListingAllowed(true);
router.attach("/static/", directory);

Let's focus now on the server resources. We can distinguish two kinds for your case:

  • the ones that exchange structured data and use POJOs wwith their annotation methods (Get, Post, ...)
  • the ones that manage representations directly or based on Restlet extensions

For the first ones, you can refer this answer on stackoverflow.

The second ones allow to use template engines to generate content to send back to the client. This is something similar to JSPs in JavaEE. We can take the sample of Freemarker (http://freemarker.org/) and its corresponding Restlet extension.

First we need to configure the Freemarker engine within the Restlet application, mainly the root directory where to find out the templates, as described below:

public class SampleApplication extends Application {
    (...)
    private Configuration configuration;

    public static Configuration configureFreeMarker(Context context) {
        Configuration configuration = new Configuration();
        ClassTemplateLoader loader = new ClassTemplateLoader(
            SampleAppApplication.class,
            "/org/myapp/sample/server/templates/");
        configuration.setTemplateLoader(loader);
        // configuration.setCacheStorage(new StrongCacheStorage());
        return configuration;
    }

    public Configuration getConfiguration() {
        return configuration;
    }
}

You can then create Freemarker representations to leverage such templates to generate the output content (dynamic content generated on the server side), as described below:

private SampleApplication getSampleApplication() {
    return (SampleApplication)getApplication();
}

private Representation toRepresentation(Map<String, Object> map,
    String templateName, MediaType mediaType) {
    return new TemplateRepresentation(templateName,
        getSampleApplication().getConfiguration(), map, mediaType);
}

@Get("html")
public Representation getHtml() {
    Map<String, Object> model = new HashMap<String, Object>();

    model.put("titre", "my title");
    model.put("users", getUsers());

    return toRepresentation(model,
        "myTemplate", MediaType.TEXT_HTML);
}

Here is the content of the template myTemplate.

<html>
    <head>
        <title>${title}</title>
    </head>
    <body>
    Users:<br/><br/>
    <ul>
        <#list users as user>
        <li>${user.lastName} ${user.firstName}</li>
        </#list>
    </ul>
    </body>
</html>

Hope it helps. Thierry

Community
  • 1
  • 1
Thierry Templier
  • 198,364
  • 44
  • 396
  • 360
  • Thanks a lot. This answer is really well structured. I needed the structure. I have a few questions more. I don't know I can write here again.. – user1915570 Feb 09 '15 at 10:42
  • You're welcome! If your questions are related to this one, I think that we can go on here within comments ;-) If not, feel free to post a new question on stackoverflow and I'll answer you... – Thierry Templier Feb 09 '15 at 12:09
  • thanks again. I haven't tried freemarker yet. There is the sentence in your explanation "This i's something similar to JSPs in JavaEE", If freemarker helps to work similar to JSPs in JavaEE. Then why not using JSPs/JavaEE. Exactly what is the benefit of using JavaSE over JavaEE. because it looks like using JavaSE needs extra effort to work similar to JavaEE. such as using extra tools, extensions like freemarker. when I looked restlet user guide in [restlet homepage] (http://restlet.com/technical-resources/restlet-framework/guide/2.3/editions/jee/overview). they explained like this: – user1915570 Feb 09 '15 at 12:21
  • **- Restlet edition for JavaEE**: _"This edition is aimed for development and deployment of Restlet applications inside Java EE application server, or more precisely inside Servlet containers such as Apache Tomcat."_ **- Restlet edition for JavaSE**: _"This edition is aimed for development and deployment of Restlet applications inside a regular Java virtual machine using the internal HTTP server of the Restlet Engine, or a pluggable one such as Jetty."_ – user1915570 Feb 09 '15 at 12:25
  • There is another answer about JavaSE vs. JavaEE (http://stackoverflow.com/questions/1065240/whats-the-main-difference-between-java-se-and-java-ee) "Java SE stands for Java standard edition and is normally for developing desktop applications, forms the core/base API. Java EE stands for Java enterprise edition for applications which run on servers, for example web sites – user1915570 Feb 09 '15 at 12:33
  • [Q1] then, it seems that should use JavaEE for web services implementation? If there is a reason to use JavaSE over JavaEE for web services, is it because it is more lightweight to deploy? [Q2] All these are because I am implementing web services with several pages using bootstrap nav tabs.. e.g. I have 'router.attach("/testmain", testmainresource.class) , router.attach("/testmain/pageone", pageoneresource.class), router.attach("/testmain/pagetwo", pagetworesource.class)'.. all the resource classes are mapping to each nav tab and i have html files for each page.. – user1915570 Feb 09 '15 at 12:39
  • so I need to add each html files in the java resource code e.g. bootstrap nav html code to testmainresource.class... then instead of file reading, it seems there are ways to use xml (e.g. web.xml - but I am not sure this is the right one, - or I can create one to add all html parts in one xml file), [Q3] then seems I need /WEB-INF/ => this seems only available in JavaEE, maybe not???.. that's why I have started all these questions. – user1915570 Feb 09 '15 at 12:45
  • this may be because I don't know well about xml, jsp, json,etc either. so things are very confusing. but main trouble I have, how I can represent html part in a page without hardcoding inside java code and how to bind all the pages I have with bootstrap nav.. – user1915570 Feb 09 '15 at 12:47
  • [A1] If I correctly understand you wonder which of Restlet you should use. The main thing to consider is the way you want to host your application. Will it be deployed in a app server using a WAR or will you launch the application by directly running a Restlet component. Typically with a JavaEE edition, the Restlet engine is embedded within a servlet web app. The servlet container consists in an adapter for the Restlet application. The initial request is served by the app server and then delegates to the Restlet application. – Thierry Templier Feb 09 '15 at 14:29
  • With a JavaSE, this mechanism is inverted. I mean that the Restlet engine is responsible to manage the server connector (for example, an embedded Jetty). There is no deployment of your application in this case. In both cases, your Restlet application remains the same. – Thierry Templier Feb 09 '15 at 14:29
  • [A2] If you only want to static content that interacts with the server side for data, we can put everything under your "static" folder. If you want to handle dynamic content on the server side, so yes, you need to define resources for each template (if it's what you want to use to generate HTML content). You can leverage Java / OOP to organize / modularize your server code. – Thierry Templier Feb 09 '15 at 14:33
  • [A3] It depends on what you want to do with WEB-INF and web.xml. They are mainly used to define routing the servlet application and you can also hide files from outside within the folder. Can you tell more about this? – Thierry Templier Feb 09 '15 at 14:36
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/70577/discussion-between-thierry-templier-and-user1915570). – Thierry Templier Feb 09 '15 at 14:36
  • Thanks again for the good answers again. it is really helpful. I continued my questions in the chat room. – user1915570 Feb 09 '15 at 17:07