Package org.restlet

Examples of org.restlet.Component


*/
public class ExampleServer {

    public static void main(String[] args) throws Exception {
        // create Component (as ever for Restlet)
        final Component comp = new Component();
        final Server server = comp.getServers().add(Protocol.HTTP, 80);

        // create JAX-RS runtime environment
        final JaxRsApplication application = new JaxRsApplication(comp
                .getContext().createChildContext());

        // attach ApplicationConfig
        application.add(new ExampleApplication());

        // Attach the application to the component and start it
        comp.getDefaultHost().attach(application);
        comp.start();

        System.out.println("Server started on port " + server.getPort());
        System.out.println("Press key to stop server");
        System.in.read();
        System.out.println("Stopping server");
        comp.stop();
        System.out.println("Server stopped");
    }
View Full Code Here


     *            The optional arguments.
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        // Create a component
        Component component = new Component();
        component.getServers().add(Protocol.HTTP, 8111);
        component.getClients().add(Protocol.FILE);

        // Create an application
        Application application = new Part11();

        // Attach the application to the component and start it
        component.getDefaultHost().attach(application);
        component.start();
    }
View Full Code Here

     *            The optional arguments.
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        // Create a component
        Component component = new Component();
        component.getServers().add(Protocol.HTTP, 8111);

        // Create an application
        Application application = new Part10();

        // Attach the application to the component and start it
        component.getDefaultHost().attachDefault(application);
        component.start();
    }
View Full Code Here

        return "ORIGINAL: " + echoMessage + "\n" + "ECHOCOPY: " + echoCopy
                + "\n";
    }

    public void testRiap() throws Exception {
        final Component comp = new Component();
        final Application localOnly = new Application() {
            @Override
            public Restlet createInboundRoot() {
                return new Restlet(getContext()) {
                    @Override
                    public void handle(Request request, Response response) {
                        final String selfBase = "riap://application";
                        final Reference ref = request.getResourceRef();
                        final String remainder = ref.getRemainingPart();

                        Representation result = new StringRepresentation(
                                DEFAULT_MSG);

                        if (remainder.startsWith("/echo/")) {
                            result = new StringRepresentation(
                                    remainder.substring(6));
                        } else if (remainder.equals("/object")) {
                            result = new ObjectRepresentation<Serializable>(
                                    JUST_SOME_OBJ);
                        } else if (remainder.equals("/null")) {
                            result = new ObjectRepresentation<Serializable>(
                                    (Serializable) null);
                        } else if (remainder.equals("/self-aggregated")) {
                            final String echoMessage = ECHO_TEST_MSG;
                            final Reference echoRef = new LocalReference(
                                    selfBase + "/echo/" + echoMessage);
                            String echoCopy = null;
                            try {
                                ClientResource r = new ClientResource(echoRef);
                                echoCopy = r.get().getText();
                            } catch (Exception e) {
                                e.printStackTrace();
                                fail("Error getting internal reference to "
                                        + echoRef);
                            }
                            assertEquals("expected echoMessage back",
                                    echoMessage, echoCopy);
                            result = new StringRepresentation(buildAggregate(
                                    echoMessage, echoCopy));
                        }
                        response.setEntity(result);
                    }
                };
            }
        };

        comp.getInternalRouter().attach("/local", localOnly);
        String localBase = "riap://component/local";

        Client dispatcher = comp.getContext().getClientDispatcher();

        String msg = "this%20message";
        String echoURI = localBase + "/echo/" + msg;
        Representation echoRep = dispatcher.handle(
                new Request(Method.GET, echoURI)).getEntity();
View Full Code Here

     *            The optional arguments.
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        // Create a component
        Component component = new Component();
        component.getServers().add(Protocol.HTTP, 8111);

        // Create an application
        Application application = new Part12();

        // Attach the application to the component and start it
        component.getDefaultHost().attachDefault(application);
        component.start();
    }
View Full Code Here

* @author Jerome Louvel
*/
public class Part06 {
    public static void main(String[] args) throws Exception {
        // Create a component
        Component component = new Component();
        component.getServers().add(Protocol.HTTP, 8111);
        component.getClients().add(Protocol.FILE);

        // Create an application
        Application application = new Application() {
            @Override
            public Restlet createInboundRoot() {
                return new Directory(getContext(), ROOT_URI);
            }
        };

        // Attach the application to the component and start it
        component.getDefaultHost().attach(application);
        component.start();
    }
View Full Code Here

    /**
     * Test the RIAP client and server connectors.
     */
    public void testRiapConnectors() {
        Component component = new Component();
        component.getServers().add(Protocol.RIAP);
        component.getClients().add(Protocol.RIAP);

        Application app = new Application() {
            @Override
            public Restlet createInboundRoot() {
                Router router = new Router(getContext());
                router.attach("/testA", new Restlet(getContext()) {

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

                });
                router.attach("/testB", new Restlet(getContext()) {
                    public void handle(Request request, Response response) {
                        ClientResource resource = new ClientResource(
                                "riap://component/app/testA");
                        try {
                            response.setEntity(resource.get().getText(),
                                    MediaType.TEXT_PLAIN);
                        } catch (Exception e) {
                        }
                    }

                });
                return router;
            }
        };

        // Attach the private application
        component.getInternalRouter().attach("/app", app);

        try {
            component.start();

            ClientResource res = new ClientResource(
                    "riap://component/app/testA");
            Representation rep = res.get();
            assertEquals("hello, world", rep.getText());

            rep = null;
            res = new ClientResource("riap://component/app/testB");
            rep = res.get();
            assertEquals("hello, world", rep.getText());

            component.stop();
        } catch (Exception e) {
            fail(e.getMessage());
        }
    }
View Full Code Here

     *            The optional arguments.
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        // Create a component
        Component component = new Component();
        component.getServers().add(Protocol.HTTP, 8111);
        component.getClients().add(Protocol.FILE);

        // Create an application
        Application application = new Part09a();

        // Attach the application to the component and start it
        component.getDefaultHost().attachDefault(application);
        component.start();
    }
View Full Code Here

            fw = new FileWriter(testFileVl2);
            fw.write("Method=${m}/Path=${rp}");
            fw.close();

            // Create a new component
            Component component = new Component();
            component.getServers().add(Protocol.HTTP, TEST_PORT);
            component.getClients().add(Protocol.FILE);

            // Create an application filtered with FreeMarker
            MyFreemakerApplication freemarkerApplication = new MyFreemakerApplication(
                    this.testDir);
            // Create an application filtered with Velocity
            MyVelocityApplication velocityApplication = new MyVelocityApplication(
                    this.testDir);

            // Attach the applications to the component and start it
            component.getDefaultHost().attach("/freemarker",
                    freemarkerApplication);
            component.getDefaultHost().attach("/velocity", velocityApplication);

            // Now, let's start the component!
            component.start();

            // Allow extensions tunneling
            freemarkerApplication.getTunnelService().setExtensionsTunnel(true);
            velocityApplication.getTunnelService().setExtensionsTunnel(true);
            Client client = new Client(Protocol.HTTP);
            Response response = client.handle(new Request(Method.GET,
                    "http://localhost:" + TEST_PORT + "/freemarker/"
                            + testFileFm1.getName()));

            if (response.isEntityAvailable()) {
                assertEquals("Method=GET/Authority=localhost:" + TEST_PORT,
                        response.getEntity().getText());
            }

            response = client.handle(new Request(Method.GET,
                    "http://localhost:" + TEST_PORT + "/freemarker/"
                            + testFileFm2.getName()));
            assertTrue(response.getStatus().isSuccess());

            if (response.isEntityAvailable()) {
                assertEquals("Method=${m}/Authority=${ra}", response
                        .getEntity().getText());
            }

            response = client.handle(new Request(Method.GET,
                    "http://localhost:" + TEST_PORT + "/velocity/"
                            + testFileVl1.getName()));

            if (response.isEntityAvailable()) {
                assertEquals("Method=GET/Path=/velocity/testVl1", response
                        .getEntity().getText());
            }

            response = client.handle(new Request(Method.GET,
                    "http://localhost:" + TEST_PORT + "/velocity/"
                            + testFileVl2.getName()));
            assertTrue(response.getStatus().isSuccess());

            if (response.isEntityAvailable()) {
                assertEquals("Method=${m}/Path=${rp}", response.getEntity()
                        .getText());
            }

            // Now, let's stop the component!
            component.stop();
            client.stop();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
View Full Code Here

        // Create a temporary directory for the tests
        this.testDir = new File(System.getProperty("java.io.tmpdir"),
                "DirectoryTestCase/tests1" + new Date().getTime());

        // Create a new Restlet component
        final Component clientComponent = new Component();
        clientComponent.getClients().add(Protocol.FILE);

        // Create an application
        final MyApplication application = new MyApplication(this.testDir);
        // Attach the application to the component and start it
        clientComponent.getDefaultHost().attach("", application);

        // Now, let's start the component!
        clientComponent.start();

        // Allow extensions tunneling
        application.getTunnelService().setExtensionsTunnel(true);
        BioUtils.delete(this.testDir, true);
        this.testDir = new File(System.getProperty("java.io.tmpdir"),
                "DirectoryTestCase/tests2" + new Date().getTime());
        this.testDir.mkdirs();
        application.setTestDirectory(testDir);

        // Test the directory Restlet with an index name
        testDirectory(application, application.getDirectory(), "index");
        BioUtils.delete(this.testDir, true);
        this.testDir = new File(System.getProperty("java.io.tmpdir"),
                "DirectoryTestCase/tests3" + new Date().getTime());
        this.testDir.mkdirs();
        application.setTestDirectory(testDir);

        // Test the directory Restlet with no index name
        testDirectory(application, application.getDirectory(), "");

        // Avoid extensions tunneling
        application.getTunnelService().setExtensionsTunnel(false);
        BioUtils.delete(this.testDir, true);
        this.testDir = new File(System.getProperty("java.io.tmpdir"),
                "DirectoryTestCase/tests4" + new Date().getTime());
        this.testDir.mkdirs();
        application.setTestDirectory(testDir);

        // Test the directory Restlet with an index name
        testDirectory(application, application.getDirectory(), "index");
        BioUtils.delete(this.testDir, true);
        this.testDir = new File(System.getProperty("java.io.tmpdir"),
                "DirectoryTestCase/tests5" + new Date().getTime());
        this.testDir.mkdirs();
        application.setTestDirectory(testDir);

        // Test the directory Restlet with no index name
        testDirectory(application, application.getDirectory(), "");
        BioUtils.delete(this.testDir, true);
        this.testDir = new File(System.getProperty("java.io.tmpdir"),
                "DirectoryTestCase/tests6" + new Date().getTime());
        this.testDir.mkdirs();
        application.setTestDirectory(testDir);

        // Test the access to the sub directories.
        testDirectoryDeeplyAccessible(application, application.getDirectory());

        // Now, let's stop the component!
        clientComponent.stop();
    }
View Full Code Here

TOP

Related Classes of org.restlet.Component

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.