Search in sources :

Example 1 with RepositorySelectionDialog

use of org.netxms.ui.eclipse.serverconfig.dialogs.RepositorySelectionDialog in project netxms by netxms.

the class ExportFileBuilder method publishStage2.

/**
 * Publish configuration - stage 2
 *
 * @param repositories
 */
private void publishStage2(List<Repository> repositories) {
    RepositorySelectionDialog dlg = new RepositorySelectionDialog(getSite().getShell(), repositories);
    if (dlg.open() != Window.OK)
        return;
    final Repository repository = dlg.getSelection();
    doExport(new ExportCompletionHandler() {

        @Override
        public void exportCompleted(final String xml) {
            new ConsoleJob("Publish configuration", ExportFileBuilder.this, Activator.PLUGIN_ID, null) {

                @Override
                protected void runInternal(IProgressMonitor monitor) throws Exception {
                    URL url = new URL(repository.getUrl() + "/rest-api/push-export?accessToken=" + repository.getAuthToken());
                    URLConnection conn = url.openConnection();
                    if (!(conn instanceof HttpURLConnection)) {
                        throw new Exception("Unsupported URL type");
                    }
                    ((HttpURLConnection) conn).setRequestMethod("POST");
                    ((HttpURLConnection) conn).setRequestProperty("User-Agent", "NetXMS Console/" + NXCommon.VERSION);
                    ((HttpURLConnection) conn).setRequestProperty("Content-Type", "application/xml; charset=utf-8");
                    ((HttpURLConnection) conn).setDoOutput(true);
                    ((HttpURLConnection) conn).setAllowUserInteraction(false);
                    ((HttpURLConnection) conn).setUseCaches(false);
                    OutputStream out = conn.getOutputStream();
                    try {
                        out.write(xml.getBytes("UTF-8"));
                        out.flush();
                        int responseCode = ((HttpURLConnection) conn).getResponseCode();
                        Activator.logInfo("Publish config: url=" + url.toString() + " response=" + responseCode);
                        if (responseCode != 200) {
                            throw new Exception(String.format("HTTP error %d", responseCode));
                        }
                    } finally {
                        out.close();
                    }
                }

                @Override
                protected String getErrorMessage() {
                    return "Cannot publish configuration to repository";
                }
            }.start();
        }
    });
}
Also used : OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) RepositorySelectionDialog(org.netxms.ui.eclipse.serverconfig.dialogs.RepositorySelectionDialog) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) Repository(org.netxms.client.market.Repository) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) HttpURLConnection(java.net.HttpURLConnection) ConsoleJob(org.netxms.ui.eclipse.jobs.ConsoleJob)

Aggregations

FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1 HttpURLConnection (java.net.HttpURLConnection)1 URL (java.net.URL)1 URLConnection (java.net.URLConnection)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 Repository (org.netxms.client.market.Repository)1 ConsoleJob (org.netxms.ui.eclipse.jobs.ConsoleJob)1 RepositorySelectionDialog (org.netxms.ui.eclipse.serverconfig.dialogs.RepositorySelectionDialog)1