All Downloads are FREE. Search and download functionalities are using the official Maven repository.

JavaPlayFramework.openapiUtils.mustache Maven / Gradle / Ivy

There is a newer version: 7.9.0
Show newest version
package openapitools;

import play.mvc.With;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.text.SimpleDateFormat;
import java.util.*;

{{#useBeanValidation}}
import {{javaxPackage}}.validation.ConstraintViolation;
import {{javaxPackage}}.validation.Validation;
import {{javaxPackage}}.validation.Validator;
import {{javaxPackage}}.validation.ValidatorFactory;
{{/useBeanValidation}}

public class OpenAPIUtils {

{{#wrapCalls}}
    @With(ApiCall.class)
    @Target({ ElementType.TYPE, ElementType.METHOD })
    @Retention(RetentionPolicy.RUNTIME)
    public @interface ApiAction {
    }
{{/wrapCalls}}

{{#useBeanValidation}}
    public static  void validate(T obj) {
        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        Validator validator = factory.getValidator();
        Set> constraintViolations = validator.validate(obj);
        if (constraintViolations.size() > 0) {
            StringBuilder errors = new StringBuilder();
            for (ConstraintViolation constraints : constraintViolations) {
                errors.append(String.format("%s.%s %s\n",
                constraints.getRootBeanClass().getSimpleName(),
                constraints.getPropertyPath(),
                constraints.getMessage()));
            }
            throw new RuntimeException("Bean validation : " + errors);
        }
    }
{{/useBeanValidation}}

    public static List parametersToList(String collectionFormat, String[] values){
        List params = new ArrayList<>();

        if (values == null) {
            return params;
        }

        if (values.length >= 1 && collectionFormat.equals("multi")) {
            params.addAll(Arrays.asList(values));
        } else {
            collectionFormat = (collectionFormat == null || collectionFormat.isEmpty() ? "csv" : collectionFormat); // default: csv

            String delimiter = ",";

            switch(collectionFormat) {
                case "csv": {
                    delimiter = ",";
                    break;
                }
                case "ssv": {
                    delimiter = " ";
                    break;
                }
                case "tsv": {
                    delimiter = "\t";
                    break;
                }
                case "pipes": {
                    delimiter = "|";
                    break;
                }
            }

            params = Arrays.asList(values[0].split(delimiter));
        }

        return params;
    }

    public static String parameterToString(Object param) {
        if (param == null) {
            return "";
        } else if (param instanceof Date) {
            return formatDatetime((Date) param);
        } else if (param instanceof Collection) {
            StringBuilder b = new StringBuilder();
            for (Object o : (Collection)param) {
                if (b.length() > 0) {
                    b.append(",");
            }
            b.append(String.valueOf(o));
        }

        return b.toString();
        } else {
            return String.valueOf(param);
        }
    }

    public static String formatDatetime(Date date) {
        return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", Locale.ROOT).format(date);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy