Search in sources :

Example 1 with JWhileLoop

use of com.sun.codemodel.JWhileLoop in project raml-module-builder by folio-org.

the class ClientGenerator method handleParams.

/**
 * @param paramType
 * @param valueType
 */
private boolean handleParams(JMethod method, JVar queryParams, String paramType, String valueType, String valueName) {
    JBlock methodBody = method.body();
    if (AnnotationGrabber.NON_ANNOTATED_PARAM.equals(paramType)) /*&& !FILE_UPLOAD_PARAM.equals(valueType)*/
    {
        try {
            // this will also validate the json against the pojo created from the schema
            Class<?> entityClazz = Class.forName(valueType);
            if (!valueType.equals("io.vertx.core.Handler") && !valueType.equals("io.vertx.core.Context") && !valueType.equals("java.util.Map") && !valueType.equals("io.vertx.ext.web.RoutingContext")) {
                /* this is a post or put since our only options here are receiving a reader (data in body) or
           * entity - which is also data in body - but we can only have one since a multi part body
           * should be indicated by a multipart objector input stream in the body */
                JExpression jexpr = jcodeModel.ref(io.vertx.core.buffer.Buffer.class).staticInvoke("buffer");
                JVar buffer = methodBody.decl(jcodeModel.ref(io.vertx.core.buffer.Buffer.class), "buffer", jexpr);
                if ("java.io.Reader".equals(valueType)) {
                    JVar reader = method.param(Reader.class, "reader");
                    method._throws(Exception.class);
                    JConditional ifClause = methodBody._if(reader.ne(JExpr._null()));
                    ifClause._then().directStatement("buffer.appendString(org.apache.commons.io.IOUtils.toString(reader));");
                } else if ("java.io.InputStream".equals(valueType)) {
                    JVar inputStream = method.param(InputStream.class, "inputStream");
                    JVar result = methodBody.decl(jcodeModel.ref(ByteArrayOutputStream.class), "result", JExpr._new(jcodeModel.ref(ByteArrayOutputStream.class)));
                    JVar byteA = methodBody.decl(jcodeModel.BYTE.array(), "buffer1", JExpr.newArray(jcodeModel.BYTE, 1024));
                    JVar length = methodBody.decl(jcodeModel.INT, "length");
                    // http://stackoverflow.com/questions/26037015/how-do-i-force-enclose-a-codemodel-expression-in-brackets
                    JWhileLoop whileClause = methodBody._while(JExpr.TRUE);
                    whileClause.body().assign(length, inputStream.invoke("read").arg(byteA));
                    whileClause.body()._if(length.eq(JExpr.lit(-1)))._then()._break();
                    whileClause.body().add(result.invoke("write").arg(byteA).arg(JExpr.lit(0)).arg(length));
                    methodBody.add(buffer.invoke("appendBytes").arg(result.invoke("toByteArray")));
                    method._throws(IOException.class);
                } else if ("javax.mail.internet.MimeMultipart".equals(valueType)) {
                    JVar mimeMultiPart = method.param(MimeMultipart.class, "mimeMultipart");
                    method._throws(MessagingException.class);
                    method._throws(IOException.class);
                    JBlock b1 = methodBody._if(mimeMultiPart.ne(JExpr._null()))._then();
                    JVar parts = b1.decl(jcodeModel.INT, "parts", mimeMultiPart.invoke("getCount"));
                    JVar sb = b1.decl(jcodeModel._ref(StringBuilder.class), "sb", JExpr._new(jcodeModel.ref(StringBuilder.class)));
                    JForLoop forClause = b1._for();
                    JVar iVar = forClause.init(jcodeModel._ref(int.class), "i", JExpr.lit(0));
                    forClause.test(iVar.lt(parts));
                    forClause.update(iVar.incr());
                    JBlock fBody = forClause.body();
                    JVar bp = fBody.decl(jcodeModel.ref(javax.mail.BodyPart.class), "bp", mimeMultiPart.invoke("getBodyPart").arg(iVar));
                    fBody.add(sb.invoke(APPEND).arg("----BOUNDARY\r\n"));
                    fBody.add(sb.invoke(APPEND).arg("Content-Disposition: \""));
                    fBody.add(sb.invoke(APPEND).arg(bp.invoke("getDisposition")));
                    fBody.add(sb.invoke(APPEND).arg("\"; name=\""));
                    fBody.add(sb.invoke(APPEND).arg(bp.invoke("getFileName")));
                    fBody.add(sb.invoke(APPEND).arg("\"; filename=\")"));
                    fBody.add(sb.invoke(APPEND).arg(bp.invoke("getFileName")));
                    fBody.add(sb.invoke(APPEND).arg("\"\r\n"));
                    fBody.add(sb.invoke(APPEND).arg("Content-Type: application/octet-stream\r\n"));
                    fBody.add(sb.invoke(APPEND).arg("Content-Transfer-Encoding: binary\r\n"));
                    b1.add(sb.invoke(APPEND).arg("----BOUNDARY\r\n"));
                    b1.add(buffer.invoke("appendString").arg(sb.invoke("toString")));
                } else {
                    String objParamName = entityClazz.getSimpleName();
                    JConditional ifClause = methodBody._if(JExpr.ref(objParamName).ne(JExpr._null()));
                    JBlock b = ifClause._then();
                    if (mappingType.equals("postgres")) {
                        b.directStatement("buffer.appendString(" + "org.folio.rest.tools.ClientHelpers.pojo2json(" + objParamName + "));");
                    } else {
                        b.directStatement("buffer.appendString(" + "org.folio.rest.tools.utils.JsonUtils.entity2Json(" + objParamName + ").encode());");
                    }
                    method.param(entityClazz, entityClazz.getSimpleName());
                }
                return true;
            }
        } catch (Exception e) {
            log.log(Level.SEVERE, e.getMessage(), e);
        }
    } else if (AnnotationGrabber.PATH_PARAM.equals(paramType)) {
        method.param(String.class, valueName);
    } else if (AnnotationGrabber.HEADER_PARAM.equals(paramType)) {
        method.param(String.class, valueName);
        functionSpecificHeaderParams.add("request.putHeader(\"" + valueName + "\", " + valueName + ");");
    } else if (AnnotationGrabber.QUERY_PARAM.equals(paramType)) {
        // support date, enum, numbers or strings as query parameters
        try {
            if (valueType.contains("String")) {
                method.param(String.class, valueName);
                addParameter(new ParameterDetails(methodBody, queryParams, valueName).withOp(ParameterOp.ENCODE));
            } else if (valueType.contains("Date")) {
                method.param(Date.class, valueName);
                addParameter(new ParameterDetails(methodBody, queryParams, valueName).withOp(ParameterOp.FORMAT_DATE));
            } else if (valueType.contains("int")) {
                method.param(int.class, valueName);
                addParameter(new ParameterDetails(methodBody, queryParams, valueName).nullCheck(false));
            } else if (valueType.contains("boolean")) {
                method.param(boolean.class, valueName);
                addParameter(new ParameterDetails(methodBody, queryParams, valueName).nullCheck(false));
            } else if (valueType.contains("BigDecimal")) {
                method.param(BigDecimal.class, valueName);
                addParameter(new ParameterDetails(methodBody, queryParams, valueName));
            } else if (valueType.contains("Number")) {
                method.param(Number.class, valueName);
                addParameter(new ParameterDetails(methodBody, queryParams, valueName));
            } else if (valueType.contains("Integer")) {
                method.param(Integer.class, valueName);
                addParameter(new ParameterDetails(methodBody, queryParams, valueName));
            } else if (valueType.contains("Boolean")) {
                method.param(Boolean.class, valueName);
                addParameter(new ParameterDetails(methodBody, queryParams, valueName));
            } else if (valueType.contains("List")) {
                method.param(String[].class, valueName);
                addParameter(new ParameterDetails(methodBody, queryParams, valueName).withOp(ParameterOp.PROCESS_LIST));
            } else {
                // enum object type
                Class<?> enumClazz = classForName(valueType);
                if (enumClazz.isEnum()) {
                    method.param(enumClazz, valueName);
                    addParameter(new ParameterDetails(methodBody, queryParams, valueName));
                }
            }
        } catch (Exception e) {
            log.log(Level.SEVERE, e.getMessage(), e);
        }
    }
    return false;
}
Also used : Buffer(io.vertx.core.buffer.Buffer) InputStream(java.io.InputStream) JWhileLoop(com.sun.codemodel.JWhileLoop) JExpression(com.sun.codemodel.JExpression) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException) BigDecimal(java.math.BigDecimal) JBlock(com.sun.codemodel.JBlock) JConditional(com.sun.codemodel.JConditional) JDefinedClass(com.sun.codemodel.JDefinedClass) JClass(com.sun.codemodel.JClass) JForLoop(com.sun.codemodel.JForLoop) JVar(com.sun.codemodel.JVar)

Aggregations

JBlock (com.sun.codemodel.JBlock)1 JClass (com.sun.codemodel.JClass)1 JConditional (com.sun.codemodel.JConditional)1 JDefinedClass (com.sun.codemodel.JDefinedClass)1 JExpression (com.sun.codemodel.JExpression)1 JForLoop (com.sun.codemodel.JForLoop)1 JVar (com.sun.codemodel.JVar)1 JWhileLoop (com.sun.codemodel.JWhileLoop)1 Buffer (io.vertx.core.buffer.Buffer)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 BigDecimal (java.math.BigDecimal)1 MessagingException (javax.mail.MessagingException)1