| 1 | |
package org.kuali.maven.common; |
| 2 | |
|
| 3 | |
import java.util.Collection; |
| 4 | |
|
| 5 | |
import org.apache.commons.lang.StringUtils; |
| 6 | |
import org.apache.commons.logging.Log; |
| 7 | |
import org.apache.commons.logging.LogFactory; |
| 8 | |
import org.apache.maven.project.MavenProject; |
| 9 | |
|
| 10 | 0 | public class UrlBuilder { |
| 11 | 0 | private static final Log log = LogFactory.getLog(UrlBuilder.class); |
| 12 | |
|
| 13 | |
protected boolean isTargetGroupId(final MavenProject project, final String organizationGroupId) { |
| 14 | |
|
| 15 | 0 | if (project == null || organizationGroupId == null) { |
| 16 | 0 | return false; |
| 17 | |
} |
| 18 | |
|
| 19 | |
|
| 20 | 0 | return organizationGroupId.equals(project.getGroupId()); |
| 21 | |
} |
| 22 | |
|
| 23 | |
protected String getTrimmedGroupId(final MavenProject project, final String organizationGroupId) { |
| 24 | 0 | String groupId = project.getGroupId(); |
| 25 | 0 | if (StringUtils.isEmpty(organizationGroupId)) { |
| 26 | 0 | return groupId; |
| 27 | |
} |
| 28 | 0 | if (!groupId.startsWith(organizationGroupId)) { |
| 29 | 0 | log.warn("Group Id: '" + groupId + "' does not start with '" + organizationGroupId + "'"); |
| 30 | 0 | return groupId; |
| 31 | |
} |
| 32 | 0 | String s = StringUtils.replace(groupId, organizationGroupId, ""); |
| 33 | 0 | if (s.startsWith(".")) { |
| 34 | 0 | s = s.substring(1); |
| 35 | |
} |
| 36 | 0 | s = s.replace(".", "/"); |
| 37 | 0 | return s; |
| 38 | |
} |
| 39 | |
|
| 40 | |
protected boolean isAppendArtifactId(final MavenProject project, final String trimmedGroupId) { |
| 41 | |
|
| 42 | 0 | if (isEmpty(project.getModules())) { |
| 43 | 0 | return true; |
| 44 | |
} |
| 45 | |
|
| 46 | |
|
| 47 | 0 | if (!trimmedGroupId.endsWith(project.getArtifactId())) { |
| 48 | 0 | return true; |
| 49 | |
} |
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | 0 | return false; |
| 60 | |
} |
| 61 | |
|
| 62 | |
public String getSitePath(final MavenProject project, final String organizationGroupId) { |
| 63 | 0 | String trimmedGroupId = getTrimmedGroupId(project, organizationGroupId); |
| 64 | 0 | StringBuilder sb = new StringBuilder(trimmedGroupId); |
| 65 | 0 | if (sb.length() > 0) { |
| 66 | 0 | sb.append("/"); |
| 67 | |
} |
| 68 | 0 | if (isAppendArtifactId(project, trimmedGroupId)) { |
| 69 | 0 | sb.append(project.getArtifactId() + "/"); |
| 70 | |
} |
| 71 | 0 | sb.append(project.getVersion()); |
| 72 | 0 | return sb.toString(); |
| 73 | |
} |
| 74 | |
|
| 75 | |
protected boolean isEmpty(final Collection<?> c) { |
| 76 | 0 | if (c == null) { |
| 77 | 0 | return true; |
| 78 | |
} |
| 79 | 0 | if (c.isEmpty()) { |
| 80 | 0 | return true; |
| 81 | |
} |
| 82 | 0 | return false; |
| 83 | |
} |
| 84 | |
|
| 85 | |
protected String getBaseUrl(final String protocol, final String hostname, final MavenProject project, |
| 86 | |
final SiteContext context) { |
| 87 | 0 | StringBuilder sb = new StringBuilder(); |
| 88 | 0 | sb.append(protocol); |
| 89 | 0 | sb.append("://"); |
| 90 | 0 | sb.append(hostname); |
| 91 | 0 | sb.append("/"); |
| 92 | 0 | sb.append(getSitePath(project, context.getOrganizationGroupId())); |
| 93 | 0 | sb.append("/"); |
| 94 | 0 | return sb.toString(); |
| 95 | |
} |
| 96 | |
|
| 97 | |
protected boolean isBaseCase(final MavenProject project, final String organizationGroupId) { |
| 98 | |
|
| 99 | |
|
| 100 | 0 | if (isTargetGroupId(project, organizationGroupId)) { |
| 101 | 0 | return true; |
| 102 | |
} |
| 103 | |
|
| 104 | |
|
| 105 | 0 | MavenProject parent = project.getParent(); |
| 106 | |
|
| 107 | |
|
| 108 | 0 | if (parent == null) { |
| 109 | 0 | return true; |
| 110 | |
} |
| 111 | |
|
| 112 | |
|
| 113 | 0 | if (isTargetGroupId(parent, organizationGroupId)) { |
| 114 | 0 | return true; |
| 115 | |
} |
| 116 | |
|
| 117 | |
|
| 118 | 0 | return false; |
| 119 | |
} |
| 120 | |
|
| 121 | |
public String getPublicUrl(final MavenProject project, final SiteContext context) { |
| 122 | 0 | String organizationGroupId = context.getOrganizationGroupId(); |
| 123 | 0 | if (isBaseCase(project, organizationGroupId)) { |
| 124 | 0 | return getBaseUrl(context.getPublicUrlProtocol(), context.getHostname(), project, context); |
| 125 | |
} else { |
| 126 | 0 | return getPublicUrl(project.getParent(), context) + project.getArtifactId() + "/"; |
| 127 | |
} |
| 128 | |
} |
| 129 | |
|
| 130 | |
public String getPublishUrl(final MavenProject project, final SiteContext context) { |
| 131 | 0 | String organizationGroupId = context.getOrganizationGroupId(); |
| 132 | 0 | if (isBaseCase(project, organizationGroupId)) { |
| 133 | 0 | return getBaseUrl(context.getPublishUrlProtocol(), context.getBucket(), project, context); |
| 134 | |
} else { |
| 135 | 0 | return getPublishUrl(project.getParent(), context) + project.getArtifactId() + "/"; |
| 136 | |
} |
| 137 | |
} |
| 138 | |
|
| 139 | |
public boolean isSnapshot(final MavenProject project) { |
| 140 | 0 | String version = project.getVersion(); |
| 141 | 0 | int pos = version.toUpperCase().indexOf("SNAPSHOT"); |
| 142 | 0 | boolean isSnapshot = pos != -1; |
| 143 | 0 | return isSnapshot; |
| 144 | |
} |
| 145 | |
|
| 146 | |
public MavenProject getMavenProject(final String groupId, final String artifactId, final String packaging) { |
| 147 | 0 | MavenProject project = new MavenProject(); |
| 148 | 0 | project.setGroupId(groupId); |
| 149 | 0 | project.setArtifactId(artifactId); |
| 150 | 0 | project.setPackaging(packaging); |
| 151 | 0 | return project; |
| 152 | |
} |
| 153 | |
|
| 154 | |
public String getDownloadUrl(final MavenProject project, final SiteContext context) { |
| 155 | 0 | String prefix = context.getDownloadPrefix(); |
| 156 | 0 | StringBuilder sb = new StringBuilder(); |
| 157 | 0 | sb.append(prefix); |
| 158 | 0 | if (!prefix.endsWith("/")) { |
| 159 | 0 | sb.append("/"); |
| 160 | |
} |
| 161 | 0 | if (isSnapshot(project)) { |
| 162 | 0 | sb.append(context.getDownloadSnapshotPrefix()); |
| 163 | |
} else { |
| 164 | 0 | sb.append(context.getDownloadReleasePrefix()); |
| 165 | |
} |
| 166 | 0 | sb.append("/"); |
| 167 | 0 | String groupId = project.getGroupId(); |
| 168 | 0 | sb.append(groupId.replace('.', '/')); |
| 169 | 0 | sb.append("/"); |
| 170 | 0 | sb.append(project.getArtifactId()); |
| 171 | 0 | sb.append("/"); |
| 172 | 0 | sb.append(project.getVersion()); |
| 173 | 0 | sb.append("/"); |
| 174 | 0 | return sb.toString(); |
| 175 | |
} |
| 176 | |
|
| 177 | |
} |