1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 }