@@ -47,29 +47,23 @@ public static ClientServiceFacade getInstance() {
4747
4848 public CriteriaResponse executeCriteria (final Map <String , Object > properties , final Switcher switcher )
4949 throws SwitcherException {
50+ if (!this .isTokenValid (properties )) {
51+ this .auth (properties );
52+ }
53+
54+ final Response response = this .clientService .executeCriteriaService (properties , switcher );
5055
51- try {
52- if (!this .isTokenValid (properties )) {
53- this .auth (properties );
54- }
55-
56- final Response response = this .clientService .executeCriteriaService (properties , switcher );
57-
58- if (response .getStatus () == 401 ) {
59- throw new SwitcherKeyNotAvailableForComponentException (
60- properties .get (SwitcherContextParam .COMPONENT ).toString (), switcher .getSwitcherKey ());
61- } else if (response .getStatus () != 200 ) {
62- throw new SwitcherKeyNotFoundException (switcher .getSwitcherKey ());
63- }
64-
65- final CriteriaResponse criteriaReponse = response .readEntity (CriteriaResponse .class );
66- criteriaReponse .setSwitcherKey (switcher .getSwitcherKey ());
67- response .close ();
68- return criteriaReponse ;
69- } catch (final SwitcherException e ) {
70- throw e ;
56+ if (response .getStatus () == 401 ) {
57+ throw new SwitcherKeyNotAvailableForComponentException (
58+ properties .get (SwitcherContextParam .COMPONENT ).toString (), switcher .getSwitcherKey ());
59+ } else if (response .getStatus () != 200 ) {
60+ throw new SwitcherKeyNotFoundException (switcher .getSwitcherKey ());
7161 }
7262
63+ final CriteriaResponse criteriaReponse = response .readEntity (CriteriaResponse .class );
64+ criteriaReponse .setSwitcherKey (switcher .getSwitcherKey ());
65+ response .close ();
66+ return criteriaReponse ;
7367 }
7468
7569 public Snapshot resolveSnapshot (final Map <String , Object > properties )
@@ -88,8 +82,7 @@ public Snapshot resolveSnapshot(final Map<String, Object> properties)
8882 } catch (final SwitcherException e ) {
8983 throw e ;
9084 } catch (final Exception e ) {
91- throw new SwitcherAPIConnectionException (properties .containsKey (SwitcherContextParam .URL ) ?
92- (String ) properties .get (SwitcherContextParam .URL ) : StringUtils .EMPTY , e );
85+ throw new SwitcherAPIConnectionException (getAPIConnectionException (properties ), e );
9386 }
9487
9588 }
@@ -111,8 +104,7 @@ public boolean checkSnapshotVersion(final Map<String, Object> properties, final
111104 } catch (final SwitcherException e ) {
112105 throw e ;
113106 } catch (final Exception e ) {
114- throw new SwitcherAPIConnectionException (properties .containsKey (SwitcherContextParam .URL ) ?
115- (String ) properties .get (SwitcherContextParam .URL ) : StringUtils .EMPTY , e );
107+ throw new SwitcherAPIConnectionException (getAPIConnectionException (properties ), e );
116108 }
117109
118110 }
@@ -132,8 +124,7 @@ private void auth(final Map<String, Object> properties) throws SwitcherException
132124 throw e ;
133125 } catch (final Exception e ) {
134126 this .setSilentModeExpiration (properties );
135- throw new SwitcherAPIConnectionException (properties .containsKey (SwitcherContextParam .URL ) ?
136- (String ) properties .get (SwitcherContextParam .URL ) : StringUtils .EMPTY , e );
127+ throw new SwitcherAPIConnectionException (getAPIConnectionException (properties ), e );
137128 }
138129 }
139130
@@ -143,16 +134,12 @@ private boolean isTokenValid(final Map<String, Object> properties)
143134 if (properties .containsKey (ClientService .AUTH_RESPONSE )) {
144135 final AuthResponse authResponse = (AuthResponse ) properties .get (ClientService .AUTH_RESPONSE );
145136
146- if (authResponse .getToken ().equals (SwitcherContextParam .SILENT_MODE )) {
147- if (!authResponse .isExpired ()) {
148- throw new SwitcherAPIConnectionException (properties .containsKey (SwitcherContextParam .URL ) ?
149- (String ) properties .get (SwitcherContextParam .URL ) : StringUtils .EMPTY );
150- }
137+ if (authResponse .getToken ().equals (SwitcherContextParam .SILENT_MODE ) && !authResponse .isExpired ()) {
138+ throw new SwitcherAPIConnectionException (getAPIConnectionException (properties ));
151139 } else {
152140 if (!this .clientService .isAlive (properties )) {
153141 this .setSilentModeExpiration (properties );
154- throw new SwitcherAPIConnectionException (properties .containsKey (SwitcherContextParam .URL ) ?
155- (String ) properties .get (SwitcherContextParam .URL ) : StringUtils .EMPTY );
142+ throw new SwitcherAPIConnectionException (getAPIConnectionException (properties ));
156143 }
157144
158145 return !authResponse .isExpired ();
@@ -161,6 +148,11 @@ private boolean isTokenValid(final Map<String, Object> properties)
161148
162149 return false ;
163150 }
151+
152+ private String getAPIConnectionException (final Map <String , Object > properties ) {
153+ return properties .containsKey (SwitcherContextParam .URL ) ?
154+ (String ) properties .get (SwitcherContextParam .URL ) : StringUtils .EMPTY ;
155+ }
164156
165157 private void setSilentModeExpiration (final Map <String , Object > properties )
166158 throws SwitcherInvalidDateTimeArgumentException {
0 commit comments