diff --git a/AndroidAnnotations/androidannotations-api/src/main/java/org/androidannotations/annotations/rest/Rest.java b/AndroidAnnotations/androidannotations-api/src/main/java/org/androidannotations/annotations/rest/Rest.java index ba21b34443..fecde6c096 100644 --- a/AndroidAnnotations/androidannotations-api/src/main/java/org/androidannotations/annotations/rest/Rest.java +++ b/AndroidAnnotations/androidannotations-api/src/main/java/org/androidannotations/annotations/rest/Rest.java @@ -196,4 +196,5 @@ Class[] converters(); Class[] interceptors() default {}; + Class requestFactory() default Void.class; } diff --git a/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/handler/rest/RestHandler.java b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/handler/rest/RestHandler.java index 8a99c36e71..d737990080 100644 --- a/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/handler/rest/RestHandler.java +++ b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/handler/rest/RestHandler.java @@ -75,6 +75,8 @@ public void validate(Element element, AnnotationElements validatedElements, IsVa validatorHelper.validateInterceptors(element, valid); + validatorHelper.validateRequestFactory(element, valid); + validatorHelper.hasInternetPermission(typeElement, androidManifest, valid); } @@ -83,6 +85,7 @@ public void process(Element element, RestHolder holder) { setRootUrl(element, holder); setConverters(element, holder); setInterceptors(element, holder); + setRequestFactory(element, holder); } private void setRootUrl(Element element, RestHolder holder) { @@ -116,4 +119,12 @@ private void setInterceptors(Element element, RestHolder holder) { } } } + + private void setRequestFactory(Element element, RestHolder holder) { + DeclaredType requestFactoryType = annotationHelper.extractAnnotationClassParameter(element, getTarget(), "requestFactory"); + if (requestFactoryType != null) { + JInvocation requestFactory = codeModelHelper.newBeanOrEBean(holder, requestFactoryType, holder.getInitContextParam()); + holder.getInit().body().add(invoke(holder.getRestTemplateField(), "setRequestFactory").arg(requestFactory)); + } + } } diff --git a/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/CanonicalNameConstants.java b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/CanonicalNameConstants.java index 6078ea665f..86c17f6181 100644 --- a/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/CanonicalNameConstants.java +++ b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/CanonicalNameConstants.java @@ -132,6 +132,7 @@ public final class CanonicalNameConstants { public static final String REST_TEMPLATE = "org.springframework.web.client.RestTemplate"; public static final String HTTP_MESSAGE_CONVERTER = "org.springframework.http.converter.HttpMessageConverter"; public static final String CLIENT_HTTP_REQUEST_INTERCEPTOR = "org.springframework.http.client.ClientHttpRequestInterceptor"; + public static final String CLIENT_HTTP_REQUEST_FACTORY = "org.springframework.http.client.ClientHttpRequestFactory"; public static final String HTTP_AUTHENTICATION = "org.springframework.http.HttpAuthentication"; public static final String HTTP_BASIC_AUTHENTICATION = "org.springframework.http.HttpBasicAuthentication"; public static final String REST_CLIENT_EXCEPTION = "org.springframework.web.client.RestClientException"; diff --git a/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/ValidatorHelper.java b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/ValidatorHelper.java index da529739a2..b4977c6552 100644 --- a/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/ValidatorHelper.java +++ b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/ValidatorHelper.java @@ -15,65 +15,9 @@ */ package org.androidannotations.helper; -import static java.util.Arrays.asList; -import static org.androidannotations.helper.AndroidConstants.LOG_DEBUG; -import static org.androidannotations.helper.AndroidConstants.LOG_ERROR; -import static org.androidannotations.helper.AndroidConstants.LOG_INFO; -import static org.androidannotations.helper.AndroidConstants.LOG_VERBOSE; -import static org.androidannotations.helper.AndroidConstants.LOG_WARN; -import static org.androidannotations.helper.CanonicalNameConstants.CLIENT_HTTP_REQUEST_INTERCEPTOR; -import static org.androidannotations.helper.CanonicalNameConstants.HTTP_MESSAGE_CONVERTER; -import static org.androidannotations.helper.CanonicalNameConstants.INTERNET_PERMISSION; -import static org.androidannotations.helper.ModelConstants.GENERATION_SUFFIX; -import static org.androidannotations.helper.ModelConstants.VALID_ENHANCED_COMPONENT_ANNOTATIONS; -import static org.androidannotations.helper.ModelConstants.VALID_ENHANCED_VIEW_SUPPORT_ANNOTATIONS; - -import java.lang.annotation.Annotation; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.TreeSet; - -import javax.lang.model.element.AnnotationMirror; -import javax.lang.model.element.Element; -import javax.lang.model.element.ElementKind; -import javax.lang.model.element.ExecutableElement; -import javax.lang.model.element.Modifier; -import javax.lang.model.element.TypeElement; -import javax.lang.model.element.VariableElement; -import javax.lang.model.type.ArrayType; -import javax.lang.model.type.DeclaredType; -import javax.lang.model.type.ErrorType; -import javax.lang.model.type.TypeKind; -import javax.lang.model.type.TypeMirror; -import javax.lang.model.util.ElementFilter; -import javax.lang.model.util.Elements; - -import org.androidannotations.annotations.EActivity; -import org.androidannotations.annotations.EBean; -import org.androidannotations.annotations.EFragment; -import org.androidannotations.annotations.EIntentService; -import org.androidannotations.annotations.EService; -import org.androidannotations.annotations.Receiver; -import org.androidannotations.annotations.Trace; -import org.androidannotations.annotations.ViewById; -import org.androidannotations.annotations.rest.Delete; -import org.androidannotations.annotations.rest.Get; -import org.androidannotations.annotations.rest.Head; -import org.androidannotations.annotations.rest.Options; -import org.androidannotations.annotations.rest.Post; -import org.androidannotations.annotations.rest.Put; -import org.androidannotations.annotations.rest.Rest; -import org.androidannotations.annotations.sharedpreferences.DefaultBoolean; -import org.androidannotations.annotations.sharedpreferences.DefaultFloat; -import org.androidannotations.annotations.sharedpreferences.DefaultInt; -import org.androidannotations.annotations.sharedpreferences.DefaultLong; -import org.androidannotations.annotations.sharedpreferences.DefaultString; -import org.androidannotations.annotations.sharedpreferences.SharedPref; +import org.androidannotations.annotations.*; +import org.androidannotations.annotations.rest.*; +import org.androidannotations.annotations.sharedpreferences.*; import org.androidannotations.api.rest.RestClientErrorHandling; import org.androidannotations.api.rest.RestClientHeaders; import org.androidannotations.api.rest.RestClientRootUrl; @@ -83,6 +27,18 @@ import org.androidannotations.model.AnnotationElements; import org.androidannotations.process.IsValid; +import javax.lang.model.element.*; +import javax.lang.model.type.*; +import javax.lang.model.util.ElementFilter; +import javax.lang.model.util.Elements; +import java.lang.annotation.Annotation; +import java.util.*; + +import static java.util.Arrays.asList; +import static org.androidannotations.helper.AndroidConstants.*; +import static org.androidannotations.helper.CanonicalNameConstants.*; +import static org.androidannotations.helper.ModelConstants.*; + public class ValidatorHelper { private static final List VALID_REST_INTERFACES = asList(RestClientHeaders.class.getName(), RestClientErrorHandling.class.getName(), RestClientRootUrl.class.getName(), RestClientSupport.class.getName()); @@ -1161,6 +1117,40 @@ public void hasInternetPermission(Element element, AndroidManifest androidManife } } + public void validateRequestFactory(Element element, IsValid valid) { + TypeMirror clientHttpRequestFactoryType = annotationHelper.typeElementFromQualifiedName(CLIENT_HTTP_REQUEST_FACTORY).asType(); + DeclaredType requestFactory = annotationHelper.extractAnnotationClassParameter(element, annotationHelper.getTarget(), "requestFactory"); + if (requestFactory != null) { + if (annotationHelper.isSubtype(requestFactory, clientHttpRequestFactoryType)) { + Element requestFactoryElement = requestFactory.asElement(); + if (requestFactoryElement.getKind().isClass()) { + if (!annotationHelper.isAbstract(requestFactoryElement)) { + if (requestFactoryElement.getAnnotation(EBean.class) != null) { + return; + } + List constructors = ElementFilter.constructorsIn(requestFactoryElement.getEnclosedElements()); + for (ExecutableElement constructor : constructors) { + if (annotationHelper.isPublic(constructor) && constructor.getParameters().isEmpty()) { + return; + } + } + valid.invalidate(); + annotationHelper.printAnnotationError(element, "The requestFactory class must have a public no argument constructor or must be annotated with @EBean"); + } else { + valid.invalidate(); + annotationHelper.printAnnotationError(element, "The requestFactory class must not be abstract"); + } + } else { + valid.invalidate(); + annotationHelper.printAnnotationError(element, "The requestFactory class must be a class"); + } + } else { + valid.invalidate(); + annotationHelper.printAnnotationError(element, "The requestFactory class must be a subtype of " + CLIENT_HTTP_REQUEST_FACTORY); + } + } + } + public void hasBeforeTextChangedMethodParameters(ExecutableElement executableElement, IsValid valid) { List parameters = executableElement.getParameters(); boolean charSequenceParameterFound = false; diff --git a/AndroidAnnotations/functional-test-1-5/src/main/java/org/androidannotations/test15/rest/MyRequestFactory.java b/AndroidAnnotations/functional-test-1-5/src/main/java/org/androidannotations/test15/rest/MyRequestFactory.java new file mode 100644 index 0000000000..d8ffd3059c --- /dev/null +++ b/AndroidAnnotations/functional-test-1-5/src/main/java/org/androidannotations/test15/rest/MyRequestFactory.java @@ -0,0 +1,30 @@ +/** + * Copyright (C) 2010-2013 eBusiness Information, Excilys Group + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed To in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package org.androidannotations.test15.rest; + +import org.androidannotations.annotations.EBean; +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; + +@EBean +public class MyRequestFactory extends HttpComponentsClientHttpRequestFactory { + + private static final int TIME_OUT = 30 * 1000; + + public MyRequestFactory() { + setConnectTimeout(TIME_OUT); + setReadTimeout(TIME_OUT); + } +} diff --git a/AndroidAnnotations/functional-test-1-5/src/main/java/org/androidannotations/test15/rest/MyService.java b/AndroidAnnotations/functional-test-1-5/src/main/java/org/androidannotations/test15/rest/MyService.java index c9bfa3170f..7d3efb31e4 100644 --- a/AndroidAnnotations/functional-test-1-5/src/main/java/org/androidannotations/test15/rest/MyService.java +++ b/AndroidAnnotations/functional-test-1-5/src/main/java/org/androidannotations/test15/rest/MyService.java @@ -42,7 +42,9 @@ import org.springframework.web.client.RestTemplate; // if defined, the rootUrl will be added as a prefix to every request -@Rest(rootUrl = "http://company.com/ajax/services", converters = { MappingJacksonHttpMessageConverter.class, EBeanConverter.class }, interceptors = { RequestInterceptor.class, EBeanInterceptor.class }) +@Rest(rootUrl = "http://company.com/ajax/services", converters = { MappingJacksonHttpMessageConverter.class, EBeanConverter.class }, + interceptors = { RequestInterceptor.class, EBeanInterceptor.class }, + requestFactory = MyRequestFactory.class) public interface MyService { // *** GET ***