Search in sources :

Example 1 with ContextResourceClientFactory

use of org.qi4j.library.rest.client.api.ContextResourceClientFactory in project qi4j-sdk by Qi4j.

the class ContextResourceClientFactoryTest method startWebServer.

@Before
public void startWebServer() throws Exception {
    server = new Server(Protocol.HTTP, 8888);
    ContextRestlet restlet = module.newObject(ContextRestlet.class, new org.restlet.Context());
    ChallengeAuthenticator guard = new ChallengeAuthenticator(null, ChallengeScheme.HTTP_BASIC, "testRealm");
    MapVerifier mapVerifier = new MapVerifier();
    mapVerifier.getLocalSecrets().put("rickard", "secret".toCharArray());
    guard.setVerifier(mapVerifier);
    guard.setNext(restlet);
    server.setNext(guard);
    server.start();
    // START SNIPPET: client-create1
    Client client = new Client(Protocol.HTTP);
    ContextResourceClientFactory contextResourceClientFactory = module.newObject(ContextResourceClientFactory.class, client);
    contextResourceClientFactory.setAcceptedMediaTypes(MediaType.APPLICATION_JSON);
    // END SNIPPET: client-create1
    // START SNIPPET: client-create2
    contextResourceClientFactory.setErrorHandler(new ErrorHandler().onError(ErrorHandler.AUTHENTICATION_REQUIRED, new ResponseHandler() {

        boolean tried = false;

        @Override
        public HandlerCommand handleResponse(Response response, ContextResourceClient client) {
            if (tried)
                throw new ResourceException(response.getStatus());
            tried = true;
            client.getContextResourceClientFactory().getInfo().setUser(new User("rickard", "secret"));
            // Try again
            return refresh();
        }
    }).onError(ErrorHandler.RECOVERABLE_ERROR, new ResponseHandler() {

        @Override
        public HandlerCommand handleResponse(Response response, ContextResourceClient client) {
            // Try to restart
            return refresh();
        }
    }));
    // END SNIPPET: client-create2
    // START SNIPPET: client-create3
    Reference ref = new Reference("http://localhost:8888/");
    crc = contextResourceClientFactory.newClient(ref);
// END SNIPPET: client-create3
}
Also used : ErrorHandler(org.qi4j.library.rest.client.api.ErrorHandler) ContextResourceClientFactory(org.qi4j.library.rest.client.api.ContextResourceClientFactory) User(org.restlet.security.User) Server(org.restlet.Server) ResponseHandler(org.qi4j.library.rest.client.spi.ResponseHandler) Reference(org.restlet.data.Reference) Response(org.restlet.Response) HandlerCommand(org.qi4j.library.rest.client.api.HandlerCommand) ContextRestlet(org.qi4j.library.rest.server.api.ContextRestlet) ChallengeAuthenticator(org.restlet.security.ChallengeAuthenticator) MapVerifier(org.restlet.security.MapVerifier) ResourceException(org.restlet.resource.ResourceException) ContextResourceClient(org.qi4j.library.rest.client.api.ContextResourceClient) Client(org.restlet.Client) ContextResourceClient(org.qi4j.library.rest.client.api.ContextResourceClient) Before(org.junit.Before)

Example 2 with ContextResourceClientFactory

use of org.qi4j.library.rest.client.api.ContextResourceClientFactory in project qi4j-sdk by Qi4j.

the class RssReaderTest method testReadRssFeed.

@Test
public void testReadRssFeed() {
    Client client = new Client(Protocol.HTTPS);
    Reference ref = new Reference("https://github.com/Qi4j/qi4j-sdk/commits/develop.atom");
    ContextResourceClientFactory contextResourceClientFactory = module.newObject(ContextResourceClientFactory.class, client);
    contextResourceClientFactory.registerResponseReader(new ResponseReader() {

        @Override
        public Object readResponse(Response response, Class<?> resultType) throws ResourceException {
            if (resultType.equals(Document.class)) {
                try {
                    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
                    documentBuilderFactory.setNamespaceAware(false);
                    return documentBuilderFactory.newDocumentBuilder().parse(response.getEntity().getStream());
                } catch (Exception e) {
                    throw new ResourceException(e);
                }
            }
            return null;
        }
    });
    contextResourceClientFactory.setErrorHandler(new ErrorHandler().onError(ErrorHandler.RECOVERABLE_ERROR, new ResponseHandler() {

        @Override
        public HandlerCommand handleResponse(Response response, ContextResourceClient client) {
            System.out.println(">> REFRESH on recoverable error: " + response.getStatus());
            return refresh();
        }
    }));
    crc = contextResourceClientFactory.newClient(ref);
    crc.onResource(new ResultHandler<Document>() {

        Iterator<Node> itemNodes;

        @Override
        public HandlerCommand handleResult(Document result, ContextResourceClient client) {
            try {
                final XPath xPath = XPathFactory.newInstance().newXPath();
                System.out.println("== " + xPath.evaluate("feed/title", result) + " ==");
                final NodeList nodes = (NodeList) xPath.evaluate("feed/entry", result, XPathConstants.NODESET);
                List<Node> items = new ArrayList<Node>();
                for (int i = 0; i < nodes.getLength(); i++) {
                    items.add(nodes.item(i));
                }
                itemNodes = items.iterator();
                return processEntry(xPath);
            } catch (XPathExpressionException e) {
                throw new ResourceException(e);
            }
        }

        private HandlerCommand processEntry(final XPath xPath) throws XPathExpressionException {
            if (!itemNodes.hasNext()) {
                return null;
            }
            Node item = itemNodes.next();
            String title = xPath.evaluate("title", item);
            String detailUrl = xPath.evaluate("link/@href", item);
            System.out.println("-- " + title + " --");
            System.out.println("-- " + detailUrl + " --");
            return processEntry(xPath);
        }
    });
    crc.start();
}
Also used : ContextResourceClientFactory(org.qi4j.library.rest.client.api.ContextResourceClientFactory) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) ResponseHandler(org.qi4j.library.rest.client.spi.ResponseHandler) XPathExpressionException(javax.xml.xpath.XPathExpressionException) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) HandlerCommand(org.qi4j.library.rest.client.api.HandlerCommand) ResourceException(org.restlet.resource.ResourceException) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) List(java.util.List) ContextResourceClient(org.qi4j.library.rest.client.api.ContextResourceClient) Client(org.restlet.Client) ContextResourceClient(org.qi4j.library.rest.client.api.ContextResourceClient) XPath(javax.xml.xpath.XPath) ErrorHandler(org.qi4j.library.rest.client.api.ErrorHandler) Reference(org.restlet.data.Reference) NodeList(org.w3c.dom.NodeList) XPathExpressionException(javax.xml.xpath.XPathExpressionException) ResourceException(org.restlet.resource.ResourceException) AssemblyException(org.qi4j.bootstrap.AssemblyException) Response(org.restlet.Response) ResponseReader(org.qi4j.library.rest.client.spi.ResponseReader) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Example 3 with ContextResourceClientFactory

use of org.qi4j.library.rest.client.api.ContextResourceClientFactory in project qi4j-sdk by Qi4j.

the class ContinuousIntegrationTest method startWebServer.

@Before
public void startWebServer() throws Exception {
    server = new Server(Protocol.HTTP, 8888);
    ContextRestlet restlet = module.newObject(ContextRestlet.class, new org.restlet.Context());
    ChallengeAuthenticator guard = new ChallengeAuthenticator(null, ChallengeScheme.HTTP_BASIC, "testRealm");
    MapVerifier mapVerifier = new MapVerifier();
    mapVerifier.getLocalSecrets().put("rickard", "secret".toCharArray());
    guard.setVerifier(mapVerifier);
    guard.setNext(restlet);
    server.setNext(guard);
    server.start();
    // START SNIPPET: client-create1
    Client client = new Client(Protocol.HTTP);
    ContextResourceClientFactory contextResourceClientFactory = module.newObject(ContextResourceClientFactory.class, client);
    contextResourceClientFactory.setAcceptedMediaTypes(MediaType.APPLICATION_JSON);
    // END SNIPPET: client-create1
    // START SNIPPET: client-create2
    contextResourceClientFactory.setErrorHandler(new ErrorHandler().onError(ErrorHandler.AUTHENTICATION_REQUIRED, new ResponseHandler() {

        boolean tried = false;

        @Override
        public HandlerCommand handleResponse(Response response, ContextResourceClient client) {
            if (tried) {
                throw new ResourceException(response.getStatus());
            }
            tried = true;
            client.getContextResourceClientFactory().getInfo().setUser(new User("rickard", "secret"));
            // Try again
            return refresh();
        }
    }).onError(ErrorHandler.RECOVERABLE_ERROR, new ResponseHandler() {

        @Override
        public HandlerCommand handleResponse(Response response, ContextResourceClient client) {
            // Try to restart
            return refresh();
        }
    }));
    // END SNIPPET: client-create2
    // START SNIPPET: client-create3
    Reference ref = new Reference("http://localhost:8888/");
    crc = contextResourceClientFactory.newClient(ref);
// END SNIPPET: client-create3
}
Also used : ErrorHandler(org.qi4j.library.rest.client.api.ErrorHandler) ContextResourceClientFactory(org.qi4j.library.rest.client.api.ContextResourceClientFactory) User(org.restlet.security.User) Server(org.restlet.Server) ResponseHandler(org.qi4j.library.rest.client.spi.ResponseHandler) Reference(org.restlet.data.Reference) Response(org.restlet.Response) HandlerCommand(org.qi4j.library.rest.client.api.HandlerCommand) ContextRestlet(org.qi4j.library.rest.server.api.ContextRestlet) ChallengeAuthenticator(org.restlet.security.ChallengeAuthenticator) MapVerifier(org.restlet.security.MapVerifier) ResourceException(org.restlet.resource.ResourceException) ContextResourceClient(org.qi4j.library.rest.client.api.ContextResourceClient) Client(org.restlet.Client) ContextResourceClient(org.qi4j.library.rest.client.api.ContextResourceClient) Before(org.junit.Before)

Aggregations

ContextResourceClient (org.qi4j.library.rest.client.api.ContextResourceClient)3 ContextResourceClientFactory (org.qi4j.library.rest.client.api.ContextResourceClientFactory)3 ErrorHandler (org.qi4j.library.rest.client.api.ErrorHandler)3 HandlerCommand (org.qi4j.library.rest.client.api.HandlerCommand)3 ResponseHandler (org.qi4j.library.rest.client.spi.ResponseHandler)3 Client (org.restlet.Client)3 Response (org.restlet.Response)3 Reference (org.restlet.data.Reference)3 ResourceException (org.restlet.resource.ResourceException)3 Before (org.junit.Before)2 ContextRestlet (org.qi4j.library.rest.server.api.ContextRestlet)2 Server (org.restlet.Server)2 ChallengeAuthenticator (org.restlet.security.ChallengeAuthenticator)2 MapVerifier (org.restlet.security.MapVerifier)2 User (org.restlet.security.User)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 XPath (javax.xml.xpath.XPath)1 XPathExpressionException (javax.xml.xpath.XPathExpressionException)1