Search in sources :

Example 21 with SerializationException

use of org.apache.pivot.serialization.SerializationException in project pivot by apache.

the class ExpensesWindow method initialize.

@Override
public void initialize(Map<String, Object> namespace, URL location, Resources resources) {
    expenseTableView = (TableView) namespace.get("expenseTableView");
    activityIndicator = (ActivityIndicator) namespace.get("activityIndicator");
    activityIndicatorBoxPane = (BoxPane) namespace.get("activityIndicatorBoxPane");
    // Load the add/edit sheet
    try {
        BXMLSerializer bxmlSerializer = new BXMLSerializer();
        expenseSheet = (ExpenseSheet) bxmlSerializer.readObject(ExpenseSheet.class, "expense_sheet.bxml", true);
    } catch (IOException exception) {
        throw new RuntimeException(exception);
    } catch (SerializationException exception) {
        throw new RuntimeException(exception);
    }
    // Create the delete confirmation prompt
    ArrayList<String> options = new ArrayList<>((String) resources.get("ok"), (String) resources.get("cancel"));
    deleteConfirmationPrompt = new Prompt(MessageType.QUESTION, (String) resources.get("confirmDelete"), options);
    // Attach event listeners
    expenseTableView.getTableViewSelectionListeners().add(new TableViewSelectionListener() {

        @Override
        public void selectedRowChanged(TableView tableView, Object previousSelectedRow) {
            int selectedIndex = expenseTableView.getSelectedIndex();
            editSelectedExpenseAction.setEnabled(selectedIndex != -1);
            deleteSelectedExpenseAction.setEnabled(selectedIndex != -1);
        }
    });
}
Also used : SerializationException(org.apache.pivot.serialization.SerializationException) ArrayList(org.apache.pivot.collections.ArrayList) Prompt(org.apache.pivot.wtk.Prompt) IOException(java.io.IOException) TableViewSelectionListener(org.apache.pivot.wtk.TableViewSelectionListener) BXMLSerializer(org.apache.pivot.beans.BXMLSerializer) TableView(org.apache.pivot.wtk.TableView)

Example 22 with SerializationException

use of org.apache.pivot.serialization.SerializationException in project pivot by apache.

the class QueryServlet method doPut.

@Override
protected final void doPut(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    Path path = getPath(request);
    boolean created = false;
    try {
        validate(Query.Method.PUT, path);
        Object value = null;
        if (request.getContentLength() > 0) {
            Serializer<?> serializer = createSerializer(Query.Method.PUT, path);
            value = serializer.readObject(request.getInputStream());
        }
        created = doPut(path, value);
    } catch (SerializationException exception) {
        throw new ServletException(exception);
    } catch (QueryException exception) {
        response.setStatus(exception.getStatus());
        response.flushBuffer();
    }
    if (!response.isCommitted()) {
        response.setStatus(created ? Query.Status.CREATED : Query.Status.NO_CONTENT);
        setResponseHeaders(response);
        response.setContentLength(0);
        response.flushBuffer();
    }
}
Also used : ServletException(javax.servlet.ServletException) QueryException(org.apache.pivot.web.QueryException) SerializationException(org.apache.pivot.serialization.SerializationException)

Example 23 with SerializationException

use of org.apache.pivot.serialization.SerializationException in project pivot by apache.

the class Query method execute.

@SuppressWarnings("unchecked")
protected Object execute(final Method method, final Object value) throws QueryException {
    Object result = value;
    URL location = getLocation();
    HttpURLConnection connection = null;
    Serializer<Object> serializerLocal = (Serializer<Object>) this.serializer;
    bytesSent.set(0);
    bytesReceived.set(0);
    bytesExpected = -1;
    status = 0;
    String message = null;
    try {
        // Clear any properties from a previous response
        responseHeaders.clear();
        // Open a connection
        if (proxy == null) {
            connection = (HttpURLConnection) location.openConnection();
        } else {
            connection = (HttpURLConnection) location.openConnection(proxy);
        }
        connection.setRequestMethod(method.toString());
        connection.setAllowUserInteraction(false);
        connection.setInstanceFollowRedirects(false);
        connection.setUseCaches(false);
        if (connection instanceof HttpsURLConnection && hostnameVerifier != null) {
            HttpsURLConnection httpsConnection = (HttpsURLConnection) connection;
            httpsConnection.setHostnameVerifier(hostnameVerifier);
        }
        // Set the request headers
        if (result != null) {
            connection.setRequestProperty("Content-Type", serializerLocal.getMIMEType(result));
        }
        for (String key : requestHeaders) {
            for (int i = 0, n = requestHeaders.getLength(key); i < n; i++) {
                if (i == 0) {
                    connection.setRequestProperty(key, requestHeaders.get(key, i));
                } else {
                    connection.addRequestProperty(key, requestHeaders.get(key, i));
                }
            }
        }
        // Set the input/output state
        connection.setDoInput(true);
        connection.setDoOutput(result != null);
        // Connect to the server
        connection.connect();
        queryListeners.connected(this);
        // Write the request body
        if (result != null) {
            try (OutputStream outputStream = connection.getOutputStream()) {
                serializerLocal.writeObject(result, new MonitoredOutputStream(outputStream));
            }
        }
        // Notify listeners that the request has been sent
        queryListeners.requestSent(this);
        // Set the response info
        status = connection.getResponseCode();
        message = connection.getResponseMessage();
        // Record the content length
        bytesExpected = connection.getContentLengthLong();
        // NOTE Header indexes start at 1, not 0
        int i = 1;
        for (String key = connection.getHeaderFieldKey(i); key != null; key = connection.getHeaderFieldKey(++i)) {
            responseHeaders.add(key, connection.getHeaderField(i));
        }
        // If the response was anything other than 2xx, throw an exception
        int statusPrefix = status / 100;
        if (statusPrefix != 2) {
            queryListeners.failed(this);
            throw new QueryException(status, message);
        }
        // Read the response body
        if (method == Method.GET && status == Query.Status.OK) {
            try (InputStream inputStream = connection.getInputStream()) {
                result = serializerLocal.readObject(new MonitoredInputStream(inputStream));
            }
        }
        // Notify listeners that the response has been received
        queryListeners.responseReceived(this);
    } catch (IOException exception) {
        queryListeners.failed(this);
        throw new QueryException(exception);
    } catch (SerializationException exception) {
        queryListeners.failed(this);
        throw new QueryException(exception);
    } catch (RuntimeException exception) {
        queryListeners.failed(this);
        throw exception;
    }
    return result;
}
Also used : SerializationException(org.apache.pivot.serialization.SerializationException) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) JSONSerializer(org.apache.pivot.json.JSONSerializer) Serializer(org.apache.pivot.serialization.Serializer)

Example 24 with SerializationException

use of org.apache.pivot.serialization.SerializationException in project pivot by apache.

the class Pivot714 method getWindow.

public Window getWindow(final Window ownerArgument) {
    this.owner = ownerArgument;
    final BXMLSerializer bxmlSerializer = new BXMLSerializer();
    try {
        result = (Dialog) bxmlSerializer.readObject(Pivot714.class.getResource("pivot_714.bxml"));
    } catch (IOException e) {
        e.printStackTrace();
    } catch (SerializationException e) {
        e.printStackTrace();
    }
    final ListButton motif = (ListButton) bxmlSerializer.getNamespace().get("motif");
    ArrayList<String> al = new ArrayList<>();
    al.add("One");
    al.add("Two");
    motif.setListData(al);
    CalendarButton cbDate = (CalendarButton) bxmlSerializer.getNamespace().get("date");
    dcl = (new DialogCloseListener() {

        @Override
        public void dialogClosed(Dialog dialog, boolean modal) {
        // empty block
        }
    });
    cbDate.getCalendarButtonSelectionListeners().add(new CalendarButtonSelectionListener() {

        @Override
        public void selectedDateChanged(CalendarButton calendarButton, CalendarDate previousSelectedDate) {
        // empty block
        }
    });
    return result;
}
Also used : SerializationException(org.apache.pivot.serialization.SerializationException) ArrayList(org.apache.pivot.collections.ArrayList) CalendarButtonSelectionListener(org.apache.pivot.wtk.CalendarButtonSelectionListener) IOException(java.io.IOException) ListButton(org.apache.pivot.wtk.ListButton) CalendarDate(org.apache.pivot.util.CalendarDate) CalendarButton(org.apache.pivot.wtk.CalendarButton) Dialog(org.apache.pivot.wtk.Dialog) DialogCloseListener(org.apache.pivot.wtk.DialogCloseListener) BXMLSerializer(org.apache.pivot.beans.BXMLSerializer)

Example 25 with SerializationException

use of org.apache.pivot.serialization.SerializationException in project pivot by apache.

the class JavascriptConsoleTest method loadWindowFromURL.

/**
 * Load (and returns) a Window, given its URL and serializer to use <p> Note
 * that if public this method could be called even from JS in a bxml file
 * (but a reference to the current application has to be put in serializer
 * namespace). <p> Note that all Exceptions are catched inside this method,
 * to not expose them to JS code.
 *
 * @param urlString the URL of the bxml file to load, as a String
 * @param bxmlSerializer the serializer to use, or if null a new one will be created
 * @return the Window instance
 */
public Window loadWindowFromURL(String urlString, final BXMLSerializer bxmlSerializer) {
    logObject("loadWindow from \"" + urlString + "\", with the serializer " + bxmlSerializer);
    BXMLSerializer serializer = bxmlSerializer;
    if (serializer == null) {
        serializer = new BXMLSerializer();
    }
    Window loadedWindow = null;
    try {
        URL url = new URL(urlString);
        // force the location, so it will be possible to decode resources
        // like labels ...
        serializer.setLocation(url);
        loadedWindow = (Window) serializer.readObject(url);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (SerializationException e) {
        e.printStackTrace();
    }
    return loadedWindow;
}
Also used : Window(org.apache.pivot.wtk.Window) MalformedURLException(java.net.MalformedURLException) SerializationException(org.apache.pivot.serialization.SerializationException) IOException(java.io.IOException) BXMLSerializer(org.apache.pivot.beans.BXMLSerializer) URL(java.net.URL)

Aggregations

SerializationException (org.apache.pivot.serialization.SerializationException)49 IOException (java.io.IOException)28 BXMLSerializer (org.apache.pivot.beans.BXMLSerializer)14 URL (java.net.URL)9 JSONSerializer (org.apache.pivot.json.JSONSerializer)9 ArrayList (org.apache.pivot.collections.ArrayList)8 Component (org.apache.pivot.wtk.Component)7 File (java.io.File)6 List (org.apache.pivot.collections.List)6 QueryException (org.apache.pivot.web.QueryException)6 ComponentMouseButtonListener (org.apache.pivot.wtk.ComponentMouseButtonListener)6 InputStream (java.io.InputStream)5 Sequence (org.apache.pivot.collections.Sequence)5 Button (org.apache.pivot.wtk.Button)5 ButtonPressListener (org.apache.pivot.wtk.ButtonPressListener)5 Mouse (org.apache.pivot.wtk.Mouse)5 PushButton (org.apache.pivot.wtk.PushButton)5 MalformedURLException (java.net.MalformedURLException)4 ScriptException (javax.script.ScriptException)4 TextInput (org.apache.pivot.wtk.TextInput)4