Search in sources :

Example 1 with Resource

use of org.qi4j.library.rest.common.Resource in project qi4j-sdk by Qi4j.

the class ContextResourceClientFactoryTest method testQueryListAndCommand.

@Test
public void testQueryListAndCommand() {
    // START SNIPPET: query-list-and-command
    crc.onResource(new ResultHandler<Resource>() {

        @Override
        public HandlerCommand handleResult(Resource result, ContextResourceClient client) {
            return query("commandwithvalue");
        }
    }).onQuery("commandwithvalue", new ResultHandler<Links>() {

        @Override
        public HandlerCommand handleResult(Links result, ContextResourceClient client) {
            Link link = LinksUtil.withId("right", result);
            return command(link);
        }
    }).onCommand("commandwithvalue", new ResponseHandler() {

        @Override
        public HandlerCommand handleResponse(Response response, ContextResourceClient client) {
            System.out.println("Done");
            return null;
        }
    });
    crc.start();
// END SNIPPET: query-list-and-command
}
Also used : HandlerCommand(org.qi4j.library.rest.client.api.HandlerCommand) Response(org.restlet.Response) ResponseHandler(org.qi4j.library.rest.client.spi.ResponseHandler) SubResource(org.qi4j.library.rest.server.api.SubResource) ContextResource(org.qi4j.library.rest.server.api.ContextResource) Resource(org.qi4j.library.rest.common.Resource) Links(org.qi4j.library.rest.common.link.Links) ResultHandler(org.qi4j.library.rest.client.spi.ResultHandler) ContextResourceClient(org.qi4j.library.rest.client.api.ContextResourceClient) Link(org.qi4j.library.rest.common.link.Link) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Example 2 with Resource

use of org.qi4j.library.rest.common.Resource in project qi4j-sdk by Qi4j.

the class ContinuousIntegrationTest method testRunBuildProgressive.

@Test
public void testRunBuildProgressive() {
    // START SNIPPET: query-list-and-command-progressive
    crc.onResource(new ResultHandler<Resource>() {

        @Override
        public HandlerCommand handleResult(Resource result, ContextResourceClient client) {
            return query("runbuild").onSuccess(new ResultHandler<Links>() {

                @Override
                public HandlerCommand handleResult(Links result, ContextResourceClient client) {
                    Link link = LinksUtil.withId("any", result);
                    return command(link).onSuccess(new ResponseHandler() {

                        @Override
                        public HandlerCommand handleResponse(Response response, ContextResourceClient client) {
                            System.out.println("Done");
                            return null;
                        }
                    });
                }
            });
        }
    });
    crc.start();
// END SNIPPET: query-list-and-command-progressive
}
Also used : HandlerCommand(org.qi4j.library.rest.client.api.HandlerCommand) Response(org.restlet.Response) ResponseHandler(org.qi4j.library.rest.client.spi.ResponseHandler) ContextResource(org.qi4j.library.rest.server.api.ContextResource) Resource(org.qi4j.library.rest.common.Resource) Links(org.qi4j.library.rest.common.link.Links) ResultHandler(org.qi4j.library.rest.client.spi.ResultHandler) ContextResourceClient(org.qi4j.library.rest.client.api.ContextResourceClient) Link(org.qi4j.library.rest.common.link.Link) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Example 3 with Resource

use of org.qi4j.library.rest.common.Resource in project qi4j-sdk by Qi4j.

the class ContextResourceClientFactoryTest method testQueryListAndCommandProgressive.

@Test
public void testQueryListAndCommandProgressive() {
    // START SNIPPET: query-list-and-command-progressive
    crc.onResource(new ResultHandler<Resource>() {

        @Override
        public HandlerCommand handleResult(Resource result, ContextResourceClient client) {
            return query("commandwithvalue").onSuccess(new ResultHandler<Links>() {

                @Override
                public HandlerCommand handleResult(Links result, ContextResourceClient client) {
                    Link link = LinksUtil.withId("right", result);
                    return command(link).onSuccess(new ResponseHandler() {

                        @Override
                        public HandlerCommand handleResponse(Response response, ContextResourceClient client) {
                            System.out.println("Done");
                            return null;
                        }
                    });
                }
            });
        }
    });
    crc.start();
// END SNIPPET: query-list-and-command-progressive
}
Also used : HandlerCommand(org.qi4j.library.rest.client.api.HandlerCommand) Response(org.restlet.Response) ResponseHandler(org.qi4j.library.rest.client.spi.ResponseHandler) SubResource(org.qi4j.library.rest.server.api.SubResource) ContextResource(org.qi4j.library.rest.server.api.ContextResource) Resource(org.qi4j.library.rest.common.Resource) Links(org.qi4j.library.rest.common.link.Links) ResultHandler(org.qi4j.library.rest.client.spi.ResultHandler) ContextResourceClient(org.qi4j.library.rest.client.api.ContextResourceClient) Link(org.qi4j.library.rest.common.link.Link) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Example 4 with Resource

use of org.qi4j.library.rest.common.Resource in project qi4j-sdk by Qi4j.

the class ContinuousIntegrationTest method testRunBuild.

@Test
public void testRunBuild() {
    // START SNIPPET: query-list-and-command
    crc.onResource(new ResultHandler<Resource>() {

        @Override
        public HandlerCommand handleResult(Resource result, ContextResourceClient client) {
            return query("runbuild");
        }
    }).onQuery("runbuild", new ResultHandler<Links>() {

        @Override
        public HandlerCommand handleResult(Links result, ContextResourceClient client) {
            Link link = LinksUtil.withId("any", result);
            return command(link);
        }
    }).onCommand("runbuild", new ResponseHandler() {

        @Override
        public HandlerCommand handleResponse(Response response, ContextResourceClient client) {
            System.out.println("Done");
            return null;
        }
    });
    crc.start();
// END SNIPPET: query-list-and-command
}
Also used : HandlerCommand(org.qi4j.library.rest.client.api.HandlerCommand) Response(org.restlet.Response) ResponseHandler(org.qi4j.library.rest.client.spi.ResponseHandler) ContextResource(org.qi4j.library.rest.server.api.ContextResource) Resource(org.qi4j.library.rest.common.Resource) Links(org.qi4j.library.rest.common.link.Links) ResultHandler(org.qi4j.library.rest.client.spi.ResultHandler) ContextResourceClient(org.qi4j.library.rest.client.api.ContextResourceClient) Link(org.qi4j.library.rest.common.link.Link) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Example 5 with Resource

use of org.qi4j.library.rest.common.Resource in project qi4j-sdk by Qi4j.

the class ContextResource method resource.

private void resource() {
    Request request = Request.getCurrent();
    Response response = Response.getCurrent();
    if (!request.getMethod().equals(org.restlet.data.Method.GET)) {
        response.setStatus(Status.CLIENT_ERROR_METHOD_NOT_ALLOWED);
        return;
    }
    ObjectSelection objectSelection = current();
    // Check for interaction->method mappings
    if (ResourceDelete.class.isAssignableFrom(getClass())) {
        response.getAllowedMethods().add(org.restlet.data.Method.DELETE);
    }
    if (ResourceUpdate.class.isAssignableFrom(getClass())) {
        response.getAllowedMethods().add(org.restlet.data.Method.PUT);
    }
    // Construct resource
    ValueBuilder<Resource> builder = module.newValueBuilder(Resource.class);
    List<Link> queriesProperty = builder.prototype().queries().get();
    for (Method query : resourceQueries) {
        if (constraints.isValid(query, objectSelection, module)) {
            ValueBuilder<Link> linkBuilder = module.newValueBuilder(Link.class);
            Link prototype = linkBuilder.prototype();
            prototype.classes().set("query");
            prototype.text().set(humanReadable(query.getName()));
            prototype.href().set(query.getName().toLowerCase());
            prototype.rel().set(query.getName().toLowerCase());
            prototype.id().set(query.getName().toLowerCase());
            queriesProperty.add(linkBuilder.newInstance());
        }
    }
    List<Link> commandsProperty = builder.prototype().commands().get();
    for (Method command : resourceCommands) {
        if (constraints.isValid(command, objectSelection, module)) {
            ValueBuilder<Link> linkBuilder = module.newValueBuilder(Link.class);
            Link prototype = linkBuilder.prototype();
            prototype.classes().set("command");
            prototype.text().set(humanReadable(command.getName()));
            prototype.href().set(command.getName().toLowerCase());
            prototype.rel().set(command.getName().toLowerCase());
            prototype.id().set(command.getName().toLowerCase());
            commandsProperty.add(linkBuilder.newInstance());
        }
    }
    List<Link> resourcesProperty = builder.prototype().resources().get();
    for (Method subResource : subResources.values()) {
        if (constraints.isValid(subResource, objectSelection, module)) {
            ValueBuilder<Link> linkBuilder = module.newValueBuilder(Link.class);
            Link prototype = linkBuilder.prototype();
            prototype.classes().set("resource");
            prototype.text().set(humanReadable(subResource.getName()));
            prototype.href().set(subResource.getName().toLowerCase() + "/");
            prototype.rel().set(subResource.getName().toLowerCase());
            prototype.id().set(subResource.getName().toLowerCase());
            resourcesProperty.add(linkBuilder.newInstance());
        }
    }
    try {
        Method indexMethod = resourceMethodQueries.get("index");
        if (indexMethod != null) {
            Object index = convert(indexMethod.invoke(this));
            if (index != null && index instanceof ValueComposite) {
                builder.prototype().index().set((ValueComposite) index);
            }
        }
    } catch (Throwable e) {
    // Ignore
    }
    try {
        responseWriter.writeResponse(builder.newInstance(), response);
    } catch (Throwable e) {
        handleException(response, e);
    }
}
Also used : Response(org.restlet.Response) Request(org.restlet.Request) Resource(org.qi4j.library.rest.common.Resource) Method(java.lang.reflect.Method) ValueComposite(org.qi4j.api.value.ValueComposite) Link(org.qi4j.library.rest.common.link.Link)

Aggregations

Resource (org.qi4j.library.rest.common.Resource)6 Link (org.qi4j.library.rest.common.link.Link)5 Response (org.restlet.Response)5 Test (org.junit.Test)4 ContextResourceClient (org.qi4j.library.rest.client.api.ContextResourceClient)4 HandlerCommand (org.qi4j.library.rest.client.api.HandlerCommand)4 ResponseHandler (org.qi4j.library.rest.client.spi.ResponseHandler)4 ResultHandler (org.qi4j.library.rest.client.spi.ResultHandler)4 Links (org.qi4j.library.rest.common.link.Links)4 ContextResource (org.qi4j.library.rest.server.api.ContextResource)4 AbstractQi4jTest (org.qi4j.test.AbstractQi4jTest)4 SubResource (org.qi4j.library.rest.server.api.SubResource)2 TemplateException (freemarker.template.TemplateException)1 IOException (java.io.IOException)1 Writer (java.io.Writer)1 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ValueComposite (org.qi4j.api.value.ValueComposite)1 Request (org.restlet.Request)1