Skip to content

Commit 5c48c37

Browse files
Merge pull request #145 from TanyaEf/master
New strategy of error handling, bugFix
2 parents f5fe2b6 + 560a419 commit 5c48c37

File tree

14 files changed

+533
-58
lines changed

14 files changed

+533
-58
lines changed

README.md

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ Table of Contents
5252
* [Viewing Server Attributes](#viewing-server-attributes).
5353
* [Setting Server Attributes](#setting-server-attributes).
5454
* [Deleting Server Attributes](#deleting-server-attributes).
55+
* [Getting attributes permissions](#getting-attributes-permissions).
5556
4. [The Roles Service](#the-roles-service).
5657
* [Searching for Roles](#searching-for-roles).
5758
* [Viewing a Role](#viewing-a-role).
@@ -629,10 +630,9 @@ The code below retrieves single attribute defined for the user:
629630
.attribute(attribute.getName())
630631
.get()
631632
.getEntity();
632-
633-
```
633+
```
634634
You may work work with user as object:
635-
```java
635+
```java
636636
CleintUser userObject = new ClientUser();
637637
userObject.setName("jasperadmin");
638638
HypermediaAttribute userAttribute = session
@@ -641,50 +641,48 @@ The code below retrieves single attribute defined for the user:
641641
.attribute(attribute.getName())
642642
.get()
643643
.getEntity();
644-
```
644+
```
645645
If user belong to organization you may specify it by name or as object:
646-
```java
646+
```java
647647
HypermediaAttribute userAttribute = session
648648
.attributesService()
649649
.forOrganization("organization_1")
650650
.forUser("jasperadmin")
651651
.attribute(attribute.getName())
652652
.get()
653653
.getEntity();
654-
655-
```
656-
```java
654+
657655
ClientTenant orgObject = new CleintTenant();
658656
orgObject.setId("someId");
657+
659658
HypermediaAttribute userAttribute = session
660659
.attributesService()
661660
.forOrganization(orgObject)
662661
.forUser("jasperadmin")
663662
.attribute(attribute.getName())
664663
.get()
665664
.getEntity();
666-
667-
```
668-
The code below retrieves the list of attributes defined for the user.
665+
```
666+
667+
The code below retrieves the list of attributes defined for the user.
669668
```java
670669
HypermediaAttribute userAttribute = session
671670
.attributesService()
672671
.forUser("jasperadmin")
673672
.attribute("attributeName")
674673
.get()
675674
.getEntity();
676-
677675
```
676+
678677
If user belong to organization you may specify organization:
679-
```java
678+
```java
680679
HypermediaAttribute userAttribute = session
681680
.attributesService()
682681
.forOrganization("organization_1")
683682
.forUser("jasperadmin")
684683
.attribute(attribute.getName())
685684
.get()
686685
.getEntity();
687-
688686
```
689687
You can get the list of attributes that includes the name and value of each attribute:
690688
```java
@@ -713,6 +711,7 @@ serverAttributes.setProfileAttributes(asList(new HypermediaAttribute(new ClientU
713711
.attributes("test_attribute_1","test_attribute_2")
714712
.createOrUpdate(serverAttributes);
715713
```
714+
716715
Be careful with definition of attribute names because the server uses different strategies for creating or updating attributes depending on list of attribute names, list of attributes and existing attributes on the server:
717716
1. if requested attribute name in `attributes()` method matches with attribute name of object defined in `createOrUpdate()` method and the attribute does not exist on the server it will be *created* on the server;
718717
2. if requested attribute name in `attributes()` method matches with attribute name of object defined in `createOrUpdate()` method and the attribute exists on the server it will be *updated* on the server;
@@ -901,6 +900,19 @@ session
901900
.attributes("max_threads", "admin_cell_phone")
902901
.delete();
903902
```
903+
###Getting attributes permissions
904+
Since `6.1` version of `JaspersoftReportServer` you can obtain attributes with permissions using additional parameter `setIncludePermissions()`:
905+
```java
906+
907+
HypermediaAttribute entity = session
908+
.attributesService()
909+
.attribute("attribute_name")
910+
.setIncludePermissions(true)
911+
.get()
912+
.getEntity();
913+
```
914+
Pay attention, the setting `setIncludePermission()` specify only the **server response format**, you can not set any permissions with this setting.
915+
904916
###The Roles Service
905917
It provides similar methods that allow you to list, view, create, modify, and delete roles. The new service provides improved search functionality, including user-based role searches. Because the role ID is used in the URL, this service can operate only on roles whose ID is less than 100 characters long and does not contain spaces or special symbols. Unlike resource IDs, the role ID is the role name and can be modified.
906918
####Searching for Roles
@@ -1716,6 +1728,23 @@ final Map<String, String> bundle = session
17161728
.getEntity();
17171729
```
17181730
###Exception handling
1731+
You can choose strategy of errors that are specified by status code of server response:
1732+
1. handling of errors directly. This is allied by default.
1733+
2. getting operation result in any case with null entity and handling error after calling `getEntity()` method:
1734+
```java
1735+
OperationResult<InputStream> result = session
1736+
.thumbnailsService()
1737+
.thumbnail()
1738+
.report("/")
1739+
.get(); // response status is 406, but exception won't be thrown
1740+
result.getEntity(); // the error will be handled and an exception will be thrown
1741+
```
1742+
To apply the second strategy set `handleErrors` property of `RestCleintConfiguration` to `false`:
1743+
```java
1744+
configuration.setHandleErrors(false);
1745+
```
1746+
or specify this property in configuration file (for details, read section [Configuration](https://github.com/Jaspersoft/jrs-rest-java-client/blob/master/README.md#configuration)).
1747+
17191748
You can customize exception handling for each endpoint. To do this you need to pass `com.jaspersoft.jasperserver.jaxrs.client.core.exceptions.handling.ErrorHandler` implementation to `JerseyRequestBuilder.buildRequest()` factory method.
17201749

17211750
JRS REST client exception handling system is based on `com.jaspersoft.jasperserver.jaxrs.client.core.exceptions.handling.ErrorHandler` interface. Its `void handleError(Response response)` method is responsible for all error handling logic. You can use existed handlers, define your own handlers or extend existed handlers.

src/main/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/attributes/SingleAttributeAdapter.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,24 @@ public void run() {
9191
return task;
9292
}
9393

94+
/**
95+
* @deprecated Replaced by {@link SingleAttributeAdapter#createOrUpdate(HypermediaAttribute)}.
96+
*/
9497
public OperationResult<HypermediaAttribute> createOrUpdate(ClientUserAttribute attribute) {
95-
return buildRequest()
96-
.setContentType(MimeTypeUtil.toCorrectContentMime(sessionStorage.getConfiguration(), "application/hal+{mime}"))
97-
.put(attribute);
98+
99+
return this.createOrUpdate(new HypermediaAttribute(attribute));
100+
}
101+
102+
103+
public OperationResult<HypermediaAttribute> createOrUpdate(HypermediaAttribute attribute) {
104+
JerseyRequest<HypermediaAttribute> request = buildRequest();
105+
if (includePermissions) {
106+
request.setContentType(MimeTypeUtil.toCorrectContentMime(sessionStorage.getConfiguration(), "application/hal+{mime}"));
107+
}
108+
return request.put(attribute);
98109
}
99110

100-
public <R> RequestExecution asyncCreateOrUpdate(final ClientUserAttribute userAttribute,
111+
public <R> RequestExecution asyncCreateOrUpdate(final HypermediaAttribute userAttribute,
101112
final Callback<OperationResult<HypermediaAttribute>, R> callback) {
102113
final JerseyRequest<HypermediaAttribute> request = buildRequest();
103114
RequestExecution task = new RequestExecution(new Runnable() {

src/main/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/thumbnails/SingleThumbnailAdapter.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.jaspersoft.jasperserver.jaxrs.client.apiadapters.AbstractAdapter;
44
import com.jaspersoft.jasperserver.jaxrs.client.core.JerseyRequest;
55
import com.jaspersoft.jasperserver.jaxrs.client.core.SessionStorage;
6+
import com.jaspersoft.jasperserver.jaxrs.client.core.exceptions.MandatoryParameterNotFoundException;
67
import com.jaspersoft.jasperserver.jaxrs.client.core.exceptions.handling.DefaultErrorHandler;
78
import com.jaspersoft.jasperserver.jaxrs.client.core.operationresult.OperationResult;
89

@@ -37,7 +38,10 @@ public OperationResult<InputStream> get() {
3738
}
3839

3940
private JerseyRequest<InputStream> request() {
41+
if (params.size() == 0) {
42+
throw new MandatoryParameterNotFoundException("URI of report should be specified");
43+
}
4044
return JerseyRequest.buildRequest(sessionStorage, InputStream.class,
41-
new String[]{"/thumbnails" + params.get("uri").get(0)}, new DefaultErrorHandler());
45+
new String[]{"/thumbnails", params.get("uri").get(0)}, new DefaultErrorHandler());
4246
}
4347
}

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@
2121

2222
package com.jaspersoft.jasperserver.jaxrs.client.core;
2323

24+
import com.jaspersoft.jasperserver.jaxrs.client.core.enums.RequestMethod;
2425
import com.jaspersoft.jasperserver.jaxrs.client.core.exceptions.JSClientWebException;
2526
import com.jaspersoft.jasperserver.jaxrs.client.core.exceptions.handling.DefaultErrorHandler;
2627
import com.jaspersoft.jasperserver.jaxrs.client.core.exceptions.handling.ErrorHandler;
28+
import com.jaspersoft.jasperserver.jaxrs.client.core.operationresult.NullEntityOperationResult;
2729
import com.jaspersoft.jasperserver.jaxrs.client.core.operationresult.OperationResult;
2830
import com.jaspersoft.jasperserver.jaxrs.client.core.operationresult.OperationResultFactory;
2931
import com.jaspersoft.jasperserver.jaxrs.client.core.operationresult.OperationResultFactoryImpl;
@@ -56,6 +58,7 @@ public class JerseyRequest<ResponseType> implements RequestBuilder<ResponseType>
5658
private WebTarget usersWebTarget;
5759
private String contentType;
5860
private String acceptType;
61+
private Boolean handleErrors;
5962

6063
protected JerseyRequest(SessionStorage sessionStorage, Class<ResponseType> responseClass) {
6164
operationResultFactory = new OperationResultFactoryImpl();
@@ -84,6 +87,7 @@ private void init(SessionStorage sessionStorage) {
8487

8588
usersWebTarget = sessionStorage.getRootTarget()
8689
.path("/rest_v2");
90+
handleErrors = configuration.getHandleErrors();
8791
}
8892

8993
public static <T> JerseyRequest<T> buildRequest(SessionStorage sessionStorage, Class<T> responseClass, String[] path) {
@@ -152,7 +156,7 @@ private OperationResult<ResponseType> executeRequest(int httpMethod, Invocation.
152156
private OperationResult<ResponseType> executeRequest(int httpMethod, Invocation.Builder request, Object entity) {
153157
Response response = null;
154158
if (restrictedHttpMethods && (httpMethod != POST || httpMethod != GET) ) {
155-
request.header("X-HTTP-Method-Override", httpMethod);
159+
request.header("X-HTTP-Method-Override", RequestMethod.values()[httpMethod].toString());
156160
response = request.post(Entity.entity(entity, contentType));
157161
} else {
158162
switch (httpMethod) {
@@ -171,14 +175,24 @@ private OperationResult<ResponseType> executeRequest(int httpMethod, Invocation.
171175
}
172176

173177
if (response != null && response.getStatus() == 411 && (httpMethod != POST || httpMethod != GET)) {
174-
request.header("X-HTTP-Method-Override", httpMethod);
178+
request.header("X-HTTP-Method-Override", RequestMethod.values()[httpMethod].toString());
175179
executeRequest(POST, request, entity);
176180
}
177181
}
178182

179183
if (response != null && response.getStatus() >= 400) {
180-
errorHandler.handleError(response);
181-
}
184+
if (handleErrors) {
185+
errorHandler.handleError(response);
186+
} else {
187+
return (responseGenericType != null) ? new NullEntityOperationResult<ResponseType>(response, responseGenericType, errorHandler)
188+
: new NullEntityOperationResult<ResponseType>(response, responseClass, errorHandler);
189+
}
190+
errorHandler.handleError(response);
191+
} else {
192+
return (responseGenericType != null) ? new NullEntityOperationResult<ResponseType>(response, responseGenericType, errorHandler)
193+
: new NullEntityOperationResult<ResponseType>(response, responseClass, errorHandler);
194+
}
195+
182196
return (responseGenericType != null) ? operationResultFactory.getOperationResult(response, responseGenericType)
183197
: operationResultFactory.getOperationResult(response, responseClass);
184198
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public class RestClientConfiguration {
5353
private Boolean restrictedHttpMethods = false;
5454
private Boolean logHttp = false;
5555
private Boolean logHttpEntity = false;
56+
private Boolean handleErrors = true;
5657
private Integer connectionTimeout;
5758
private Integer readTimeout;
5859
private TrustManager[] trustManagers;
@@ -145,6 +146,11 @@ public static RestClientConfiguration loadConfiguration(Properties properties) {
145146
configuration.setRestrictedHttpMethods(Boolean.valueOf(restrictedHttpMethods));
146147
}
147148

149+
String handleErrors = properties.getProperty("handleErrors");
150+
if (isStringValid(handleErrors) && BOOLEAN_PATTERN.matcher(handleErrors).matches()) {
151+
configuration.setHandleErrors(Boolean.valueOf(handleErrors));
152+
}
153+
148154
String contentMimeType = properties.getProperty("contentMimeType");
149155
if (isStringValid(contentMimeType)) {
150156
try {
@@ -216,6 +222,15 @@ public RestClientConfiguration setRestrictedHttpMethods(Boolean restrictedHttpMe
216222
return this;
217223
}
218224

225+
public Boolean getHandleErrors() {
226+
return handleErrors;
227+
}
228+
229+
public RestClientConfiguration setHandleErrors(Boolean handleErrors) {
230+
this.handleErrors = handleErrors;
231+
return this;
232+
}
233+
219234
public RestClientConfiguration setLogHttp(Boolean logHttp) {
220235
this.logHttp = logHttp;
221236
return this;

src/main/java/com/jaspersoft/jasperserver/jaxrs/client/core/enums/RequestMethod.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@
55
*/
66
public enum RequestMethod {
77
GET,
8-
POST;
8+
DELETE,
9+
POST,
10+
PUT;
911
}

src/main/java/com/jaspersoft/jasperserver/jaxrs/client/core/exceptions/MandatoryParameterNotFoundException.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ public class MandatoryParameterNotFoundException extends JSClientWebException {
3434
public final static String MANDATORY_PARAMETER_ERROR = "mandatory.parameter.error";
3535

3636
public MandatoryParameterNotFoundException() {
37-
super(); //To change body of overridden methods use File | Settings | File Templates.
37+
super();
3838
}
3939

4040
public MandatoryParameterNotFoundException(String message) {
41-
super(message); //To change body of overridden methods use File | Settings | File Templates.
41+
super(message);
4242
}
4343

4444
public MandatoryParameterNotFoundException(String message, List<ErrorDescriptor> errorDescriptors) {

src/main/java/com/jaspersoft/jasperserver/jaxrs/client/core/operationresult/NullEntityOperationResult.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,36 @@
2222
package com.jaspersoft.jasperserver.jaxrs.client.core.operationresult;
2323

2424

25+
import com.jaspersoft.jasperserver.jaxrs.client.core.exceptions.handling.ErrorHandler;
2526
import javax.ws.rs.core.GenericType;
2627
import javax.ws.rs.core.Response;
2728

2829
public class NullEntityOperationResult<T> extends OperationResult<T> {
30+
private ErrorHandler defaultErrorHandler;
2931

30-
public NullEntityOperationResult(Response response, Class entityClass) {
32+
public NullEntityOperationResult(Response response, Class<T> entityClass) {
3133
super(response, entityClass);
3234
}
3335

3436
public NullEntityOperationResult(Response response, GenericType<T> genericType) {
3537
super(response, genericType);
3638
}
3739

40+
public NullEntityOperationResult(Response response, Class<T> entityClass, ErrorHandler defaultErrorHandler) {
41+
super(response, entityClass);
42+
this.defaultErrorHandler = defaultErrorHandler;
43+
}
44+
45+
public NullEntityOperationResult(Response response, GenericType<T> genericType, ErrorHandler defaultErrorHandler) {
46+
super(response, genericType);
47+
this.defaultErrorHandler = defaultErrorHandler;
48+
}
49+
3850
@Override
3951
public T getEntity() {
52+
if (defaultErrorHandler != null) {
53+
defaultErrorHandler.handleError(response);
54+
}
4055
return null;
4156
}
4257
}

src/main/java/com/jaspersoft/jasperserver/jaxrs/client/core/operationresult/OperationResult.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ public OperationResult(Response response, Class<? extends T> entityClass) {
3838
this.entityClass = entityClass;
3939
}
4040

41-
4241
@SuppressWarnings("unchecked")
4342
public OperationResult(Response response, GenericType<T> genericEntity) {
4443
this.response = response;
@@ -50,6 +49,7 @@ public OperationResult(Response response, GenericType<T> genericEntity) {
5049
public T getEntity() {
5150
try {
5251
if (entity == null) {
52+
5353
if (genericEntity != null) {
5454
entity = response.readEntity(genericEntity);
5555
} else {
@@ -81,4 +81,8 @@ public Response getResponse() {
8181
public Class<? extends T> getEntityClass() {
8282
return entityClass;
8383
}
84+
85+
public int getResponseStatus() {
86+
return this.getResponse().getStatus();
87+
}
8488
}

src/test/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/thumbnails/SingleThumbnailAdapterTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public void should_return_proper_operation_result() {
113113
when(buildRequest(
114114
eq(sessionStorageMock),
115115
eq(InputStream.class),
116-
eq(new String[]{"/thumbnails/public/Samples/Reports/07g.RevenueDetailReport"}),
116+
eq(new String[]{"/thumbnails", "/public/Samples/Reports/07g.RevenueDetailReport"}),
117117
any(DefaultErrorHandler.class))).thenReturn(jerseyRequestMock);
118118
when(jerseyRequestMock.setAccept("image/jpeg")).thenReturn(requestBuilderMock);
119119
when(requestBuilderMock.get()).thenReturn(operationResultMock);
@@ -133,7 +133,7 @@ public void should_return_proper_operation_result() {
133133
buildRequest(
134134
eq(sessionStorageMock),
135135
eq(InputStream.class),
136-
eq(new String[]{"/thumbnails/public/Samples/Reports/07g.RevenueDetailReport"}),
136+
eq(new String[]{"/thumbnails", "/public/Samples/Reports/07g.RevenueDetailReport"}),
137137
any(DefaultErrorHandler.class));
138138

139139
}

0 commit comments

Comments
 (0)