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