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 (isAppendArtifactId(project, trimmedGroupId)) { |
66 | 0 | if (sb.length() > 0) { |
67 | 0 | sb.append("/"); |
68 | |
} |
69 | 0 | sb.append(project.getArtifactId()); |
70 | |
} |
71 | 0 | return sb.toString(); |
72 | |
} |
73 | |
|
74 | |
protected boolean isEmpty(final Collection<?> c) { |
75 | 0 | if (c == null) { |
76 | 0 | return true; |
77 | |
} |
78 | 0 | if (c.isEmpty()) { |
79 | 0 | return true; |
80 | |
} |
81 | 0 | return false; |
82 | |
} |
83 | |
|
84 | |
protected String getBaseUrl(final String protocol, final String hostname, final MavenProject project, |
85 | |
final SiteContext context) { |
86 | 0 | StringBuilder sb = new StringBuilder(); |
87 | 0 | sb.append(protocol); |
88 | 0 | sb.append("://"); |
89 | 0 | sb.append(hostname); |
90 | 0 | sb.append("/"); |
91 | 0 | sb.append(getSitePath(project, context.getOrganizationGroupId())); |
92 | 0 | sb.append("/"); |
93 | 0 | sb.append(project.getVersion()); |
94 | 0 | sb.append("/"); |
95 | 0 | return sb.toString(); |
96 | |
} |
97 | |
|
98 | |
protected boolean isBaseCase(final MavenProject project, final String organizationGroupId) { |
99 | |
|
100 | |
|
101 | 0 | if (isTargetGroupId(project, organizationGroupId)) { |
102 | 0 | return true; |
103 | |
} |
104 | |
|
105 | |
|
106 | 0 | MavenProject parent = project.getParent(); |
107 | |
|
108 | |
|
109 | 0 | if (parent == null) { |
110 | 0 | return true; |
111 | |
} |
112 | |
|
113 | |
|
114 | 0 | if (isTargetGroupId(parent, organizationGroupId)) { |
115 | 0 | return true; |
116 | |
} |
117 | |
|
118 | |
|
119 | 0 | return false; |
120 | |
} |
121 | |
|
122 | |
public String getPublicUrl(final MavenProject project, final SiteContext context) { |
123 | 0 | String organizationGroupId = context.getOrganizationGroupId(); |
124 | 0 | if (isBaseCase(project, organizationGroupId)) { |
125 | 0 | return getBaseUrl(context.getPublicUrlProtocol(), context.getHostname(), project, context); |
126 | |
} else { |
127 | 0 | return getPublicUrl(project.getParent(), context) + project.getArtifactId() + "/"; |
128 | |
} |
129 | |
} |
130 | |
|
131 | |
public String getPublishUrl(final MavenProject project, final SiteContext context) { |
132 | 0 | String organizationGroupId = context.getOrganizationGroupId(); |
133 | 0 | if (isBaseCase(project, organizationGroupId)) { |
134 | 0 | return getBaseUrl(context.getPublishUrlProtocol(), context.getBucket(), project, context); |
135 | |
} else { |
136 | 0 | return getPublishUrl(project.getParent(), context) + project.getArtifactId() + "/"; |
137 | |
} |
138 | |
} |
139 | |
|
140 | |
public boolean isSnapshot(final MavenProject project) { |
141 | 0 | String version = project.getVersion(); |
142 | 0 | int pos = version.toUpperCase().indexOf("SNAPSHOT"); |
143 | 0 | boolean isSnapshot = pos != -1; |
144 | 0 | return isSnapshot; |
145 | |
} |
146 | |
|
147 | |
public MavenProject getMavenProject(final String groupId, final String artifactId, final String packaging) { |
148 | 0 | MavenProject project = new MavenProject(); |
149 | 0 | project.setGroupId(groupId); |
150 | 0 | project.setArtifactId(artifactId); |
151 | 0 | project.setPackaging(packaging); |
152 | 0 | return project; |
153 | |
} |
154 | |
|
155 | |
public String getDownloadUrl(final MavenProject project, final SiteContext context) { |
156 | 0 | String prefix = context.getDownloadPrefix(); |
157 | 0 | StringBuilder sb = new StringBuilder(); |
158 | 0 | sb.append(prefix); |
159 | 0 | if (!prefix.endsWith("/")) { |
160 | 0 | sb.append("/"); |
161 | |
} |
162 | 0 | if (isSnapshot(project)) { |
163 | 0 | sb.append(context.getDownloadSnapshotPrefix()); |
164 | |
} else { |
165 | 0 | sb.append(context.getDownloadReleasePrefix()); |
166 | |
} |
167 | 0 | sb.append("/"); |
168 | 0 | String groupId = project.getGroupId(); |
169 | 0 | sb.append(groupId.replace('.', '/')); |
170 | 0 | sb.append("/"); |
171 | 0 | sb.append(project.getArtifactId()); |
172 | 0 | sb.append("/"); |
173 | 0 | sb.append(project.getVersion()); |
174 | 0 | sb.append("/"); |
175 | 0 | return sb.toString(); |
176 | |
} |
177 | |
|
178 | |
} |