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

Commit

Permalink
Release 0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
machaval committed May 10, 2019
1 parent fc15653 commit e0f90c6
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 44 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.mule.raml.codegen</groupId>
<artifactId>raml-client-generator-parent</artifactId>
<version>0.3-SNAPSHOT</version>
<version>0.3</version>
<packaging>pom</packaging>

<modules>
Expand Down
2 changes: 1 addition & 1 deletion raml-client-generator-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<parent>
<groupId>org.mule.raml.codegen</groupId>
<artifactId>raml-client-generator-parent</artifactId>
<version>0.3-SNAPSHOT</version>
<version>0.3</version>
</parent>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@

public class Jersey2RestClientGeneratorImpl implements RestClientGenerator {

private final Logger logger = LoggerFactory.getLogger(getClass());
private final Logger logger = LoggerFactory.getLogger(getClass());

private static final String BODY_PARAM_NAME = "body";
private static final String HEADERS_PARAM_NAME = "headers";
private static final String TOKEN_PARAM_NAME = "authorizationToken";
Expand All @@ -64,22 +64,22 @@ public class Jersey2RestClientGeneratorImpl implements RestClientGenerator {

@Override
public void callHttpMethod(@Nonnull JCodeModel cm, @Nonnull JDefinedClass resourceClass, @Nonnull JType returnType, @Nullable JBodyType bodyType, @Nullable JType queryParameterType, @Nullable JType headerParameterType, @Nonnull Action action, ApiModel apiModel) {
callHttpMethod(cm, resourceClass, returnType, OutputVersion.v1, bodyType, queryParameterType, headerParameterType, action, apiModel);
callHttpMethod(cm, resourceClass, returnType, OutputVersion.v1, bodyType, queryParameterType, headerParameterType, action, apiModel);
}

@Override
public void callHttpMethod(@Nonnull JCodeModel cm, @Nonnull JDefinedClass resourceClass, @Nonnull JType returnType, @Nonnull OutputVersion outputVersion, @Nullable JBodyType bodyType, @Nullable JType queryParameterType, @Nullable JType headerParameterType, @Nonnull Action action, ApiModel apiModel) {
if (action.getType() == ActionType.PATCH) {
logger.warn("Patch is not supported");
logger.warn("Patch is not supported");
return;
}

// Declare the method with the required inputs
JMethod actionMethod;
if (outputVersion.ordinal() >= OutputVersion.v2.ordinal() ) {
actionMethod = resourceClass.method(JMod.PUBLIC, responseClass.narrow(returnType), action.getType().name().toLowerCase());
if (outputVersion.ordinal() >= OutputVersion.v2.ordinal()) {
actionMethod = resourceClass.method(JMod.PUBLIC, responseClass.narrow(returnType), action.getType().name().toLowerCase());
} else {
actionMethod = resourceClass.method(JMod.PUBLIC, returnType, action.getType().name().toLowerCase());
actionMethod = resourceClass.method(JMod.PUBLIC, returnType, action.getType().name().toLowerCase());
}
if (StringUtils.isNotBlank(action.getDescription())) {
actionMethod.javadoc().add(action.getDescription());
Expand All @@ -106,7 +106,8 @@ public void callHttpMethod(@Nonnull JCodeModel cm, @Nonnull JDefinedClass resour
}

final JVar authenticationParam;
if (isOauth20SecuredBy(action.getResource()) || isOauth20SecuredBy(apiModel)) {
//If global but not locally specified
if (isOauth20SecuredBy(action.getResource()) || (isOauth20SecuredBy(apiModel) && action.getResource().getSecuredBy().isEmpty())) {
authenticationParam = actionMethod.param(String.class, TOKEN_PARAM_NAME);
} else {
authenticationParam = null;
Expand Down Expand Up @@ -192,36 +193,36 @@ public void callHttpMethod(@Nonnull JCodeModel cm, @Nonnull JDefinedClass resour

ifBlock._throw(
JExpr._new(exceptionClass)
.arg(statusInfo.invoke("getStatusCode"))
.arg(statusInfo.invoke("getReasonPhrase"))
.arg(responseVal.invoke("getStringHeaders"))
.arg(responseVal)
);
.arg(statusInfo.invoke("getStatusCode"))
.arg(statusInfo.invoke("getReasonPhrase"))
.arg(responseVal.invoke("getStringHeaders"))
.arg(responseVal)
);

if (returnType != cm.VOID) {
JInvocation jInvocation;
JInvocation jInvocation;
if (returnType.equals(cm.ref(Object.class))) {
jInvocation = responseVal.invoke("getEntity");
jInvocation = responseVal.invoke("getEntity");
} else {
if (returnType instanceof JClass && !((JClass) returnType).getTypeParameters().isEmpty()) {
final JClass narrow = cm.anonymousClass(cm.ref(GenericType.class).narrow(returnType));
jInvocation = responseVal.invoke("readEntity").arg(JExpr._new(narrow));
} else {
jInvocation = responseVal.invoke("readEntity").arg(JExpr.dotclass(cm.ref(returnType.fullName())));
jInvocation = responseVal.invoke("readEntity").arg(JExpr.dotclass(cm.ref(returnType.fullName())));
}
}
if ( outputVersion.ordinal() >= OutputVersion.v2.ordinal() ) {
JInvocation apiResponseInvocation = JExpr._new(responseClass.narrow(returnType));
apiResponseInvocation.arg(jInvocation);
apiResponseInvocation.arg(responseVal.invoke("getStringHeaders"));

if (outputVersion.ordinal() >= OutputVersion.v2.ordinal()) {
JInvocation apiResponseInvocation = JExpr._new(responseClass.narrow(returnType));
apiResponseInvocation.arg(jInvocation);
apiResponseInvocation.arg(responseVal.invoke("getStringHeaders"));
apiResponseInvocation.arg(responseVal);
final JVar apiResponseVal = body.decl(responseClass.narrow(returnType), "apiResponse", apiResponseInvocation);
body._return(apiResponseVal);
} else {
body._return(jInvocation);
body._return(jInvocation);
}
} else if ( outputVersion.ordinal() >= OutputVersion.v2.ordinal() ) {
} else if (outputVersion.ordinal() >= OutputVersion.v2.ordinal()) {
JInvocation apiResponseInvocation = JExpr._new(responseClass.narrow(Void.class));
apiResponseInvocation.arg(JExpr._null());
apiResponseInvocation.arg(responseVal.invoke("getStringHeaders"));
Expand Down Expand Up @@ -257,7 +258,7 @@ public void buildCustomException(JCodeModel cm, String basePackage, String apiNa
JFieldVar statusCodeField = customExceptionClass.field(JMod.PRIVATE, Integer.TYPE, "statusCode");
JFieldVar reasonField = customExceptionClass.field(JMod.PRIVATE, String.class, "reason");
JClass rawHeadersClass = cm.ref(MultivaluedMap.class)
.narrow(cm.ref(String.class), cm.ref(String.class));
.narrow(cm.ref(String.class), cm.ref(String.class));
JFieldVar headersField = customExceptionClass.field(JMod.PRIVATE, rawHeadersClass, "headers");
JFieldVar responseField = customExceptionClass.field(JMod.PRIVATE, Response.class, "response");

Expand All @@ -281,7 +282,7 @@ public void buildCustomException(JCodeModel cm, String basePackage, String apiNa
statusCodeParameter = containerConstructorWithoutHeadersAndResponse.param(Integer.TYPE, "statusCode");
reasonParameter = containerConstructorWithoutHeadersAndResponse.param(String.class, "reason");
JInvocation thisInvocation = JExpr.invoke("this")
.arg(statusCodeParameter).arg(reasonParameter).arg(JExpr._null()).arg(JExpr._null());
.arg(statusCodeParameter).arg(reasonParameter).arg(JExpr._null()).arg(JExpr._null());
containerConstructorWithoutHeadersAndResponse.body().add(thisInvocation);

JMethod statusCodeGetterMethod = customExceptionClass.method(JMod.PUBLIC, Integer.TYPE, "getStatusCode");
Expand All @@ -301,42 +302,42 @@ public void buildCustomException(JCodeModel cm, String basePackage, String apiNa
exceptionClass = cm.ref(RuntimeException.class);
}
}

@Override
public void buildCustomResponse(JCodeModel cm, String basePackage, ApiModel apiModel) throws JClassAlreadyExistsException {
String apiName = apiModel.getTitle();

String apiName = apiModel.getTitle();

JDefinedClass customResponseClass = cm._class(basePackage + "." + "responses" + "." + NameHelper.toValidClassName(apiName) + "Response");
JTypeVar genericType = customResponseClass.generify("T");

JFieldVar bodyField = customResponseClass.field(JMod.PRIVATE, genericType, "body");
JClass rawHeadersClass = cm.ref(MultivaluedMap.class)
.narrow(cm.ref(String.class), cm.ref(String.class));
.narrow(cm.ref(String.class), cm.ref(String.class));
JFieldVar headersField = customResponseClass.field(JMod.PRIVATE, rawHeadersClass, "headers");
JFieldVar responseField = customResponseClass.field(JMod.PRIVATE, Response.class, "response");


JMethod containerConstructor = customResponseClass.constructor(JMod.PUBLIC);

JVar bodyParameter = containerConstructor.param(genericType, "body");
containerConstructor.body().assign(JExpr._this().ref(bodyField), bodyParameter);

JVar headersParameter = containerConstructor.param(rawHeadersClass, "headers");
containerConstructor.body().assign(JExpr._this().ref(headersField), headersParameter);

JVar responseParameter = containerConstructor.param(Response.class, "response");
containerConstructor.body().assign(JExpr._this().ref(responseField), responseParameter);

JMethod bodyGetterMethod = customResponseClass.method(JMod.PUBLIC, genericType, "getBody");
bodyGetterMethod.body()._return(JExpr._this().ref(bodyField));

JMethod headersGetterMethod = customResponseClass.method(JMod.PUBLIC, rawHeadersClass, "getHeaders");
headersGetterMethod.body()._return(JExpr._this().ref(headersField));

JMethod responseGetterMethod = customResponseClass.method(JMod.PUBLIC, Response.class, "getResponse");
responseGetterMethod.body()._return(JExpr._this().ref(responseField));

responseClass = customResponseClass;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static boolean isOauth20SecuredBy(ApiModel apiModel) {
private static boolean isOauth20(List<SecurityScheme> securedBy) {
boolean containsOauth = false;
for (SecurityScheme securityScheme : securedBy) {
containsOauth = containsOauth || securityScheme.getType().equals(SecuritySchemesHelper.OAUTH_20);
containsOauth = containsOauth || (securityScheme != null && securityScheme.getType().equals(SecuritySchemesHelper.OAUTH_20));
}

return containsOauth;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public List<SecurityScheme> getSecuredBy() {
for (SecuritySchemeRef securitySchemeRef : securitySchemeRefs) {
if (securitySchemeRef != null) {
result.add(new SecuritySchemeImpl(securitySchemeRef.securityScheme()));
} else {
//Add null to specify empty securedBy
result.add(null);
}
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@ securedBy: [ oauth_2_0 ]
example: |
[{"user" : "Mariano"}]
/login:
securedBy:
securedBy: [null]
get:
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
package oauth_override.resource.api.login;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status.Family;
import oauth_override.exceptions.CoreServicesAPIReferenceException;

public class Login {

Expand All @@ -26,4 +31,14 @@ private String getBaseUri() {
return _baseUrl;
}

public void get() {
WebTarget target = this._client.target(getBaseUri());
final javax.ws.rs.client.Invocation.Builder invocationBuilder = target.request(MediaType.APPLICATION_JSON_TYPE);
Response response = invocationBuilder.get();
if (response.getStatusInfo().getFamily()!= Family.SUCCESSFUL) {
Response.StatusType statusInfo = response.getStatusInfo();
throw new CoreServicesAPIReferenceException(statusInfo.getStatusCode(), statusInfo.getReasonPhrase(), response.getStringHeaders(), response);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@ securedBy: [ oauth_2_0 ]
example: |
[{"user" : "Mariano"}]
/login:
securedBy:
securedBy: [null]
get:
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
package oauth_override.resource.api.login;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status.Family;
import oauth_override.exceptions.CoreServicesAPIReferenceException;
import oauth_override.responses.CoreServicesAPIReferenceResponse;

public class Login {

Expand All @@ -26,4 +32,16 @@ private String getBaseUri() {
return _baseUrl;
}

public CoreServicesAPIReferenceResponse<Void> get() {
WebTarget target = this._client.target(getBaseUri());
final javax.ws.rs.client.Invocation.Builder invocationBuilder = target.request(MediaType.APPLICATION_JSON_TYPE);
Response response = invocationBuilder.get();
if (response.getStatusInfo().getFamily()!= Family.SUCCESSFUL) {
Response.StatusType statusInfo = response.getStatusInfo();
throw new CoreServicesAPIReferenceException(statusInfo.getStatusCode(), statusInfo.getReasonPhrase(), response.getStringHeaders(), response);
}
CoreServicesAPIReferenceResponse<Void> apiResponse = new CoreServicesAPIReferenceResponse<Void>(null, response.getStringHeaders(), response);
return apiResponse;
}

}
2 changes: 1 addition & 1 deletion raml-client-generator-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<parent>
<groupId>org.mule.raml.codegen</groupId>
<artifactId>raml-client-generator-parent</artifactId>
<version>0.3-SNAPSHOT</version>
<version>0.3</version>
</parent>

<properties>
Expand Down
2 changes: 1 addition & 1 deletion raml-client-generator-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<parent>
<groupId>org.mule.raml.codegen</groupId>
<artifactId>raml-client-generator-parent</artifactId>
<version>0.3-SNAPSHOT</version>
<version>0.3</version>
</parent>

<dependencies>
Expand Down

0 comments on commit e0f90c6

Please sign in to comment.