Coverage Report - org.kuali.maven.common.UrlBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
UrlBuilder
0%
0/87
0%
0/38
3.308
 
 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  
                 // Return false if we can't do a meaningful comparison
 20  0
                 if (project == null || organizationGroupId == null) {
 21  0
                         return false;
 22  
                 }
 23  
 
 24  
                 // Check that it matches our target groupId
 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  
                 // Always append the artifactId for single module projects
 47  0
                 if (isEmpty(project.getModules())) {
 48  0
                         return true;
 49  
                 }
 50  
 
 51  
                 // Always append the artifactId if it is different than the final
 52  
                 // portion of the groupId
 53  0
                 if (!trimmedGroupId.endsWith(project.getArtifactId())) {
 54  0
                         return true;
 55  
                 }
 56  
 
 57  
                 /**
 58  
                  * We have a multi-module build where the artifactId for the top level project is the same as the final portion
 59  
                  * of the groupId.<br>
 60  
                  * eg org.kuali.rice:rice Return false here so the public url is:<br>
 61  
                  * http://site.kuali.org/rice/1.0.3<br>
 62  
                  * instead of<br>
 63  
                  * http://site.kuali.org/rice/rice/1.0.3<br>
 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  
                 // If this is the organizations groupId return true
 106  
                 // This happens when using Maven to generate a site about the Kuali
 107  
                 // parent pom itself
 108  0
                 if (isTargetGroupId(project, organizationGroupId)) {
 109  0
                         return true;
 110  
                 }
 111  
 
 112  
                 // Otherwise, inspect the parent project
 113  0
                 MavenProject parent = project.getParent();
 114  
 
 115  
                 // If there is no parent, return true
 116  0
                 if (parent == null) {
 117  0
                         return true;
 118  
                 }
 119  
 
 120  
                 // If the parent groupId is the organizations groupId return true
 121  0
                 if (isTargetGroupId(parent, organizationGroupId)) {
 122  0
                         return true;
 123  
                 }
 124  
 
 125  
                 // False otherwise
 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  
 }