2222import es .upv .i3m .grycap .file .Utf8File ;
2323import es .upv .i3m .grycap .im .exceptions .ImClientErrorException ;
2424import es .upv .i3m .grycap .im .exceptions .ImClientException ;
25+ import es .upv .i3m .grycap .im .exceptions .ImClientServerErrorException ;
2526import es .upv .i3m .grycap .im .lang .ImMessages ;
2627import es .upv .i3m .grycap .im .pojo .ResponseError ;
2728import es .upv .i3m .grycap .im .rest .client .parameters .RestParameter ;
3031
3132import java .nio .file .Path ;
3233
34+ import javax .ws .rs .ServerErrorException ;
3335import javax .ws .rs .WebApplicationException ;
3436import javax .ws .rs .client .Client ;
3537import javax .ws .rs .client .Entity ;
@@ -87,26 +89,22 @@ public ImClient(final String targetUrl, final String authorizationHeader)
8789 }
8890
8991 /**
90- * Creates a new client using the 'imServiceUrl' as endpoint.<br>
91- * Loads the authorization credentials from the 'authorizationHeader'
92- * parameter. It also enables to set the read and connection timeouts.
92+ * Set the client connection timeout in milliseconds.
9393 *
94- * @param targetUrl
95- * : url of the IM rest service
96- * @param authorizationHeader
97- * : string with the authorization content
9894 * @param connectTimeout
9995 * : int with the client connection timeout
96+ */
97+ public void setConnectTimeout (final int connectTimeout ) {
98+ this .client .property ("jersey.config.client.connectTimeout" , connectTimeout );
99+ }
100+
101+ /**
102+ * Set the client read timeout in milliseconds.
103+ *
100104 * @param readTimeout
101105 * : int with the client read timeout
102106 */
103- public ImClient (final String targetUrl , final String authorizationHeader ,
104- final int connectTimeout , final int readTimeout )
105- throws ImClientException {
106- this .targetUrl = targetUrl ;
107- this .authorizationHeader = authorizationHeader ;
108- this .client = createRestClient ();
109- this .client .property ("jersey.config.client.connectTimeout" , connectTimeout );
107+ public void setReadTimeout (final int readTimeout ) {
110108 this .client .property ("jersey.config.client.readTimeout" , readTimeout );
111109 }
112110
@@ -148,6 +146,8 @@ public <T> T get(final String path, final Class<T> type,
148146 logCallInfo (HttpMethods .GET , path );
149147 return configureClient (path , parameters ).get (type );
150148
149+ } catch (ServerErrorException exception ) {
150+ throw new ImClientServerErrorException (createReponseError (exception ));
151151 } catch (WebApplicationException exception ) {
152152 throw new ImClientErrorException (createReponseError (exception ));
153153 }
@@ -163,6 +163,8 @@ public <T> T delete(final String path, final Class<T> type,
163163 Builder clientConfigured = configureClient (path , parameters );
164164 return clientConfigured .delete (type );
165165
166+ } catch (ServerErrorException exception ) {
167+ throw new ImClientServerErrorException (createReponseError (exception ));
166168 } catch (WebApplicationException exception ) {
167169 throw new ImClientErrorException (createReponseError (exception ));
168170 }
@@ -199,6 +201,8 @@ public <T> T post(final String path, final String bodyContent,
199201 Entity .entity (normalizedBodyContent , contentType );
200202 Builder clientConfigured = configureClient (path , parameters );
201203 return clientConfigured .post (content , type );
204+ } catch (ServerErrorException exception ) {
205+ throw new ImClientServerErrorException (createReponseError (exception ));
202206 } catch (WebApplicationException exception ) {
203207 throw new ImClientErrorException (createReponseError (exception ));
204208 }
@@ -229,6 +233,8 @@ public <T> T put(final String path, final String bodyContent,
229233 Entity .entity (normalizedBodyContent , contentType );
230234 Builder clientConfigured = configureClient (path , parameters );
231235 return clientConfigured .put (content , type );
236+ } catch (ServerErrorException exception ) {
237+ throw new ImClientServerErrorException (createReponseError (exception ));
232238 } catch (WebApplicationException exception ) {
233239 throw new ImClientErrorException (createReponseError (exception ));
234240 }
0 commit comments