1515 */
1616package jenkins .plugins .jclouds .blobstore ;
1717
18- import java .io .IOException ;
19- import java .util .Properties ;
20- import java .util .Set ;
21- import java .util .logging .Level ;
22- import java .util .logging .Logger ;
23- import java .security .DigestInputStream ;
24- import java .security .MessageDigest ;
25- import java .security .NoSuchAlgorithmException ;
26-
18+ import com .cloudbees .plugins .credentials .common .StandardUsernameCredentials ;
19+ import com .cloudbees .plugins .credentials .common .StandardUsernameListBoxModel ;
2720import com .google .common .collect .ImmutableSet ;
2821import com .google .common .collect .ImmutableSet .Builder ;
2922import com .google .common .collect .ImmutableSortedSet ;
3023import com .google .common .collect .Iterables ;
3124import com .google .inject .Module ;
32-
25+ import com . thoughtworks . xstream . converters . UnmarshallingContext ;
3326import hudson .Extension ;
3427import hudson .FilePath ;
3528import hudson .Util ;
4336import hudson .util .ListBoxModel ;
4437import hudson .util .Secret ;
4538import hudson .util .XStream2 ;
46-
39+ import java .io .IOException ;
40+ import java .security .DigestInputStream ;
41+ import java .security .MessageDigest ;
42+ import java .security .NoSuchAlgorithmException ;
43+ import java .util .Properties ;
44+ import java .util .Set ;
45+ import java .util .logging .Level ;
46+ import java .util .logging .Logger ;
4747import jenkins .model .Jenkins ;
48-
48+ import jenkins .plugins .jclouds .internal .CredentialsHelper ;
49+ import jenkins .plugins .jclouds .internal .LocationHelper ;
50+ import jenkins .plugins .jclouds .modules .JenkinsConfigurationModule ;
4951import org .jclouds .Constants ;
5052import org .jclouds .ContextBuilder ;
5153import org .jclouds .apis .Apis ;
5557import org .jclouds .domain .Location ;
5658import org .jclouds .logging .jdk .config .JDKLoggingModule ;
5759import org .jclouds .providers .Providers ;
58-
60+ import org .kohsuke .accmod .Restricted ;
61+ import org .kohsuke .accmod .restrictions .DoNotUse ;
5962import org .kohsuke .stapler .AncestorInPath ;
6063import org .kohsuke .stapler .DataBoundConstructor ;
6164import org .kohsuke .stapler .QueryParameter ;
6265import org .kohsuke .stapler .verb .POST ;
6366
64- import org .kohsuke .accmod .Restricted ;
65- import org .kohsuke .accmod .restrictions .DoNotUse ;
66-
67- import com .cloudbees .plugins .credentials .common .StandardUsernameCredentials ;
68- import com .cloudbees .plugins .credentials .common .StandardUsernameListBoxModel ;
69-
70- import com .thoughtworks .xstream .converters .UnmarshallingContext ;
71-
72- import jenkins .plugins .jclouds .internal .CredentialsHelper ;
73- import jenkins .plugins .jclouds .internal .LocationHelper ;
74- import jenkins .plugins .jclouds .modules .JenkinsConfigurationModule ;
75-
7667/**
7768 * Model class for Blobstore profile. User can configure multiple profiles to upload artifacts to different providers.
7869 *
7970 * @author Vijay Kiran
8071 */
81- public class BlobStoreProfile extends AbstractDescribableImpl <BlobStoreProfile > {
72+ public class BlobStoreProfile extends AbstractDescribableImpl <BlobStoreProfile > {
8273
8374 private static final Logger LOGGER = Logger .getLogger (BlobStoreProfile .class .getName ());
8475
@@ -97,8 +88,13 @@ public class BlobStoreProfile extends AbstractDescribableImpl<BlobStoreProfile>
9788 private final transient String credential ;
9889
9990 @ DataBoundConstructor
100- public BlobStoreProfile (final String profileName , final String providerName , final String credentialsId ,
101- final String endPointUrl , final String locationId , final boolean trustAll ) {
91+ public BlobStoreProfile (
92+ final String profileName ,
93+ final String providerName ,
94+ final String credentialsId ,
95+ final String endPointUrl ,
96+ final String locationId ,
97+ final boolean trustAll ) {
10298 this .profileName = profileName ;
10399 this .providerName = providerName ;
104100 this .credentialsId = credentialsId ;
@@ -161,20 +157,23 @@ public String getLocationId() {
161157 return locationId ;
162158 }
163159
164- static final Iterable <Module > MODULES = ImmutableSet .<Module >of (new JDKLoggingModule () {
165- @ Override
166- public org .jclouds .logging .Logger .LoggerFactory createLoggerFactory () {
167- return new BlobStoreLogger .Factory ();
168- }
169- }, new JenkinsConfigurationModule ());
170-
160+ static final Iterable <Module > MODULES = ImmutableSet .<Module >of (
161+ new JDKLoggingModule () {
162+ @ Override
163+ public org .jclouds .logging .Logger .LoggerFactory createLoggerFactory () {
164+ return new BlobStoreLogger .Factory ();
165+ }
166+ },
167+ new JenkinsConfigurationModule ());
171168
172169 private static BlobStoreContext ctx (final String provider , final String credId , final Properties overrides ) {
173170 // correct the classloader so that extensions can be found
174171 Thread .currentThread ().setContextClassLoader (Apis .class .getClassLoader ());
175172 CredentialsHelper .setProject (credId , overrides );
176173 return CredentialsHelper .setCredentials (ContextBuilder .newBuilder (provider ), credId )
177- .overrides (overrides ).modules (MODULES ).buildView (BlobStoreContext .class );
174+ .overrides (overrides )
175+ .modules (MODULES )
176+ .buildView (BlobStoreContext .class );
178177 }
179178
180179 private static Properties buildJCloudsOverrides (final String url , final boolean relaxed ) {
@@ -202,7 +201,8 @@ static BlobStoreContext ctx(final String provider, final String credId, final St
202201 * @throws IOException if an IO error occurs.
203202 * @throws InterruptedException If the upload gets interrupted.
204203 */
205- public void upload (final String container , final String path , final FilePath filePath ) throws IOException , InterruptedException {
204+ public void upload (final String container , final String path , final FilePath filePath )
205+ throws IOException , InterruptedException {
206206 if (filePath .isDirectory ()) {
207207 throw new IOException (filePath + " is a directory" );
208208 }
@@ -232,8 +232,11 @@ public void upload(final String container, final String path, final FilePath fil
232232 MessageDigest md5 = MessageDigest .getInstance ("MD5" );
233233 DigestInputStream dis = new DigestInputStream (filePath .read (), md5 );
234234
235- Blob blob = blobStore .blobBuilder (destPath )
236- .payload (dis ).contentLength (filePath .length ()).build ();
235+ Blob blob = blobStore
236+ .blobBuilder (destPath )
237+ .payload (dis )
238+ .contentLength (filePath .length ())
239+ .build ();
237240 blobStore .putBlob (container , blob );
238241 bsc .close ();
239242 String md5local = Util .toHexString (md5 .digest ()).toLowerCase ();
@@ -242,12 +245,17 @@ public void upload(final String container, final String path, final FilePath fil
242245 try (BlobStoreContext bsc2 = ctx (providerName , credentialsId , endPointUrl , trustAll )) {
243246 blobStore = bsc2 .getBlobStore ();
244247 LOGGER .info ("Fetching remote MD5sum for " + destPath );
245- String md5remote = blobStore .blobMetadata (container , destPath )
246- .getContentMetadata ().getContentMD5AsHashCode ().toString ();
248+ String md5remote = blobStore
249+ .blobMetadata (container , destPath )
250+ .getContentMetadata ()
251+ .getContentMD5AsHashCode ()
252+ .toString ();
247253 if (md5local .equals (md5remote )) {
248- LOGGER .info ("Published " + destPath + " to container " + container + " with profile " + profileName );
254+ LOGGER .info ("Published " + destPath + " to container " + container + " with profile "
255+ + profileName );
249256 } else {
250- LOGGER .warning ("MD5 mismatch while publishing " + destPath + " to container " + container + " with profile " + profileName );
257+ LOGGER .warning ("MD5 mismatch while publishing " + destPath + " to container " + container
258+ + " with profile " + profileName );
251259 throw new IOException ("MD5 mismatch while publishing" );
252260 }
253261 break ;
@@ -289,20 +297,23 @@ public FormValidation doCheckEndPointUrl(@QueryParameter String value) {
289297 }
290298
291299 @ POST
292- public ListBoxModel doFillCredentialsIdItems (@ AncestorInPath ItemGroup context , @ QueryParameter String currentValue ) {
293- if (!(context instanceof AccessControlled ? (AccessControlled ) context : Jenkins .get ()).hasPermission (Computer .CONFIGURE )) {
300+ public ListBoxModel doFillCredentialsIdItems (
301+ @ AncestorInPath ItemGroup context , @ QueryParameter String currentValue ) {
302+ if (!(context instanceof AccessControlled ? (AccessControlled ) context : Jenkins .get ())
303+ .hasPermission (Computer .CONFIGURE )) {
294304 return new StandardUsernameListBoxModel ().includeCurrentValue (currentValue );
295305 }
296306 return new StandardUsernameListBoxModel ()
297- .includeAs (ACL .SYSTEM2 , context , StandardUsernameCredentials .class ).includeCurrentValue (currentValue );
307+ .includeAs (ACL .SYSTEM2 , context , StandardUsernameCredentials .class )
308+ .includeCurrentValue (currentValue );
298309 }
299310
300311 ImmutableSortedSet <String > getAllProviders () {
301312 // correct the classloader so that extensions can be found
302313 Thread .currentThread ().setContextClassLoader (Apis .class .getClassLoader ());
303314 // TODO: apis need endpoints, providers don't; do something smarter
304315 // with this stuff :)
305- Builder <String > builder = ImmutableSet .<String > builder ();
316+ Builder <String > builder = ImmutableSet .<String >builder ();
306317 builder .addAll (Iterables .transform (Apis .viewableAs (BlobStoreContext .class ), Apis .idFunction ()));
307318 builder .addAll (Iterables .transform (Providers .viewableAs (BlobStoreContext .class ), Providers .idFunction ()));
308319 return ImmutableSortedSet .copyOf (builder .build ());
@@ -323,28 +334,31 @@ public ListBoxModel doFillProviderNameItems(@AncestorInPath ItemGroup context) {
323334 }
324335
325336 @ POST
326- public FormValidation doTestConnection (@ QueryParameter ("providerName" ) final String provider ,
327- @ QueryParameter ("credentialsId" ) final String credId ,
328- @ QueryParameter ("endPointUrl" ) final String url ,
329- @ QueryParameter ("trustAll" ) final boolean relaxed ) throws IOException {
330-
337+ public FormValidation doTestConnection (
338+ @ QueryParameter ("providerName" ) final String provider ,
339+ @ QueryParameter ("credentialsId" ) final String credId ,
340+ @ QueryParameter ("endPointUrl" ) final String url ,
341+ @ QueryParameter ("trustAll" ) final boolean relaxed )
342+ throws IOException {
331343
332344 Jenkins .get ().checkPermission (Jenkins .ADMINISTER );
333345 if (null == Util .fixEmptyAndTrim (credId )) {
334346 return FormValidation .error ("BlobStore credentials not specified." );
335347 }
336348 FormValidation res = FormValidation .ok ("Connection succeeded!" );
337- try (BlobStoreContext ctx = ctx (Util .fixEmptyAndTrim (provider ), credId , Util .fixEmptyAndTrim (url ), relaxed )) {
349+ try (BlobStoreContext ctx =
350+ ctx (Util .fixEmptyAndTrim (provider ), credId , Util .fixEmptyAndTrim (url ), relaxed )) {
338351 ctx .getBlobStore ().list ();
339352 } catch (Exception ex ) {
340- res = FormValidation .error ("Cannot connect to specified BlobStore, please check the credentials: "
341- + ex .getMessage ());
353+ res = FormValidation .error (
354+ "Cannot connect to specified BlobStore, please check the credentials: " + ex .getMessage ());
342355 }
343356 return res ;
344357 }
345358
346359 @ POST
347- public ListBoxModel doFillLocationIdItems (@ QueryParameter String providerName ,
360+ public ListBoxModel doFillLocationIdItems (
361+ @ QueryParameter String providerName ,
348362 @ QueryParameter String credentialsId ,
349363 @ QueryParameter String endPointUrl ) {
350364
@@ -367,7 +381,8 @@ public ListBoxModel doFillLocationIdItems(@QueryParameter String providerName,
367381 }
368382
369383 @ POST
370- public FormValidation doValidateLocationId (@ QueryParameter ("providerName" ) final String provider ,
384+ public FormValidation doValidateLocationId (
385+ @ QueryParameter ("providerName" ) final String provider ,
371386 @ QueryParameter ("credentialsId" ) final String credId ,
372387 @ QueryParameter ("endPointUrl" ) final String url ,
373388 @ QueryParameter ("locationId" ) final String locId ) {
@@ -390,14 +405,18 @@ public FormValidation doValidateLocationId(@QueryParameter("providerName") final
390405 for (Location location : locations ) {
391406 if (!location .getId ().equals (testLoc )) {
392407 if (location .getId ().contains (testLoc )) {
393- return FormValidation .warning ("Sorry cannot find the location id, " + "Did you mean: " + location .getId () + "?\n " + location );
408+ return FormValidation .warning ("Sorry cannot find the location id, " + "Did you mean: "
409+ + location .getId () + "?\n " + location );
394410 }
395411 } else {
396412 return FormValidation .ok ("Location Id is valid." );
397413 }
398414 }
399415 } catch (Exception ex ) {
400- res = FormValidation .error ("Unable to check the location id, " + "please check if the credentials you provided are correct." , ex );
416+ res = FormValidation .error (
417+ "Unable to check the location id, "
418+ + "please check if the credentials you provided are correct." ,
419+ ex );
401420 }
402421 return res ;
403422 }
@@ -411,11 +430,15 @@ public ConverterImpl(XStream2 xstream) {
411430 super (xstream );
412431 }
413432
414- @ Override protected void callback (BlobStoreProfile bsp , UnmarshallingContext context ) {
415- if ((null == bsp .getCredentialsId () || bsp .getCredentialsId ().isEmpty ()) && null != bsp .identity && !bsp .identity .isEmpty ()) {
416- final String description = "JClouds BlobStore " + bsp .profileName + " - auto-migrated" ;
417- bsp .setCredentialsId (CredentialsHelper .convertCredentials (description , bsp .identity , Secret .fromString (bsp .credential )));
418- }
433+ @ Override
434+ protected void callback (BlobStoreProfile bsp , UnmarshallingContext context ) {
435+ if ((null == bsp .getCredentialsId () || bsp .getCredentialsId ().isEmpty ())
436+ && null != bsp .identity
437+ && !bsp .identity .isEmpty ()) {
438+ final String description = "JClouds BlobStore " + bsp .profileName + " - auto-migrated" ;
439+ bsp .setCredentialsId (CredentialsHelper .convertCredentials (
440+ description , bsp .identity , Secret .fromString (bsp .credential )));
441+ }
419442 }
420443 }
421444}
0 commit comments