Skip to content

Commit

Permalink
517221: Initialize Import Favorites with random favorites lists of ot…
Browse files Browse the repository at this point in the history
…her users

Allow api.eclipse.org favorite URLs in d&d handler filter

Bug: 517221
Task-Url: https://bugs.eclipse.org/bugs/show_bug.cgi?id=517221
  • Loading branch information
creckord committed Jun 14, 2017
1 parent ca12ba8 commit d24c5e1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ public class UserFavoritesService extends AbstractDataStorageService implements
private static final Pattern JSON_OWNER_PROFILE_URL_ATTRIBUTE_PATTERN = Pattern
.compile(String.format(JSON_ATTRIBUTE_REGEX, "html_profile_url"), Pattern.MULTILINE); //$NON-NLS-1$

public static final Pattern FAVORITES_URL_PATTERN = Pattern
.compile("(?:^|/)user/([^/#?]+)(/favorites)?([/#?].*)?$"); //$NON-NLS-1$

private static final String KEY = "mpc_favorites"; //$NON-NLS-1$

private static final int RETRY_COUNT = 3;
Expand Down Expand Up @@ -238,6 +241,7 @@ private static String getAttribute(Pattern attributePattern, String attributeNam

private String getFavoritesListUrl(String entryBody, String id) {
String marketplaceBaseUri = getMarketplaceBaseUri();
//We use the HTML URL shown in the web frontend instead of the API URL, because that's what's advertised
String explicitUrl = getAttribute(JSON_LIST_URL_ATTRIBUTE_PATTERN, null, entryBody);
if (explicitUrl != null && explicitUrl.trim().length() > 0) {
try {
Expand Down Expand Up @@ -459,7 +463,24 @@ public List<INode> getFavorites(URI uri, IProgressMonitor monitor) throws IOExce
return toNodes(nodeIds);
}

public static void validateURI(URI uri) {
private URI normalizeURI(URI uri) {
validateUri(uri);
String marketplaceBaseUri = getMarketplaceBaseUri();
marketplaceBaseUri = URLUtil.appendPath(marketplaceBaseUri, ""); //$NON-NLS-1$
marketplaceBaseUri = URLUtil.setScheme(marketplaceBaseUri, uri.getScheme());
if (!uri.toString().startsWith(marketplaceBaseUri)) {
return uri;
}
Matcher matcher = FAVORITES_URL_PATTERN.matcher(uri.toString());
if (matcher.find()) {
String name = matcher.group(1);
return getStorageService().getServiceUri()
.resolve("marketplace/favorites/?name=" + URLUtil.urlEncode(name));
}
return uri;
}

public static void validateUri(URI uri) {
if ("".equals(uri.toString()) //$NON-NLS-1$
|| ((uri.getHost() == null || "".equals(uri.getHost())) //$NON-NLS-1$
&& (uri.getScheme() != null && uri.getScheme().toLowerCase().startsWith("http"))) //$NON-NLS-1$
Expand All @@ -471,9 +492,9 @@ public static void validateURI(URI uri) {
}

public List<String> getFavoriteIds(final URI uri, IProgressMonitor monitor) throws IOException {
validateURI(uri);
URI normalizedUri = normalizeURI(uri);
try {
return new AbstractJSONListRequest<String>(uri, JSON_MPC_FAVORITES_PATTERN) {
return new AbstractJSONListRequest<String>(normalizedUri, JSON_MPC_FAVORITES_PATTERN) {

@Override
protected String parseListElement(String listElement) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,12 @@ public static String urlDecode(String path) {
return path;
}
}

public static String setScheme(String url, String scheme) {
int schemeSeparator = url.indexOf(":"); //$NON-NLS-1$
if (schemeSeparator == -1) {
throw new IllegalArgumentException();
}
return scheme + url.substring(schemeSeparator);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.eclipse.epp.internal.mpc.core.MarketplaceClientCore;
import org.eclipse.epp.internal.mpc.core.model.Node;
import org.eclipse.epp.internal.mpc.core.service.DefaultMarketplaceService;
import org.eclipse.epp.internal.mpc.core.service.UserFavoritesService;
import org.eclipse.epp.internal.mpc.core.util.URLUtil;
import org.eclipse.epp.internal.mpc.ui.CatalogRegistry;
import org.eclipse.epp.internal.mpc.ui.MarketplaceClientUi;
Expand Down Expand Up @@ -62,8 +63,10 @@ public abstract class MarketplaceUrlHandler {

private static final Pattern NODE_URL_PATTERN = Pattern.compile("(?:^|/)node/([^/#?]+)"); //$NON-NLS-1$

private static final Pattern FAVORITES_URL_PATTERN = Pattern
.compile("(?:^|/)user/([^/#?]+)(/favorites)?([/#?].*)?$"); //$NON-NLS-1$
private static final Pattern FAVORITES_URL_PATTERN = UserFavoritesService.FAVORITES_URL_PATTERN;

private static final Pattern FAVORITES_API_URL_PATTERN = Pattern
.compile("(?:^|/)marketplace/favorites/?(?:\\?(?:[^#]*&)name=.*)?$"); //$NON-NLS-1$

public static class SolutionInstallationInfo {

Expand Down Expand Up @@ -190,7 +193,8 @@ public static boolean isPotentialSolution(String url) {
}

public static boolean isPotentialFavoritesList(String url) {
return url != null && FAVORITES_URL_PATTERN.matcher(url).find();
return url != null
&& (FAVORITES_URL_PATTERN.matcher(url).find() || FAVORITES_API_URL_PATTERN.matcher(url).find());
}

public static void triggerInstall(SolutionInstallationInfo info) {
Expand Down

0 comments on commit d24c5e1

Please sign in to comment.