5353@ Singleton
5454@ Path ("/rundeck/maven/options" )
5555public 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