Skip to content

Commit ca0588f

Browse files
committed
Merge pull request androidannotations#579 from dhleong/475_Rest_Authentication
androidannotations#475 - Rest Authentication
2 parents 9f58869 + 01dc8c8 commit ca0588f

17 files changed

Lines changed: 627 additions & 26 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Copyright (C) 2010-2013 eBusiness Information, Excilys Group
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed To in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package org.androidannotations.annotations.rest;
17+
18+
import java.lang.annotation.ElementType;
19+
import java.lang.annotation.Retention;
20+
import java.lang.annotation.RetentionPolicy;
21+
import java.lang.annotation.Target;
22+
23+
@Retention(RetentionPolicy.CLASS)
24+
@Target({ ElementType.METHOD, ElementType.TYPE })
25+
public @interface RequiresAuthentication {
26+
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Copyright (C) 2010-2013 eBusiness Information, Excilys Group
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed To in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package org.androidannotations.annotations.rest;
17+
18+
import java.lang.annotation.ElementType;
19+
import java.lang.annotation.Retention;
20+
import java.lang.annotation.RetentionPolicy;
21+
import java.lang.annotation.Target;
22+
23+
@Retention(RetentionPolicy.CLASS)
24+
@Target({ ElementType.METHOD, ElementType.TYPE })
25+
public @interface RequiresCookie {
26+
public String[] value();
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Copyright (C) 2010-2013 eBusiness Information, Excilys Group
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed To in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package org.androidannotations.annotations.rest;
17+
18+
import java.lang.annotation.ElementType;
19+
import java.lang.annotation.Retention;
20+
import java.lang.annotation.RetentionPolicy;
21+
import java.lang.annotation.Target;
22+
23+
@Retention(RetentionPolicy.CLASS)
24+
@Target({ ElementType.METHOD, ElementType.TYPE })
25+
public @interface RequiresCookieInUrl {
26+
public String[] value();
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Copyright (C) 2010-2013 eBusiness Information, Excilys Group
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed To in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package org.androidannotations.annotations.rest;
17+
18+
import java.lang.annotation.ElementType;
19+
import java.lang.annotation.Retention;
20+
import java.lang.annotation.RetentionPolicy;
21+
import java.lang.annotation.Target;
22+
23+
@Retention(RetentionPolicy.CLASS)
24+
@Target({ ElementType.METHOD, ElementType.TYPE })
25+
public @interface RequiresHeader {
26+
String[] value();
27+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Copyright (C) 2010-2013 eBusiness Information, Excilys Group
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed To in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package org.androidannotations.annotations.rest;
17+
18+
import java.lang.annotation.ElementType;
19+
import java.lang.annotation.Retention;
20+
import java.lang.annotation.RetentionPolicy;
21+
import java.lang.annotation.Target;
22+
23+
@Retention(RetentionPolicy.CLASS)
24+
@Target({ ElementType.METHOD, ElementType.TYPE })
25+
public @interface SetsCookie {
26+
27+
public String[] value();
28+
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import javax.lang.model.element.ExecutableElement;
2727
import javax.lang.model.element.VariableElement;
2828

29+
import org.androidannotations.processing.rest.MethodProcessor;
2930
import org.androidannotations.validation.IsValid;
3031

3132
public class RestAnnotationHelper extends TargetAnnotationHelper {
@@ -43,6 +44,13 @@ public void urlVariableNamesExistInParameters(ExecutableElement element, Set<Str
4344
parametersName.add(parameter.getSimpleName().toString());
4445
}
4546

47+
String[] cookiesToUrl = MethodProcessor.retrieveRequiredUrlCookieNames(element);
48+
if (cookiesToUrl != null) {
49+
for (String cookie : cookiesToUrl) {
50+
parametersName.add(cookie);
51+
}
52+
}
53+
4654
for (String variableName : variableNames) {
4755
if (!parametersName.contains(variableName)) {
4856
valid.invalidate();

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ public class ValidatorHelper {
7979
private static final List<String> ANDROID_FRAGMENT_QUALIFIED_NAMES = asList(CanonicalNameConstants.FRAGMENT, CanonicalNameConstants.SUPPORT_V4_FRAGMENT);
8080

8181
private static final String METHOD_NAME_SET_ROOT_URL = "setRootUrl";
82+
private static final String METHOD_NAME_SET_AUTHENTICATION = "setAuthentication";
83+
private static final String METHOD_NAME_GET_COOKIE = "getCookie";
84+
private static final String METHOD_NAME_GET_HEADER = "getHeader";
8285

8386
private static final String METHOD_NAME_GET_ROOT_URL = "getRootUrl";
8487

@@ -785,7 +788,10 @@ public void unannotatedMethodReturnsRestTemplate(TypeElement typeElement, IsVali
785788
List<? extends Element> enclosedElements = typeElement.getEnclosedElements();
786789
boolean foundGetRestTemplateMethod = false;
787790
boolean foundSetRestTemplateMethod = false;
791+
boolean foundSetAuthenticationMethod = false;
788792
boolean foundSetRootUrlMethod = false;
793+
boolean foundGetCookieMethod = false;
794+
boolean foundGetHeaderMethod = false;
789795
boolean foundGetRootUrlMethod = false;
790796

791797
for (Element enclosedElement : enclosedElements) {
@@ -851,15 +857,46 @@ public void unannotatedMethodReturnsRestTemplate(TypeElement typeElement, IsVali
851857
}
852858
} else if (executableElement.getSimpleName().toString().equals(METHOD_NAME_SET_ROOT_URL) && !foundSetRootUrlMethod) {
853859
foundSetRootUrlMethod = true;
860+
} else if (executableElement.getSimpleName().toString().equals(METHOD_NAME_SET_AUTHENTICATION) && !foundSetAuthenticationMethod) {
861+
foundSetAuthenticationMethod = true;
854862
} else {
855863
valid.invalidate();
856864
annotationHelper.printError(enclosedElement, "The method to set a RestTemplate should have only one RestTemplate parameter on a " + TargetAnnotationHelper.annotationName(Rest.class) + " annotated interface");
857865

858866
}
867+
} else if (parameters.size() == 2) {
868+
VariableElement firstParameter = parameters.get(0);
869+
VariableElement secondParameter = parameters.get(1);
870+
if (!(firstParameter.asType().toString().equals(CanonicalNameConstants.STRING) && secondParameter.asType().toString().equals(CanonicalNameConstants.STRING))) {
871+
valid.invalidate();
872+
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");
873+
}
859874
} else {
860875
valid.invalidate();
861876
annotationHelper.printError(enclosedElement, "The method to set a RestTemplate should have only one RestTemplate parameter on a " + TargetAnnotationHelper.annotationName(Rest.class) + " annotated interface");
862877
}
878+
} else if (returnType.toString().equals(CanonicalNameConstants.STRING)) {
879+
List<? extends VariableElement> parameters = executableElement.getParameters();
880+
if (parameters.size() == 1) {
881+
VariableElement firstParameter = parameters.get(0);
882+
if (firstParameter.asType().toString().equals(CanonicalNameConstants.STRING)) {
883+
if (executableElement.getSimpleName().toString().equals(METHOD_NAME_GET_COOKIE) && !foundGetCookieMethod) {
884+
foundGetCookieMethod = true;
885+
} else if (executableElement.getSimpleName().toString().equals(METHOD_NAME_GET_HEADER) && !foundGetHeaderMethod) {
886+
foundGetHeaderMethod = true;
887+
} else {
888+
valid.invalidate();
889+
annotationHelper.printError(enclosedElement, "Only one getCookie(String) and one getHeader(String) method are allowed on a " + TargetAnnotationHelper.annotationName(Rest.class) + " annotated interface");
890+
}
891+
} else {
892+
valid.invalidate();
893+
annotationHelper.printError(enclosedElement, "Only getCookie(String) and getHeader(String) can return a String on a " + TargetAnnotationHelper.annotationName(Rest.class) + " annotated interface");
894+
}
895+
896+
} else {
897+
valid.invalidate();
898+
annotationHelper.printError(enclosedElement, "The only methods that can return a String on a " + TargetAnnotationHelper.annotationName(Rest.class) + " annotated interface are getCookie(String) and getHeader(String)");
899+
}
863900
} else {
864901
valid.invalidate();
865902
annotationHelper.printError(enclosedElement, "All methods should be annotated in a " + TargetAnnotationHelper.annotationName(Rest.class) + " annotated interface, except the ones that returns or set a RestTemplate");

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/processing/rest/GetPostProcessor.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import com.sun.codemodel.JCodeModel;
4343
import com.sun.codemodel.JDefinedClass;
4444
import com.sun.codemodel.JExpr;
45+
import com.sun.codemodel.JExpression;
4546
import com.sun.codemodel.JInvocation;
4647
import com.sun.codemodel.JPackage;
4748

@@ -267,7 +268,7 @@ protected JInvocation addResponseEntityArg(JInvocation restCall, MethodProcessor
267268
}
268269

269270
@Override
270-
protected JInvocation addResultCallMethod(JInvocation restCall, MethodProcessorHolder methodHolder) {
271+
protected JExpression addResultCallMethod(JExpression restCall, MethodProcessorHolder methodHolder) {
271272
JClass generatedReturnType = methodHolder.getMethodReturnClass();
272273
if (generatedReturnType == null) {
273274
return restCall;

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/processing/rest/GetProcessor.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ protected JExpression generateHttpEntityVar(MethodProcessorHolder methodHolder)
5454
JClass httpEntity = holder.refClass(CanonicalNameConstants.HTTP_ENTITY);
5555

5656
JBlock body = methodHolder.getBody();
57-
JVar httpHeadersVar = generateHttpHeadersVar(holder, body, executableElement);
57+
JVar httpHeadersVar = generateHttpHeadersVar(methodHolder, holder, body, executableElement);
5858

5959
boolean hasHeaders = httpHeadersVar != null;
6060

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/processing/rest/HeadProcessor.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import com.sun.codemodel.JClass;
2727
import com.sun.codemodel.JCodeModel;
2828
import com.sun.codemodel.JExpr;
29-
import com.sun.codemodel.JInvocation;
29+
import com.sun.codemodel.JExpression;
3030

3131
public class HeadProcessor extends MethodProcessor {
3232

@@ -55,7 +55,7 @@ public void process(Element element, JCodeModel codeModel, EBeanHolder holder) t
5555
}
5656

5757
@Override
58-
protected JInvocation addResultCallMethod(JInvocation restCall, MethodProcessorHolder methodHolder) {
58+
protected JExpression addResultCallMethod(JExpression restCall, MethodProcessorHolder methodHolder) {
5959
return JExpr.invoke(restCall, "getHeaders");
6060
}
6161

0 commit comments

Comments
 (0)