From e374b7f1c6d8eba8bc3a94a6eb4b19851f775996 Mon Sep 17 00:00:00 2001 From: ssprang Date: Mon, 27 Jan 2014 17:08:13 +0100 Subject: [PATCH] Made plugin search more specificly for the artifacts, removed unused code, added some more info to the most common error message, added ignore of DS_Store and made the plugin ignore inherited polopoly.version property when printing supported version. --- .gitignore | 1 + .../plugin/nexus/MavenInfoMacro.java | 23 +++----- .../plugin/nexus/data/MetadataManager.java | 56 +++++++------------ 3 files changed, 29 insertions(+), 51 deletions(-) diff --git a/.gitignore b/.gitignore index ed2166d..da84b0b 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ target/ .settings/ .classpath .project +.DS_Store amps-standalone diff --git a/src/main/java/com/atex/confluence/plugin/nexus/MavenInfoMacro.java b/src/main/java/com/atex/confluence/plugin/nexus/MavenInfoMacro.java index 06ed5d2..13097d4 100644 --- a/src/main/java/com/atex/confluence/plugin/nexus/MavenInfoMacro.java +++ b/src/main/java/com/atex/confluence/plugin/nexus/MavenInfoMacro.java @@ -9,7 +9,6 @@ import java.util.Properties; import java.util.Set; import java.util.TreeSet; -import java.util.logging.Level; import javax.servlet.http.HttpServletRequest; @@ -242,7 +241,8 @@ private String getPluginMetaDataTable(String groupId, String artifactId, String result.append(""); result.append("{html}"); } else { - result.append("{warning}Metadata model not available{warning}"); + result.append("{warning}Metadata model not available through search{warning}"); + result.append(String.format("{html}

For debugging purposes, search URL: %s

{html}", metadataManager.getSearchURI(groupId, artifactId))); } } catch (AddressNotFoundException e) { result.append("{warning}Please make sure the Nexus url is correctly configured{warning}"); @@ -282,18 +282,6 @@ private String getCIEnv(CiManagement cim) { return result.toString(); } - private String getVersions(ExtendedModel model) { - StringBuilder builder = new StringBuilder(); - for(Artifact a: model.getArtifacts()) { - if(builder.length() != 0) { - builder.append("\n"); - } - builder.append(a.getVersion()); - } - - return builder.toString(); - } - /** * This method gather all releases from the artifact and generate the code of a drop down field at front end. * @param model @@ -574,7 +562,12 @@ private String readSpecificDependencyVersion(List dependencies, Stri if (artifactId!=null && !artifactId.isEmpty()) { for (Dependency dependency: dependencies) { if (artifactId.trim().equalsIgnoreCase(dependency.getArtifactId().trim())) { - return dependency.getVersion().trim(); + //This results in e.g. ${polopoly.version} if read from parent + String dependencyVersion = dependency.getVersion().trim(); + if (!org.apache.commons.lang.StringUtils.isBlank(dependencyVersion) + && !dependencyVersion.startsWith("$")) { + return dependencyVersion; + } } } } diff --git a/src/main/java/com/atex/confluence/plugin/nexus/data/MetadataManager.java b/src/main/java/com/atex/confluence/plugin/nexus/data/MetadataManager.java index 739af2b..74c3222 100644 --- a/src/main/java/com/atex/confluence/plugin/nexus/data/MetadataManager.java +++ b/src/main/java/com/atex/confluence/plugin/nexus/data/MetadataManager.java @@ -14,6 +14,7 @@ import org.apache.commons.httpclient.HttpMethod; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods.GetMethod; +import org.apache.commons.lang.StringUtils; import org.apache.maven.model.Model; import org.apache.maven.model.io.xpp3.MavenXpp3Reader; import org.codehaus.plexus.util.xml.pull.XmlPullParserException; @@ -52,20 +53,10 @@ public MetadataManager(Configuration configuration) { * @throws IOException when search failed */ public List getMetadatas(String groupId) throws IOException { - Response result = getResponse(groupId); + Response result = getResponse(groupId, null); return getPoms(result.getLatestReleases(), result.getRepositories()); } - /** - * Search maven model based on artifactId using default groupId - * @param artifactId of the maven to be search - * @return model of the maven, else null when not found - * @throws IOException when search failed - */ -// public Model getMetadata(String artifactId) throws IOException { -// return getMetadata(null, artifactId); -// } - /** * Search maven model based on groupId and artifactId * @param groupId of the artifacts to be search @@ -75,7 +66,7 @@ public List getMetadatas(String groupId) throws IOException { * @throws IOException when search failed */ public ExtendedModel getMetadata(String groupId, String artifactId, String version) throws IOException { - Response result = getResponse(groupId); + Response result = getResponse(groupId, artifactId); Artifact artifact = result.getByArtifactId(artifactId); if(artifact == null) { return null; @@ -91,7 +82,6 @@ public ExtendedModel getMetadata(String groupId, String artifactId, String versi } } - public List getRepositories() throws IOException, ParserConfigurationException, SAXException { List repositories = new ArrayList(); HttpMethod get = doGetHttpMethod(configuration.getSearchRepositoriesURI()); @@ -109,16 +99,23 @@ public List getRepositories() throws IOException, ParserConfiguratio } return repositories; } - - private synchronized Response getResponse(String groupId) throws IOException { + + private synchronized Response getResponse(String groupId, String artifactId) throws IOException { + HttpMethod get = doGetHttpMethod(getSearchURI(groupId, artifactId)); + return parseInputStreamToResponse(get.getResponseBodyAsStream()); + } + + public synchronized String getSearchURI(String groupId, String artifactId) { if(groupId == null || groupId.trim().isEmpty()) { groupId = configuration.getGroupId(); } - - HttpMethod get = doGetHttpMethod(configuration.getSearchURI() + "?g=" + groupId); - return parseInputStreamToResponse(get.getResponseBodyAsStream()); + String searchURI = configuration.getSearchURI() + "?g=" + groupId; + if(!StringUtils.isBlank(artifactId)) { + searchURI = searchURI + "&a=" + artifactId.trim(); + } + return searchURI; } - + private Response parseInputStreamToResponse(InputStream inputStream) { Response response = new Response(); DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); @@ -144,7 +141,7 @@ private Response parseInputStreamToResponse(InputStream inputStream) { } return response; } - + private Repository getRepository(Element el) { Repository repository = new Repository(); repository.setRepositoryId(getTextValue(el, "id")); @@ -152,7 +149,7 @@ private Repository getRepository(Element el) { return repository; } - + private Artifact getArtifact(Element el) { Artifact artifact = new Artifact(); artifact.setGroupId(getTextValue(el, "groupId")); @@ -165,7 +162,7 @@ private Artifact getArtifact(Element el) { return artifact; } - + private String getTextValue(Element ele, String tagName) { String textVal = null; NodeList nl = ele.getElementsByTagName(tagName); @@ -175,7 +172,7 @@ private String getTextValue(Element ele, String tagName) { } return textVal; } - + private List getPoms(List artifacts, List repositories) throws IOException { List poms = new ArrayList(); for(Artifact artifact: artifacts) { @@ -232,7 +229,6 @@ private List getPoms(List artifacts, List r return poms; } - private HttpMethod doGetHttpMethod(String url) throws IOException { HttpMethod get = new GetMethod(url); HttpClient client = new HttpClient(); @@ -253,18 +249,6 @@ private HttpMethod doGetHttpMethod(String url) throws IOException { return get; } - private String getUrl(Artifact artifact, List repositories) { - String groupIdPath = artifact.getGroupId().replace(".", "/"); - String url = "/" + groupIdPath + "/" + artifact.getArtifactId() + "/" + artifact.getVersion() + "/" + artifact.getArtifactId() + "-" + artifact.getVersion() + ".pom"; - for(Repository repository: repositories) { - if(repository.getRepositoryId().equals(artifact.getLatestReleaseRepositoryId())) { - url = repository.getRepositoryURL() + url; - break; - } - } - return url; - } - /** * This method get the url of the specific version/release of the plugin. * Url of latest version will be return if version is null or empty.