use of com.helger.jcodemodel.JForEach in project androidannotations by androidannotations.
the class RestMethodHandler method setCookies.
private IJExpression setCookies(ExecutableElement executableElement, RestHolder restHolder, JBlock methodBody, JInvocation exchangeCall) {
String[] settingCookies = restAnnotationHelper.settingCookies(executableElement);
if (settingCookies != null) {
boolean methodReturnVoid = executableElement.getReturnType().getKind() == TypeKind.VOID;
AbstractJClass exchangeResponseClass = restAnnotationHelper.retrieveResponseClass(executableElement.getReturnType(), restHolder);
AbstractJType narrowType = exchangeResponseClass == null || methodReturnVoid ? getCodeModel().VOID : exchangeResponseClass;
AbstractJClass responseEntityClass = getJClass(RESPONSE_ENTITY).narrow(narrowType);
JVar responseEntity = methodBody.decl(responseEntityClass, "response", exchangeCall);
// set cookies
AbstractJClass stringListClass = getClasses().LIST.narrow(getClasses().STRING);
AbstractJClass stringArrayClass = getClasses().STRING.array();
JArray cookiesArray = JExpr.newArray(getClasses().STRING);
for (String cookie : settingCookies) {
cookiesArray.add(JExpr.lit(cookie));
}
JVar requestedCookiesVar = methodBody.decl(stringArrayClass, "requestedCookies", cookiesArray);
JInvocation setCookiesList = JExpr.invoke(responseEntity, "getHeaders").invoke("get").arg("Set-Cookie");
JVar allCookiesList = methodBody.decl(stringListClass, "allCookies", setCookiesList);
// for loop over list... add if in string array
JForEach forEach = //
methodBody._if(allCookiesList.ne(JExpr._null()))._then().forEach(getClasses().STRING, "rawCookie", allCookiesList);
JVar rawCookieVar = forEach.var();
JBlock forLoopBody = forEach.body();
JForEach innerForEach = forLoopBody.forEach(getClasses().STRING, "thisCookieName", requestedCookiesVar);
JBlock innerBody = innerForEach.body();
JBlock thenBlock = innerBody._if(JExpr.invoke(rawCookieVar, "startsWith").arg(innerForEach.var().plus("=")))._then();
// where does the cookie VALUE end?
JInvocation valueEnd = rawCookieVar.invoke("indexOf").arg(JExpr.lit(';'));
JVar valueEndVar = thenBlock.decl(getCodeModel().INT, "valueEnd", valueEnd);
JBlock fixValueEndBlock = thenBlock._if(valueEndVar.eq(JExpr.lit(-1)))._then();
fixValueEndBlock.assign(valueEndVar, rawCookieVar.invoke("length"));
IJExpression indexOfValue = rawCookieVar.invoke("indexOf").arg("=").plus(JExpr.lit(1));
JInvocation cookieValue = rawCookieVar.invoke("substring").arg(indexOfValue).arg(valueEndVar);
thenBlock.invoke(restHolder.getAvailableCookiesField(), "put").arg(innerForEach.var()).arg(cookieValue);
thenBlock._break();
return responseEntity;
}
return exchangeCall;
}
Aggregations