Hi  Zsolt,

I send you a sample application that illustrates the scenario.

Best regards,
Thierry Boileau
> Hello
>
> Is it possible to authenticate a request via the RIAP protocol? One of
> my application is guarded by BASIC authentication, and I'd like to use
> it from another restlet application. Both application are in the same
> component in servlet environment.
>
> Any documentation, code snippet would help.
>
> Thanks
>
> Zsolt
>
> ------------------------------------------------------
> http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2426345
>
>

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2428826
package riap;

import org.restlet.Application;
import org.restlet.Component;
import org.restlet.Request;
import org.restlet.Response;
import org.restlet.Restlet;
import org.restlet.data.ChallengeScheme;
import org.restlet.data.LocalReference;
import org.restlet.data.MediaType;
import org.restlet.data.Protocol;
import org.restlet.resource.ClientResource;
import org.restlet.routing.Router;
import org.restlet.security.ChallengeAuthenticator;
import org.restlet.security.MapVerifier;

public class Test {

    public static void main(String[] args) throws Exception {
        Component c = new Component();
        c.getServers().add(Protocol.HTTP, 8182);
        c.getDefaultHost().attach("/a2", new A2());
        c.getInternalRouter().attach("/a1", new A1());
        c.getInternalRouter().attach("/a2", new A2());
        c.start();
        ClientResource r = new ClientResource(
                "http://localhost:8182/a2/restlet";);
        r.get().write(System.out);
        c.stop();
    }

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

            Restlet hello = new Restlet(getContext()) {
                @Override
                public void handle(Request request, Response response) {
                    response.setEntity("hello, world", MediaType.TEXT_PLAIN);
                }
            };

            Restlet restlet = new Restlet(getContext()) {
                @Override
                public void handle(Request request, Response response) {
                    try {
                        ClientResource r = new ClientResource(LocalReference
                                .createRiapReference(
                                        LocalReference.RIAP_APPLICATION,
                                        "/hello"));
                        r.get().write(System.out);
                        System.out.println(" - called from a1");
                    } catch (Exception e) {
                        System.out.println("Error when requesting /hello");
                    }

                    response.setEntity("hello, world from A1: "
                            + request.getResourceRef().getScheme(),
                            MediaType.TEXT_PLAIN);
                }
            };

            router.attach("/hello", hello);
            router.attach("/restlet", restlet);

            ChallengeAuthenticator guard = new ChallengeAuthenticator(
                    getContext(), ChallengeScheme.HTTP_BASIC, "realm");
            MapVerifier verifier = new MapVerifier();
            verifier.getLocalSecrets().put("scott", "tiger".toCharArray());
            guard.setVerifier(verifier);
            guard.setNext(router);

            return guard;
        }
    }

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

            Restlet hello = new Restlet(getContext()) {
                @Override
                public void handle(Request request, Response response) {
                    response.setEntity("hello, world", MediaType.TEXT_PLAIN);
                }
            };

            Restlet restlet = new Restlet(getContext()) {
                @Override
                public void handle(Request request, Response response) {
                    try {
                        ClientResource r = new ClientResource(LocalReference
                                .createRiapReference(
                                        LocalReference.RIAP_COMPONENT,
                                        "/a1/restlet"));
                        r.setChallengeResponse(ChallengeScheme.HTTP_BASIC,
                                "scott", "tiger");
                        r.get().write(System.out);
                        System.out.println(" - called from a2");
                    } catch (Exception e) {
                        System.out.println("Error when requesting /a1");
                    }

                    ClientResource r2 = new ClientResource(LocalReference
                            .createRiapReference(
                                    LocalReference.RIAP_APPLICATION, "/hello"));
                    try {
                        r2.get().write(System.out);
                        System.out.println(" - called from a2");
                    } catch (Exception e) {
                        System.out
                                .println("Error when requesting /hello from A2");
                    }

                    response.setEntity("hello, world from A2: "
                            + request.getResourceRef().getScheme(),
                            MediaType.TEXT_PLAIN);
                }
            };

            router.attach("/hello", hello);
            router.attach("/restlet", restlet);
            return router;
        }
    }
}

Reply via email to