Search in sources :

Example 1 with JArray

use of com.helger.jcodemodel.JArray 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;
}
Also used : AbstractJType(com.helger.jcodemodel.AbstractJType) JBlock(com.helger.jcodemodel.JBlock) IJExpression(com.helger.jcodemodel.IJExpression) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JInvocation(com.helger.jcodemodel.JInvocation) JForEach(com.helger.jcodemodel.JForEach) JVar(com.helger.jcodemodel.JVar) JArray(com.helger.jcodemodel.JArray)

Aggregations

AbstractJClass (com.helger.jcodemodel.AbstractJClass)1 AbstractJType (com.helger.jcodemodel.AbstractJType)1 IJExpression (com.helger.jcodemodel.IJExpression)1 JArray (com.helger.jcodemodel.JArray)1 JBlock (com.helger.jcodemodel.JBlock)1 JForEach (com.helger.jcodemodel.JForEach)1 JInvocation (com.helger.jcodemodel.JInvocation)1 JVar (com.helger.jcodemodel.JVar)1