• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

devonfw / IDEasy / 14172572291

31 Mar 2025 01:20PM UTC coverage: 67.764% (+0.3%) from 67.422%
14172572291

push

github

web-flow
#103: create java resemblance for security.json (#1183)

Co-authored-by: jan-vcapgemini <59438728+jan-vcapgemini@users.noreply.github.com>
Co-authored-by: Jörg Hohwiller <hohwille@users.noreply.github.com>

3053 of 4934 branches covered (61.88%)

Branch coverage included in aggregate %.

7880 of 11200 relevant lines covered (70.36%)

3.07 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

18.49
cli/src/main/java/com/devonfw/tools/ide/tool/repository/CustomToolRepositoryImpl.java
1
package com.devonfw.tools.ide.tool.repository;
2

3
import java.net.URLDecoder;
4
import java.nio.charset.StandardCharsets;
5
import java.nio.file.Files;
6
import java.nio.file.Path;
7
import java.util.ArrayList;
8
import java.util.Collection;
9
import java.util.Collections;
10
import java.util.HashMap;
11
import java.util.List;
12
import java.util.Map;
13

14
import com.devonfw.tools.ide.context.IdeContext;
15
import com.devonfw.tools.ide.tool.ToolCommandlet;
16
import com.devonfw.tools.ide.url.model.file.UrlDownloadFileMetadata;
17
import com.devonfw.tools.ide.url.model.file.json.ToolDependency;
18
import com.devonfw.tools.ide.url.model.file.json.ToolSecurity;
19
import com.devonfw.tools.ide.version.GenericVersionRange;
20
import com.devonfw.tools.ide.version.VersionIdentifier;
21

22
/**
23
 * Implementation of {@link CustomToolRepository}.
24
 */
25
public class CustomToolRepositoryImpl extends AbstractToolRepository implements CustomToolRepository {
26

27
  private final String id;
28

29
  private final Map<String, CustomToolMetadata> toolsMap;
30

31
  private final Collection<CustomToolMetadata> tools;
32

33
  /**
34
   * The constructor.
35
   *
36
   * @param context the owning {@link IdeContext}.
37
   * @param tools the {@link CustomToolMetadata}s.
38
   */
39
  public CustomToolRepositoryImpl(IdeContext context, Collection<CustomToolMetadata> tools) {
40

41
    super(context);
3✔
42
    this.toolsMap = new HashMap<>(tools.size());
7✔
43
    String repoId = null;
2✔
44
    for (CustomToolMetadata tool : tools) {
6!
45
      String name = tool.getTool();
×
46
      CustomToolMetadata duplicate = this.toolsMap.put(name, tool);
×
47
      if (duplicate != null) {
×
48
        throw new IllegalStateException("Duplicate custom tool '" + name + "'!");
×
49
      }
50
      if (repoId == null) {
×
51
        repoId = computeId(tool.getRepositoryUrl());
×
52
      }
53
    }
×
54
    if (repoId == null) {
2!
55
      repoId = "custom";
2✔
56
    }
57
    this.id = repoId;
3✔
58
    this.tools = Collections.unmodifiableCollection(this.toolsMap.values());
6✔
59
  }
1✔
60

61
  private static String computeId(String url) {
62

63
    String id = url;
×
64
    int schemaIndex = id.indexOf("://");
×
65
    if (schemaIndex > 0) {
×
66
      id = id.substring(schemaIndex + 3); // remove schema like "https://"
×
67
      id = URLDecoder.decode(id, StandardCharsets.UTF_8);
×
68
    }
69
    id = id.replace('\\', '/').replace("//", "/"); // normalize slashes
×
70
    if (id.startsWith("/")) {
×
71
      id = id.substring(1);
×
72
    }
73
    StringBuilder sb = new StringBuilder(id.length());
×
74
    if (schemaIndex > 0) { // was a URL?
×
75
      int slashIndex = id.indexOf('/');
×
76
      if (slashIndex > 0) {
×
77
        sb.append(id.substring(0, slashIndex).replace(':', '_'));
×
78
        sb.append('/');
×
79
        id = id.substring(slashIndex + 1);
×
80
      }
81
    }
82
    int length = id.length();
×
83
    for (int i = 0; i < length; i++) {
×
84
      char c = id.charAt(i);
×
85
      if (((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z')) || ((c >= '0') && (c <= '9')) || (c == '.')
×
86
          || (c == '-')) {
87
        sb.append(c);
×
88
      } else {
89
        sb.append('_');
×
90
      }
91
    }
92
    return sb.toString();
×
93
  }
94

95
  @Override
96
  public String getId() {
97

98
    return this.id;
×
99
  }
100

101
  @Override
102
  protected UrlDownloadFileMetadata getMetadata(String tool, String edition, VersionIdentifier version, ToolCommandlet toolCommandlet) {
103

104
    CustomToolMetadata customTool = getCustomTool(tool);
×
105
    if (!version.equals(customTool.getVersion())) {
×
106
      throw new IllegalArgumentException("Undefined version '" + version + "' for custom tool '" + tool
×
107
          + "' - expected version '" + customTool.getVersion() + "'!");
×
108
    }
109
    if (!edition.equals(customTool.getEdition())) {
×
110
      throw new IllegalArgumentException("Undefined edition '" + edition + "' for custom tool '" + tool + "'!");
×
111
    }
112
    return customTool;
×
113
  }
114

115
  private CustomToolMetadata getCustomTool(String tool) {
116
    CustomToolMetadata customTool = this.toolsMap.get(tool);
×
117
    if (customTool == null) {
×
118
      throw new IllegalArgumentException("Undefined custom tool '" + tool + "'!");
×
119
    }
120
    return customTool;
×
121
  }
122

123
  @Override
124
  public VersionIdentifier resolveVersion(String tool, String edition, GenericVersionRange version, ToolCommandlet toolCommandlet) {
125

126
    CustomToolMetadata customTool = getCustomTool(tool);
×
127
    VersionIdentifier customToolVersion = customTool.getVersion();
×
128
    if (!version.contains(customToolVersion)) {
×
129
      throw new IllegalStateException(customTool + " does not satisfy version to install " + version);
×
130
    }
131
    return customToolVersion;
×
132
  }
133

134
  @Override
135
  public List<VersionIdentifier> getSortedVersions(String tool, String edition, ToolCommandlet toolCommandlet) {
136

137
    CustomToolMetadata customTool = getCustomTool(tool);
×
138
    return List.of(customTool.getVersion());
×
139
  }
140

141
  @Override
142
  public Collection<CustomToolMetadata> getTools() {
143

144
    return this.tools;
3✔
145
  }
146

147
  @Override
148
  public Collection<ToolDependency> findDependencies(String tool, String edition, VersionIdentifier version) {
149

150
    return Collections.emptyList();
×
151
  }
152

153
  @Override
154
  public ToolSecurity findSecurity(String tool, String edition) {
155
    return ToolSecurity.getEmpty();
×
156
  }
157

158
  @Override
159
  public List<String> getSortedEditions(String tool) {
160

161
    return List.of(tool);
×
162
  }
163

164
  /**
165
   * @param context the owning {@link IdeContext}.
166
   * @return the {@link CustomToolRepository}.
167
   */
168
  public static CustomToolRepository of(IdeContext context) {
169

170
    Path settingsPath = context.getSettingsPath();
3✔
171
    Path customToolsJsonFile = null;
2✔
172
    if (settingsPath != null) {
2!
173
      customToolsJsonFile = settingsPath.resolve(IdeContext.FILE_CUSTOM_TOOLS);
4✔
174
    }
175
    List<CustomToolMetadata> tools;
176
    if ((customToolsJsonFile != null) && Files.exists(customToolsJsonFile)) {
7!
177
      CustomToolsJson customToolsJson = CustomToolsJsonMapper.loadJson(customToolsJsonFile);
×
178
      tools = CustomToolsJsonMapper.convert(customToolsJson, context);
×
179
    } else {
×
180
      tools = new ArrayList<>();
4✔
181
    }
182
    return new CustomToolRepositoryImpl(context, tools);
6✔
183
  }
184

185
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc