Skip to content

Commit 72b0e15

Browse files
committed
Merge branch 'vincentjames501-develop' into develop
2 parents d4a09b6 + 96e10f9 commit 72b0e15

5 files changed

Lines changed: 46 additions & 1 deletion

File tree

‎AndroidAnnotations/androidannotations-api/src/main/java/org/androidannotations/api/rest/RestClientHeaders.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,11 @@ public interface RestClientHeaders {
6969
* @param password
7070
*/
7171
void setHttpBasicAuth(String user, String password);
72+
73+
/**
74+
* Sets the Authorization: Bearer header as documented in RFC6750
75+
*
76+
* @param token
77+
*/
78+
void setBearerAuth(String token);
7279
}

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/APTCodeModelHelper.java‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,10 @@ public List<ExecutableElement> getMethods(TypeElement typeElement) {
370370
}
371371

372372
public JMethod implementMethod(GeneratedClassHolder holder, List<ExecutableElement> methods, String methodName, String returnType, String... parameterTypes) {
373+
return implementMethod(holder, methods, methodName, returnType, false, parameterTypes);
374+
}
375+
376+
public JMethod implementMethod(GeneratedClassHolder holder, List<ExecutableElement> methods, String methodName, String returnType, boolean finalParams, String... parameterTypes) {
373377
// First get the ExecutableElement method object from the util function.
374378
ExecutableElement method = getMethod(methods, methodName, returnType, parameterTypes);
375379
JMethod jmethod = null;
@@ -384,9 +388,10 @@ public JMethod implementMethod(GeneratedClassHolder holder, List<ExecutableEleme
384388
jmethod.annotate(Override.class);
385389

386390
// Create the parameters.
391+
int paramMods = finalParams ? JMod.FINAL : JMod.NONE;
387392
for (int i = 0; i < method.getParameters().size(); i++) {
388393
VariableElement param = method.getParameters().get(i);
389-
jmethod.param(holder.refClass(parameterTypes[i]), param.getSimpleName().toString());
394+
jmethod.param(paramMods, holder.refClass(parameterTypes[i]), param.getSimpleName().toString());
390395
}
391396
}
392397

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/ValidatorHelper.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ public class ValidatorHelper {
8888

8989
private static final String METHOD_NAME_SET_ROOT_URL = "setRootUrl";
9090
private static final String METHOD_NAME_SET_AUTHENTICATION = "setAuthentication";
91+
private static final String METHOD_NAME_SET_BEARER_AUTH = "setBearerAuth";
9192
private static final String METHOD_NAME_GET_COOKIE = "getCookie";
9293
private static final String METHOD_NAME_GET_HEADER = "getHeader";
9394

@@ -752,6 +753,7 @@ public void unannotatedMethodReturnsRestTemplate(TypeElement typeElement, IsVali
752753
boolean foundGetRestTemplateMethod = false;
753754
boolean foundSetRestTemplateMethod = false;
754755
boolean foundSetAuthenticationMethod = false;
756+
boolean foundSetBearerAuthMethod = false;
755757
boolean foundSetRootUrlMethod = false;
756758
boolean foundGetCookieMethod = false;
757759
boolean foundGetHeaderMethod = false;
@@ -822,6 +824,8 @@ public void unannotatedMethodReturnsRestTemplate(TypeElement typeElement, IsVali
822824
foundSetRootUrlMethod = true;
823825
} else if (executableElement.getSimpleName().toString().equals(METHOD_NAME_SET_AUTHENTICATION) && !foundSetAuthenticationMethod) {
824826
foundSetAuthenticationMethod = true;
827+
} else if (executableElement.getSimpleName().toString().equals(METHOD_NAME_SET_BEARER_AUTH) && !foundSetBearerAuthMethod) {
828+
foundSetBearerAuthMethod = true;
825829
} else {
826830
valid.invalidate();
827831
annotationHelper.printError(enclosedElement, "The method to set a RestTemplate should have only one RestTemplate parameter on a " + TargetAnnotationHelper.annotationName(Rest.class) + " annotated interface");

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/holder/RestHolder.java‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import static com.sun.codemodel.JExpr._new;
1919
import static com.sun.codemodel.JExpr._this;
20+
import static com.sun.codemodel.JExpr.lit;
2021
import static com.sun.codemodel.JMod.FINAL;
2122
import static com.sun.codemodel.JMod.PUBLIC;
2223
import static org.androidannotations.helper.CanonicalNameConstants.REST_TEMPLATE;
@@ -34,12 +35,16 @@
3435
import org.androidannotations.process.ProcessHolder;
3536

3637
import com.sun.codemodel.ClassType;
38+
import com.sun.codemodel.JBlock;
3739
import com.sun.codemodel.JClass;
40+
import com.sun.codemodel.JDefinedClass;
3841
import com.sun.codemodel.JExpr;
42+
import com.sun.codemodel.JExpression;
3943
import com.sun.codemodel.JFieldVar;
4044
import com.sun.codemodel.JInvocation;
4145
import com.sun.codemodel.JMethod;
4246
import com.sun.codemodel.JMod;
47+
import com.sun.codemodel.JVar;
4348

4449
public class RestHolder extends BaseGeneratedClassHolder {
4550

@@ -78,6 +83,7 @@ private void implementMethods() {
7883

7984
// authentication
8085
implementSetBasicAuth(methods);
86+
implementSetBearerAuth(methods);
8187
implementSetAuthentication(methods);
8288

8389
// cookies and headers
@@ -132,6 +138,26 @@ private void implementSetBasicAuth(List<ExecutableElement> methods) {
132138
}
133139
}
134140

141+
private void implementSetBearerAuth(List<ExecutableElement> methods) {
142+
JMethod setBearerMethod = codeModelHelper.implementMethod(this, methods, "setBearerAuth", TypeKind.VOID.toString(), true, STRING);
143+
144+
if (setBearerMethod != null) {
145+
JVar tokenParamVar = setBearerMethod.params().get(0);
146+
JExpression tokenExpr = lit("Bearer ").plus(tokenParamVar);
147+
148+
JClass authClass = classes().HTTP_AUTHENTICATION;
149+
JDefinedClass anonymousHttpAuthClass = codeModel().anonymousClass(authClass);
150+
151+
JMethod getHeaderValueMethod = anonymousHttpAuthClass.method(JMod.PUBLIC, String.class, "getHeaderValue");
152+
getHeaderValueMethod.annotate(Override.class);
153+
JBlock getHeaderValueMethodBody = getHeaderValueMethod.body();
154+
getHeaderValueMethodBody._return(tokenExpr);
155+
156+
JBlock setBearerBody = setBearerMethod.body();
157+
setBearerBody.assign(_this().ref(getAuthenticationField()), _new(anonymousHttpAuthClass));
158+
}
159+
}
160+
135161
private void implementSetAuthentication(List<ExecutableElement> methods) {
136162
JMethod setAuthMethod = codeModelHelper.implementMethod(this, methods, "setAuthentication", TypeKind.VOID.toString(), CanonicalNameConstants.HTTP_AUTHENTICATION);
137163

‎AndroidAnnotations/functional-test-1-5/src/main/java/org/androidannotations/test15/rest/MyService.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.androidannotations.annotations.rest.Rest;
4242
import org.androidannotations.annotations.rest.SetsCookie;
4343
import org.androidannotations.api.rest.MediaType;
44+
import org.androidannotations.api.rest.RestClientHeaders;
4445

4546
// if defined, the rootUrl will be added as a prefix to every request
4647
@Rest(rootUrl = "http://company.com/ajax/services", converters = { MappingJacksonHttpMessageConverter.class }, interceptors = { RequestInterceptor.class })
@@ -274,4 +275,6 @@ public interface MyService {
274275
void setAuthentication(HttpAuthentication auth);
275276

276277
void setHttpBasicAuth(String username, String password);
278+
279+
void setBearerAuth(String token);
277280
}

0 commit comments

Comments
 (0)