diff --git a/DiscordBeam/build.gradle.kts b/DiscordBeam/build.gradle.kts index f7ba7ed..58b206c 100644 --- a/DiscordBeam/build.gradle.kts +++ b/DiscordBeam/build.gradle.kts @@ -1,6 +1,6 @@ import org.jetbrains.changelog.markdownToHTML -version = "1.20.6" +version = "1.20.7" group = "de.lukweb.share" plugins { @@ -36,6 +36,9 @@ tasks { patchPluginXml { changeNotes.set(provider { """ + 1.20.7: + * Prepares for 2023.2 release + 1.20.6: * Prepares for 2023.1 release diff --git a/DiscordBeam/src/main/java/de/lukweb/discordbeam/DiscordSettingsPage.java b/DiscordBeam/src/main/java/de/lukweb/discordbeam/DiscordSettingsPage.java index 63fc4e3..504b04f 100644 --- a/DiscordBeam/src/main/java/de/lukweb/discordbeam/DiscordSettingsPage.java +++ b/DiscordBeam/src/main/java/de/lukweb/discordbeam/DiscordSettingsPage.java @@ -41,7 +41,7 @@ public class DiscordSettingsPage implements SearchableConfigurable { public DiscordSettingsPage() { this.disposable = Disposer.newDisposable(); - Disposer.register(ApplicationManager.getApplication().getService(DiscordSettings.class), disposable); + Disposer.register(DiscordSettings.getInstance(), disposable); } @NotNull diff --git a/DiscordBeam/src/main/java/de/lukweb/discordbeam/uploaders/HastebinUploaderImpl.java b/DiscordBeam/src/main/java/de/lukweb/discordbeam/uploaders/HastebinUploaderImpl.java index 0ec5a97..8931c71 100644 --- a/DiscordBeam/src/main/java/de/lukweb/discordbeam/uploaders/HastebinUploaderImpl.java +++ b/DiscordBeam/src/main/java/de/lukweb/discordbeam/uploaders/HastebinUploaderImpl.java @@ -27,6 +27,12 @@ public void onHaste(String hasteUrl) { latch.countDown(); } + @Override + public void onAuthorizationRequired() { + theError.set(new IOException("hastebin requires an API key, configure it in the 'Haste It' settings tab")); + latch.countDown(); + } + @Override public void onFailure(Throwable ex) { theError.set(new IOException(ex)); diff --git a/HasteIt/build.gradle.kts b/HasteIt/build.gradle.kts index bb965ae..f27b5d3 100644 --- a/HasteIt/build.gradle.kts +++ b/HasteIt/build.gradle.kts @@ -1,6 +1,6 @@ import org.jetbrains.changelog.markdownToHTML -version = "1.20.6" +version = "1.20.7" group = "de.lukweb.share" plugins { @@ -36,6 +36,10 @@ tasks { patchPluginXml { changeNotes.set(provider { """ + 1.20.7: + * Prepares for 2023.2 release + * Implements a new setting to provide an API key for hastebin + 1.20.6: * Prepares for 2023.1 release diff --git a/HasteIt/src/main/java/de/lukweb/hasteit/HasteMenu.java b/HasteIt/src/main/java/de/lukweb/hasteit/HasteMenu.java index 1755222..448a6ea 100644 --- a/HasteIt/src/main/java/de/lukweb/hasteit/HasteMenu.java +++ b/HasteIt/src/main/java/de/lukweb/hasteit/HasteMenu.java @@ -36,6 +36,11 @@ public void onHaste(String hasteUrl) { uploadSuccessNotification(hasteUrl, fileName, project); } + @Override + public void onAuthorizationRequired() { + errorNotification("Configure an API key for hastebin in the settings."); + } + @Override public void onFailure(Throwable ex) { errorNotification(ex.getClass().getName() + " while uploading: " + ex.getMessage()); diff --git a/HasteIt/src/main/java/de/lukweb/hasteit/HasteSettings.java b/HasteIt/src/main/java/de/lukweb/hasteit/HasteSettings.java index f64f2d6..b6a0965 100644 --- a/HasteIt/src/main/java/de/lukweb/hasteit/HasteSettings.java +++ b/HasteIt/src/main/java/de/lukweb/hasteit/HasteSettings.java @@ -53,7 +53,23 @@ public String computeUploadURL() { } public String computeFileURL(String hasteCode, String extension) { + if (getBaseURL().equals(DEFAULT_URL)) { + return getBaseURL() + "/share/" + hasteCode + "." + extension; + } + return getBaseURL() + "/" + hasteCode + "." + extension; } + public String getAPIKey() { + return getState().getAPIKey(); + } + + public String getAPIKeyOrEmpty() { + if (getAPIKey() == null) { + return ""; + } else { + return getAPIKey(); + } + } + } diff --git a/HasteIt/src/main/java/de/lukweb/hasteit/HasteSettingsPage.form b/HasteIt/src/main/java/de/lukweb/hasteit/HasteSettingsPage.form index c6d25bc..415f5b4 100644 --- a/HasteIt/src/main/java/de/lukweb/hasteit/HasteSettingsPage.form +++ b/HasteIt/src/main/java/de/lukweb/hasteit/HasteSettingsPage.form @@ -1,6 +1,6 @@
- + @@ -10,7 +10,7 @@ - + @@ -40,6 +40,33 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/HasteIt/src/main/java/de/lukweb/hasteit/HasteSettingsPage.java b/HasteIt/src/main/java/de/lukweb/hasteit/HasteSettingsPage.java index eacda77..a4f6cab 100644 --- a/HasteIt/src/main/java/de/lukweb/hasteit/HasteSettingsPage.java +++ b/HasteIt/src/main/java/de/lukweb/hasteit/HasteSettingsPage.java @@ -1,7 +1,6 @@ package de.lukweb.hasteit; import com.intellij.openapi.Disposable; -import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.options.Configurable; import com.intellij.openapi.options.ConfigurationException; import com.intellij.openapi.options.SearchableConfigurable; @@ -16,6 +15,10 @@ import javax.swing.*; import javax.swing.event.DocumentEvent; +import java.net.MalformedURLException; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; public class HasteSettingsPage implements SearchableConfigurable { @@ -25,10 +28,13 @@ public class HasteSettingsPage implements SearchableConfigurable { private JLabel labelHasteUrl; private JBTextField textHasteUrl; private JButton buttonReset; + private JLabel labelApiKey; + private JBTextField textApiKey; + private JButton buttonGetApiKey; public HasteSettingsPage() { disposable = Disposer.newDisposable(); - Disposer.register(ApplicationManager.getApplication().getService(HasteSettings.class), disposable); + Disposer.register(HasteSettings.getInstance(), disposable); } @Override @@ -59,6 +65,22 @@ protected void textChanged(@NotNull DocumentEvent e) { buttonReset.addActionListener(l -> textHasteUrl.setText(HasteSettings.DEFAULT_URL)); + labelApiKey.setLabelFor(textApiKey); + textApiKey.getDocument().addDocumentListener(new DocumentAdapter() { + @Override + protected void textChanged(@NotNull DocumentEvent e) { + updateResetButton(); + } + }); + buttonGetApiKey.addActionListener(l -> { + try { + URL docUrl = new URI("https://www.toptal.com/developers/hastebin/documentation").toURL(); + ShareWebTools.openURL(docUrl); + } catch (URISyntaxException | MalformedURLException e) { + throw new RuntimeException(e); + } + }); + reset(); updateResetButton(); @@ -71,7 +93,12 @@ public JComponent getPreferredFocusedComponent() { } private void updateResetButton() { - boolean canReset = !textHasteUrl.getText().equals(textHasteUrl.getEmptyText().getText()); + HasteSettings settings = HasteSettings.getInstance(); + + boolean canReset = + !textHasteUrl.getText().equals(textHasteUrl.getEmptyText().getText()) || + !textApiKey.getText().equals(settings.getAPIKeyOrEmpty()); + buttonReset.setEnabled(canReset); } @@ -83,6 +110,10 @@ public boolean isModified() { return true; } + if (Configurable.isFieldModified(textApiKey, settingsState.getAPIKeyOrEmpty())) { + return true; + } + return false; } @@ -90,6 +121,7 @@ public boolean isModified() { public void reset() { HasteSettings settings = HasteSettings.getInstance(); textHasteUrl.setText(settings.getBaseURL()); + textApiKey.setText(settings.getAPIKeyOrEmpty()); } @Override @@ -108,6 +140,12 @@ public void apply() throws ConfigurationException { settingsState.setBaseURL(HasteSettings.DEFAULT_URL); reset(); } + + if (textApiKey.getText().isEmpty()) { + settingsState.getState().setAPIKey(null); + } else { + settingsState.getState().setAPIKey(textApiKey.getText()); + } } @Override diff --git a/HasteIt/src/main/java/de/lukweb/hasteit/HasteSettingsState.java b/HasteIt/src/main/java/de/lukweb/hasteit/HasteSettingsState.java index d566e8d..298a7f4 100644 --- a/HasteIt/src/main/java/de/lukweb/hasteit/HasteSettingsState.java +++ b/HasteIt/src/main/java/de/lukweb/hasteit/HasteSettingsState.java @@ -5,6 +5,7 @@ public class HasteSettingsState implements ShareSettingsState { private String customUrl = null; + private String apiKey = null; public String getCustomUrl() { return customUrl; @@ -13,4 +14,12 @@ public String getCustomUrl() { public void setCustomUrl(String customUrl) { this.customUrl = customUrl; } + + public String getAPIKey() { + return apiKey; + } + + public void setAPIKey(String apiKey) { + this.apiKey = apiKey; + } } diff --git a/HasteIt/src/main/java/de/lukweb/hasteit/HasteUploader.java b/HasteIt/src/main/java/de/lukweb/hasteit/HasteUploader.java index 653ef0c..4f6e896 100644 --- a/HasteIt/src/main/java/de/lukweb/hasteit/HasteUploader.java +++ b/HasteIt/src/main/java/de/lukweb/hasteit/HasteUploader.java @@ -30,6 +30,9 @@ public void upload(String content, String extension, HasteResult result) { con.setRequestMethod("POST"); con.setRequestProperty("User-Agent", "Mozilla/5.0"); con.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); + if (settings.getAPIKey() != null) { + con.setRequestProperty("Authorization", "Bearer " + settings.getAPIKey()); + } // Send the post request con.setDoOutput(true); @@ -38,6 +41,11 @@ public void upload(String content, String extension, HasteResult result) { wr.flush(); wr.close(); + if (con.getResponseCode() == 401) { + result.onAuthorizationRequired(); + return; + } + BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; StringBuilder response = new StringBuilder(); @@ -61,6 +69,8 @@ public void upload(String content, String extension, HasteResult result) { public interface HasteResult extends ShareResult { + void onAuthorizationRequired(); + void onHaste(String hasteUrl); }