Skip to content

Commit

Permalink
Unlink security from Portfolio Report if the onlineId does not exist …
Browse files Browse the repository at this point in the history
…anymore
  • Loading branch information
buchen committed Sep 20, 2024
1 parent d7ea3de commit 8065145
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 7 deletions.
3 changes: 2 additions & 1 deletion name.abuchen.portfolio.ui/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ Require-Bundle: name.abuchen.portfolio;bundle-version="0.70.5",
org.eclipse.ui.forms,
org.eclipse.e4.ui.css.core,
org.eclipse.ui.themes,
org.eclipse.jface
org.eclipse.jface,
org.apache.httpcomponents.core5.httpcore5
Automatic-Module-Name: name.abuchen.portfolio.ui
Service-Component: OSGI-INF/name.abuchen.portfolio.ui.util.theme.ColorAndFontProviderImpl.xml,
OSGI-INF/name.abuchen.portfolio.ui.theme.CustomThemeEngineManager.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
import java.util.List;
import java.util.Optional;

import org.apache.hc.core5.http.HttpStatus;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;

import name.abuchen.portfolio.model.Client;
import name.abuchen.portfolio.model.Security;
import name.abuchen.portfolio.online.QuoteFeed;
import name.abuchen.portfolio.online.SecuritySearchProvider.ResultItem;
import name.abuchen.portfolio.online.impl.PortfolioReportNet;
import name.abuchen.portfolio.online.impl.PortfolioReportQuoteFeed;
import name.abuchen.portfolio.ui.Messages;
import name.abuchen.portfolio.ui.PortfolioPlugin;
import name.abuchen.portfolio.util.WebAccess.WebAccessException;
Expand Down Expand Up @@ -60,7 +63,28 @@ protected IStatus run(IProgressMonitor monitor)
}
catch (WebAccessException e)
{
PortfolioPlugin.log(security.getName() + ": " + e.getMessage()); //$NON-NLS-1$
// check if the onlineId has become permanently unavailable and,
// if necessary, remove the online id

if (e.getHttpErrorCode() == HttpStatus.SC_NOT_FOUND
&& "Security not found".equals(e.getHeader("X-Error"))) //$NON-NLS-1$ //$NON-NLS-2$
{
security.setOnlineId(null);

if (PortfolioReportQuoteFeed.ID.equals(security.getFeed()))
security.setFeed(QuoteFeed.MANUAL);

if (PortfolioReportQuoteFeed.ID.equals(security.getLatestFeed()))
security.setFeed(null);

isDirty = true;

PortfolioPlugin.log("Unlinking " + security.getName() + ": " + e.getMessage()); //$NON-NLS-1$
}
else
{
PortfolioPlugin.log(security.getName() + ": " + e.getMessage()); //$NON-NLS-1$
}
}
catch (IOException e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,33 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.hc.client5.http.ClientProtocolException;
import org.apache.hc.client5.http.HttpResponseException;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.classic.methods.HttpUriRequestBase;
import org.apache.hc.client5.http.config.ConnectionConfig;
import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.client5.http.cookie.StandardCookieSpec;
import org.apache.hc.client5.http.impl.classic.BasicHttpClientResponseHandler;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.HttpStatus;
import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.impl.EnglishReasonPhraseCatalog;
import org.apache.hc.core5.http.io.HttpClientResponseHandler;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.io.entity.StringEntity;
import org.apache.hc.core5.http.message.BasicHeader;
import org.apache.hc.core5.net.URIBuilder;
Expand Down Expand Up @@ -102,17 +112,62 @@ public static class WebAccessException extends IOException
{
private static final long serialVersionUID = 1L;
private final int httpErrorCode;
private final Map<String, String> headers;

public WebAccessException(String message, int httpErrorCode)
public WebAccessException(String message, int httpErrorCode, Map<String, String> headers)
{
super(message);
this.httpErrorCode = httpErrorCode;
this.headers = headers;
}

public int getHttpErrorCode()
{
return httpErrorCode;
}

public String getHeader(String key)
{
return headers.get(key);
}
}

private static class CustomResponseHandler implements HttpClientResponseHandler<String>
{
private final String uri;

public CustomResponseHandler(String uri)
{
super();
this.uri = uri;
}

@Override
public String handleResponse(final ClassicHttpResponse response) throws IOException
{
final HttpEntity entity = response.getEntity();
if (response.getCode() >= HttpStatus.SC_REDIRECTION)
{
EntityUtils.consume(entity);

var headers = Stream.of(response.getHeaders())
.collect(Collectors.toMap(Header::getName, Header::getValue));

throw new WebAccessException(buildMessage(uri, response.getCode()), response.getCode(), headers);
}

if (entity == null)
return null;

try
{
return EntityUtils.toString(entity);
}
catch (final ParseException ex)
{
throw new ClientProtocolException(ex);
}
}
}

public static final RequestConfig defaultRequestConfig = RequestConfig.custom()
Expand Down Expand Up @@ -215,19 +270,21 @@ private String executeWith(Request function) throws IOException

URI uri = builder.build();
HttpUriRequestBase request = function.create(uri);
return client.execute(request, new BasicHttpClientResponseHandler());

return client.execute(request, new CustomResponseHandler(uri.toString()));
}
catch (HttpResponseException e)
{
throw new WebAccessException(buildMessage(builder.toString(), e.getStatusCode()), e.getStatusCode());
throw new WebAccessException(buildMessage(builder.toString(), e.getStatusCode()), e.getStatusCode(),
new HashMap<>());
}
catch (URISyntaxException e)
{
throw new IOException(e);
}
}

private String buildMessage(String uri, int statusCode)
private static String buildMessage(String uri, int statusCode)
{
String message = String.valueOf(statusCode);
try
Expand Down

0 comments on commit 8065145

Please sign in to comment.