Search code examples
javagoogle-app-enginerestlet

Restlet web.xml config for new component


I have set up an App Engine Restlet Project (v 2.2.2) which returns a html or json response (web or android client) and different data from a db for different users. I have implemented HTTP basic authentication. It all works quite well.

my basic setup atm (I have simplified it ofc):

MyApplication.java

public class MyApplication extends Application {

    private ChallengeAuthenticator authenticatior;
    private ChallengeAuthenticator createAuthenticator() {...}
    public boolean authenticate(Request request, Response response) {...}

    @Override
    public Restlet createInboundRoot() {
        this.authenticatior = createAuthenticator();        
        Router router = new Router(getContext());
        router.attachDefault(MyRestlet.class);
        authenticatior.setNext(router);
        return authenticatior;
    }

MyRestlet.java

public class MyRestlet extends ServerResource {

        @Get("json")
        public Representation getJSON() {
            MyApplication app = (MyApplication) getApplication();
                if (!app.authenticate(getRequest(), getResponse())) {
                    // Not authenticated
                    return null;
                }
                else {
                    return data;     
                }

        @Get("html")
        public String getHTML() {...}
}

web.xml

<?xml ...>
<display-name>MyName</display-name>  

<context-param>
<param-name>org.restlet.application</param-name>
<param-value>x.MyApplication</param-value>
</context-param>

<servlet>
<servlet-name>MyRestlet</servlet-name>
<servlet-class>org.restlet.ext.servlet.ServerServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>MyRestlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>

Now I want to add encryption and created keys/certificates. My guide was this tutorial. I wanted to try to add a simple component first, stay with HTTP and change the port to 8183 as done in this tutorial. I have now played around quite a bit and cannot seem to get my component to work. So my question is: Where would I put this main-Method (the following code is taken from the tutorial)? Into which class should I insert it or should I create a seperate server-class and what exactly would the required changes to the web.xml look like (I did not find much concerning this and I suspect that this is the central problem)? Any help is appreciated!

public static void main(String[] args) throws Exception {
    // Create a new Restlet component and add a HTTP server connector to it
    Component component = new Component();
    component.getServers().add(Protocol.HTTP, 8182);

    // Then attach it to the local host
    component.getDefaultHost().attach("/trace", Part05.class);

    // Now, let's start the component!
    // Note that the HTTP server connector is also automatically started.
    component.start();
}

Solution

  • So I was trying to convert a JavaSE RESTLET app to a JavaEE app. What I understood from the "Restlet in Action" book, Pg 72, is that we dont need to attach a server to the component as Servlet engine handles the incoming HTTP and HTTPS requests.

    The tutorial also points to the component file an an INIT-PARAM or param-name in your web.xml. However for me, none of above 2 changes are working.

    If I point to the application class as an INIT-PARAM and not use a HTTP connector as suggested in the book, i am able to access the APIs but not able to make any calls from the client class as I get the below error -

    WARNING: The protocol used by this request is not declared in the list of client connectors. (HTTP/1.1). In case you are using an instance of the Component class, check its "clients" property. Not Found (404) - The server has not found anything matching the request URI

    Hope these info help you in some way


    EDIT

    I found the solution to the above problem - I had to add the below piece of code

            <init-param>
                <param-name>org.restlet.clients</param-name>
                <param-value>HTTP HTTPS FILE</param-value>
            </init-param>
    

    Also for your problem, you need to configure the Component via an XML. Below URLs will give more info -

    http://restlet.com/learn/javadocs/2.1/jee/ext/org/restlet/ext/servlet/ServerServlet.html http://restlet.com/learn/javadocs/2.1/jee/api/org/restlet/Component.html?is-external=true