Skip to content

Commit 29e43d7

Browse files
author
TanyaEf
committed
Resolved merge conflicts
2 parents 0e54097 + 82c2579 commit 29e43d7

File tree

9 files changed

+249
-22
lines changed

9 files changed

+249
-22
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.jaspersoft.jasperserver.jaxrs.client.apiadapters.core;
2+
3+
import com.jaspersoft.jasperserver.jaxrs.client.core.*;
4+
import org.testng.annotations.AfterMethod;
5+
import org.testng.annotations.BeforeMethod;
6+
import org.testng.annotations.Test;
7+
8+
import static org.testng.AssertJUnit.assertNotNull;
9+
10+
/**
11+
* @author Tetiana Iefimenko
12+
*/
13+
public class JasperserverRestClientIT {
14+
15+
16+
private RestClientConfiguration config;
17+
private JasperserverRestClient client;
18+
Session session;
19+
20+
@BeforeMethod
21+
public void before() {
22+
config = new RestClientConfiguration("http://localhost:4444/jasperserver-pro");
23+
config.setAcceptMimeType(MimeType.JSON);
24+
config.setContentMimeType(MimeType.JSON);
25+
config.setJrsVersion(JRSVersion.v6_0_0);
26+
client = new JasperserverRestClient(config);
27+
}
28+
29+
@Test
30+
public void should_return_session_via_j_sucurity_check() {
31+
config.setAuthenticationType(AuthenticationType.SPRING);
32+
session = client.authenticate("superuser", "superuser");
33+
assertNotNull(session);
34+
assertNotNull(session.getStorage().getSessionId());
35+
}
36+
37+
@Test
38+
public void should_return_session_via_rest_login() {
39+
session = client.authenticate("superuser", "superuser");
40+
assertNotNull(session);
41+
assertNotNull(session.getStorage().getSessionId());
42+
}
43+
44+
@Test
45+
public void should_return_session_via_basic_login() {
46+
config.setAuthenticationType(AuthenticationType.BASIC);
47+
session = client.authenticate("jasperadmin", "jasperadmin");
48+
assertNotNull(session);
49+
}
50+
51+
@AfterMethod
52+
public void after() {
53+
session.logout();
54+
}
55+
}
56+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright (C) 2005 - 2014 Jaspersoft Corporation. All rights reserved.
3+
* http://www.jaspersoft.com.
4+
*
5+
* Unless you have purchased a commercial license agreement from Jaspersoft,
6+
* the following license terms apply:
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Affero General Public License as
10+
* published by the Free Software Foundation, either version 3 of the
11+
* License, or (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU Affero General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Affero General Public License
19+
* along with this program.&nbsp; If not, see <http://www.gnu.org/licenses/>.
20+
*/
21+
22+
package com.jaspersoft.jasperserver.jaxrs.client.core;
23+
24+
public enum AuthenticationType {
25+
REST, SPRING, BASIC;
26+
}

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

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
package com.jaspersoft.jasperserver.jaxrs.client.core;
2222

2323
import com.jaspersoft.jasperserver.jaxrs.client.core.exceptions.handling.DefaultErrorHandler;
24+
import com.jaspersoft.jasperserver.jaxrs.client.filters.BasicAuthenticationFilter;
2425
import com.jaspersoft.jasperserver.jaxrs.client.filters.SessionOutputFilter;
26+
import org.glassfish.jersey.client.ClientProperties;
2527

2628
import javax.ws.rs.client.Entity;
2729
import javax.ws.rs.client.WebTarget;
@@ -38,11 +40,16 @@ public JasperserverRestClient(RestClientConfiguration configuration) {
3840
}
3941
this.configuration = configuration;
4042
}
43+
4144
public Session authenticate(String username, String password) {
42-
AuthenticationCredentials credentials = new AuthenticationCredentials(username, password);
43-
SessionStorage sessionStorage = new SessionStorage(configuration, credentials);
44-
login(sessionStorage);
45-
return new Session(sessionStorage);
45+
46+
if (username != null && username.length() > 0 && password != null && password.length() > 0) {
47+
AuthenticationCredentials credentials = new AuthenticationCredentials(username, password);
48+
SessionStorage sessionStorage = new SessionStorage(configuration, credentials);
49+
login(sessionStorage);
50+
return new Session(sessionStorage);
51+
}
52+
return null;
4653
}
4754

4855
public AnonymousSession getAnonymousSession() {
@@ -53,13 +60,22 @@ protected void login(SessionStorage storage) {
5360

5461
AuthenticationCredentials credentials = storage.getCredentials();
5562
WebTarget rootTarget = storage.getRootTarget();
63+
if (configuration.getAuthenticationType() == AuthenticationType.BASIC) {
64+
rootTarget.register(new BasicAuthenticationFilter(credentials));
65+
return;
66+
}
5667
Form form = new Form();
5768
form.param("j_username", credentials.getUsername()).param("j_password", credentials.getPassword());
58-
59-
WebTarget target = rootTarget.path("/rest/login");
69+
WebTarget target;
70+
if (configuration.getAuthenticationType() == AuthenticationType.SPRING) {
71+
target = rootTarget.path("/j_spring_security_check")
72+
.property(ClientProperties.FOLLOW_REDIRECTS, Boolean.FALSE);
73+
} else {
74+
target = rootTarget.path("/rest/login");
75+
}
6076
Response response = target.request().post(Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE));
6177
String sessionId = null;
62-
if (response.getStatus() == ResponseStatus.OK) {
78+
if (isResponseSuccessful(response)) {
6379
sessionId = response.getCookies().get("JSESSIONID").getValue();
6480
storage.setSessionId(sessionId);
6581
} else {
@@ -68,4 +84,14 @@ protected void login(SessionStorage storage) {
6884
rootTarget.register(new SessionOutputFilter(sessionId));
6985
}
7086

87+
private boolean isResponseSuccessful(Response response) {
88+
if ((configuration.getAuthenticationType() == AuthenticationType.SPRING
89+
|| configuration.getAuthenticationType() == AuthenticationType.REST)
90+
&& (response.getStatus() == ResponseStatus.FOUND
91+
|| response.getStatus() == ResponseStatus.OK)) {
92+
return true;
93+
}
94+
return false;
95+
}
96+
7197
}

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,19 @@
2121

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

24+
import org.apache.commons.logging.Log;
25+
import org.apache.commons.logging.LogFactory;
26+
27+
import javax.net.ssl.TrustManager;
28+
import javax.net.ssl.X509TrustManager;
29+
2430
import java.io.InputStream;
2531
import java.security.cert.CertificateException;
2632
import java.security.cert.X509Certificate;
2733
import java.util.Properties;
2834
import java.util.regex.Matcher;
2935
import java.util.regex.Pattern;
30-
import javax.net.ssl.TrustManager;
31-
import javax.net.ssl.X509TrustManager;
32-
import org.apache.commons.logging.Log;
33-
import org.apache.commons.logging.LogFactory;
36+
3437

3538
public class RestClientConfiguration {
3639

@@ -41,10 +44,12 @@ public class RestClientConfiguration {
4144
private MimeType contentMimeType = MimeType.JSON;
4245
private MimeType acceptMimeType = MimeType.JSON;
4346
private JRSVersion jrsVersion = JRSVersion.v5_5_0;
47+
private AuthenticationType authenticationType = AuthenticationType.REST;
48+
private Boolean restrictedHttpMethods = false;
4449
private TrustManager[] trustManagers;
4550
private Integer connectionTimeout;
51+
4652
private Integer readTimeout;
47-
private Boolean restrictedHttpMethods = false;
4853

4954
public RestClientConfiguration(String jasperReportsServerUrl) {
5055
this();
@@ -79,6 +84,13 @@ public void setJasperReportsServerUrl(String jasperReportsServerUrl) {
7984
throw new IllegalArgumentException("Given parameter is not a URL");
8085
this.jasperReportsServerUrl = jasperReportsServerUrl;
8186
}
87+
public AuthenticationType getAuthenticationType() {
88+
return authenticationType;
89+
}
90+
91+
public void setAuthenticationType(AuthenticationType authenticationType) {
92+
this.authenticationType = authenticationType;
93+
}
8294

8395
public MimeType getContentMimeType() {
8496
return contentMimeType;
@@ -104,7 +116,7 @@ public void setJrsVersion(JRSVersion jrsVersion) {
104116
this.jrsVersion = jrsVersion;
105117
}
106118

107-
public TrustManager[] getTrustManagers() {
119+
public TrustManager[] getTrustManagers() {
108120
return trustManagers;
109121
}
110122

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,24 @@
3838
import javax.ws.rs.core.Response;
3939

4040

41+
4142
public class Session extends AnonymousSession{
4243

44+
4345
public Session(SessionStorage sessionStorage) {
4446
super(sessionStorage);
4547
}
4648

4749
public void logout() {
48-
WebTarget target = storage.getRootTarget().path("/exituser.html");
49-
Response response = target.request().get();
50-
if (response.getStatus() >= 400) {
51-
new DefaultErrorHandler().handleError(response);
50+
if (storage.getConfiguration().getAuthenticationType() == AuthenticationType.BASIC) {
51+
storage.getCredentials().setUsername(null);
52+
storage.getCredentials().setPassword(null);
53+
} else {
54+
WebTarget target = storage.getRootTarget().path("/exituser.html");
55+
Response response = target.request().get();
56+
if (response.getStatus() >= 400) {
57+
new DefaultErrorHandler().handleError(response);
58+
}
5259
}
5360
}
5461

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,20 @@
2424
import com.fasterxml.jackson.databind.DeserializationFeature;
2525
import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
2626
import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
27-
import java.security.SecureRandom;
27+
28+
import org.glassfish.jersey.client.ClientProperties;
29+
import org.glassfish.jersey.jackson.JacksonFeature;
30+
31+
2832
import javax.net.ssl.HostnameVerifier;
2933
import javax.net.ssl.SSLContext;
3034
import javax.net.ssl.SSLSession;
3135
import javax.ws.rs.client.Client;
3236
import javax.ws.rs.client.ClientBuilder;
3337
import javax.ws.rs.client.WebTarget;
34-
import org.glassfish.jersey.client.ClientProperties;
35-
import org.glassfish.jersey.jackson.JacksonFeature;
38+
39+
import java.security.SecureRandom;
40+
3641

3742
public class SessionStorage {
3843

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.jaspersoft.jasperserver.jaxrs.client.filters;
2+
3+
import com.jaspersoft.jasperserver.jaxrs.client.core.AuthenticationCredentials;
4+
5+
import javax.ws.rs.client.ClientRequestContext;
6+
import javax.ws.rs.client.ClientRequestFilter;
7+
import javax.ws.rs.core.MultivaluedMap;
8+
import javax.xml.bind.DatatypeConverter;
9+
import java.io.IOException;
10+
import java.io.UnsupportedEncodingException;
11+
12+
/**
13+
* @author Tetiana Iefimenko
14+
*/
15+
public class BasicAuthenticationFilter implements ClientRequestFilter {
16+
private final AuthenticationCredentials credentials;
17+
18+
public BasicAuthenticationFilter(AuthenticationCredentials credentials) {
19+
this.credentials = credentials;
20+
21+
}
22+
23+
@Override
24+
public void filter(ClientRequestContext clientRequestContext) throws IOException {
25+
MultivaluedMap<String, Object> headers = clientRequestContext.getHeaders();
26+
if (credentials.getUsername() == null && credentials.getPassword() == null) {
27+
return;
28+
}
29+
final String basicAuthentication = getBasicAuthentication();
30+
headers.add("Authorization", basicAuthentication);
31+
32+
}
33+
34+
private String getBasicAuthentication() {
35+
String token = this.credentials.getUsername() + ":" + this.credentials.getPassword();
36+
try {
37+
return "Basic " + DatatypeConverter.printBase64Binary(token.getBytes("UTF-8"));
38+
} catch (UnsupportedEncodingException ex) {
39+
throw new IllegalStateException("Cannot encode with UTF-8", ex);
40+
}
41+
}
42+
}
43+

src/test/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/serverInfo/ServerInfoServiceTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,6 @@ public void should_return_proper_server_version() throws Exception {
151151
PowerMockito.verifyPrivate(service, times(1)).invoke("buildServerInfoRequest", "/since");
152152
assertSame(retrieved, operationResultMock);
153153
}
154-
155154
@Test
156155
public void should_return_proper_server_details() {
157156

0 commit comments

Comments
 (0)