1   /*
2    * Copyright 2023 LINE Corporation
3    *
4    * LINE Corporation licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   *
16   */
17  
18  package com.linecorp.centraldogma.server.internal.storage.repository;
19  
20  import static com.linecorp.centraldogma.server.internal.storage.repository.MirrorUtil.split;
21  import static com.linecorp.centraldogma.server.mirror.MirrorSchemes.SCHEME_DOGMA;
22  import static java.util.Objects.requireNonNull;
23  
24  import java.net.URI;
25  import java.util.regex.Matcher;
26  import java.util.regex.Pattern;
27  
28  import com.linecorp.centraldogma.server.internal.mirror.CentralDogmaMirror;
29  import com.linecorp.centraldogma.server.mirror.Mirror;
30  import com.linecorp.centraldogma.server.mirror.MirrorContext;
31  import com.linecorp.centraldogma.server.mirror.MirrorProvider;
32  
33  public final class CentralDogmaMirrorProvider implements MirrorProvider {
34  
35      private static final Pattern DOGMA_PATH_PATTERN = Pattern.compile("^/([^/]+)/([^/]+)\\.dogma$");
36  
37      @Override
38      public Mirror newMirror(MirrorContext context) {
39          requireNonNull(context, "context");
40  
41          final URI remoteUri = context.remoteUri();
42          final String scheme = remoteUri.getScheme();
43          if (scheme == null) {
44              return null;
45          }
46  
47          if (!SCHEME_DOGMA.equals(scheme)) {
48              return null;
49          }
50          final String[] components = split(remoteUri, "dogma");
51          final URI remoteRepoUri = URI.create(components[0]);
52          final Matcher matcher = DOGMA_PATH_PATTERN.matcher(remoteRepoUri.getPath());
53          if (!matcher.find()) {
54              throw new IllegalArgumentException(
55                      "cannot determine project name and repository name: " + remoteUri +
56                      " (expected: dogma://<host>[:<port>]/<project>/<repository>.dogma[<remotePath>])");
57          }
58  
59          final String remoteProject = matcher.group(1);
60          final String remoteRepo = matcher.group(2);
61  
62          return new CentralDogmaMirror(context.schedule(), context.direction(), context.credential(),
63                                        context.localRepo(), context.localPath(),
64                                        remoteRepoUri, remoteProject, remoteRepo, components[1],
65                                        context.gitignore());
66      }
67  }