Package org.restlet.resource

Examples of org.restlet.resource.ClientResource


    public static void main(String[] args) throws IOException,
            ResourceException {

        // Define our Restlet client resources.
        ClientResource app = new ClientResource(
                "http://localhost:8111/firstResource");
        ClientResource itemsResource = new ClientResource(
                "http://localhost:8111/firstResource/items");
        ClientResource itemResource = null;

        // Displays the WADL documentation of the application
        app.options().write(System.out);

        // Create a new item
        Item item = new Item("item1", "this is an item.");
        Representation r = itemsResource.post(getRepresentation(item));
        if (itemsResource.getStatus().isSuccess()) {
            itemResource = new ClientResource(r.getLocationRef());
        }

        if (itemResource != null) {
            // Prints the representation of the newly created resource.
            get(itemResource);

            // Prints the list of registered items.
            get(itemsResource);

            // Update the item
            item.setDescription("This is an other description");
            itemResource.put(getRepresentation(item));

            // Prints the list of registered items.
            get(itemsResource);

            // delete the item
            itemResource.delete();

            // Print the list of registered items.
            get(itemsResource);
        }
    }
View Full Code Here


        String text = "Test content\r\nLine 2\r\nLine2";
        LocalReference fr = LocalReference
                .createFileReference(File.createTempFile("Restlet", ".txt."
                        + Language.DEFAULT.getName()));

        ClientResource resource = new ClientResource(fr);
        try {
            // Update the text of the temporary file
            resource.put(new StringRepresentation(text));
        } catch (ResourceException e) {
        }
        assertTrue(resource.getStatus().isSuccess());

        try {
            // Get the text and compare to the original
            resource.get();
        } catch (ResourceException e) {
        }
        assertEquals(Status.SUCCESS_OK, resource.getStatus());

        try {
            // Delete the file
            resource.delete();
        } catch (ResourceException e) {
        }
        assertEquals(Status.SUCCESS_NO_CONTENT, resource.getStatus());
    }
View Full Code Here

        String fzr = zr + "!/test.txt";
        String fzd = zr + "!/dir/";
        String fzr2 = fzd + "test2.txt";

        // Write the text to file
        ClientResource r = new ClientResource(fzr);
        r.put(new StringRepresentation(text));
        assertTrue(r.getStatus().equals(Status.SUCCESS_CREATED));

        // Get the text and compare to the original
        r.get();
        assertTrue(r.getStatus().equals(Status.SUCCESS_OK));
        assertEquals(r.getResponseEntity().getText(), text);
        r.release();

        // Write the text to file
        ClientResource r2 = new ClientResource(fzr2);
        r2.put(new StringRepresentation(text2));
        assertTrue(r2.getStatus().equals(Status.SUCCESS_OK));

        // Checking first one was not overwritten
        r.get();
        assertTrue(r.getStatus().equals(Status.SUCCESS_OK));
        assertEquals(r.getResponseEntity().getText(), text);
        r.release();

        // Put a directory
        ClientResource rd = new ClientResource(fzd);
        rd.put(new EmptyRepresentation());
        assertTrue(rd.getStatus().equals(Status.SUCCESS_OK));

        rd.get();
        assertTrue(rd.getStatus().equals(Status.SUCCESS_OK));

        // Checking second one was output
        r2.get();
        assertTrue("Could not get " + fzr2, r2.getStatus().equals(
                Status.SUCCESS_OK));
        assertEquals(r2.getResponseEntity().getText(), text2);

        ClientResource rTest2 = new ClientResource(zr + "!test2");
        rTest2.get();
        assertFalse(rTest2.getStatus().equals(Status.SUCCESS_OK));

        // Try to replace file by directory
        ClientResource r2d = new ClientResource(fzr2 + "/");
        r2d.put(new EmptyRepresentation());
        assertFalse(r2d.getStatus().equals(Status.SUCCESS_OK));
    }
View Full Code Here

        // Create the target URI, encoding the user name
        String uri = "https://maps.example.com/user/" + Reference.encode(user);

        // Invoke the web service
        new ClientResource(uri).put(input.getWebRepresentation());
    }
View Full Code Here

        form.add("pass", fbPass);

        String q = form.getQueryString();
        Reference redirRef = new Reference(params.getBaseRef(),
                params.getAuthorizePath(), q, null);
        ClientResource authResource = new CookieCopyClientResource(
                redirRef.toUri());
        authResource.setFollowingRedirects(false); // token is in a 3xx
        Representation r = authResource.get();

        int maxRedirCnt = 10; // Stop the maddness if out of hand...
        int cnt = 0;

        while (authResource.getStatus().isRedirection()) {
            String fragment = authResource.getLocationRef().getFragment();
            if (fragment != null && fragment.length() > 0) {
                Form f = new Form(fragment);

                String accessToken = f
                        .getFirstValue(OAuthServerResource.ACCESS_TOKEN);

                String refreshToken = f
                        .getFirstValue(OAuthServerResource.REFRESH_TOKEN);

                long expiresIn = 0;
                String exp = f.getFirstValue(OAuthServerResource.EXPIRES_IN);
                if (exp != null && exp.length() > 0) {
                    expiresIn = Long.parseLong(exp);
                }

                if (accessToken != null && accessToken.length() > 0) {
                    Context.getCurrentLogger().info(
                            "Successful UserAgent flow : AccessToken = "
                                    + accessToken + " RefreshToken = "
                                    + refreshToken + " ExpiresIn = "
                                    + expiresIn);
                    break;
                }
            }

            if (++cnt >= maxRedirCnt)
                break;

            Context.getCurrentLogger().info(
                    "Redir to = " + authResource.getLocationRef());
            authResource.setReference(authResource.getLocationRef());
            authResource.get();
        }

        if (authResource.getStatus().isSuccess()) {
            result = authResource.getCookieSettings();
        }

        r.release();
        authResource.release();

        return result;
    }
View Full Code Here

    /** Base application URI. */
    public static final String APPLICATION_URI = "http://localhost:3000/v1";

    public static void deleteBookmark(String userName, String password,
            String uri) {
        ClientResource resource = getAuthenticatedResource(getBookmarkUri(
                userName, uri), userName, password);
        resource.delete();
        System.out.println(resource.getStatus() + " : "
                + resource.getLocationRef());
    }
View Full Code Here

     *            The reference to the WCF service.
     */
    public Service(Reference serviceRef) {
        try {
            // Test the given service URI which may be actually redirected.
            ClientResource cr = new ClientResource(serviceRef);
            if (cr.getNext() == null) {
                // The context does not provide a client connector.
                // Let instantiate our own.
                Protocol rProtocol = cr.getProtocol();
                Reference rReference = cr.getReference();
                Protocol protocol = (rProtocol != null) ? rProtocol
                        : (rReference != null) ? rReference.getSchemeProtocol()
                                : null;

                if (protocol != null) {
                    this.clientConnector = new Client(protocol);
                    // Set the next handler for reuse
                    cr.setNext(this.clientConnector);
                }
            }

            cr.setFollowingRedirects(false);
            cr.get();

            if (cr.getStatus().isRedirection()) {
                this.serviceRef = cr.getLocationRef();
            } else {
                this.serviceRef = cr.getReference();
            }
        } catch (Throwable e) {
            this.serviceRef = serviceRef;
        }
    }
View Full Code Here

     */
    public void addEntity(String entitySetName, Object entity) throws Exception {
        if (entity != null) {
            Entry entry = toEntry(entity);

            ClientResource resource = createResource(entitySetName);
            if (getMetadata() == null) {
                throw new Exception("Can't add entity to this entity set "
                        + resource.getReference()
                        + " due to the lack of the service's metadata.");
            }

            try {
                // TODO Fix chunked request with net client connector
                ByteArrayOutputStream o = new ByteArrayOutputStream();
                entry.write(o);
                StringRepresentation r = new StringRepresentation(o.toString(),
                        MediaType.APPLICATION_ATOM);
                Representation rep = resource.post(r);
                EntryContentHandler<?> entryContentHandler = new EntryContentHandler<Object>(
                        entity.getClass(), (Metadata) getMetadata(),
                        getLogger());
                Feed feed = new Feed();
                feed.getEntries().add(new Entry(rep, entryContentHandler));
            } catch (ResourceException re) {
                throw new ResourceException(re.getStatus(),
                        "Can't add entity to this entity set "
                                + resource.getReference());
            } finally {
                this.latestRequest = resource.getRequest();
                this.latestResponse = resource.getResponse();
            }
        }
    }
View Full Code Here

     * @param reference
     *            The reference of the target resource.
     * @return An instance of {@link ClientResource}.
     */
    public ClientResource createResource(Reference reference) {
        ClientResource resource = new ClientResource(reference);
        if (clientConnector != null) {
            // We provide our own cient connector.
            resource.setNext(clientConnector);
        }
        resource.setChallengeResponse(getCredentials());

        if (getClientVersion() != null || getMaxClientVersion() != null) {
            Form form = new Form();
            if (getClientVersion() != null) {
                form.add("DataServiceVersion", getClientVersion());
            }
            if (getMaxClientVersion() != null) {
                form.add("MaxDataServiceVersion", getMaxClientVersion());
            }
            resource.getRequestAttributes().put(
                    HeaderConstants.ATTRIBUTE_HEADERS, form);
        }

        return resource;
    }
View Full Code Here

    public void deleteEntity(Object entity) throws ResourceException {
        if (getMetadata() == null) {
            return;
        }

        ClientResource resource = createResource(getSubpath(entity));

        try {
            resource.delete();
        } catch (ResourceException re) {
            throw new ResourceException(re.getStatus(),
                    "Can't delete this entity " + resource.getReference());
        } finally {
            this.latestRequest = resource.getRequest();
            this.latestResponse = resource.getResponse();
        }
    }
View Full Code Here

TOP

Related Classes of org.restlet.resource.ClientResource

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.