Search in sources :

Example 6 with GetQuery

use of org.apache.pivot.web.GetQuery in project pivot by apache.

the class SuggestionDemo method getSuggestions.

private void getSuggestions() {
    if (suggestionQuery != null && suggestionQuery.isPending()) {
        suggestionQuery.abort();
    }
    // Get the query text
    String text;
    try {
        text = URLEncoder.encode(textInput.getText(), "UTF-8");
    } catch (UnsupportedEncodingException exception) {
        throw new RuntimeException(exception);
    }
    // Create query
    suggestionQuery = new GetQuery("search.yahooapis.com", "/WebSearchService/V1/relatedSuggestion");
    suggestionQuery.getParameters().put("appid", getClass().getName());
    suggestionQuery.getParameters().put("query", text);
    suggestionQuery.getParameters().put("output", "json");
    suggestionQuery.execute(new TaskAdapter<>(new TaskListener<Object>() {

        @Override
        public void taskExecuted(Task<Object> task) {
            if (task == suggestionQuery) {
                List<?> suggestions = null;
                Object result = JSON.get(task.getResult(), "ResultSet.Result");
                if (result instanceof List<?>) {
                    suggestions = (List<?>) result;
                }
                if (suggestions == null || suggestions.getLength() == 0) {
                    suggestionPopup.close();
                } else {
                    suggestionPopup.setSuggestionData(suggestions);
                    suggestionPopup.open(textInput, new SuggestionPopupCloseListener() {

                        @Override
                        public void suggestionPopupClosed(SuggestionPopup suggestionPopupArgument) {
                            if (suggestionPopupArgument.getResult()) {
                                String textLocal;
                                try {
                                    textLocal = URLEncoder.encode(textInput.getText(), "UTF-8");
                                } catch (UnsupportedEncodingException exception) {
                                    throw new RuntimeException(exception);
                                }
                                String location = "http://search.yahoo.com/search?p=" + textLocal;
                                try {
                                    Desktop.getDesktop().browse(new URI(location));
                                } catch (IOException exception) {
                                    System.err.println(exception);
                                } catch (URISyntaxException exception) {
                                    System.err.println(exception);
                                }
                            }
                        }
                    });
                }
                activityIndicator.setActive(false);
                suggestionQuery = null;
            }
        }

        @Override
        public void executeFailed(Task<Object> task) {
            if (task == suggestionQuery) {
                System.err.println(task.getFault());
                activityIndicator.setActive(false);
                suggestionQuery = null;
            }
        }
    }));
    activityIndicator.setActive(true);
}
Also used : Task(org.apache.pivot.util.concurrent.Task) SuggestionPopupCloseListener(org.apache.pivot.wtk.SuggestionPopupCloseListener) SuggestionPopup(org.apache.pivot.wtk.SuggestionPopup) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) GetQuery(org.apache.pivot.web.GetQuery) TaskListener(org.apache.pivot.util.concurrent.TaskListener) List(org.apache.pivot.collections.List)

Example 7 with GetQuery

use of org.apache.pivot.web.GetQuery in project pivot by apache.

the class RESTDemoTest method testCRUD.

@Test
public void testCRUD() throws IOException, SerializationException, QueryException {
    JSONSerializer jsonSerializer = new JSONSerializer();
    Object contact = jsonSerializer.readObject(getClass().getResourceAsStream("contact.json"));
    // Create
    PostQuery postQuery = new PostQuery(hostname, port, "/pivot-demos/rest_demo", secure);
    postQuery.setValue(contact);
    URL location = postQuery.execute();
    assertNotNull(location);
    String path = location.getPath();
    // Read
    GetQuery getQuery = new GetQuery(hostname, port, path, secure);
    Object result = getQuery.execute();
    assertArrayEquals((Object[]) JSON.get(contact, "address.street"), (Object[]) JSON.get(result, "address.street"));
    assertEquals(contact, result);
    // Update
    JSON.put(contact, "name", "Joseph User");
    PutQuery putQuery = new PutQuery(hostname, port, path, secure);
    putQuery.setValue(contact);
    boolean created = putQuery.execute();
    assertFalse(created);
    assertEquals(contact, getQuery.execute());
    // Delete
    DeleteQuery deleteQuery = new DeleteQuery(hostname, port, path, secure);
    deleteQuery.execute();
    assertEquals(deleteQuery.getStatus(), Query.Status.NO_CONTENT);
}
Also used : GetQuery(org.apache.pivot.web.GetQuery) PostQuery(org.apache.pivot.web.PostQuery) DeleteQuery(org.apache.pivot.web.DeleteQuery) PutQuery(org.apache.pivot.web.PutQuery) URL(java.net.URL) JSONSerializer(org.apache.pivot.json.JSONSerializer) Test(org.junit.Test)

Example 8 with GetQuery

use of org.apache.pivot.web.GetQuery in project pivot by apache.

the class RSSFeedDemo method initialize.

@Override
public void initialize(Map<String, Object> namespace, URL location, Resources resources) {
    feedListView = (ListView) namespace.get("feedListView");
    cardPane = (CardPane) namespace.get("cardPane");
    statusLabel = (Label) namespace.get("statusLabel");
    feedListView.getComponentMouseButtonListeners().add(new ComponentMouseButtonListener() {

        private int index = -1;

        @Override
        public boolean mouseClick(Component component, Mouse.Button button, int x, int y, int count) {
            if (count == 1) {
                index = feedListView.getItemAt(y);
            } else if (count == 2 && feedListView.getItemAt(y) == index) {
                Element itemElement = (Element) feedListView.getListData().get(index);
                String link = XML.getText(itemElement, "link");
                Desktop desktop = Desktop.getDesktop();
                try {
                    desktop.browse(new URL(link).toURI());
                } catch (MalformedURLException exception) {
                    throw new RuntimeException(exception);
                } catch (URISyntaxException exception) {
                    throw new RuntimeException(exception);
                } catch (IOException exception) {
                    System.out.println("Unable to open " + link + " in default browser.");
                }
            }
            return false;
        }
    });
    GetQuery getQuery = new GetQuery("feeds.dzone.com", "/javalobby/frontpage");
    getQuery.setSerializer(new XMLSerializer());
    getQuery.getParameters().put("format", "xml");
    getQuery.execute(new TaskAdapter<>(new TaskListener<Object>() {

        @Override
        public void taskExecuted(Task<Object> task) {
            Element root = (Element) task.getResult();
            feedListView.setListData(XML.getElements(root, "channel", "item"));
            cardPane.setSelectedIndex(1);
        }

        @Override
        public void executeFailed(Task<Object> task) {
            statusLabel.setText(task.getFault().toString());
        }
    }));
}
Also used : MalformedURLException(java.net.MalformedURLException) XMLSerializer(org.apache.pivot.xml.XMLSerializer) Task(org.apache.pivot.util.concurrent.Task) Element(org.apache.pivot.xml.Element) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URL(java.net.URL) Mouse(org.apache.pivot.wtk.Mouse) Desktop(java.awt.Desktop) GetQuery(org.apache.pivot.web.GetQuery) TaskListener(org.apache.pivot.util.concurrent.TaskListener) ComponentMouseButtonListener(org.apache.pivot.wtk.ComponentMouseButtonListener) Component(org.apache.pivot.wtk.Component)

Aggregations

GetQuery (org.apache.pivot.web.GetQuery)8 Task (org.apache.pivot.util.concurrent.Task)6 TaskListener (org.apache.pivot.util.concurrent.TaskListener)6 List (org.apache.pivot.collections.List)5 IOException (java.io.IOException)2 URISyntaxException (java.net.URISyntaxException)2 URL (java.net.URL)2 ArrayList (org.apache.pivot.collections.ArrayList)2 Test (org.junit.Test)2 Desktop (java.awt.Desktop)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 MalformedURLException (java.net.MalformedURLException)1 URI (java.net.URI)1 DateFormat (java.text.DateFormat)1 Date (java.util.Date)1 BXMLSerializer (org.apache.pivot.beans.BXMLSerializer)1 Map (org.apache.pivot.collections.Map)1 JSONSerializer (org.apache.pivot.json.JSONSerializer)1 CSVSerializer (org.apache.pivot.serialization.CSVSerializer)1 DeleteQuery (org.apache.pivot.web.DeleteQuery)1