|
15 | 15 | */ |
16 | 16 | package org.androidannotations.holder; |
17 | 17 |
|
18 | | -import static com.sun.codemodel.JExpr._new; |
19 | 18 | import static com.sun.codemodel.JExpr._this; |
| 19 | +import static com.sun.codemodel.JExpr.lit; |
| 20 | +import static com.sun.codemodel.JExpr.ref; |
| 21 | +import static com.sun.codemodel.JExpr._new; |
20 | 22 | import static com.sun.codemodel.JMod.FINAL; |
21 | 23 | import static com.sun.codemodel.JMod.PUBLIC; |
22 | 24 | import static org.androidannotations.helper.CanonicalNameConstants.REST_TEMPLATE; |
|
28 | 30 | import javax.lang.model.element.TypeElement; |
29 | 31 | import javax.lang.model.type.TypeKind; |
30 | 32 |
|
31 | | -import org.androidannotations.api.rest.RestErrorHandler; |
32 | | -import org.androidannotations.helper.APTCodeModelHelper; |
33 | | -import org.androidannotations.helper.CanonicalNameConstants; |
34 | | -import org.androidannotations.helper.ModelConstants; |
35 | | -import org.androidannotations.process.ProcessHolder; |
36 | | - |
37 | 33 | import com.sun.codemodel.ClassType; |
| 34 | +import com.sun.codemodel.JBlock; |
38 | 35 | import com.sun.codemodel.JClass; |
| 36 | +import com.sun.codemodel.JDefinedClass; |
39 | 37 | import com.sun.codemodel.JExpr; |
40 | 38 | import com.sun.codemodel.JFieldVar; |
41 | 39 | import com.sun.codemodel.JInvocation; |
42 | 40 | import com.sun.codemodel.JMethod; |
43 | 41 | import com.sun.codemodel.JMod; |
| 42 | +import org.androidannotations.api.rest.RestErrorHandler; |
| 43 | +import org.androidannotations.helper.APTCodeModelHelper; |
| 44 | +import org.androidannotations.helper.CanonicalNameConstants; |
| 45 | +import org.androidannotations.helper.ModelConstants; |
| 46 | +import org.androidannotations.process.ProcessHolder; |
44 | 47 |
|
45 | 48 | public class RestHolder extends BaseGeneratedClassHolder { |
46 | 49 |
|
@@ -82,6 +85,7 @@ private void implementMethods() { |
82 | 85 |
|
83 | 86 | // authentication |
84 | 87 | implementSetBasicAuth(methods); |
| 88 | + implementSetBearerAuth(methods); |
85 | 89 | implementSetAuthentication(methods); |
86 | 90 |
|
87 | 91 | // cookies and headers |
@@ -136,6 +140,27 @@ private void implementSetBasicAuth(List<ExecutableElement> methods) { |
136 | 140 | } |
137 | 141 | } |
138 | 142 |
|
| 143 | + private void implementSetBearerAuth(List<ExecutableElement> methods) { |
| 144 | + JMethod setBearerMethod = codeModelHelper.implementMethod(this, methods, "setBearerAuth", TypeKind.VOID.toString(), STRING); |
| 145 | + |
| 146 | + if (setBearerMethod != null) { |
| 147 | + JClass authClass = classes().HTTP_AUTHENTICATION; |
| 148 | + JDefinedClass anonymousHttpAuthClass = codeModel().anonymousClass(authClass); |
| 149 | + |
| 150 | + JMethod getHeaderValueMethod = anonymousHttpAuthClass.method(JMod.PUBLIC, String.class, "getHeaderValue"); |
| 151 | + getHeaderValueMethod.annotate(Override.class); |
| 152 | + JBlock getHeaderValueMethodBody = getHeaderValueMethod.body(); |
| 153 | + |
| 154 | + JBlock setBearerBody = setBearerMethod.body(); |
| 155 | + |
| 156 | + setBearerBody.decl(FINAL, refClass(String.class), "bearerHeader", lit("Bearer ").plus(setBearerMethod.params().get(0))); |
| 157 | + |
| 158 | + getHeaderValueMethodBody._return(ref("bearerHeader")); |
| 159 | + |
| 160 | + setBearerBody.assign(_this().ref(getAuthenticationField()), _new(anonymousHttpAuthClass)); |
| 161 | + } |
| 162 | + } |
| 163 | + |
139 | 164 | private void implementSetAuthentication(List<ExecutableElement> methods) { |
140 | 165 | JMethod setAuthMethod = codeModelHelper.implementMethod(this, methods, "setAuthentication", TypeKind.VOID.toString(), CanonicalNameConstants.HTTP_AUTHENTICATION); |
141 | 166 |
|
|
0 commit comments