View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.internal.impl;
20  
21  import org.apache.maven.api.di.Inject;
22  import org.apache.maven.api.di.Named;
23  import org.apache.maven.api.di.Singleton;
24  import org.apache.maven.api.model.DistributionManagement;
25  import org.apache.maven.api.model.Model;
26  import org.apache.maven.api.model.Scm;
27  import org.apache.maven.api.model.Site;
28  import org.apache.maven.api.services.ModelBuilderRequest;
29  import org.apache.maven.api.services.model.ModelUrlNormalizer;
30  import org.apache.maven.api.services.model.UrlNormalizer;
31  
32  /**
33   * Normalizes URLs to remove the ugly parent references "../" that got potentially inserted by URL adjustment during
34   * model inheritance.
35   *
36   */
37  @Named
38  @Singleton
39  public class DefaultModelUrlNormalizer implements ModelUrlNormalizer {
40  
41      private final UrlNormalizer urlNormalizer;
42  
43      @Inject
44      public DefaultModelUrlNormalizer(UrlNormalizer urlNormalizer) {
45          this.urlNormalizer = urlNormalizer;
46      }
47  
48      @Override
49      public Model normalize(Model model, ModelBuilderRequest request) {
50          if (model == null) {
51              return null;
52          }
53  
54          Model.Builder builder = Model.newBuilder(model);
55          builder.url(normalize(model.getUrl()));
56  
57          Scm scm = model.getScm();
58          if (scm != null) {
59              builder.scm(Scm.newBuilder(scm)
60                      .url(normalize(scm.getUrl()))
61                      .connection(normalize(scm.getConnection()))
62                      .developerConnection(normalize(scm.getDeveloperConnection()))
63                      .build());
64          }
65  
66          DistributionManagement dist = model.getDistributionManagement();
67          if (dist != null) {
68              Site site = dist.getSite();
69              if (site != null) {
70                  builder.distributionManagement(dist.withSite(site.withUrl(normalize(site.getUrl()))));
71              }
72          }
73  
74          return builder.build();
75      }
76  
77      private String normalize(String url) {
78          return urlNormalizer.normalize(url);
79      }
80  }