Skip to content

Commit a476ab0

Browse files
Merge branch '479_ParamInterceptorsRest' into develop
Conflicts: AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/ValidatorHelper.java
2 parents 582897d + c044909 commit a476ab0

15 files changed

Lines changed: 307 additions & 1 deletion

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@
2525
public @interface Rest {
2626
String rootUrl() default "";
2727
Class<?>[] converters();
28+
Class<?>[] interceptors() default {};
2829
}

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

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

1818
import java.net.URI;
1919
import java.sql.SQLException;
20+
import java.util.ArrayList;
2021
import java.util.Collections;
2122
import java.util.Set;
2223

@@ -33,6 +34,7 @@ public final class CanonicalNameConstants {
3334
public static final String SQL_EXCEPTION = SQLException.class.getCanonicalName();
3435
public static final String INTEGER = Integer.class.getCanonicalName();
3536
public static final String BOOLEAN = Boolean.class.getCanonicalName();
37+
public static final String ARRAYLIST = ArrayList.class.getCanonicalName();
3638

3739
/*
3840
* Android
@@ -106,6 +108,7 @@ public final class CanonicalNameConstants {
106108
public static final String HTTP_ENTITY = "org.springframework.http.HttpEntity";
107109
public static final String REST_TEMPLATE = "org.springframework.web.client.RestTemplate";
108110
public static final String HTTP_MESSAGE_CONVERTER = "org.springframework.http.converter.HttpMessageConverter";
111+
public static final String CLIENT_HTTP_REQUEST_INTERCEPTOR = "org.springframework.http.client.ClientHttpRequestInterceptor";
109112

110113
/*
111114
* RoboGuice

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import static org.androidannotations.helper.AndroidConstants.LOG_INFO;
2222
import static org.androidannotations.helper.AndroidConstants.LOG_VERBOSE;
2323
import static org.androidannotations.helper.AndroidConstants.LOG_WARN;
24+
import static org.androidannotations.helper.CanonicalNameConstants.CLIENT_HTTP_REQUEST_INTERCEPTOR;
2425
import static org.androidannotations.helper.CanonicalNameConstants.HTTP_MESSAGE_CONVERTER;
2526
import static org.androidannotations.helper.CanonicalNameConstants.INTERNET_PERMISSION;
2627
import static org.androidannotations.helper.ModelConstants.GENERATION_SUFFIX;
@@ -1175,4 +1176,41 @@ public void hasInternetPermission(Element element, AndroidManifest androidManife
11751176
annotationHelper.printAnnotationError(element, "Your application must require the INTERNET permission.");
11761177
}
11771178
}
1179+
1180+
public void validateInterceptors(Element element, IsValid valid) {
1181+
TypeMirror clientHttpRequestInterceptorType = annotationHelper.typeElementFromQualifiedName(CLIENT_HTTP_REQUEST_INTERCEPTOR).asType();
1182+
TypeMirror clientHttpRequestInterceptorTypeErased = annotationHelper.getTypeUtils().erasure(clientHttpRequestInterceptorType);
1183+
List<DeclaredType> interceptors = annotationHelper.extractAnnotationClassArrayParameter(element, annotationHelper.getTarget(), "interceptors");
1184+
if(interceptors == null) {
1185+
return;
1186+
}
1187+
for (DeclaredType interceptorType : interceptors) {
1188+
TypeMirror erasedInterceptorType = annotationHelper.getTypeUtils().erasure(interceptorType);
1189+
if (annotationHelper.isSubtype(erasedInterceptorType, clientHttpRequestInterceptorTypeErased)) {
1190+
Element interceptorElement = interceptorType.asElement();
1191+
if (interceptorElement.getKind().isClass()) {
1192+
if (!annotationHelper.isAbstract(interceptorElement)) {
1193+
List<ExecutableElement> constructors = ElementFilter.constructorsIn(interceptorElement.getEnclosedElements());
1194+
for (ExecutableElement constructor : constructors) {
1195+
if (annotationHelper.isPublic(constructor) && constructor.getParameters().isEmpty()) {
1196+
return;
1197+
}
1198+
}
1199+
valid.invalidate();
1200+
annotationHelper.printAnnotationError(element, "The interceptor class must have a public no argument constructor");
1201+
} else {
1202+
valid.invalidate();
1203+
annotationHelper.printAnnotationError(element, "The interceptor class must not be abstract");
1204+
}
1205+
} else {
1206+
valid.invalidate();
1207+
annotationHelper.printAnnotationError(element, "The interceptor class must be a class");
1208+
}
1209+
} else {
1210+
valid.invalidate();
1211+
annotationHelper.printAnnotationError(element, "The interceptor class must be a subtype of " + CLIENT_HTTP_REQUEST_INTERCEPTOR);
1212+
}
1213+
}
1214+
}
1215+
11781216
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static com.sun.codemodel.JExpr._this;
2020
import static com.sun.codemodel.JExpr.invoke;
2121
import static com.sun.codemodel.JExpr.lit;
22+
import static org.androidannotations.helper.CanonicalNameConstants.ARRAYLIST;
2223
import static org.androidannotations.helper.CanonicalNameConstants.REST_TEMPLATE;
2324
import static org.androidannotations.helper.CanonicalNameConstants.STRING;
2425

@@ -101,6 +102,19 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHo
101102
constructorBody.add(invoke(holder.restTemplateField, "getMessageConverters").invoke("add").arg(_new(converterClass)));
102103
}
103104
}
105+
106+
{
107+
// Interceptors
108+
List<DeclaredType> interceptors = annotationHelper.extractAnnotationClassArrayParameter(element, getTarget(), "interceptors");
109+
if (interceptors != null) {
110+
JClass listClass = eBeansHolder.refClass(ARRAYLIST);
111+
constructorBody.add(invoke(holder.restTemplateField, "setInterceptors").arg(_new(listClass)));
112+
for (DeclaredType interceptorType : interceptors) {
113+
JClass interceptorClass = eBeansHolder.refClass(interceptorType.toString());
114+
constructorBody.add(invoke(holder.restTemplateField, "getInterceptors").invoke("add").arg(_new(interceptorClass)));
115+
}
116+
}
117+
}
104118
constructorBody.assign(holder.rootUrlField, lit(typeElement.getAnnotation(Rest.class).rootUrl()));
105119
}
106120

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/validation/rest/RestValidator.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ public boolean validate(Element element, AnnotationElements validatedElements) {
6262

6363
validatorHelper.validateConverters(element, valid);
6464

65+
validatorHelper.validateInterceptors(element, valid);
66+
6567
return valid.isValid();
6668
}
6769

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Copyright (C) 2010-2012 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.rest;
17+
18+
import org.springframework.http.client.ClientHttpRequestInterceptor;
19+
20+
public abstract class AbstractInterceptor implements ClientHttpRequestInterceptor {
21+
22+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Copyright (C) 2010-2012 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.rest;
17+
18+
import org.androidannotations.annotations.rest.Rest;
19+
20+
@Rest(converters = { AbstractInterceptor.class })
21+
public interface ClientWithAbstractInterceptor {
22+
23+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Copyright (C) 2010-2012 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.rest;
17+
18+
import org.androidannotations.annotations.rest.Rest;
19+
import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter;
20+
21+
@Rest(converters = { MappingJacksonHttpMessageConverter.class }, interceptors = { Object.class })
22+
public interface ClientWithNonInterceptor {
23+
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Copyright (C) 2010-2012 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.rest;
17+
18+
import org.androidannotations.annotations.rest.Rest;
19+
import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter;
20+
21+
@Rest(converters = { MappingJacksonHttpMessageConverter.class }, interceptors = { ValidInterceptor.class })
22+
public interface ClientWithValidInterceptor {
23+
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Copyright (C) 2010-2012 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.rest;
17+
18+
import org.androidannotations.annotations.rest.Rest;
19+
import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter;
20+
21+
@Rest(converters = { MappingJacksonHttpMessageConverter.class }, interceptors = { WrongConstructorInterceptor.class })
22+
public interface ClientWithWrongConstructorInterceptor {
23+
24+
}

0 commit comments

Comments
 (0)