Skip to content

Commit 3c87d43

Browse files
authored
Merge pull request #60 from indigo-dc/new_exception
New exception
2 parents 76397cc + 1acc6e1 commit 3c87d43

File tree

5 files changed

+82
-14
lines changed

5 files changed

+82
-14
lines changed

src/main/java/es/upv/i3m/grycap/im/InfrastructureManager.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,26 @@ private ImClient getImClient() {
9999
return imClient;
100100
}
101101

102+
/**
103+
* Set the client connection timeout in milliseconds.
104+
*
105+
* @param connectTimeout
106+
* : int with the client connection timeout
107+
*/
108+
public void setConnectTimeout(final int connectTimeout) {
109+
imClient.setConnectTimeout(connectTimeout);
110+
}
111+
112+
/**
113+
* Set the client read timeout in milliseconds.
114+
*
115+
* @param readTimeout
116+
* : int with the client read timeout
117+
*/
118+
public void setReadTimeout(final int readTimeout) {
119+
imClient.setReadTimeout(readTimeout);
120+
}
121+
102122
/**
103123
* Create and configure an infrastructure with the requirements specified in
104124
* the document of the body contents.<br>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Copyright (C) GRyCAP - I3M - UPV
3+
*
4+
* <p>Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* <p>http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* <p>Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package es.upv.i3m.grycap.im.exceptions;
18+
19+
import es.upv.i3m.grycap.im.pojo.ResponseError;
20+
21+
public class ImClientServerErrorException extends ImClientException {
22+
23+
private static final long serialVersionUID = 6007438931649254821L;
24+
private final ResponseError responseError;
25+
26+
public ImClientServerErrorException(ResponseError responseError) {
27+
this.responseError = responseError;
28+
}
29+
30+
public ResponseError getResponseError() {
31+
return responseError;
32+
}
33+
34+
}

src/main/java/es/upv/i3m/grycap/im/rest/client/ImClient.java

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import es.upv.i3m.grycap.file.Utf8File;
2323
import es.upv.i3m.grycap.im.exceptions.ImClientErrorException;
2424
import es.upv.i3m.grycap.im.exceptions.ImClientException;
25+
import es.upv.i3m.grycap.im.exceptions.ImClientServerErrorException;
2526
import es.upv.i3m.grycap.im.lang.ImMessages;
2627
import es.upv.i3m.grycap.im.pojo.ResponseError;
2728
import es.upv.i3m.grycap.im.rest.client.parameters.RestParameter;
@@ -30,6 +31,7 @@
3031

3132
import java.nio.file.Path;
3233

34+
import javax.ws.rs.ServerErrorException;
3335
import javax.ws.rs.WebApplicationException;
3436
import javax.ws.rs.client.Client;
3537
import 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
}

src/test/java/es/upv/i3m/grycap/im/InfrastructureManagerTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ public static void setRestClient() {
9999
try {
100100
im = new InfrastructureManager(IM_DUMMY_PROVIDER_URL,
101101
Paths.get(AUTH_FILE_PATH));
102+
im.setConnectTimeout(100000);
103+
im.setReadTimeout(100000);
102104
} catch (ImClientException exception) {
103105
ImJavaApiLogger.severe(InfrastructureManagerTest.class,
104106
exception.getMessage());
@@ -141,6 +143,10 @@ public void testCreateAndDestroyAsyncInfrastructure() throws ImClientException {
141143
String uri = newInfrastructureUri.getUri();
142144
Assert.assertEquals(false, uri.isEmpty());
143145
String infId = newInfrastructureUri.getInfrastructureId();
146+
try {
147+
Thread.sleep(3000);
148+
} catch (Exception e) {
149+
}
144150
getIm().destroyInfrastructure(infId);
145151
}
146152

src/test/java/es/upv/i3m/grycap/im/rest/client/ImClientTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package es.upv.i3m.grycap.im.rest.client;
1818

1919
import es.upv.i3m.grycap.ImTestWatcher;
20+
import es.upv.i3m.grycap.im.exceptions.ImClientServerErrorException;
2021
import es.upv.i3m.grycap.im.exceptions.ImClientErrorException;
2122
import es.upv.i3m.grycap.im.exceptions.ImClientException;
2223
import es.upv.i3m.grycap.im.pojo.ResponseError;
@@ -34,6 +35,7 @@ public class ImClientTest extends ImTestWatcher {
3435
private static final String AUTH_FILE_PATH = "./src/test/resources/auth.dat";
3536
private static final Integer EXPECTED_ERROR_CODE = 404;
3637
private static final String EXPECTED_ERROR_MESSAGE = "Not found: '/'";
38+
private static final String IM_FAKE_PROVIDER_URL = "http://localhost:1234";
3739

3840
private ImClient getImClient() {
3941
return imClient;

0 commit comments

Comments
 (0)