Search in sources :

Example 1 with Link

use of org.qi4j.library.rest.common.link.Link 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 Link

use of org.qi4j.library.rest.common.link.Link 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 Link

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

the class LinksResponseWriter method writeResponse.

@Override
public boolean writeResponse(final Object result, final Response response) throws ResourceException {
    if (result instanceof Link) {
        MediaType type = getVariant(response.getRequest(), ENGLISH, supportedLinkMediaTypes).getMediaType();
        if (MediaType.APPLICATION_JSON.equals(type)) {
            response.setEntity(new StringRepresentation(((Link) result).toString(), MediaType.APPLICATION_JSON));
            return true;
        } else {
            response.setStatus(Status.REDIRECTION_TEMPORARY);
            Link link = (Link) result;
            Reference reference = new Reference(response.getRequest().getResourceRef(), link.href().get());
            response.setLocationRef(reference);
            return true;
        }
    } else if (result instanceof Links) {
        MediaType type = getVariant(response.getRequest(), ENGLISH, supportedLinksMediaTypes).getMediaType();
        Representation rep;
        if (MediaType.APPLICATION_JSON.equals(type)) {
            rep = createJsonRepresentation((Links) result);
        } else if (MediaType.TEXT_HTML.equals(type)) {
            rep = createTextHtmlRepresentation(result, response);
        } else if (MediaType.APPLICATION_ATOM.equals(type)) {
            rep = createAtomRepresentation(result, response);
        } else {
            return false;
        }
        response.setEntity(rep);
        return true;
    }
    return false;
}
Also used : StringRepresentation(org.restlet.representation.StringRepresentation) Reference(org.restlet.data.Reference) MediaType(org.restlet.data.MediaType) Links(org.qi4j.library.rest.common.link.Links) StringRepresentation(org.restlet.representation.StringRepresentation) Representation(org.restlet.representation.Representation) WriterRepresentation(org.restlet.representation.WriterRepresentation) Link(org.qi4j.library.rest.common.link.Link)

Example 4 with Link

use of org.qi4j.library.rest.common.link.Link 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 5 with Link

use of org.qi4j.library.rest.common.link.Link 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)

Aggregations

Link (org.qi4j.library.rest.common.link.Link)6 Resource (org.qi4j.library.rest.common.Resource)5 Links (org.qi4j.library.rest.common.link.Links)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 ContextResource (org.qi4j.library.rest.server.api.ContextResource)4 AbstractQi4jTest (org.qi4j.test.AbstractQi4jTest)4 SubResource (org.qi4j.library.rest.server.api.SubResource)2 Method (java.lang.reflect.Method)1 ValueComposite (org.qi4j.api.value.ValueComposite)1 Request (org.restlet.Request)1 MediaType (org.restlet.data.MediaType)1 Reference (org.restlet.data.Reference)1 Representation (org.restlet.representation.Representation)1 StringRepresentation (org.restlet.representation.StringRepresentation)1 WriterRepresentation (org.restlet.representation.WriterRepresentation)1