Skip to content

Commit 121053b

Browse files
authored
Merge pull request #7 from gumtreeuk/master
Fix NPE on empty content & add option to get content with different extension/classifier
2 parents 2a13a14 + bf1337b commit 121053b

File tree

3 files changed

+40
-13
lines changed

3 files changed

+40
-13
lines changed

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
FROM sonatype/nexus3
22

33
#Nexus 3 version to use
4-
ARG NEXUS_VERSION=3.5.1-02
4+
ARG NEXUS_VERSION=3.6.0
55
ARG RUNDECK_PLUGIN_VERSION=1.0.0-SNAPSHOT
66

77
USER root
@@ -14,7 +14,7 @@ RUN mkdir -p system/com/nongfenqi/nexus/plugin/${RUNDECK_PLUGIN_VERSION}
1414

1515
ADD build/libs/nexus3-rundeck-plugin-${RUNDECK_PLUGIN_VERSION}.jar \
1616
system/com/nongfenqi/nexus/plugin/${RUNDECK_PLUGIN_VERSION}/nexus3-rundeck-plugin-${RUNDECK_PLUGIN_VERSION}.jar
17-
RUN chomd 644 system/com/nongfenqi/nexus/plugin/${RUNDECK_PLUGIN_VERSION}/nexus3-rundeck-plugin-${RUNDECK_PLUGIN_VERSION}.jar
17+
RUN chmod 644 system/com/nongfenqi/nexus/plugin/${RUNDECK_PLUGIN_VERSION}/nexus3-rundeck-plugin-${RUNDECK_PLUGIN_VERSION}.jar
1818

1919
RUN echo "bundle.mvn\:com.nongfenqi.nexus.plugin/nexus3-rundeck-plugin/"${RUNDECK_PLUGIN_VERSION}" = mvn:com.nongfenqi.nexus.plugin/nexus3-rundeck-plugin/"${RUNDECK_PLUGIN_VERSION} \
2020
>> etc/karaf/profile.cfg \

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,17 @@ The plugin provides the following new HTTP resources :
3535
- `a` : artifactId of the artifacts to match
3636
- `p` : packaging of the artifacts to match ('jar', 'war', etc)
3737
- `c` : classifier of the artifacts to match ('sources', 'javadoc', etc)
38-
- `l` : limit - max number of results to return (default 10)
38+
- `l` : limit - max number of results to return, default value is 10
3939

4040
- `http://NEXUS_HOST/service/siesta/rundeck/maven/options/content` : return artifact stream
4141
Parameters (all required) :
4242
- `r` : repository ID to search in (null for searching in all indexed repositories)
4343
- `g` : groupId of the artifacts to match
4444
- `a` : artifactId of the artifacts to match
45-
- `v` : artifact version
45+
- `v` : artifact version, default value is latest version
46+
- `c` : classifier of the artifacts to match ('sources', 'javadoc', etc)
47+
- `p` : packaging of the artifacts to match ('jar', 'war', etc), default value is jar
48+
4649

4750
Note that if you want to retrieve the artifact from your Rundeck script, you can use content api, example is:
4851

src/main/java/com/nongfenqi/nexus/plugin/rundeck/RundeckMavenResource.java

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,8 @@
5353
@Singleton
5454
@Path("/rundeck/maven/options")
5555
public class RundeckMavenResource
56-
extends ComponentSupport
57-
implements Resource
58-
{
56+
extends ComponentSupport
57+
implements Resource {
5958
private final SearchService searchService;
6059

6160
private final RepositoryManager repositoryManager;
@@ -78,8 +77,18 @@ public Response content(
7877
@QueryParam("r") String repositoryName,
7978
@QueryParam("g") String groupId,
8079
@QueryParam("a") String artifactId,
81-
@QueryParam("v") String version
80+
@QueryParam("v") String version,
81+
@QueryParam("c") String classifier,
82+
@QueryParam("p") @DefaultValue("jar") String extension
8283
) {
84+
85+
86+
// default version
87+
version = Optional.ofNullable(version).orElse(latestVersion(
88+
repositoryName, groupId, artifactId, classifier, extension
89+
));
90+
91+
// valid params
8392
if (isBlank(repositoryName) || isBlank(groupId) || isBlank(artifactId) || isBlank(version)) {
8493
return NOT_FOUND;
8594
}
@@ -102,7 +111,11 @@ public Response content(
102111
return commitAndReturn(NOT_FOUND, tx);
103112
}
104113

105-
String path = groupId.replace(".", "/") + "/" + artifactId + "/" + version + "/" + artifactId + "-" + version + ".jar";
114+
String fileName = artifactId + "-" + version + (isBlank(classifier) ? "" : ("-" + classifier)) + "." + extension;
115+
String path = groupId.replace(".", "/") +
116+
"/" + artifactId +
117+
"/" + version +
118+
"/" + fileName;
106119
Asset asset = tx.findAssetWithProperty("name", path, bucket);
107120
log.debug("rundeck download asset: {}", asset);
108121
if (null == asset) {
@@ -113,7 +126,7 @@ public Response content(
113126
Blob blob = tx.requireBlob(asset.requireBlobRef());
114127
Response.ResponseBuilder ok = Response.ok(blob.getInputStream());
115128
ok.header("Content-Type", blob.getHeaders().get("BlobStore.content-type"));
116-
ok.header("Content-Disposition", "attachment;filename=\"" + path.substring(path.lastIndexOf("/")) + "\"");
129+
ok.header("Content-Disposition", "attachment;filename=\"" + fileName + "\"");
117130
return commitAndReturn(ok.build(), tx);
118131
}
119132

@@ -162,15 +175,25 @@ public List<RundeckXO> version(
162175
.collect(Collectors.toList());
163176
}
164177

178+
private String latestVersion(String repositoryName, String groupId, String artifactId, String classifier, String extension) {
179+
List<RundeckXO> latestVersion = version(1, repositoryName, groupId, artifactId, classifier, extension);
180+
if (!latestVersion.isEmpty()) {
181+
return latestVersion.get(0).getValue();
182+
}
183+
return null;
184+
}
185+
165186
private RundeckXO his2RundeckXO(SearchHit hit) {
166187
String version = (String) hit.getSource().get("version");
167188

168189
List<Map<String, Object>> assets = (List<Map<String, Object>>) hit.getSource().get("assets");
169190
Map<String, Object> attributes = (Map<String, Object>) assets.get(0).get("attributes");
170191
Map<String, Object> content = (Map<String, Object>) attributes.get("content");
171-
long lastModified = (long) content.get("last_modified");
172-
173-
String lastModifiedTime = DateUtils.formatDate(new Date(lastModified), "yyyy-MM-dd HH:mm:ss");
192+
String lastModifiedTime = "null";
193+
if (content != null && content.containsKey("last_modified")) {
194+
Long lastModified = (Long) content.get("last_modified");
195+
lastModifiedTime = DateUtils.formatDate(new Date(lastModified), "yyyy-MM-dd HH:mm:ss");
196+
}
174197

175198
return RundeckXO.builder().name(version + " (" + lastModifiedTime + ")").value(version).build();
176199
}
@@ -181,4 +204,5 @@ private Response commitAndReturn(Response response, StorageTx tx) {
181204
}
182205
return response;
183206
}
207+
184208
}

0 commit comments

Comments
 (0)