Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Implement setHttpBasicAuth method
  • Loading branch information
dhleong committed May 5, 2013
commit 0afc92d5cc106bc77dba277bb723780e5fc4190a
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,7 @@ public void unannotatedMethodReturnsRestTemplate(TypeElement typeElement, IsVali
VariableElement secondParameter = parameters.get(1);
if (!(firstParameter.asType().toString().equals(CanonicalNameConstants.STRING) && secondParameter.asType().toString().equals(CanonicalNameConstants.STRING))) {
valid.invalidate();
annotationHelper.printError(enclosedElement, "The method to set headers or cookies should have only String parameters on a " + TargetAnnotationHelper.annotationName(Rest.class) + " annotated interface");
annotationHelper.printError(enclosedElement, "The method to set headers, cookies, or HTTP Basic Auth should have only String parameters on a " + TargetAnnotationHelper.annotationName(Rest.class) + " annotated interface");
}
} else {
valid.invalidate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,28 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHo
}
}

// Implement setHttpBasicAuth method
for (ExecutableElement method : methods) {
List<? extends VariableElement> parameters = method.getParameters();
if (parameters.size() == 2 && method.getReturnType().getKind() == TypeKind.VOID) {
VariableElement firstParameter = parameters.get(0);
VariableElement secondParameter = parameters.get(1);
if (firstParameter.asType().toString().equals(STRING) && secondParameter.asType().toString().equals(STRING) && method.getSimpleName().toString().equals("setHttpBasicAuth")) {
JMethod setBasicAuthMethod = holder.restImplementationClass.method(JMod.PUBLIC, codeModel.VOID, method.getSimpleName().toString());
setBasicAuthMethod.annotate(Override.class);

JVar userParam = setBasicAuthMethod.param(stringClass, firstParameter.getSimpleName().toString());
JVar passParam = setBasicAuthMethod.param(stringClass, secondParameter.getSimpleName().toString());

JClass basicAuthClass = eBeansHolder.refClass("org.springframework.http.HttpBasicAuthentication");
JInvocation basicAuthentication = JExpr._new(basicAuthClass).arg(userParam).arg(passParam);

setBasicAuthMethod.body().assign(_this().ref(holder.authenticationField), basicAuthentication);
break; // Only one implementation
}
}
}

// Implement setAuthentication method
for (ExecutableElement method : methods) {
List<? extends VariableElement> parameters = method.getParameters();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,4 +230,6 @@ ResponseEntity<Event[][]> getEventsArrayOfArrays2(String location, int year)
String getHeader(String headerName);

void setAuthentication(HttpAuthentication auth);

void setHttpBasicAuth(String username, String password);
}