Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
full implementation of restful headers (I think). Need to do lots of …
…testing
  • Loading branch information
maltzj committed Apr 15, 2014
commit 3f31e6da01ff2a62a917d7dc48d45737925fea3f
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.androidannotations.annotations.rest;

/**
* Use on methods in {@link Rest} annotated class to add headers to a given method
*/
public @interface Headers {
Header[] value();
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
Expand All @@ -40,6 +41,8 @@
import javax.lang.model.type.WildcardType;

import org.androidannotations.annotations.rest.Accept;
import org.androidannotations.annotations.rest.Header;
import org.androidannotations.annotations.rest.Headers;
import org.androidannotations.annotations.rest.RequiresAuthentication;
import org.androidannotations.annotations.rest.RequiresCookie;
import org.androidannotations.annotations.rest.RequiresCookieInUrl;
Expand Down Expand Up @@ -194,6 +197,39 @@ public String[] requiredHeaders(ExecutableElement executableElement) {
}
}

private Map<String, String> declaredHeaders(ExecutableElement executableElement) {
Headers headers = executableElement.getAnnotation(Headers.class);
//HEY CODE REVIEWER: DO I NEED TO CALL .getEnclosingElement HERE() WHY/WHY NOT?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use GitHub comments instead of commenting this kind of things directly into the code...

Map<String, String> headerMap = new HashMap<String, String>();
if (headers != null) {
Header[] headerList = headers.value();

// Prevent an empty annotation from crashing things
if (headerList != null) {
for (Header header : headerList) {
headerMap.putAll(processHeader(header));
}
}
}

Header header = executableElement.getAnnotation(Header.class);
if (header != null) {
headerMap.putAll(processHeader(header));
}

return headerMap;
}

private Map<String, String> processHeader(Header singleHeader) {
if (singleHeader == null) {
return null;
} else {
Map<String, String> headerInfo = new HashMap<String, String>();
headerInfo.put(singleHeader.key(), singleHeader.value());
return headerInfo;
}
}

public String[] requiredCookies(ExecutableElement executableElement) {
RequiresCookie cookieAnnotation = executableElement.getAnnotation(RequiresCookie.class);
if (cookieAnnotation == null) {
Expand Down Expand Up @@ -265,6 +301,14 @@ 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
Map<String, String> declaredHeaders = declaredHeaders(executableElement);
if (declaredHeaders != null) {
for (Map.Entry<String, String> declaredHeader: declaredHeaders.entrySet()) {
body.add(JExpr.invoke(httpHeadersVar, "set").arg(declaredHeader.getKey()).arg(declaredHeader.getValue()));
}
}

if (requiresCookies) {
JClass stringBuilderClass = holder.classes().STRING_BUILDER;
JVar cookiesValueVar = body.decl(stringBuilderClass, "cookiesValue", JExpr._new(stringBuilderClass));
Expand All @@ -279,12 +323,11 @@ public JVar declareHttpHeaders(ExecutableElement executableElement, RestHolder h
body.add(JExpr.invoke(httpHeadersVar, "set").arg("Cookie").arg(cookiesToString));
}

if (requiresHeaders) {
if (requiresHeaders) {
for (String header : headers) {
JInvocation headerValue = JExpr.invoke(holder.getAvailableHeadersField(), "get").arg(header);
body.add(JExpr.invoke(httpHeadersVar, "set").arg(header).arg(headerValue));
}

}

if (requiresAuth) {
Expand Down