Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compose Metadata Analyzer: Refactor to support V2 and V1 repositories #4470

Merged
merged 6 commits into from
Dec 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
*/
package org.dependencytrack.tasks.repositories;

import alpine.common.logging.Logger;
import alpine.notification.Notification;
import alpine.notification.NotificationLevel;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
Expand All @@ -33,25 +35,32 @@
import org.dependencytrack.notification.NotificationScope;
import org.dependencytrack.util.HttpUtil;

import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import alpine.common.logging.Logger;
import alpine.notification.Notification;
import alpine.notification.NotificationLevel;

/**
* Base abstract class that all IMetaAnalyzer implementations should likely extend.
* Base abstract class that all IMetaAnalyzer implementations should likely
* extend.
*
* @author Steve Springett
* @since 3.1.0
*/
public abstract class AbstractMetaAnalyzer implements IMetaAnalyzer {

protected String repositoryId;
protected String baseUrl;

protected String username;

protected String password;


/**
* {@inheritDoc}
*/
public void setRepositoryId(String repositoryId) {
this.repositoryId = repositoryId.trim();
}

/**
* {@inheritDoc}
*/
Expand All @@ -75,28 +84,30 @@ protected String urlEncode(final String value) {
return URLEncoder.encode(value, StandardCharsets.UTF_8).replace("+", "%20");
}

protected void handleUnexpectedHttpResponse(final Logger logger, String url, final int statusCode, final String statusText, final Component component) {
protected void handleUnexpectedHttpResponse(final Logger logger, String url, final int statusCode,
final String statusText, final Component component) {
logger.debug("HTTP Status : " + statusCode + " " + statusText);
logger.debug(" - RepositoryType URL : " + url);
logger.debug(" - Package URL : " + component.getPurl().canonicalize());
Notification.dispatch(new Notification()
.scope(NotificationScope.SYSTEM)
.group(NotificationGroup.REPOSITORY)
.title(NotificationConstants.Title.REPO_ERROR)
.content("An error occurred while communicating with an " + supportedRepositoryType().name() + " repository. URL: " + url + " HTTP Status: " + statusCode + ". Check log for details." )
.level(NotificationLevel.ERROR)
);
.content("An error occurred while communicating with an " + supportedRepositoryType().name()
+ " repository. URL: " + url + " HTTP Status: " + statusCode + ". Check log for details.")
.level(NotificationLevel.ERROR));
}

protected void handleRequestException(final Logger logger, final Exception e) {
logger.error("Request failure", e);
e.printStackTrace();
Notification.dispatch(new Notification()
.scope(NotificationScope.SYSTEM)
.group(NotificationGroup.REPOSITORY)
.title(NotificationConstants.Title.REPO_ERROR)
.content("An error occurred while communicating with an " + supportedRepositoryType().name() + " repository. Check log for details. " + e.getMessage())
.level(NotificationLevel.ERROR)
);
.content("An error occurred while communicating with an " + supportedRepositoryType().name()
+ " repository. Check log for details. " + e.getMessage())
.level(NotificationLevel.ERROR));
}

protected CloseableHttpResponse processHttpRequest(String url) throws IOException {
Expand All @@ -109,7 +120,7 @@ protected CloseableHttpResponse processHttpRequest(String url) throws IOExceptio
request.addHeader("Authorization", HttpUtil.basicAuthHeaderValue(username, password));
}
return HttpClientPool.getClient().execute(request);
}catch (URISyntaxException ex){
} catch (URISyntaxException ex) {
handleRequestException(logger, ex);
return null;
}
Expand Down
Loading
Loading