aboutsummaryrefslogtreecommitdiff
path: root/harness/src/main/java/com/android/csuite/core/RoboLoginConfigProvider.java
blob: 2cfb4d0bb6053fcf8093c14f66f1e396336a9bcf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.csuite.core;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;

import org.junit.Assert;

import java.nio.file.Files;
import java.nio.file.Path;

public final class RoboLoginConfigProvider {
    @VisibleForTesting static final String ROBOSCRIPT_FILE_SUFFIX = ".roboscript";
    @VisibleForTesting static final String CRAWL_GUIDANCE_FILE_SUFFIX = "_cg.txt";
    private static final String ROBOSCRIPT_CMD_FLAG = "--robo-script-file";
    private static final String CRAWL_GUIDANCE_CMD_FLAG = "--text-guide-file";

    private final Path mLoginFilesDir;

    public RoboLoginConfigProvider(Path loginFilesDir) {
        Assert.assertTrue(
                "Please provide a valid directory that contains crawler login files.",
                Files.isDirectory(loginFilesDir));
        this.mLoginFilesDir = loginFilesDir;
    }

    /**
     * Finds the config file to use from the given directory for the corresponding app package and
     * returns the {@link RoboLoginConfig} that contains the resulting login arguments. The
     * directory should contain only one config file per package name. If both Roboscript and
     * CrawlGuidance files are present, only the Roboscript file will be used."
     */
    public RoboLoginConfig findConfigFor(String packageName, boolean isUtpClient) {
        Path crawlGuidanceFile = mLoginFilesDir.resolve(packageName + CRAWL_GUIDANCE_FILE_SUFFIX);
        Path roboScriptFile = mLoginFilesDir.resolve(packageName + ROBOSCRIPT_FILE_SUFFIX);

        if (Files.exists(roboScriptFile) && !isUtpClient) {
            return new RoboLoginConfig(
                    ImmutableList.of(ROBOSCRIPT_CMD_FLAG, roboScriptFile.toString()));
        }

        if (Files.exists(crawlGuidanceFile) && !isUtpClient) {
            return new RoboLoginConfig(
                    ImmutableList.of(CRAWL_GUIDANCE_CMD_FLAG, crawlGuidanceFile.toString()));
        }

        if (Files.exists(roboScriptFile) && isUtpClient) {
            return new RoboLoginConfig(
                    ImmutableList.of(
                            "--crawler-asset", "robo.script=" + roboScriptFile.toString()));
        }

        if (Files.exists(crawlGuidanceFile) && isUtpClient) {
            return new RoboLoginConfig(
                    ImmutableList.of("--crawl-guidance-proto-path", crawlGuidanceFile.toString()));
        }

        return new RoboLoginConfig(ImmutableList.of());
    }

    /*
     * A class returned by RoboLoginConfigProvider that contains the login arguments
     * to be passed to the crawler.
     */
    public static final class RoboLoginConfig {
        private final ImmutableList<String> mLoginArgs;

        public RoboLoginConfig(ImmutableList<String> loginArgs) {
            this.mLoginArgs = loginArgs;
        }

        /* Returns the login arguments for this config which can be passed to the crawler. */
        public ImmutableList<String> getLoginArgs() {
            return mLoginArgs;
        }
    }
}