1616
1717package com .akamai .edgegrid .signer ;
1818
19+ import java .util .Comparator ;
1920import java .util .Objects ;
2021import java .util .Set ;
2122import java .util .TreeSet ;
2223
23- import org .apache .commons .lang3 .Validate ;
24- import org .apache .commons .lang3 .builder .Builder ;
25- import org .apache .commons .lang3 .builder .CompareToBuilder ;
26- import org .apache .commons .lang3 .builder .ToStringBuilder ;
27- import org .apache .commons .lang3 .builder .ToStringStyle ;
28-
2924/**
3025 * This is a representation of client credential used to sign an EdgeGrid request. This object is
3126 * immutable, so you probably want to build an instance using {@link ClientCredentialBuilder} or one
@@ -39,6 +34,12 @@ public class ClientCredential implements Comparable<ClientCredential> {
3934 /** This is the default {@code maxBodySize} to apply if not explicitly set in a credential. */
4035 public static final int DEFAULT_MAX_BODY_SIZE_IN_BYTES = 131072 ;
4136
37+ /** An {@link Integer} {@link Comparator}. */
38+ private static Comparator <Integer > integerComparator = new NullSafeComparator <>();
39+
40+ /** A {@link String} {@link Comparator}. */
41+ private static Comparator <String > stringComparator = new NullSafeComparator <>();
42+
4243 private String accessToken ;
4344 private String clientSecret ;
4445 private String clientToken ;
@@ -67,13 +68,24 @@ public static ClientCredentialBuilder builder() {
6768
6869 @ Override
6970 public int compareTo (ClientCredential that ) {
70- return new CompareToBuilder ()
71- .append (this .accessToken , that .accessToken )
72- .append (this .clientSecret , that .clientSecret )
73- .append (this .clientToken , that .clientToken )
74- .append (this .host , that .host )
75- .append (this .maxBodySize , that .maxBodySize )
76- .build ();
71+ if (that == null ) {
72+ return 1 ;
73+ }
74+ int comparison = 0 ;
75+ comparison = stringComparator .compare (this .accessToken , that .accessToken );
76+ if (comparison == 0 ) {
77+ comparison = stringComparator .compare (this .clientSecret , that .clientSecret );
78+ }
79+ if (comparison == 0 ) {
80+ comparison = stringComparator .compare (this .clientToken , that .clientToken );
81+ }
82+ if (comparison == 0 ) {
83+ comparison = stringComparator .compare (this .host , that .host );
84+ }
85+ if (comparison == 0 ) {
86+ comparison = integerComparator .compare (this .maxBodySize , that .maxBodySize );
87+ }
88+ return comparison ;
7789 }
7890
7991 @ Override
@@ -118,17 +130,18 @@ public int hashCode() {
118130
119131 @ Override
120132 public String toString () {
121- return new ToStringBuilder (this , ToStringStyle .JSON_STYLE )
122- .append ("accessToken" , accessToken )
123- .append ("clientSecret" , clientSecret )
124- .append ("clientToken" , clientToken )
125- .append ("headersToSign" , headersToSign )
126- .append ("host" , host )
127- .append ("maxBodySize" , getMaxBodySize ()) // note: intentionally using accessor here.
128- .build ();
133+ return new StringBuilder ("[ " )
134+ .append ("accessToken: " ).append (accessToken ).append ("; " )
135+ .append ("clientSecret: " ).append (clientSecret ).append ("; " )
136+ .append ("clientToken: " ).append (clientToken ).append ("; " )
137+ .append ("headersToSign: " ).append (headersToSign ).append ("; " )
138+ .append ("host: " ).append (host ).append ("; " )
139+ .append ("maxBodySize: " ).append (getMaxBodySize ()) // note: intentionally using accessor here.
140+ .append (" ]" )
141+ .toString ();
129142 }
130143
131- public static class ClientCredentialBuilder implements Builder < ClientCredential > {
144+ public static class ClientCredentialBuilder {
132145 private String accessToken ;
133146 private String clientSecret ;
134147 private String clientToken ;
@@ -151,7 +164,9 @@ public ClientCredentialBuilder() {
151164 * @return reference back to this builder instance
152165 */
153166 public ClientCredentialBuilder clientToken (String clientToken ) {
154- Validate .notBlank (clientToken , "clientToken cannot be blank" );
167+ if (Objects .isNull (clientToken ) || "" .equals (clientToken )) {
168+ throw new IllegalArgumentException ("clientToken cannot be empty" );
169+ }
155170 this .clientToken = clientToken ;
156171 return this ;
157172 }
@@ -164,7 +179,9 @@ public ClientCredentialBuilder clientToken(String clientToken) {
164179 * @return reference back to this builder instance
165180 */
166181 public ClientCredentialBuilder clientSecret (String clientSecret ) {
167- Validate .notBlank (clientSecret , "clientSecret cannot be blank" );
182+ if (Objects .isNull (clientSecret ) || "" .equals (clientSecret )) {
183+ throw new IllegalArgumentException ("clientSecret cannot be empty" );
184+ }
168185 this .clientSecret = clientSecret ;
169186 return this ;
170187 }
@@ -176,7 +193,9 @@ public ClientCredentialBuilder clientSecret(String clientSecret) {
176193 * @return reference back to this builder instance
177194 */
178195 public ClientCredentialBuilder accessToken (String accessToken ) {
179- Validate .notBlank (accessToken , "accessToken cannot be blank" );
196+ if (Objects .isNull (accessToken ) || "" .equals (accessToken )) {
197+ throw new IllegalArgumentException ("accessToken cannot be empty" );
198+ }
180199 this .accessToken = accessToken ;
181200 return this ;
182201 }
@@ -218,8 +237,9 @@ public ClientCredentialBuilder headersToSign(Set<String> headersToSign) {
218237 * @return reference back to this builder instance
219238 */
220239 public ClientCredentialBuilder headerToSign (String headerName ) {
221- Validate .notBlank (headerName , "headerName cannot be blank" );
222-
240+ if (Objects .isNull (headerName ) || "" .equals (headerName )) {
241+ throw new IllegalArgumentException ("headerName cannot be empty" );
242+ }
223243 this .headersToSign .add (headerName .toLowerCase ());
224244 return this ;
225245 }
@@ -231,7 +251,9 @@ public ClientCredentialBuilder headerToSign(String headerName) {
231251 * @return reference back to this builder instance
232252 */
233253 public ClientCredentialBuilder host (String host ) {
234- Validate .notBlank (host , "host cannot be blank" );
254+ if (Objects .isNull (host ) || "" .equals (host )) {
255+ throw new IllegalArgumentException ("host cannot be empty" );
256+ }
235257 this .host = host ;
236258 return this ;
237259 }
@@ -252,12 +274,11 @@ public ClientCredentialBuilder maxBodySize(int maxBodySize) {
252274 *
253275 * @return reference back to this builder instance
254276 */
255- @ Override
256277 public ClientCredential build () {
257- Validate . notBlank (accessToken , "accessToken cannot be blank " );
258- Validate . notBlank (clientSecret , "clientSecret cannot be blank " );
259- Validate . notBlank (clientToken , "clientToken cannot be blank " );
260- Validate . notBlank (host , "host cannot be blank " );
278+ Objects . requireNonNull (accessToken , "accessToken cannot be empty " );
279+ Objects . requireNonNull (clientSecret , "clientSecret cannot be empty " );
280+ Objects . requireNonNull (clientToken , "clientToken cannot be empty " );
281+ Objects . requireNonNull (host , "host cannot be empty " );
261282 return new ClientCredential (this );
262283 }
263284
0 commit comments