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

- Fixed property matcher for full list owner name
- Show owner picture as icon on list entry (if returned by REST
API)
- Make owner name a link to owner's profile page (if returned by REST
API)

Bug: 517221
Task-Url: https://bugs.eclipse.org/bugs/show_bug.cgi?id=517221
  • Loading branch information
creckord committed Jun 14, 2017
1 parent 820e31b commit ca12ba8
Show file tree
Hide file tree
Showing 13 changed files with 235 additions and 65 deletions.
2 changes: 2 additions & 0 deletions org.eclipse.epp.mpc.core/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ Import-Package: org.apache.http;version="4.4.0",
org.apache.http.conn.params;version="4.5.2",
org.apache.http.conn.socket;version="4.5.2",
org.apache.http.conn.ssl;version="4.5.2",
org.apache.http.entity;version="4.4.0",
org.apache.http.impl.auth;version="4.5.2",
org.apache.http.impl.client;version="4.5.2",
org.apache.http.impl.conn;version="4.5.2",
org.apache.http.message;version="4.4.0",
org.apache.http.params;version="4.4.0",
org.apache.http.protocol;version="4.4.0",
org.apache.http.util;version="4.4.0",
org.eclipse.equinox.p2.core;version="2.0.0"
Bundle-ActivationPolicy: lazy
Bundle-Activator: org.eclipse.epp.internal.mpc.core.MarketplaceClientCorePlugin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public class FavoriteList extends Identifiable implements IFavoriteList {

private String owner;

private String ownerProfileUrl;

private String icon;

public String getOwner() {
return owner == null ? getId() : owner;
}
Expand All @@ -30,6 +34,22 @@ public void setOwner(String owner) {
this.owner = owner;
}

public String getOwnerProfileUrl() {
return ownerProfileUrl;
}

public void setOwnerProfileUrl(String ownerProfileUrl) {
this.ownerProfileUrl = ownerProfileUrl;
}

public String getIcon() {
return icon;
}

public void setIcon(String icon) {
this.icon = icon;
}

public List<INode> getNodes() {
return nodes;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand All @@ -39,6 +41,7 @@
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.epp.internal.mpc.core.MarketplaceClientCore;
import org.eclipse.epp.internal.mpc.core.model.FavoriteList;
import org.eclipse.epp.internal.mpc.core.transport.httpclient.RequestTemplate;
import org.eclipse.epp.internal.mpc.core.util.URLUtil;
Expand All @@ -48,6 +51,7 @@
import org.eclipse.epp.mpc.core.service.IUserFavoritesService;
import org.eclipse.epp.mpc.core.service.QueryHelper;
import org.eclipse.epp.mpc.core.service.ServiceHelper;
import org.eclipse.osgi.util.NLS;
import org.eclipse.userstorage.IBlob;
import org.eclipse.userstorage.internal.Session;
import org.eclipse.userstorage.internal.util.IOUtil;
Expand Down Expand Up @@ -89,8 +93,8 @@ public class UserFavoritesService extends AbstractDataStorageService implements
* Matches a single string attribute in a dict. Returns the matched attribute name in the first match group and the
* attribute value in the second.
*/
private static final String JSON_ATTRIBUTE_REGEX = "(?:[,\\{]|^)\\s*\"(" + TEMPLATE_VARIABLE //$NON-NLS-1$
+ ")\"\\s*:\\s*\"([^\"]*)\"\\s*(?:,|$)"; //$NON-NLS-1$
private static final String JSON_ATTRIBUTE_REGEX = "(?<=[,\\{]|^)\\s*\"(" + TEMPLATE_VARIABLE //$NON-NLS-1$
+ ")\"\\s*:\\s*\"([^\"]*)\"\\s*(?=[,\\}]|$)"; //$NON-NLS-1$

private static final Pattern JSON_MPC_FAVORITES_PATTERN = Pattern
.compile(String.format(JSON_ATTRIBUTE_OBJECT_LIST_REGEX, "mpc_favorites"), Pattern.MULTILINE); //$NON-NLS-1$
Expand All @@ -110,13 +114,14 @@ public class UserFavoritesService extends AbstractDataStorageService implements
private static final Pattern JSON_CONTENT_ID_ATTRIBUTE_PATTERN = Pattern
.compile(String.format(JSON_ATTRIBUTE_REGEX, "content_id"), Pattern.MULTILINE); //$NON-NLS-1$

private static final Pattern USER_MAIL_PATTERN = Pattern.compile(".+\\@.+"); //$NON-NLS-1$
private static final Pattern JSON_LIST_URL_ATTRIBUTE_PATTERN = Pattern
.compile(String.format(JSON_ATTRIBUTE_REGEX, "html_mpc_favorites_url"), Pattern.MULTILINE); //$NON-NLS-1$

private static final String FAVORITES_API__ENDPOINT = "/api/mpc_favorites?{0}={1}"; //$NON-NLS-1$
private static final Pattern JSON_OWNER_ICON_ATTRIBUTE_PATTERN = Pattern
.compile(String.format(JSON_ATTRIBUTE_REGEX, "picture"), Pattern.MULTILINE); //$NON-NLS-1$

private static final String FAVORITES_API__USER_MAIL = "mail"; //$NON-NLS-1$

private static final String FAVORITES_API__USER_NAME = "name"; //$NON-NLS-1$
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$

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

Expand Down Expand Up @@ -202,22 +207,54 @@ protected IFavoriteList parseListElement(String entryBody) {
if (label != null && (label.equals(id) || label.equals(owner))) {
label = null;
}
String favoritesListUrl = getFavoritesListUrl(id);
String favoritesListUrl = getFavoritesListUrl(entryBody, id);
if (favoritesListUrl == null) {
return null;
}
String icon = getAttribute(JSON_OWNER_ICON_ATTRIBUTE_PATTERN, null, entryBody);
String profileUrl = getAttribute(JSON_OWNER_PROFILE_URL_ATTRIBUTE_PATTERN, null, entryBody);
IFavoriteList favoritesByUserId = QueryHelper.favoritesByUserId(id);
((FavoriteList) favoritesByUserId).setOwner(owner);
((FavoriteList) favoritesByUserId).setOwnerProfileUrl(profileUrl);
((FavoriteList) favoritesByUserId).setName(label);
((FavoriteList) favoritesByUserId).setUrl(favoritesListUrl);
((FavoriteList) favoritesByUserId).setIcon(icon);
return favoritesByUserId;
}

}.execute(randomFavoritesUri);
}

private String getFavoritesListUrl(String id) {
private static String getAttribute(Pattern attributePattern, String attributeName, String entryBody) {
Matcher matcher = attributePattern.matcher(entryBody);
while (matcher.find()) {
String matchedName = matcher.group(1);
if (attributeName == null || attributeName.equals(matchedName)) {
return matcher.group(2);
}
}
return null;
}

private String getFavoritesListUrl(String entryBody, String id) {
String marketplaceBaseUri = getMarketplaceBaseUri();
String explicitUrl = getAttribute(JSON_LIST_URL_ATTRIBUTE_PATTERN, null, entryBody);
if (explicitUrl != null && explicitUrl.trim().length() > 0) {
try {
//Check that it's a valid URL
URL url = URLUtil.toURL(explicitUrl);
URI uri = url.toURI();
if (!uri.isAbsolute()) {
uri = new URI(marketplaceBaseUri).resolve(uri);
}
return uri.toURL().toString();
} catch (Exception ex) {
MarketplaceClientCore
.error(NLS.bind("Invalid list URL {0} for favorites list {1} - falling back to default URL",
explicitUrl, id),
ex);
}
}
String path = String.format(MARKETPLACE_USER_FAVORITES_ENDPOINT, URLUtil.encode(id));
return URLUtil.appendPath(marketplaceBaseUri, path);
}
Expand All @@ -235,19 +272,19 @@ private String getMarketplaceBaseUri() {
return DefaultMarketplaceService.DEFAULT_SERVICE_LOCATION;
}

protected String findFavoritesListOwner(String entryBody) {
private static String findFavoritesListOwner(String entryBody) {
return findFavoritesNameOrId(entryBody, JSON_OWNER_ATTRIBUTE_PATTERN);
}

private String findFavoritesListLabel(String entryBody) {
private static String findFavoritesListLabel(String entryBody) {
return findFavoritesNameOrId(entryBody, JSON_NAME_ATTRIBUTE_PATTERN);
}

private String findFavoritesListId(String entryBody) {
private static String findFavoritesListId(String entryBody) {
return findFavoritesNameOrId(entryBody, JSON_USER_ID_ATTRIBUTE_PATTERN);
}

private String findFavoritesNameOrId(String entryBody, Pattern pattern) {
private static String findFavoritesNameOrId(String entryBody, Pattern pattern) {
String result = null;
Matcher matcher = pattern.matcher(entryBody);
while (matcher.find()) {
Expand Down Expand Up @@ -453,22 +490,6 @@ protected String parseListElement(String listElement) {
}
}

private static String read(InputStream in) throws IOException {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
IOUtil.copy(in, baos);
return StringUtil.fromUTF(baos.toByteArray());
} catch (RuntimeException ex) {
Throwable cause = ex.getCause();
if (cause instanceof IOException) {
throw (IOException) cause;
}
throw ex;
} finally {
IOUtil.close(in);
}
}

private static ProtocolException malformedContentException(final URI endpoint, String body) {
return new ProtocolException("GET", endpoint, "1.1", MALFORMED_CONTENT_ERROR_CODE, //$NON-NLS-1$ //$NON-NLS-2$
"Malformed response content: " + body); //$NON-NLS-1$
Expand Down Expand Up @@ -569,12 +590,33 @@ protected Request configureRequest(Request request, URI uri) {
}

@Override
protected List<T> handleResponseStream(InputStream content) throws IOException {
String body = read(content);
protected List<T> handleResponseStream(InputStream content, Charset charset) throws IOException {
String body = read(content, charset);
body = body.trim();
return handleBody(uri, body);
}

private static String read(InputStream in, Charset charset) throws IOException {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
IOUtil.copy(in, baos);
byte[] bytes = baos.toByteArray();
if (bytes == null) {
return StringUtil.EMPTY;
}

return new String(bytes, charset == null ? StringUtil.UTF8 : charset.name());
} catch (RuntimeException ex) {
Throwable cause = ex.getCause();
if (cause instanceof IOException) {
throw (IOException) cause;
}
throw ex;
} finally {
IOUtil.close(in);
}
}

protected List<T> handleBody(final URI uri, String body) throws ProtocolException {
List<T> favoriteIds = new ArrayList<T>();
if (!"".equals(body)) { //$NON-NLS-1$
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.nio.charset.Charset;
import java.util.concurrent.TimeUnit;

import org.apache.http.HttpEntity;
Expand Down Expand Up @@ -130,12 +131,12 @@ protected InputStream handleResponse(Response response) throws ClientProtocolExc
HttpResponse returnResponse = response.returnResponse();
HttpEntity entity = returnResponse.getEntity();
StatusLine statusLine = returnResponse.getStatusLine();
handleResponseStatus(statusLine.getStatusCode(), statusLine.getReasonPhrase(), entity);
handleResponseStatus(statusLine.getStatusCode(), statusLine.getReasonPhrase());
return handleResponseEntity(entity);
}

@Override
protected InputStream handleResponseStream(InputStream content) throws IOException {
protected InputStream handleResponseStream(InputStream content, Charset charset) throws IOException {
return content;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.nio.charset.Charset;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
Expand All @@ -24,6 +25,9 @@
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.fluent.Request;
import org.apache.http.client.fluent.Response;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.entity.ContentType;
import org.apache.http.util.EntityUtils;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
Expand Down Expand Up @@ -103,10 +107,15 @@ protected T handleResponse(Response response) throws ClientProtocolException, IO
return response.handleResponse(new ResponseHandler<T>() {

public T handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
final StatusLine statusLine = response.getStatusLine();
final HttpEntity entity = response.getEntity();
handleResponseStatus(statusLine.getStatusCode(), statusLine.getReasonPhrase(), entity);
return handleResponseEntity(entity);
HttpEntity entity = null;
try {
final StatusLine statusLine = response.getStatusLine();
entity = response.getEntity();
handleResponseStatus(statusLine.getStatusCode(), statusLine.getReasonPhrase());
return handleResponseEntity(entity);
} finally {
closeResponse(response, entity);
}
}
});
}
Expand All @@ -115,16 +124,22 @@ protected T handleResponseEntity(HttpEntity entity) throws IOException {
if (entity == null) {
return handleEmptyResponse();
}
return handleResponseStream(entity.getContent());
Charset charset = null;
ContentType contentType = ContentType.get(entity);
if (contentType != null) {
charset = contentType.getCharset();
}

return handleResponseStream(entity.getContent(), charset);
}

protected abstract T handleResponseStream(InputStream content) throws IOException;
protected abstract T handleResponseStream(InputStream content, Charset charset) throws IOException;

protected T handleEmptyResponse() {
return null;
}

protected void handleResponseStatus(int statusCode, String reasonPhrase, HttpEntity entity)
protected void handleResponseStatus(int statusCode, String reasonPhrase)
throws IllegalStateException, IOException {
if (statusCode >= 300) {
if (statusCode == 404) {
Expand All @@ -133,4 +148,14 @@ protected void handleResponseStatus(int statusCode, String reasonPhrase, HttpEnt
throw new HttpResponseException(statusCode, reasonPhrase);
}
}

private static void closeResponse(HttpResponse response, final HttpEntity entity) throws IOException {
if (entity != null) {
EntityUtils.consumeQuietly(entity);
}
if (response instanceof CloseableHttpResponse) {
CloseableHttpResponse closeable = (CloseableHttpResponse) response;
closeable.close();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,9 @@
public interface IFavoriteList extends IIdentifiable {
String getOwner();

String getOwnerProfileUrl();

String getIcon();

List<INode> getNodes();
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,15 @@ public String getListUrl() {
return getFavoriteList().getUrl();
}

public String getOwnerProfileUrl() {
return getFavoriteList().getOwnerProfileUrl();
}

public String getOwner() {
String provider = getProvider();
if (provider != null) {
return provider;
}
return getFavoriteList().getOwner();
}
}
Loading

0 comments on commit ca12ba8

Please sign in to comment.