Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWim Jongman2018-05-11 17:55:08 +0000
committerWim Jongman2018-05-11 17:57:39 +0000
commit17355c81a7dc85dd97206a735c95180fe94b38f0 (patch)
tree2bbc9d6e5853d0d6e14cd85efabd5e999bba79df /org.eclipse.tips.json
parentb6400463660b9910904b064bd27d72bbe0c100ec (diff)
downloadeclipse.platform.ua-17355c81a7dc85dd97206a735c95180fe94b38f0.tar.gz
eclipse.platform.ua-17355c81a7dc85dd97206a735c95180fe94b38f0.tar.xz
eclipse.platform.ua-17355c81a7dc85dd97206a735c95180fe94b38f0.zip
Bug 534178: [Tips] Tip deploy job
* Abandoned internet download due to wontfix * Fixed warnings Change-Id: I93487e25b6cc179f20fb0cf7a1f3975bd16eeaf9 Signed-off-by: Wim Jongman <wim.jongman@remainsoftware.com>
Diffstat (limited to 'org.eclipse.tips.json')
-rw-r--r--org.eclipse.tips.json/src/org/eclipse/tips/json/JsonTipProvider.java38
-rw-r--r--org.eclipse.tips.json/src/org/eclipse/tips/json/internal/ProviderLoader.java177
-rw-r--r--org.eclipse.tips.json/src/org/eclipse/tips/json/internal/Util.java57
3 files changed, 61 insertions, 211 deletions
diff --git a/org.eclipse.tips.json/src/org/eclipse/tips/json/JsonTipProvider.java b/org.eclipse.tips.json/src/org/eclipse/tips/json/JsonTipProvider.java
index 3a1a8dd78..566821da7 100644
--- a/org.eclipse.tips.json/src/org/eclipse/tips/json/JsonTipProvider.java
+++ b/org.eclipse.tips.json/src/org/eclipse/tips/json/JsonTipProvider.java
@@ -62,37 +62,14 @@ public abstract class JsonTipProvider extends TipProvider {
}
/**
- * Returns a specific portion of the underlying json file as a json object, if
- * the json object was not yet fetched it will be done here, making it a
- * potential costly operation. The passed part can be an array to indicate a
- * structure e.g. {"provider","variables"}.
- *
- * @param part one or more keys of the underlying json file, may not be null.
- * @return the JSon Object as a string
- * @throws IOException
- * @see {@link #loadNewTips(IProgressMonitor)}
- */
- public synchronized String getJsonObject(String... part) throws IOException {
- if (fJsonObject == null) {
- fJsonObject = loadJsonObject();
- }
- JsonObject temp = fJsonObject.getAsJsonObject(part[0]);
- for (int i = 1; i < part.length; i++) {
- temp = temp.getAsJsonObject(part[0]);
-
- }
- return temp.getAsString();
- }
-
- /**
*
* {@inheritDoc}
- *
+ *
* <p>
* <b>Implementation Details</b><br>
* The implementation of this method in this provider will fetch the json file
* and store it locally.
- *
+ *
*/
@Override
public synchronized IStatus loadNewTips(IProgressMonitor monitor) {
@@ -101,6 +78,10 @@ public abstract class JsonTipProvider extends TipProvider {
try {
subMonitor.beginTask(getDescription() + SPACE + Messages.JsonTipProvider_1, -1);
fJsonObject = loadJsonObject();
+ if (fJsonObject == null) {
+ return new Status(IStatus.INFO, "org.eclipse.tips.json",
+ MessageFormat.format("Could not parse json for {0}. Cache invalidated.", getID()), null);
+ }
JsonObject provider = fJsonObject.getAsJsonObject(JsonConstants.P_PROVIDER);
fDescription = Util.getValueOrDefault(provider, JsonConstants.P_DESCRIPTION, "not set"); //$NON-NLS-1$
fImage = Util.getValueOrDefault(provider, JsonConstants.P_IMAGE, null);
@@ -121,7 +102,12 @@ public abstract class JsonTipProvider extends TipProvider {
private JsonObject loadJsonObject() throws IOException {
try (InputStream stream = fJsonUrl.openStream(); InputStreamReader reader = new InputStreamReader(stream)) {
- return (JsonObject) new JsonParser().parse(reader);
+ Object result = new JsonParser().parse(reader);
+ if (result instanceof JsonObject) {
+ return (JsonObject) result;
+ } else {
+ return null;
+ }
}
}
diff --git a/org.eclipse.tips.json/src/org/eclipse/tips/json/internal/ProviderLoader.java b/org.eclipse.tips.json/src/org/eclipse/tips/json/internal/ProviderLoader.java
deleted file mode 100644
index 3874ad03d..000000000
--- a/org.eclipse.tips.json/src/org/eclipse/tips/json/internal/ProviderLoader.java
+++ /dev/null
@@ -1,177 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2018 Remain Software
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * wim.jongman@remainsoftware.com - initial API and implementation
- *******************************************************************************/
-package org.eclipse.tips.json.internal;
-
-import java.io.BufferedReader;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.io.FileReader;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.net.MalformedURLException;
-import java.net.URL;
-import java.nio.file.Files;
-import java.nio.file.StandardCopyOption;
-
-import org.eclipse.core.runtime.IProgressMonitor;
-import org.eclipse.core.runtime.IStatus;
-import org.eclipse.core.runtime.Platform;
-import org.eclipse.core.runtime.Status;
-import org.eclipse.tips.core.ITipManager;
-import org.eclipse.tips.core.internal.LogUtil;
-import org.eclipse.tips.json.JsonTipProvider;
-import org.osgi.framework.Bundle;
-import org.osgi.framework.FrameworkUtil;
-
-import com.google.gson.JsonArray;
-import com.google.gson.JsonElement;
-import com.google.gson.JsonObject;
-import com.google.gson.JsonParser;
-
-/**
- * A helper class to load providers from an internet location.
- *
- */
-@SuppressWarnings("restriction")
-public class ProviderLoader {
-
- /**
- * This method loads provider information from the internet and stores it
- * locally. This method should not be called in the user interface thread.
- *
- * @param pManager the tip manager
- * @param baseURL the location of the providers.json file
- * @param stateLocation the place to store state
- */
- public static void loadProviderData(ITipManager pManager, String baseURL, File stateLocation) {
- loadProviders(pManager, baseURL, stateLocation);
- }
-
- private static void loadProviders(ITipManager pManager, String pBaseURL, File stateLocation) {
- try {
- URL webFile = new URL(pBaseURL + "index.json"); //$NON-NLS-1$
- File target = new File(stateLocation, "index.json"); //$NON-NLS-1$
- try (InputStream in = webFile.openStream()) {
- Files.copy(in, target.toPath(), StandardCopyOption.REPLACE_EXISTING);
- }
- pManager.log(LogUtil.info(String.format(Messages.ProviderLoader_1, target.toPath())));
- createProviders(pManager, target, pBaseURL, stateLocation);
- } catch (Exception e) {
- String symbolicName = FrameworkUtil.getBundle(ProviderLoader.class).getSymbolicName();
- pManager.log(new Status(IStatus.ERROR, symbolicName, Messages.ProviderLoader_3, e));
- }
- }
-
- private static void createProviders(ITipManager pManager, File pTarget, String pBaseURL, File stateLocation)
- throws Exception {
- try (FileReader reader = new FileReader(pTarget)) {
- JsonObject value = (JsonObject) new JsonParser().parse(reader);
- JsonArray providers = value.getAsJsonArray(JsonConstants.P_PROVIDER);
- providers.forEach(provider -> loadProvider(pManager, provider, pBaseURL, stateLocation));
- }
- }
-
- private static void loadProvider(ITipManager pManager, JsonElement pProvider, String pBaseURL, File userLocation) {
- JsonObject provider = pProvider.getAsJsonObject();
- String version = Util.getValueOrDefault(provider, "version", null); //$NON-NLS-1$
- String location = Util.getValueOrDefault(provider, "location", null); //$NON-NLS-1$
- String bundleName = Util.getValueOrDefault(provider, "require-bundle", null); //$NON-NLS-1$
- if (version == null || bundleName == null) {
- logInvalidProvider(pManager, provider);
- return;
- }
- pManager.log(LogUtil
- .info(String.format(Messages.ProviderLoader_0,
- location, version, bundleName)));
- Bundle bundle = Platform.getBundle(bundleName);
- if (bundle == null) {
- logInvalidProvider(pManager, provider);
- return;
- }
-
- try {
- File fileLocation = new File(userLocation, bundleName);
- if (!fileLocation.exists()) {
- fileLocation.mkdirs();
- }
-
- File versionFile = new File(fileLocation, "version.txt"); //$NON-NLS-1$
- if (!versionFile.exists()) {
- versionFile.createNewFile();
- try (FileOutputStream fos = new FileOutputStream(versionFile)) {
- fos.write(version.getBytes());
- }
- }
-
- String existingVersion = getFileContent(versionFile);
- File providerFile = new File(fileLocation, "provider.json"); //$NON-NLS-1$
- if (!version.equals(existingVersion) || !providerFile.exists()) {
- pManager.log(LogUtil.info(String.format(Messages.ProviderLoader_10, existingVersion, version)));
- URL webFile = new URL(pBaseURL + "/" + location); //$NON-NLS-1$
- try (InputStream in = webFile.openStream()) {
- Files.copy(in, providerFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
- }
- try (FileOutputStream fos = new FileOutputStream(versionFile)) {
- fos.write(version.getBytes());
- }
- }
- registerProvider(pManager, bundleName, fileLocation);
-
- } catch (IOException e) {
- String symbolicName = FrameworkUtil.getBundle(ProviderLoader.class).getSymbolicName();
- pManager.log(
- new Status(IStatus.ERROR, symbolicName,
- String.format(Messages.ProviderLoader_2, pProvider.toString()), e));
- }
- }
-
- private static void registerProvider(ITipManager pManager, String bundleName, File pFileLocation)
- throws IOException, MalformedURLException {
- JsonTipProvider tipProvider = new JsonTipProvider() {
-
- @Override
- public String getID() {
- return bundleName + ".json.provider"; //$NON-NLS-1$
- }
-
- @Override
- public synchronized IStatus loadNewTips(IProgressMonitor pMonitor) {
- getManager().log(LogUtil.info(String.format(Messages.ProviderLoader_14, getID())));
- IStatus status = super.loadNewTips(pMonitor);
- getManager().log(status);
- getManager().log(LogUtil.info(String.format(Messages.ProviderLoader_15, getID())));
- return status;
- }
- };
-
- File providerFile = new File(pFileLocation, "provider.json"); //$NON-NLS-1$
- String fileLocation = providerFile.toURI().toURL().toString();
- tipProvider.setJsonUrl(fileLocation);
- pManager.log(LogUtil.info(String.format(Messages.ProviderLoader_17, fileLocation)));
- pManager.register(tipProvider);
- }
-
- public static String getFileContent(File pVersionFile) throws IOException {
- try (FileInputStream fis = new FileInputStream(pVersionFile)) {
- try (BufferedReader br = new BufferedReader(new InputStreamReader(fis))) {
- return br.readLine();
- }
- }
- }
-
- private static void logInvalidProvider(ITipManager pManager, JsonObject pProvider) {
- String symbolicName = FrameworkUtil.getBundle(ProviderLoader.class).getSymbolicName();
- pManager.log(new Status(IStatus.ERROR, symbolicName,
- String.format(Messages.ProviderLoader_4, pProvider.toString())));
- }
-} \ No newline at end of file
diff --git a/org.eclipse.tips.json/src/org/eclipse/tips/json/internal/Util.java b/org.eclipse.tips.json/src/org/eclipse/tips/json/internal/Util.java
index af7ae80ee..04bee890e 100644
--- a/org.eclipse.tips.json/src/org/eclipse/tips/json/internal/Util.java
+++ b/org.eclipse.tips.json/src/org/eclipse/tips/json/internal/Util.java
@@ -4,8 +4,15 @@ import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.text.MessageFormat;
import java.util.Map.Entry;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.osgi.framework.FrameworkUtil;
+
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
@@ -14,7 +21,7 @@ public class Util {
/**
* Parses the passed json or returns a default value.
- *
+ *
* @param jsonObject
* @param element the value to return in case the jsonObject does not
* contain the specified value.
@@ -31,7 +38,7 @@ public class Util {
/**
* Parses the passed json or returns a default value.
- *
+ *
* @param jsonObject
* @param element the value to return in case the jsonObject does not
* contain the specified value.
@@ -48,7 +55,7 @@ public class Util {
/**
* Parses the passed json or returns a default value.
- *
+ *
* @param jsonObject
* @param element the value to return in case the jsonObject does not
* contain the specified value.
@@ -69,13 +76,13 @@ public class Util {
* {@link JsonConstants#T_VARIABLES} object then this is parsed as well.
* <p>
* Example:
- *
+ *
* <pre>
- * json object: {"first": "Wim", "last": "Jongman", "variables": {"title": "Mr.", "age": 53}}
+ * json object: {"first": "Wim", "last": "Jongman", "variables": {"title": "Mr.", "age": 53}}
* input: "${title} ${first} ${last} is ${age} years old."
* output: "Mr. Wim Jongman is 53 years old"
* </pre>
- *
+ *
* @param object the input json object
* @param input the string to scan
* @return the replaced string
@@ -108,13 +115,47 @@ public class Util {
/**
* @param input the json string representation
- * @return the parsed json object
+ * @return the parsed json object or null if a json object could not be found in
+ * the string
* @throws IOException
*/
public static JsonObject getJson(String input) throws IOException {
try (InputStream stream = new ByteArrayInputStream(input.getBytes());
InputStreamReader reader = new InputStreamReader(stream)) {
- return (JsonObject) new JsonParser().parse(reader);
+ JsonElement element = new JsonParser().parse(reader);
+ if (element instanceof JsonObject) {
+ return (JsonObject) element;
+ } else {
+ return null;
+ }
+ }
+ }
+
+ /**
+ * Checks if the URL is valid.
+ *
+ * @param pUrl
+ * @return A status indicating the result.
+ * @throws IOException
+ */
+ public static IStatus isValidUrl(String pUrl) {
+ String symbolicName = FrameworkUtil.getBundle(Util.class).getSymbolicName();
+ try {
+ URL url = new URL(pUrl);
+ HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
+ int responseCode = httpCon.getResponseCode();
+ if (responseCode != 200) {
+ return new Status(IStatus.ERROR, symbolicName, MessageFormat
+ .format("Received response code {0} from {1}.", new Object[] { responseCode + "", pUrl }));
+ }
+ if (httpCon.getContentLength() <= 0) {
+ return new Status(IStatus.ERROR, symbolicName,
+ MessageFormat.format("Received empty file from {0}.", pUrl));
+ }
+ } catch (Exception e) {
+ return new Status(IStatus.ERROR, symbolicName, MessageFormat.format("Received empty file from {0}.", pUrl),
+ e);
}
+ return Status.OK_STATUS;
}
}

Back to the top