Skip to content
This repository was archived by the owner on Jan 21, 2024. It is now read-only.

Commit 7b3082a

Browse files
ca-stefan-cordesmachaval
authored andcommitted
43 securedby with uses (#44)
* #43 added testcase for securedby_with_uses (test will FAIL) TODO implementation needs to be changed to generate constructor with user/password. * #43 forgot to add "securedBy:" (test will still FAIL) TODO implementation needs to be changed to generate constructor with user/password. * #43 find SecurityScheme referenced by SecuredBy as well (e.g. via uses:) TODO implementation needs to be changed to generate constructor with user/password.
1 parent 06e2eb2 commit 7b3082a

File tree

17 files changed

+682
-0
lines changed

17 files changed

+682
-0
lines changed

raml-client-generator-core/src/main/java/org/mule/client/codegen/utils/SecuritySchemesHelper.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ public static List<Pair<String, SecurityScheme>> getSupportedSecuritySchemes(Api
3131
supportedSecuritySchemes.add(new ImmutablePair<>(schemeMap.getType(), schemeMap));
3232
}
3333
}
34+
List<SecurityScheme> securedBy = raml.getSecuredBy();
35+
for (SecurityScheme securedBySecurityScheme : securedBy) {
36+
if (SUPPORTED_SECURITY_SCHEMES.contains(securedBySecurityScheme.getType())) {
37+
supportedSecuritySchemes.add(new ImmutablePair<>(securedBySecurityScheme.getType(), securedBySecurityScheme));
38+
}
39+
}
3440
return supportedSecuritySchemes;
3541
}
3642

raml-client-generator-core/src/test/java/org/mule/client/codegen/RamlJavaClientGeneratorTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public static Iterable<Object[]> folders() {
5151
{"oauth_override"},
5252
{"recursive_type"},
5353
{"simple"},
54+
{"securedby_with_uses"},
5455
{"sub_resource_on_same_line"},
5556
{"same_path_multiple_times"},
5657
{"type_decl"},
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#%RAML 1.0
2+
version: api
3+
title: BuyosExperienceLayer
4+
baseUri: http://localhost:24045/api
5+
6+
uses:
7+
candaCommons: canda-commons/1.0.8/canda-commons.raml
8+
9+
securedBy: [candaCommons.basicAuth]
10+
11+
# Workaround as https://github.com/mulesoft-labs/raml-java-client-generator
12+
# does not detect securitySchemes placed in the "uses" section.
13+
#securitySchemes:
14+
# clientid:
15+
# description: Client ID Policy
16+
# type: Basic Authentication
17+
18+
#securedBy: [clientid]
19+
20+
/users/{id}:
21+
get:
22+
responses:
23+
200:
24+
body:
25+
application/json:
26+
example: |
27+
{ "userId": "foo" }
28+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#%RAML 1.0 Library
2+
3+
securitySchemes:
4+
basicAuth:
5+
displayName: Basic Authentication
6+
description: This API supports Basic Authentication. The client has to provide an "Authorization" header with valid credentials.
7+
type: Basic Authentication
8+
describedBy:
9+
headers:
10+
Authorization:
11+
description: Used to send valid credentials.
12+
type: string
13+
responses:
14+
401:
15+
description: Credentials are missing or could not be validated by the server.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
package securedby_with_uses.api;
3+
4+
import javax.ws.rs.client.ClientBuilder;
5+
import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
6+
import securedby_with_uses.resource.users.Users;
7+
8+
public class BuyosExperienceLayerClient {
9+
10+
private static java.lang.String username;
11+
private static java.lang.String password;
12+
private java.lang.String _baseUrl;
13+
public final Users users;
14+
15+
public BuyosExperienceLayerClient(java.lang.String baseUrl, java.lang.String username, java.lang.String password) {
16+
this.username = username;
17+
this.password = password;
18+
_baseUrl = baseUrl;
19+
users = new Users(getBaseUri(), getClient());
20+
}
21+
22+
public BuyosExperienceLayerClient(java.lang.String username, java.lang.String password) {
23+
this("http://localhost:24045/api", username, password);
24+
}
25+
26+
protected javax.ws.rs.client.Client getClient() {
27+
final javax.ws.rs.client.Client _client = ClientBuilder.newClient();
28+
_client.register(HttpAuthenticationFeature.basic(username, password));
29+
return _client;
30+
}
31+
32+
protected java.lang.String getBaseUri() {
33+
return _baseUrl;
34+
}
35+
36+
public static BuyosExperienceLayerClient create(java.lang.String baseUrl, java.lang.String username, java.lang.String password) {
37+
return new BuyosExperienceLayerClient(baseUrl, username, password);
38+
}
39+
40+
public static BuyosExperienceLayerClient create(java.lang.String username, java.lang.String password) {
41+
return new BuyosExperienceLayerClient(username, password);
42+
}
43+
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
package securedby_with_uses.exceptions;
3+
4+
import javax.ws.rs.core.MultivaluedMap;
5+
import javax.ws.rs.core.Response;
6+
7+
public class BuyosExperienceLayerException
8+
extends RuntimeException
9+
{
10+
11+
private int statusCode;
12+
private String reason;
13+
private MultivaluedMap<String, String> headers;
14+
private Response response;
15+
16+
public BuyosExperienceLayerException(int statusCode, String reason, MultivaluedMap<String, String> headers, Response response) {
17+
super(reason);
18+
this.statusCode = statusCode;
19+
this.reason = reason;
20+
this.headers = headers;
21+
this.response = response;
22+
}
23+
24+
public BuyosExperienceLayerException(int statusCode, String reason) {
25+
this(statusCode, reason, null, null);
26+
}
27+
28+
public int getStatusCode() {
29+
return this.statusCode;
30+
}
31+
32+
public String getReason() {
33+
return this.reason;
34+
}
35+
36+
public MultivaluedMap<String, String> getHeaders() {
37+
return this.headers;
38+
}
39+
40+
public Response getResponse() {
41+
return this.response;
42+
}
43+
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
package securedby_with_uses.resource.users;
3+
4+
import javax.ws.rs.client.Client;
5+
import securedby_with_uses.resource.users.id.Id;
6+
7+
public class Users {
8+
9+
private String _baseUrl;
10+
private Client _client;
11+
12+
public Users() {
13+
_baseUrl = null;
14+
_client = null;
15+
}
16+
17+
public Users(String baseUrl, Client _client) {
18+
_baseUrl = (baseUrl +"/users");
19+
this._client = _client;
20+
}
21+
22+
protected Client getClient() {
23+
return this._client;
24+
}
25+
26+
private String getBaseUri() {
27+
return _baseUrl;
28+
}
29+
30+
public Id id(String id) {
31+
return new Id(getBaseUri(), getClient(), id);
32+
}
33+
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
package securedby_with_uses.resource.users.id;
3+
4+
import java.net.URLEncoder;
5+
import javax.ws.rs.client.Client;
6+
import javax.ws.rs.client.WebTarget;
7+
import javax.ws.rs.core.MediaType;
8+
import javax.ws.rs.core.Response;
9+
import javax.ws.rs.core.Response.Status.Family;
10+
import securedby_with_uses.exceptions.BuyosExperienceLayerException;
11+
12+
public class Id {
13+
14+
private String _baseUrl;
15+
private Client _client;
16+
17+
public Id() {
18+
_baseUrl = null;
19+
_client = null;
20+
}
21+
22+
public Id(String baseUrl, Client _client, String uriParam) {
23+
_baseUrl = (baseUrl +("/"+ URLEncoder.encode(uriParam)));
24+
this._client = _client;
25+
}
26+
27+
protected Client getClient() {
28+
return this._client;
29+
}
30+
31+
private String getBaseUri() {
32+
return _baseUrl;
33+
}
34+
35+
public securedby_with_uses.resource.users.id.model.IdGETResponse get() {
36+
WebTarget target = this._client.target(getBaseUri());
37+
final javax.ws.rs.client.Invocation.Builder invocationBuilder = target.request(MediaType.APPLICATION_JSON_TYPE);
38+
Response response = invocationBuilder.get();
39+
if (response.getStatusInfo().getFamily()!= Family.SUCCESSFUL) {
40+
Response.StatusType statusInfo = response.getStatusInfo();
41+
throw new BuyosExperienceLayerException(statusInfo.getStatusCode(), statusInfo.getReasonPhrase(), response.getStringHeaders(), response);
42+
}
43+
return response.readEntity(securedby_with_uses.resource.users.id.model.IdGETResponse.class);
44+
}
45+
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
2+
package securedby_with_uses.resource.users.id.model;
3+
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
import com.fasterxml.jackson.annotation.JsonAnyGetter;
7+
import com.fasterxml.jackson.annotation.JsonAnySetter;
8+
import com.fasterxml.jackson.annotation.JsonIgnore;
9+
import com.fasterxml.jackson.annotation.JsonInclude;
10+
import com.fasterxml.jackson.annotation.JsonProperty;
11+
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
12+
13+
@JsonInclude(JsonInclude.Include.NON_NULL)
14+
@JsonPropertyOrder({
15+
"userId"
16+
})
17+
public class IdGETResponse {
18+
19+
@JsonProperty("userId")
20+
private String userId;
21+
@JsonIgnore
22+
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
23+
24+
/**
25+
* No args constructor for use in serialization
26+
*
27+
*/
28+
public IdGETResponse() {
29+
}
30+
31+
/**
32+
*
33+
* @param userId
34+
*/
35+
public IdGETResponse(String userId) {
36+
super();
37+
this.userId = userId;
38+
}
39+
40+
@JsonProperty("userId")
41+
public String getUserId() {
42+
return userId;
43+
}
44+
45+
@JsonProperty("userId")
46+
public void setUserId(String userId) {
47+
this.userId = userId;
48+
}
49+
50+
public IdGETResponse withUserId(String userId) {
51+
this.userId = userId;
52+
return this;
53+
}
54+
55+
@JsonAnyGetter
56+
public Map<String, Object> getAdditionalProperties() {
57+
return this.additionalProperties;
58+
}
59+
60+
@JsonAnySetter
61+
public void setAdditionalProperty(String name, Object value) {
62+
this.additionalProperties.put(name, value);
63+
}
64+
65+
public IdGETResponse withAdditionalProperty(String name, Object value) {
66+
this.additionalProperties.put(name, value);
67+
return this;
68+
}
69+
70+
@Override
71+
public String toString() {
72+
StringBuilder sb = new StringBuilder();
73+
sb.append(IdGETResponse.class.getName()).append('@').append(Integer.toHexString(System.identityHashCode(this))).append('[');
74+
sb.append("userId");
75+
sb.append('=');
76+
sb.append(((this.userId == null)?"<null>":this.userId));
77+
sb.append(',');
78+
sb.append("additionalProperties");
79+
sb.append('=');
80+
sb.append(((this.additionalProperties == null)?"<null>":this.additionalProperties));
81+
sb.append(',');
82+
if (sb.charAt((sb.length()- 1)) == ',') {
83+
sb.setCharAt((sb.length()- 1), ']');
84+
} else {
85+
sb.append(']');
86+
}
87+
return sb.toString();
88+
}
89+
90+
@Override
91+
public int hashCode() {
92+
int result = 1;
93+
result = ((result* 31)+((this.userId == null)? 0 :this.userId.hashCode()));
94+
result = ((result* 31)+((this.additionalProperties == null)? 0 :this.additionalProperties.hashCode()));
95+
return result;
96+
}
97+
98+
@Override
99+
public boolean equals(Object other) {
100+
if (other == this) {
101+
return true;
102+
}
103+
if ((other instanceof IdGETResponse) == false) {
104+
return false;
105+
}
106+
IdGETResponse rhs = ((IdGETResponse) other);
107+
return (((this.userId == rhs.userId)||((this.userId!= null)&&this.userId.equals(rhs.userId)))&&((this.additionalProperties == rhs.additionalProperties)||((this.additionalProperties!= null)&&this.additionalProperties.equals(rhs.additionalProperties))));
108+
}
109+
110+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#%RAML 1.0
2+
version: api
3+
title: BuyosExperienceLayer
4+
baseUri: http://localhost:24045/api
5+
6+
uses:
7+
candaCommons: canda-commons/1.0.8/canda-commons.raml
8+
9+
securedBy: [candaCommons.basicAuth]
10+
11+
# Workaround as https://github.com/mulesoft-labs/raml-java-client-generator
12+
# does not detect securitySchemes placed in the "uses" section.
13+
#securitySchemes:
14+
# clientid:
15+
# description: Client ID Policy
16+
# type: Basic Authentication
17+
18+
#securedBy: [clientid]
19+
20+
/users/{id}:
21+
get:
22+
responses:
23+
200:
24+
body:
25+
application/json:
26+
example: |
27+
{ "userId": "foo" }
28+

0 commit comments

Comments
 (0)