Search in sources :

Example 51 with JInvocation

use of com.helger.jcodemodel.JInvocation 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)

Example 52 with JInvocation

use of com.helger.jcodemodel.JInvocation in project androidannotations by androidannotations.

the class RestMethodHandler method process.

@Override
public void process(Element element, RestHolder holder) {
    ExecutableElement executableElement = (ExecutableElement) element;
    String methodName = element.getSimpleName().toString();
    AbstractJClass methodReturnClass = getMethodReturnClass(element, holder);
    boolean methodReturnVoid = executableElement.getReturnType().getKind() == TypeKind.VOID;
    // Creating method signature
    JMethod method = holder.getGeneratedClass().method(JMod.PUBLIC, methodReturnClass, methodName);
    method.annotate(Override.class);
    SortedMap<String, JVar> params = addMethodParams(executableElement, holder, method);
    JBlock methodBody = new JBlock().bracesRequired(false).indentRequired(false);
    // RestTemplate exchange() method call
    JInvocation exchangeCall = JExpr.invoke(holder.getRestTemplateField(), "exchange");
    exchangeCall.arg(getUrl(element, holder));
    exchangeCall.arg(getHttpMethod());
    exchangeCall.arg(getRequestEntity(executableElement, holder, methodBody, params));
    exchangeCall.arg(getResponseClass(element, holder));
    IJExpression urlVariables = getUrlVariables(element, holder, methodBody, params);
    if (urlVariables != null) {
        exchangeCall.arg(urlVariables);
    }
    IJExpression response = setCookies(executableElement, holder, methodBody, exchangeCall);
    if (methodReturnVoid && response.equals(exchangeCall)) {
        methodBody.add(exchangeCall);
    } else if (!methodReturnVoid) {
        methodBody._return(addResultCallMethod(response, methodReturnClass));
    }
    methodBody = surroundWithRestTryCatch(holder, methodBody, methodReturnVoid);
    codeModelHelper.copy(methodBody, method.body());
}
Also used : ExecutableElement(javax.lang.model.element.ExecutableElement) JBlock(com.helger.jcodemodel.JBlock) IJExpression(com.helger.jcodemodel.IJExpression) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JInvocation(com.helger.jcodemodel.JInvocation) JMethod(com.helger.jcodemodel.JMethod) JVar(com.helger.jcodemodel.JVar)

Example 53 with JInvocation

use of com.helger.jcodemodel.JInvocation in project androidannotations by androidannotations.

the class RestAnnotationHelper method declareHttpEntity.

public IJExpression declareHttpEntity(JBlock body, JVar entitySentToServer, JVar httpHeaders) {
    AbstractJType entityType = getEnvironment().getJClass(Object.class);
    if (entitySentToServer != null) {
        entityType = entitySentToServer.type();
        if (entityType.isPrimitive()) {
            // Don't narrow primitive types...
            entityType = entityType.boxify();
        }
    }
    AbstractJClass httpEntity = getEnvironment().getJClass(HTTP_ENTITY);
    AbstractJClass narrowedHttpEntity = httpEntity.narrow(entityType);
    JInvocation newHttpEntityVarCall = JExpr._new(narrowedHttpEntity);
    if (entitySentToServer != null) {
        newHttpEntityVarCall.arg(entitySentToServer);
    }
    if (httpHeaders != null) {
        newHttpEntityVarCall.arg(httpHeaders);
    } else if (entitySentToServer == null) {
        return JExpr._null();
    }
    return body.decl(narrowedHttpEntity, "requestEntity", newHttpEntityVarCall);
}
Also used : AbstractJType(com.helger.jcodemodel.AbstractJType) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JInvocation(com.helger.jcodemodel.JInvocation)

Example 54 with JInvocation

use of com.helger.jcodemodel.JInvocation in project androidannotations by androidannotations.

the class RestAnnotationHelper method declareUrlVariables.

public JVar declareUrlVariables(ExecutableElement element, RestHolder holder, JBlock methodBody, SortedMap<String, JVar> methodParams) {
    Map<String, String> urlNameToElementName = new HashMap<String, String>();
    for (VariableElement variableElement : element.getParameters()) {
        if (variableElement.getAnnotation(Path.class) != null) {
            urlNameToElementName.put(getUrlVariableCorrespondingTo(variableElement), variableElement.getSimpleName().toString());
        }
    }
    Set<String> urlVariables = extractUrlVariableNames(element);
    // cookies in url?
    String[] cookiesToUrl = requiredUrlCookies(element);
    if (cookiesToUrl != null) {
        for (String cookie : cookiesToUrl) {
            urlVariables.add(cookie);
        }
    }
    AbstractJClass hashMapClass = getEnvironment().getClasses().HASH_MAP.narrow(String.class, Object.class);
    if (!urlVariables.isEmpty()) {
        JVar hashMapVar = methodBody.decl(hashMapClass, "urlVariables", JExpr._new(hashMapClass));
        for (String urlVariable : urlVariables) {
            String elementName = urlNameToElementName.get(urlVariable);
            if (elementName != null) {
                JVar methodParam = methodParams.get(elementName);
                methodBody.invoke(hashMapVar, "put").arg(urlVariable).arg(methodParam);
                methodParams.remove(elementName);
            } else {
                // cookie from url
                JInvocation cookieValue = holder.getAvailableCookiesField().invoke("get").arg(JExpr.lit(urlVariable));
                methodBody.invoke(hashMapVar, "put").arg(urlVariable).arg(cookieValue);
            }
        }
        return hashMapVar;
    }
    return null;
}
Also used : Path(org.androidannotations.rest.spring.annotations.Path) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JInvocation(com.helger.jcodemodel.JInvocation) VariableElement(javax.lang.model.element.VariableElement) JVar(com.helger.jcodemodel.JVar)

Example 55 with JInvocation

use of com.helger.jcodemodel.JInvocation in project androidannotations by androidannotations.

the class RestAnnotationHelper method declareHttpHeaders.

public JVar declareHttpHeaders(ExecutableElement executableElement, RestHolder holder, JBlock body) {
    JVar httpHeadersVar = null;
    String mediaType = acceptedHeaders(executableElement);
    boolean hasMediaTypeDefined = mediaType != null;
    String[] cookies = requiredCookies(executableElement);
    boolean requiresCookies = cookies != null && cookies.length > 0;
    String[] headers = requiredHeaders(executableElement);
    boolean requiresHeaders = headers != null && headers.length > 0;
    boolean requiresAuth = requiredAuthentication(executableElement);
    boolean requiresMultipartHeader = multipartHeaderRequired(executableElement);
    Map<String, String> 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)));
    }
    if (hasMediaTypeDefined) {
        AbstractJClass collectionsClass = getEnvironment().getJClass(CanonicalNameConstants.COLLECTIONS);
        AbstractJClass mediaTypeClass = getEnvironment().getJClass(MEDIA_TYPE);
        JInvocation mediaTypeListParam = collectionsClass.staticInvoke("singletonList").arg(mediaTypeClass.staticInvoke("parseMediaType").arg(mediaType));
        body.add(JExpr.invoke(httpHeadersVar, "setAccept").arg(mediaTypeListParam));
    }
    if (headersFromAnnotations != null) {
        for (Map.Entry<String, String> header : headersFromAnnotations.entrySet()) {
            body.add(JExpr.invoke(httpHeadersVar, "set").arg(header.getKey()).arg(header.getValue()));
        }
    }
    if (requiresCookies) {
        AbstractJClass stringBuilderClass = getEnvironment().getClasses().STRING_BUILDER;
        JVar cookiesValueVar = body.decl(stringBuilderClass, "cookiesValue", JExpr._new(stringBuilderClass));
        for (String cookie : cookies) {
            JInvocation cookieValue = JExpr.invoke(holder.getAvailableCookiesField(), "get").arg(cookie);
            JInvocation cookieFormatted = getEnvironment().getClasses().STRING.staticInvoke("format").arg(String.format("%s=%%s;", cookie)).arg(cookieValue);
            JInvocation appendCookie = JExpr.invoke(cookiesValueVar, "append").arg(cookieFormatted);
            body.add(appendCookie);
        }
        JInvocation cookiesToString = cookiesValueVar.invoke("toString");
        body.add(JExpr.invoke(httpHeadersVar, "set").arg("Cookie").arg(cookiesToString));
    }
    if (requiresMultipartHeader) {
        body.add(JExpr.invoke(httpHeadersVar, "set").arg(JExpr.lit("Content-Type")).arg(getEnvironment().getJClass(MEDIA_TYPE).staticRef("MULTIPART_FORM_DATA_VALUE")));
    }
    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);
            block.add(JExpr.invoke(httpHeadersVar, "set").arg(header).arg(headerValue));
        }
    }
    if (requiresAuth) {
        // attach auth
        body.add(httpHeadersVar.invoke("setAuthorization").arg(holder.getAuthenticationField()));
    }
    return httpHeadersVar;
}
Also used : JBlock(com.helger.jcodemodel.JBlock) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JInvocation(com.helger.jcodemodel.JInvocation) Map(java.util.Map) SortedMap(java.util.SortedMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) JVar(com.helger.jcodemodel.JVar)

Aggregations

JInvocation (com.helger.jcodemodel.JInvocation)71 JVar (com.helger.jcodemodel.JVar)38 JBlock (com.helger.jcodemodel.JBlock)37 AbstractJClass (com.helger.jcodemodel.AbstractJClass)31 IJExpression (com.helger.jcodemodel.IJExpression)27 JMethod (com.helger.jcodemodel.JMethod)26 ExecutableElement (javax.lang.model.element.ExecutableElement)20 VariableElement (javax.lang.model.element.VariableElement)16 JFieldRef (com.helger.jcodemodel.JFieldRef)14 TypeMirror (javax.lang.model.type.TypeMirror)14 JConditional (com.helger.jcodemodel.JConditional)11 JDefinedClass (com.helger.jcodemodel.JDefinedClass)9 AbstractJType (com.helger.jcodemodel.AbstractJType)5 JFieldVar (com.helger.jcodemodel.JFieldVar)5 JTryBlock (com.helger.jcodemodel.JTryBlock)5 ArrayList (java.util.ArrayList)4 DeclaredType (javax.lang.model.type.DeclaredType)4 JCatchBlock (com.helger.jcodemodel.JCatchBlock)3 PageChangeHolder (org.androidannotations.holder.PageChangeHolder)3 TextWatcherHolder (org.androidannotations.holder.TextWatcherHolder)3