diff --git a/AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/helper/ValidatorHelper.java b/AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/helper/ValidatorHelper.java index 0cd4c22550..f4359397ce 100644 --- a/AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/helper/ValidatorHelper.java +++ b/AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/helper/ValidatorHelper.java @@ -214,7 +214,7 @@ public void enclosingElementHasOneOfAnnotations(Element element, List> validAnnotations, ElementValidation validation) { + public void hasOneOfAnnotations(Element reportElement, Element element, List> validAnnotations, ElementValidation validation) { checkAnnotations(reportElement, element, validAnnotations, true, validation); } diff --git a/AndroidAnnotations/androidannotations-rest-spring/rest-spring-api/src/main/java/org/androidannotations/rest/spring/annotations/Header.java b/AndroidAnnotations/androidannotations-rest-spring/rest-spring-api/src/main/java/org/androidannotations/rest/spring/annotations/Header.java new file mode 100644 index 0000000000..718ebaddcc --- /dev/null +++ b/AndroidAnnotations/androidannotations-rest-spring/rest-spring-api/src/main/java/org/androidannotations/rest/spring/annotations/Header.java @@ -0,0 +1,51 @@ +/** + * Copyright (C) 2010-2015 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.rest.spring.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Use on methods in {@link Rest} annotated classes to add headers to a + * particular method. + * + *
+ * + * Example : + * + *
+ * @Rest(converters = MappingJacksonHttpMessageConverter.class)
+ * public interface MyRestClient {
+ * 
+ * 	@Header(name = "keep-alive", name = "300")
+ * 	@Post("/test")
+ * 	void testRoute();
+ * }
+ * 
+ * + *
+ * + * @see Headers + */ +@Retention(RetentionPolicy.CLASS) +@Target(ElementType.METHOD) +public @interface Header { + String name(); + + String value(); +} diff --git a/AndroidAnnotations/androidannotations-rest-spring/rest-spring-api/src/main/java/org/androidannotations/rest/spring/annotations/Headers.java b/AndroidAnnotations/androidannotations-rest-spring/rest-spring-api/src/main/java/org/androidannotations/rest/spring/annotations/Headers.java new file mode 100644 index 0000000000..43b1a3e72b --- /dev/null +++ b/AndroidAnnotations/androidannotations-rest-spring/rest-spring-api/src/main/java/org/androidannotations/rest/spring/annotations/Headers.java @@ -0,0 +1,51 @@ +/** + * Copyright (C) 2010-2015 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.rest.spring.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Use on methods in {@link Rest} annotated classes to add multiple headers to a + * given method. + * + *
+ * + * Example : + * + *
+ * @Rest(converters = MappingJacksonHttpMessageConverter.class)
+ * public interface MyRestClient {
+ * 
+ * 	@Headers({
+ * 		@Header(name = "keep-alive", value = "300"),
+ * 		@Header(name = "cache-control", value = "64000")})
+ * 	@Post("/test")
+ * 	void testRoute();
+ * }
+ * 
+ * + *
+ * + * @see Header + */ +@Retention(RetentionPolicy.CLASS) +@Target(ElementType.METHOD) +public @interface Headers { + Header[] value(); +} diff --git a/AndroidAnnotations/androidannotations-rest-spring/rest-spring-test/src/main/java/org/androidannotations/rest/spring/test/MyService.java b/AndroidAnnotations/androidannotations-rest-spring/rest-spring-test/src/main/java/org/androidannotations/rest/spring/test/MyService.java index 268b3438c6..7c5094fe0b 100644 --- a/AndroidAnnotations/androidannotations-rest-spring/rest-spring-test/src/main/java/org/androidannotations/rest/spring/test/MyService.java +++ b/AndroidAnnotations/androidannotations-rest-spring/rest-spring-test/src/main/java/org/androidannotations/rest/spring/test/MyService.java @@ -24,6 +24,8 @@ import org.androidannotations.rest.spring.annotations.Field; import org.androidannotations.rest.spring.annotations.Get; import org.androidannotations.rest.spring.annotations.Head; +import org.androidannotations.rest.spring.annotations.Header; +import org.androidannotations.rest.spring.annotations.Headers; import org.androidannotations.rest.spring.annotations.Options; import org.androidannotations.rest.spring.annotations.Part; import org.androidannotations.rest.spring.annotations.Path; @@ -172,6 +174,19 @@ public interface MyService { @RequiresCookieInUrl("myCookieInUrl") void addEventWithPathParameters(@Path("date") String pathParam, @Field String parameter); + @Post("/events/{date}") + @RequiresHeader("SomeFancyHeader") + @Header(name = "SomeFancyHeader", value = "fancy") + @RequiresCookie("myCookie") + @RequiresCookieInUrl("myCookieInUrl") + void addEventWithHeaders(String date, String parameter); + + @Post("/events/{date}") + @Headers(@Header(name = "SomeFancyHeader", value = "fancy")) + @RequiresCookie("myCookie") + @RequiresCookieInUrl("myCookieInUrl") + void addEventWithHeadersHeadersAnnotation(String date, String parameter); + /** * Output different then input */ diff --git a/AndroidAnnotations/androidannotations-rest-spring/rest-spring-test/src/test/java/org/androidannotations/rest/spring/test/MyServiceTest.java b/AndroidAnnotations/androidannotations-rest-spring/rest-spring-test/src/test/java/org/androidannotations/rest/spring/test/MyServiceTest.java index 8f0672b0a5..58be749366 100644 --- a/AndroidAnnotations/androidannotations-rest-spring/rest-spring-test/src/test/java/org/androidannotations/rest/spring/test/MyServiceTest.java +++ b/AndroidAnnotations/androidannotations-rest-spring/rest-spring-test/src/test/java/org/androidannotations/rest/spring/test/MyServiceTest.java @@ -320,4 +320,49 @@ public void execute(MyService myService) { }); } + @Test + public void addEventWithHeadersOverriden() { + RequestTestBuilder.build() // + .requestCookie("myCookie", "myCookieValue") // + .requestHeader("SomeFancyHeader", "aFancyHeader") // + .responseContent("{'id':1,'name':'event1'}") // + .hasUrlVariables(true) // + .asserts(new RequestTestBuilder.RequestTestBuilderExecutor() { + @Override + public void execute(MyService myService) { + myService.addEventWithHeaders("now", "event"); + } + }); + } + + @Test + public void addEventWithHeaders() { + RequestTestBuilder.build() // + .requestCookie("myCookie", "myCookieValue") // + .expectedHeader("SomeFancyHeader", "fancy") // + .responseContent("{'id':1,'name':'event1'}") // + .hasUrlVariables(true) // + .asserts(new RequestTestBuilder.RequestTestBuilderExecutor() { + @Override + public void execute(MyService myService) { + myService.addEventWithHeaders("now", "event"); + } + }); + } + + @Test + public void addEventWithHeadersHeadersAnnotation() { + RequestTestBuilder.build() // + .requestCookie("myCookie", "myCookieValue") // + .expectedHeader("SomeFancyHeader", "fancy") // + .responseContent("{'id':1,'name':'event1'}") // + .hasUrlVariables(true) // + .asserts(new RequestTestBuilder.RequestTestBuilderExecutor() { + @Override + public void execute(MyService myService) { + myService.addEventWithHeadersHeadersAnnotation("now", "event"); + } + }); + } + } diff --git a/AndroidAnnotations/androidannotations-rest-spring/rest-spring/src/main/java/org/androidannotations/rest/spring/RestSpringPlugin.java b/AndroidAnnotations/androidannotations-rest-spring/rest-spring/src/main/java/org/androidannotations/rest/spring/RestSpringPlugin.java index 4ee88822f3..b8638f954e 100644 --- a/AndroidAnnotations/androidannotations-rest-spring/rest-spring/src/main/java/org/androidannotations/rest/spring/RestSpringPlugin.java +++ b/AndroidAnnotations/androidannotations-rest-spring/rest-spring/src/main/java/org/androidannotations/rest/spring/RestSpringPlugin.java @@ -24,6 +24,8 @@ import org.androidannotations.rest.spring.handler.DeleteHandler; import org.androidannotations.rest.spring.handler.GetHandler; import org.androidannotations.rest.spring.handler.HeadHandler; +import org.androidannotations.rest.spring.handler.HeaderHandler; +import org.androidannotations.rest.spring.handler.HeadersHandler; import org.androidannotations.rest.spring.handler.OptionsHandler; import org.androidannotations.rest.spring.handler.PathHandler; import org.androidannotations.rest.spring.handler.PostHandler; @@ -51,6 +53,8 @@ public List> getHandlers(AndroidAnnotationsEnvironment andr annotationHandlers.add(new HeadHandler(androidAnnotationEnv)); annotationHandlers.add(new OptionsHandler(androidAnnotationEnv)); annotationHandlers.add(new PathHandler(androidAnnotationEnv)); + annotationHandlers.add(new HeaderHandler(androidAnnotationEnv)); + annotationHandlers.add(new HeadersHandler(androidAnnotationEnv)); annotationHandlers.add(new RestServiceHandler(androidAnnotationEnv)); return annotationHandlers; } diff --git a/AndroidAnnotations/androidannotations-rest-spring/rest-spring/src/main/java/org/androidannotations/rest/spring/handler/HeaderHandler.java b/AndroidAnnotations/androidannotations-rest-spring/rest-spring/src/main/java/org/androidannotations/rest/spring/handler/HeaderHandler.java new file mode 100644 index 0000000000..768d60baa6 --- /dev/null +++ b/AndroidAnnotations/androidannotations-rest-spring/rest-spring/src/main/java/org/androidannotations/rest/spring/handler/HeaderHandler.java @@ -0,0 +1,48 @@ +/** + * Copyright (C) 2010-2015 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.rest.spring.handler; + +import javax.lang.model.element.Element; + +import org.androidannotations.AndroidAnnotationsEnvironment; +import org.androidannotations.ElementValidation; +import org.androidannotations.handler.BaseAnnotationHandler; +import org.androidannotations.rest.spring.annotations.Header; +import org.androidannotations.rest.spring.annotations.Headers; +import org.androidannotations.rest.spring.helper.RestSpringValidatorHelper; +import org.androidannotations.rest.spring.holder.RestHolder; + +public class HeaderHandler extends BaseAnnotationHandler { + + private RestSpringValidatorHelper restValidatorHelper; + + public HeaderHandler(AndroidAnnotationsEnvironment environment) { + super(Header.class, environment); + restValidatorHelper = new RestSpringValidatorHelper(environment, getTarget()); + } + + @Override + protected void validate(Element element, ElementValidation validation) { + restValidatorHelper.elementHasOneOfRestMethodAnnotations(element, validation); + + validatorHelper.doesNotHaveAnnotation(element, Headers.class, validation); + } + + @Override + public void process(Element element, RestHolder holder) throws Exception { + // empty + } +} diff --git a/AndroidAnnotations/androidannotations-rest-spring/rest-spring/src/main/java/org/androidannotations/rest/spring/handler/HeadersHandler.java b/AndroidAnnotations/androidannotations-rest-spring/rest-spring/src/main/java/org/androidannotations/rest/spring/handler/HeadersHandler.java new file mode 100644 index 0000000000..0951e450c1 --- /dev/null +++ b/AndroidAnnotations/androidannotations-rest-spring/rest-spring/src/main/java/org/androidannotations/rest/spring/handler/HeadersHandler.java @@ -0,0 +1,48 @@ +/** + * Copyright (C) 2010-2015 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.rest.spring.handler; + +import javax.lang.model.element.Element; + +import org.androidannotations.AndroidAnnotationsEnvironment; +import org.androidannotations.ElementValidation; +import org.androidannotations.handler.BaseAnnotationHandler; +import org.androidannotations.rest.spring.annotations.Header; +import org.androidannotations.rest.spring.annotations.Headers; +import org.androidannotations.rest.spring.helper.RestSpringValidatorHelper; +import org.androidannotations.rest.spring.holder.RestHolder; + +public class HeadersHandler extends BaseAnnotationHandler { + + private RestSpringValidatorHelper restValidatorHelper; + + public HeadersHandler(AndroidAnnotationsEnvironment environment) { + super(Headers.class, environment); + restValidatorHelper = new RestSpringValidatorHelper(environment, getTarget()); + } + + @Override + protected void validate(Element element, ElementValidation validation) { + restValidatorHelper.elementHasOneOfRestMethodAnnotations(element, validation); + + validatorHelper.doesNotHaveAnnotation(element, Header.class, validation); + } + + @Override + public void process(Element element, RestHolder holder) throws Exception { + // empty + } +} diff --git a/AndroidAnnotations/androidannotations-rest-spring/rest-spring/src/main/java/org/androidannotations/rest/spring/helper/RestAnnotationHelper.java b/AndroidAnnotations/androidannotations-rest-spring/rest-spring/src/main/java/org/androidannotations/rest/spring/helper/RestAnnotationHelper.java index 4f071f8191..449a822e71 100644 --- a/AndroidAnnotations/androidannotations-rest-spring/rest-spring/src/main/java/org/androidannotations/rest/spring/helper/RestAnnotationHelper.java +++ b/AndroidAnnotations/androidannotations-rest-spring/rest-spring/src/main/java/org/androidannotations/rest/spring/helper/RestAnnotationHelper.java @@ -51,6 +51,8 @@ import org.androidannotations.helper.TargetAnnotationHelper; import org.androidannotations.rest.spring.annotations.Accept; import org.androidannotations.rest.spring.annotations.Field; +import org.androidannotations.rest.spring.annotations.Header; +import org.androidannotations.rest.spring.annotations.Headers; import org.androidannotations.rest.spring.annotations.Part; import org.androidannotations.rest.spring.annotations.Path; import org.androidannotations.rest.spring.annotations.RequiresAuthentication; @@ -163,17 +165,36 @@ public boolean multipartHeaderRequired(ExecutableElement executableElement) { } public String[] requiredHeaders(ExecutableElement executableElement) { - RequiresHeader cookieAnnotation = executableElement.getAnnotation(RequiresHeader.class); - if (cookieAnnotation == null) { - cookieAnnotation = executableElement.getEnclosingElement().getAnnotation(RequiresHeader.class); + RequiresHeader requiresHeaderAnnotation = executableElement.getAnnotation(RequiresHeader.class); + if (requiresHeaderAnnotation == null) { + requiresHeaderAnnotation = executableElement.getEnclosingElement().getAnnotation(RequiresHeader.class); } - if (cookieAnnotation != null) { - return cookieAnnotation.value(); + if (requiresHeaderAnnotation != null) { + return requiresHeaderAnnotation.value(); } else { return null; } } + private Map getHeadersFromAnnotations(ExecutableElement executableElement) { + Headers headers = executableElement.getAnnotation(Headers.class); + Map headerMap = new HashMap<>(); + if (headers != null) { + Header[] headerList = headers.value(); + + for (Header header : headerList) { + headerMap.put(header.name(), header.value()); + } + } + + Header header = executableElement.getAnnotation(Header.class); + if (header != null) { + headerMap.put(header.name(), header.value()); + } + + return headerMap; + } + public String[] requiredCookies(ExecutableElement executableElement) { RequiresCookie cookieAnnotation = executableElement.getAnnotation(RequiresCookie.class); if (cookieAnnotation == null) { @@ -234,7 +255,9 @@ public JVar declareHttpHeaders(ExecutableElement executableElement, RestHolder h boolean requiresMultipartHeader = multipartHeaderRequired(executableElement); - if (hasMediaTypeDefined || requiresCookies || requiresHeaders || requiresAuth || requiresMultipartHeader) { + Map headersFromAnnotations = getHeadersFromAnnotations(executableElement); + + if (hasMediaTypeDefined || requiresCookies || requiresHeaders || requiresAuth || requiresMultipartHeader || !headersFromAnnotations.isEmpty()) { // we need the headers httpHeadersVar = body.decl(getEnvironment().getJClass(HTTP_HEADERS), "httpHeaders", JExpr._new(getEnvironment().getJClass(HTTP_HEADERS))); } @@ -247,6 +270,15 @@ public JVar declareHttpHeaders(ExecutableElement executableElement, RestHolder h body.add(JExpr.invoke(httpHeadersVar, "setAccept").arg(mediaTypeListParam)); } + // Set pre-defined headers here so that they can be overridden by any + // runtime calls + + if (headersFromAnnotations != null) { + for (Map.Entry header : headersFromAnnotations.entrySet()) { + body.add(JExpr.invoke(httpHeadersVar, "set").arg(header.getKey()).arg(header.getValue())); + } + } + if (requiresCookies) { JClass stringBuilderClass = getEnvironment().getClasses().STRING_BUILDER; JVar cookiesValueVar = body.decl(stringBuilderClass, "cookiesValue", JExpr._new(stringBuilderClass)); @@ -267,8 +299,15 @@ public JVar declareHttpHeaders(ExecutableElement executableElement, RestHolder h if (requiresHeaders) { for (String header : headers) { + JBlock block = null; + if (headersFromAnnotations.containsKey(header)) { + block = body._if(JExpr.invoke(holder.getAvailableHeadersField(), "containsKey").arg(header))._then(); + } else { + block = body; + } + JInvocation headerValue = JExpr.invoke(holder.getAvailableHeadersField(), "get").arg(header); - body.add(JExpr.invoke(httpHeadersVar, "set").arg(header).arg(headerValue)); + block.add(JExpr.invoke(httpHeadersVar, "set").arg(header).arg(headerValue)); } } diff --git a/AndroidAnnotations/androidannotations-rest-spring/rest-spring/src/main/java/org/androidannotations/rest/spring/helper/RestSpringValidatorHelper.java b/AndroidAnnotations/androidannotations-rest-spring/rest-spring/src/main/java/org/androidannotations/rest/spring/helper/RestSpringValidatorHelper.java index 9e9f5c7c56..cb58c000d6 100644 --- a/AndroidAnnotations/androidannotations-rest-spring/rest-spring/src/main/java/org/androidannotations/rest/spring/helper/RestSpringValidatorHelper.java +++ b/AndroidAnnotations/androidannotations-rest-spring/rest-spring/src/main/java/org/androidannotations/rest/spring/helper/RestSpringValidatorHelper.java @@ -563,4 +563,8 @@ public void restInterfaceHasFormConverter(Element element, ElementValidation val + FORM_HTTP_MESSAGE_CONVERTER + " (or subtype)"); } } + + public void elementHasOneOfRestMethodAnnotations(Element element, ElementValidation validation) { + hasOneOfAnnotations(element, element, REST_ANNOTATION_CLASSES, validation); + } } diff --git a/AndroidAnnotations/androidannotations-rest-spring/rest-spring/src/test/java/org/androidannotations/rest/spring/ClientWithHeaderAndHeadersOnSameMethod.java b/AndroidAnnotations/androidannotations-rest-spring/rest-spring/src/test/java/org/androidannotations/rest/spring/ClientWithHeaderAndHeadersOnSameMethod.java new file mode 100644 index 0000000000..ca55175323 --- /dev/null +++ b/AndroidAnnotations/androidannotations-rest-spring/rest-spring/src/test/java/org/androidannotations/rest/spring/ClientWithHeaderAndHeadersOnSameMethod.java @@ -0,0 +1,31 @@ +/** + * Copyright (C) 2010-2015 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.rest.spring; + +import org.androidannotations.rest.spring.annotations.Header; +import org.androidannotations.rest.spring.annotations.Headers; +import org.androidannotations.rest.spring.annotations.Post; +import org.androidannotations.rest.spring.annotations.Rest; +import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter; + +@Rest(converters = MappingJacksonHttpMessageConverter.class) +public interface ClientWithHeaderAndHeadersOnSameMethod { + + @Header(name = "testKey1", value = "testVal1") + @Headers(@Header(name = "testKey", value = "testVal")) + @Post("/test/") + void requestWithOneHeader(); +} diff --git a/AndroidAnnotations/androidannotations-rest-spring/rest-spring/src/test/java/org/androidannotations/rest/spring/ClientWithHeaderOnWrongMethod.java b/AndroidAnnotations/androidannotations-rest-spring/rest-spring/src/test/java/org/androidannotations/rest/spring/ClientWithHeaderOnWrongMethod.java new file mode 100644 index 0000000000..e2a83daaf7 --- /dev/null +++ b/AndroidAnnotations/androidannotations-rest-spring/rest-spring/src/test/java/org/androidannotations/rest/spring/ClientWithHeaderOnWrongMethod.java @@ -0,0 +1,28 @@ +/** + * Copyright (C) 2010-2015 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.rest.spring; + +import org.androidannotations.rest.spring.annotations.Header; +import org.androidannotations.rest.spring.annotations.Rest; +import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter; +import org.springframework.web.client.RestTemplate; + +@Rest(converters = MappingJacksonHttpMessageConverter.class) +public interface ClientWithHeaderOnWrongMethod { + + @Header(name = "testKey1", value = "testVal1") + RestTemplate getRestTemplate(); +} diff --git a/AndroidAnnotations/androidannotations-rest-spring/rest-spring/src/test/java/org/androidannotations/rest/spring/ClientWithMultipleHeaders.java b/AndroidAnnotations/androidannotations-rest-spring/rest-spring/src/test/java/org/androidannotations/rest/spring/ClientWithMultipleHeaders.java new file mode 100644 index 0000000000..33afadd0f3 --- /dev/null +++ b/AndroidAnnotations/androidannotations-rest-spring/rest-spring/src/test/java/org/androidannotations/rest/spring/ClientWithMultipleHeaders.java @@ -0,0 +1,30 @@ +/** + * Copyright (C) 2010-2015 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.rest.spring; + +import org.androidannotations.rest.spring.annotations.Header; +import org.androidannotations.rest.spring.annotations.Headers; +import org.androidannotations.rest.spring.annotations.Post; +import org.androidannotations.rest.spring.annotations.Rest; +import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter; + +@Rest(converters = MappingJacksonHttpMessageConverter.class) +public interface ClientWithMultipleHeaders { + + @Headers({ @Header(name = "testKey", value = "testVal"), @Header(name = "testKey1", value = "testVal1") }) + @Post("/test/") + void requestWithOneHeader(); +} diff --git a/AndroidAnnotations/androidannotations-rest-spring/rest-spring/src/test/java/org/androidannotations/rest/spring/ClientWithOneHeader.java b/AndroidAnnotations/androidannotations-rest-spring/rest-spring/src/test/java/org/androidannotations/rest/spring/ClientWithOneHeader.java new file mode 100644 index 0000000000..35014f81ba --- /dev/null +++ b/AndroidAnnotations/androidannotations-rest-spring/rest-spring/src/test/java/org/androidannotations/rest/spring/ClientWithOneHeader.java @@ -0,0 +1,30 @@ +/** + * Copyright (C) 2010-2015 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.rest.spring; + +import org.androidannotations.rest.spring.annotations.Header; +import org.androidannotations.rest.spring.annotations.Post; +import org.androidannotations.rest.spring.annotations.Rest; +import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter; + +@Rest(converters = MappingJacksonHttpMessageConverter.class) +public interface ClientWithOneHeader { + + @Header(name = "testKey", value = "testVal") + @Post("/test/") + void requestWithHeader(); + +} diff --git a/AndroidAnnotations/androidannotations-rest-spring/rest-spring/src/test/java/org/androidannotations/rest/spring/ClientWithOneHeaderInHeaders.java b/AndroidAnnotations/androidannotations-rest-spring/rest-spring/src/test/java/org/androidannotations/rest/spring/ClientWithOneHeaderInHeaders.java new file mode 100644 index 0000000000..7c28a6c653 --- /dev/null +++ b/AndroidAnnotations/androidannotations-rest-spring/rest-spring/src/test/java/org/androidannotations/rest/spring/ClientWithOneHeaderInHeaders.java @@ -0,0 +1,30 @@ +/** + * Copyright (C) 2010-2015 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.rest.spring; + +import org.androidannotations.rest.spring.annotations.Header; +import org.androidannotations.rest.spring.annotations.Headers; +import org.androidannotations.rest.spring.annotations.Post; +import org.androidannotations.rest.spring.annotations.Rest; +import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter; + +@Rest(converters = MappingJacksonHttpMessageConverter.class) +public interface ClientWithOneHeaderInHeaders { + + @Headers({ @Header(name = "testKey", value = "testVal") }) + @Post("/test/") + void requestWithOneHeader(); +} diff --git a/AndroidAnnotations/androidannotations-rest-spring/rest-spring/src/test/java/org/androidannotations/rest/spring/RestHeadersTest.java b/AndroidAnnotations/androidannotations-rest-spring/rest-spring/src/test/java/org/androidannotations/rest/spring/RestHeadersTest.java new file mode 100644 index 0000000000..c7c54f5de5 --- /dev/null +++ b/AndroidAnnotations/androidannotations-rest-spring/rest-spring/src/test/java/org/androidannotations/rest/spring/RestHeadersTest.java @@ -0,0 +1,69 @@ +/** + * Copyright (C) 2010-2015 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.rest.spring; + +import java.io.IOException; + +import org.androidannotations.internal.AndroidAnnotationProcessor; +import org.androidannotations.testutils.AAProcessorTestHelper; +import org.androidannotations.testutils.ProcessorTestHelper; +import org.junit.Before; +import org.junit.Test; + +public class RestHeadersTest extends AAProcessorTestHelper { + + @Before + public void setUp() { + addManifestProcessorParameter(RestConverterTest.class); + addProcessor(AndroidAnnotationProcessor.class); + } + + @Test + public void clientWithOneHeaderCompiles() { + ProcessorTestHelper.CompileResult result = compileFiles(ClientWithOneHeader.class); + assertCompilationSuccessful(result); + } + + @Test + public void clientWithOneHeaderInMultipleAnnotationCompiles() { + CompileResult result = compileFiles(ClientWithOneHeaderInHeaders.class); + assertCompilationSuccessful(result); + } + + @Test + public void clientWithMultipleHeadersCompiles() { + CompileResult result = compileFiles(ClientWithMultipleHeaders.class); + assertCompilationSuccessful(result); + } + + @Test + public void clientWithSingleAndMultipleHeadersOnSameMethod() throws IOException { + CompileResult result = compileFiles(ClientWithHeaderAndHeadersOnSameMethod.class); + + assertCompilationErrorOn(ClientWithHeaderAndHeadersOnSameMethod.class, "@Header(name = \"testKey1\", value = \"testVal1\")", result); + assertCompilationErrorOn(ClientWithHeaderAndHeadersOnSameMethod.class, "@Headers(@Header(name = \"testKey\", value = \"testVal\"))", result); + assertCompilationErrorCount(2, result); + } + + @Test + public void clientWithHeaderOnWrongMethod() throws IOException { + CompileResult result = compileFiles(ClientWithHeaderOnWrongMethod.class); + + assertCompilationErrorOn(ClientWithHeaderOnWrongMethod.class, "@Header(name = \"testKey1\", value = \"testVal1\")", result); + assertCompilationErrorCount(1, result); + } + +}