Skip to content

Commit 8db3313

Browse files
author
TanyaEf
committed
1.Updated RestClientConfiguration
2.Updated ReadMe
1 parent a325ded commit 8db3313

File tree

8 files changed

+160
-45
lines changed

8 files changed

+160
-45
lines changed

README.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,29 @@ To do this you should create instance of `RestClientConfiguration`. It can be do
131131
```java
132132
RestClientConfiguration configuration = RestClientConfiguration.loadConfiguration("url.properties");
133133
```
134-
File should contain only URL which is entry point to your server's REST services and it is needed to URL corresponds to this pattern `{protocol}://{host}:{port}/{contextPath}`.
134+
Here is example of configuration file:
135+
```java
136+
url=http://localhost:4444/jasperserver-pro
137+
connectionTimeout=10000
138+
readTimeout=10000
139+
jasperserverVersion=v6_0_0
140+
authenticationType=REST
141+
logHttp=true
142+
logHttpEntity=false
143+
restrictedHttpMethods=false
144+
contentMimeType=JSON
145+
acceptMimeType=JSON
146+
```
147+
File must contain at least URL which is entry point to your server's REST services and it is needed to URL corresponds to this pattern `{protocol}://{host}:{port}/{contextPath}`.
135148
####Creation of manual configuration
149+
To configure `JasperserverRestClient` manually, use the constructor of `RestClientConfiguration` and properties:
136150
```java
137151
RestClientConfiguration configuration = new RestClientConfiguration("http://localhost:8080/jasperserver");
152+
configuration.setAcceptMimeType(MimeType.JSON);
153+
configuration.setContentMimeType(MimeType.JSON);
154+
configuration.setJrsVersion(JRSVersion.v6_0_0);
155+
configuration.setLogHttp(true);
156+
configuration.setLogHttpEntity(true);
138157
```
139158
####HTTPS configuration
140159
<strong>To use HTTPS you need:</strong>

src/integration-test/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/authority/users/UsersServiceIT.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ public void before() {
2424
config.setAcceptMimeType(MimeType.JSON);
2525
config.setContentMimeType(MimeType.JSON);
2626
config.setJrsVersion(JRSVersion.v6_0_1);
27-
config.setIsJerseyRequestLogged(true);
28-
// config.setIsJSonEntitieLogged(true);
2927
config.setAuthenticationType(AuthenticationType.REST);
3028
client = new JasperserverRestClient(config);
3129
session = client.authenticate("superuser", "superuser");

src/integration-test/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/settings/SettingsServiceIT.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ public void before() {
3131
config.setAcceptMimeType(MimeType.JSON);
3232
config.setContentMimeType(MimeType.JSON);
3333
config.setJrsVersion(JRSVersion.v6_0_0);
34-
config.setIsJerseyRequestLogged(true);
35-
config.setIsJSonEntitieLogged(true);
34+
config.setLogHttp(true);
35+
config.setLogHttpEntity(true);
3636
client = new JasperserverRestClient(config);
3737
session = client.getAnonymousSession();
3838
}

src/integration-test/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/core/JasperserverRestClientIT.java renamed to src/integration-test/java/com/jaspersoft/jasperserver/jaxrs/client/core/JasperserverRestClientIT.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
package com.jaspersoft.jasperserver.jaxrs.client.apiadapters.core;
1+
package com.jaspersoft.jasperserver.jaxrs.client.core;
22

3-
import com.jaspersoft.jasperserver.jaxrs.client.core.*;
43
import org.testng.annotations.AfterMethod;
54
import org.testng.annotations.BeforeMethod;
65
import org.testng.annotations.Test;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.jaspersoft.jasperserver.jaxrs.client.core;
2+
3+
import org.testng.annotations.AfterMethod;
4+
import org.testng.annotations.Test;
5+
6+
import static junit.framework.Assert.*;
7+
8+
/**
9+
* @author Tetiana Iefimenko
10+
*/
11+
public class RestClientConfigurationIT {
12+
13+
private RestClientConfiguration config;
14+
15+
@Test
16+
public void should_load_configuration_from_file() {
17+
18+
// When
19+
20+
config = RestClientConfiguration.loadConfiguration("config.properties");
21+
// Then
22+
assertNotNull(config);
23+
assertNotNull(config.getJasperReportsServerUrl());
24+
assertNotNull(config.getJrsVersion());
25+
assertNotNull(config.getAuthenticationType());
26+
assertNotNull(config.getRestrictedHttpMethods());
27+
assertNotNull(config.getLogHttp());
28+
assertNotNull(config.getLogHttpEntity());
29+
assertNotNull(config.getAcceptMimeType());
30+
assertNotNull(config.getContentMimeType());
31+
assertTrue(config.getLogHttp());
32+
assertFalse(config.getLogHttpEntity());
33+
assertFalse(config.getLogHttpEntity());
34+
assertFalse(config.getRestrictedHttpMethods());
35+
assertEquals(config.getJrsVersion(), JRSVersion.v6_0_0);
36+
assertEquals(config.getAuthenticationType(), AuthenticationType.REST);
37+
}
38+
39+
@AfterMethod
40+
public void afterTest() {
41+
config = null;
42+
}
43+
44+
}

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

Lines changed: 80 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,17 @@ public class RestClientConfiguration {
3737

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+&@#/%=~_|]");
40+
private static final Pattern VERSION_PATTERN = Pattern.compile("^[v]\\d[_]\\d[_]\\d$");
4041

4142
private String jasperReportsServerUrl;
4243
private MimeType contentMimeType = MimeType.JSON;
4344
private MimeType acceptMimeType = MimeType.JSON;
4445
private JRSVersion jrsVersion = JRSVersion.v5_5_0;
46+
4547
private AuthenticationType authenticationType = AuthenticationType.REST;
4648
private Boolean restrictedHttpMethods = false;
47-
48-
private Boolean isJerseyRequestLogged = false;
49-
50-
private Boolean isJSonEntitieLogged = false;
49+
private Boolean logHttpEntity = false;
50+
private Boolean logHttp = false;
5151

5252
private TrustManager[] trustManagers;
5353
private Integer connectionTimeout;
@@ -90,10 +90,35 @@ public void setJasperReportsServerUrl(String jasperReportsServerUrl) {
9090
public AuthenticationType getAuthenticationType() {
9191
return authenticationType;
9292
}
93+
9394
public void setAuthenticationType(AuthenticationType authenticationType) {
9495
this.authenticationType = authenticationType;
9596
}
9697

98+
public Boolean getRestrictedHttpMethods() {
99+
return restrictedHttpMethods;
100+
}
101+
102+
public void setRestrictedHttpMethods(Boolean restrictedHttpMethods) {
103+
this.restrictedHttpMethods = restrictedHttpMethods;
104+
}
105+
106+
public Boolean getLogHttp() {
107+
return logHttp;
108+
}
109+
110+
public void setLogHttp(Boolean logHttp) {
111+
this.logHttp = logHttp;
112+
}
113+
114+
public Boolean getLogHttpEntity() {
115+
return logHttpEntity;
116+
}
117+
118+
public void setLogHttpEntity(Boolean logHttpEntity) {
119+
this.logHttpEntity = logHttpEntity;
120+
}
121+
97122
public MimeType getContentMimeType() {
98123
return contentMimeType;
99124
}
@@ -118,7 +143,7 @@ public void setJrsVersion(JRSVersion jrsVersion) {
118143
this.jrsVersion = jrsVersion;
119144
}
120145

121-
public TrustManager[] getTrustManagers() {
146+
public TrustManager[] getTrustManagers() {
122147
return trustManagers;
123148
}
124149

@@ -142,43 +167,63 @@ public void setReadTimeout(Integer readTimeout) {
142167
this.readTimeout = readTimeout;
143168
}
144169

145-
public Boolean getRestrictedHttpMethods() {
146-
return restrictedHttpMethods;
147-
}
148-
149-
public void setRestrictedHttpMethods(Boolean restrictedHttpMethods) {
150-
this.restrictedHttpMethods = restrictedHttpMethods;
151-
}
152-
153-
public Boolean getIsJerseyRequestLogged() {
154-
return isJerseyRequestLogged;
155-
}
156-
157-
public void setIsJerseyRequestLogged(Boolean isJerseyRequestLogged) {
158-
this.isJerseyRequestLogged = isJerseyRequestLogged;
159-
}
160-
161-
public Boolean getIsJSonEntitieLogged() {
162-
return isJSonEntitieLogged;
163-
}
164-
165-
public void setIsJSonEntitieLogged(Boolean isJSonEntitieLogged) {
166-
this.isJSonEntitieLogged = isJSonEntitieLogged;
167-
}
168-
169170
public static RestClientConfiguration loadConfiguration(String path) {
170-
Properties properties = loadProperties(path);
171+
Properties properties = null;
172+
if (path != null) {
173+
properties = loadProperties(path);
174+
}
175+
if (properties == null) {
176+
log.info("The properties file was not loaded");
177+
return null;
178+
}
171179

172180
RestClientConfiguration configuration = new RestClientConfiguration();
173-
configuration.setJasperReportsServerUrl(properties.getProperty("url"));
181+
String url = properties.getProperty("url");
182+
if (url != null && !url.equals("") && URL_PATTERN.matcher(url).matches()) {
183+
configuration.setJasperReportsServerUrl(url);
184+
}
174185

175186
String connectionTimeout = properties.getProperty("connectionTimeout");
176-
if (connectionTimeout != null && !connectionTimeout.equals(""))
187+
if (connectionTimeout != null && !connectionTimeout.equals("")) {
177188
configuration.setConnectionTimeout(Integer.valueOf(connectionTimeout));
189+
}
178190

179191
String readTimeout = properties.getProperty("readTimeout");
180-
if (readTimeout != null && !readTimeout.equals(""))
181-
configuration.setConnectionTimeout(Integer.valueOf(readTimeout));
192+
if (readTimeout != null && !readTimeout.equals("")) {
193+
configuration.setReadTimeout(Integer.valueOf(readTimeout));
194+
}
195+
String jrsVersion = properties.getProperty("jasperserverVersion");
196+
if (jrsVersion != null && !jrsVersion.equals("") && VERSION_PATTERN.matcher(jrsVersion).matches()) {
197+
try {
198+
configuration.setJrsVersion(JRSVersion.valueOf(jrsVersion));
199+
} catch (Exception e) {
200+
log.info("There is no version for JasperReportsServer or it isn't supported.", e);
201+
}
202+
}
203+
204+
String authenticationType = properties.getProperty("authenticationType");
205+
if (authenticationType != null && !authenticationType.equals("")) {
206+
try {
207+
configuration.setAuthenticationType(AuthenticationType.valueOf(authenticationType.toUpperCase()));
208+
} catch (Exception e) {
209+
log.info("There is no authentication type or it isn't supported.", e);
210+
}
211+
}
212+
213+
String logHttp = properties.getProperty("logHttp");
214+
if (logHttp != null && !logHttp.equals("")) {
215+
configuration.setLogHttp(Boolean.valueOf(logHttp));
216+
}
217+
218+
String logHttpEntity = properties.getProperty("logHttpEntity");
219+
if (logHttpEntity != null && !logHttpEntity.equals("")) {
220+
configuration.setLogHttpEntity(Boolean.valueOf(logHttpEntity));
221+
}
222+
223+
String restrictedHttpMethods = properties.getProperty("restrictedHttpMethods");
224+
if (restrictedHttpMethods != null && !restrictedHttpMethods.equals("")) {
225+
configuration.setRestrictedHttpMethods(Boolean.valueOf(restrictedHttpMethods));
226+
}
182227

183228
try {
184229
configuration.setContentMimeType(MimeType.valueOf(properties.getProperty("contentMimeType")));
@@ -202,9 +247,8 @@ private static Properties loadProperties(String path) {
202247
properties.load(is);
203248
} catch (Exception e) {
204249
log.info("Error when loading properties file", e);
250+
return null;
205251
}
206252
return properties;
207253
}
208-
209-
210254
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ private void init() {
9696
rootTarget = client.target(configuration.getJasperReportsServerUrl());
9797
rootTarget.register(JacksonFeature.class);
9898
rootTarget.register(provider);
99-
if (configuration.getIsJerseyRequestLogged()) {
99+
if (configuration.getLogHttp()) {
100100
rootTarget.register(initLoggingFilter());
101101
}
102102
}
@@ -107,7 +107,7 @@ private LoggingFilter initLoggingFilter() {
107107
SLF4JBridgeHandler.install();
108108

109109
return new LoggingFilter(logger,
110-
configuration.getIsJSonEntityLogged());
110+
configuration.getLogHttpEntity());
111111
}
112112

113113

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#
2+
url=http://localhost:4444/jasperserver-pro
3+
#connectionTimeout=
4+
#readTimeout=
5+
jasperserverVersion=v6_0_0
6+
authenticationType=REST
7+
logHttp=true
8+
logHttpEntity=false
9+
restrictedHttpMethods=false
10+
contentMimeType=JSON
11+
acceptMimeType=JSON

0 commit comments

Comments
 (0)