Skip to content

Commit 43b0ca4

Browse files
author
TanyaEf
committed
Updated RestClientConfiguration
1 parent db962c0 commit 43b0ca4

File tree

1 file changed

+28
-17
lines changed

1 file changed

+28
-17
lines changed

src/main/java/com/jaspersoft/jasperserver/jaxrs/client/core/RestClientConfiguration.java

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public class RestClientConfiguration {
3838
private static final Log log = LogFactory.getLog(RestClientConfiguration.class);
3939
private static final Pattern URL_PATTERN = Pattern.compile("\\b(https?)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]");
4040
private static final Pattern VERSION_PATTERN = Pattern.compile("^[v]\\d[_]\\d[_]\\d$");
41+
private static final Pattern BOOLEAN_PATTERN = Pattern.compile("^[true|false]$");
42+
private static final Pattern NUMBER_PATTERN = Pattern.compile("^\\d+$");
4143

4244
private String jasperReportsServerUrl;
4345
private MimeType contentMimeType = MimeType.JSON;
@@ -179,21 +181,21 @@ public static RestClientConfiguration loadConfiguration(String path) {
179181

180182
RestClientConfiguration configuration = new RestClientConfiguration();
181183
String url = properties.getProperty("url");
182-
if (url != null && !url.equals("") && URL_PATTERN.matcher(url).matches()) {
184+
if (isStringValid(url) && URL_PATTERN.matcher(url).matches()) {
183185
configuration.setJasperReportsServerUrl(url);
184186
}
185187

186188
String connectionTimeout = properties.getProperty("connectionTimeout");
187-
if (connectionTimeout != null && !connectionTimeout.equals("")) {
189+
if (isStringValid(connectionTimeout) && NUMBER_PATTERN.matcher(connectionTimeout).matches()) {
188190
configuration.setConnectionTimeout(Integer.valueOf(connectionTimeout));
189191
}
190192

191193
String readTimeout = properties.getProperty("readTimeout");
192-
if (readTimeout != null && !readTimeout.equals("")) {
194+
if (isStringValid(readTimeout) && NUMBER_PATTERN.matcher(readTimeout).matches()) {
193195
configuration.setReadTimeout(Integer.valueOf(readTimeout));
194196
}
195197
String jrsVersion = properties.getProperty("jasperserverVersion");
196-
if (jrsVersion != null && !jrsVersion.equals("") && VERSION_PATTERN.matcher(jrsVersion).matches()) {
198+
if (isStringValid(jrsVersion) && VERSION_PATTERN.matcher(jrsVersion).matches()) {
197199
try {
198200
configuration.setJrsVersion(JRSVersion.valueOf(jrsVersion));
199201
} catch (Exception e) {
@@ -202,7 +204,7 @@ public static RestClientConfiguration loadConfiguration(String path) {
202204
}
203205

204206
String authenticationType = properties.getProperty("authenticationType");
205-
if (authenticationType != null && !authenticationType.equals("")) {
207+
if (isStringValid(authenticationType)) {
206208
try {
207209
configuration.setAuthenticationType(AuthenticationType.valueOf(authenticationType.toUpperCase()));
208210
} catch (Exception e) {
@@ -211,32 +213,37 @@ public static RestClientConfiguration loadConfiguration(String path) {
211213
}
212214

213215
String logHttp = properties.getProperty("logHttp");
214-
if (logHttp != null && !logHttp.equals("")) {
216+
if (isStringValid(logHttp)) {
215217
configuration.setLogHttp(Boolean.valueOf(logHttp));
216218
}
217219

218220
String logHttpEntity = properties.getProperty("logHttpEntity");
219-
if (logHttpEntity != null && !logHttpEntity.equals("")) {
221+
if (isStringValid(logHttpEntity)) {
220222
configuration.setLogHttpEntity(Boolean.valueOf(logHttpEntity));
221223
}
222224

223225
String restrictedHttpMethods = properties.getProperty("restrictedHttpMethods");
224-
if (restrictedHttpMethods != null && !restrictedHttpMethods.equals("")) {
226+
if (isStringValid(restrictedHttpMethods)) {
225227
configuration.setRestrictedHttpMethods(Boolean.valueOf(restrictedHttpMethods));
226228
}
227229

228-
try {
229-
configuration.setContentMimeType(MimeType.valueOf(properties.getProperty("contentMimeType")));
230-
} catch (Exception e) {
231-
log.info("There is no mime type for content type or it isn't supported.", e);
230+
String contentMimeType = properties.getProperty("contentMimeType");
231+
if (isStringValid(contentMimeType)) {
232+
try {
233+
configuration.setContentMimeType(MimeType.valueOf(contentMimeType));
234+
} catch (Exception e) {
235+
log.info("There is no mime type for content type or it isn't supported.", e);
236+
}
232237
}
233238

234-
try {
235-
configuration.setAcceptMimeType(MimeType.valueOf(properties.getProperty("acceptMimeType")));
236-
} catch (Exception e) {
237-
log.info("There is no mime type for accept type or it isn't supported.", e);
239+
String acceptMimeType = properties.getProperty("acceptMimeType");
240+
if (isStringValid(acceptMimeType)) {
241+
try {
242+
configuration.setAcceptMimeType(MimeType.valueOf(acceptMimeType));
243+
} catch (Exception e) {
244+
log.info("There is no mime type for accept type or it isn't supported.", e);
245+
}
238246
}
239-
240247
return configuration;
241248
}
242249

@@ -251,4 +258,8 @@ private static Properties loadProperties(String path) {
251258
}
252259
return properties;
253260
}
261+
262+
private static Boolean isStringValid (String string) {
263+
return (string != null && !string.equals(null)) ? true : false;
264+
}
254265
}

0 commit comments

Comments
 (0)