Search in sources :

Example 1 with BadRequestResponse

use of io.javalin.http.BadRequestResponse in project cwms-radar-api by USACE.

the class JsonV1 method buildFormatting.

private Object buildFormatting(List<? extends CwmsDTO> daoList) {
    Object retval = null;
    if (daoList != null && !daoList.isEmpty()) {
        CwmsDTO firstObj = daoList.get(0);
        if (firstObj instanceof Office) {
            List<Office> officesList = daoList.stream().map(Office.class::cast).collect(Collectors.toList());
            retval = new OfficeFormatV1(officesList);
        } else if (dataTypesContains(firstObj.getClass())) {
            // If dataType annotated with the class we can return an array of them.
            // If a class needs to be handled differently an else_if branch can be added above
            // here and a wrapper obj used to format the return value however is desired.
            retval = daoList;
        }
        if (retval == null) {
            String klassName = "unknown";
            if (firstObj != null) {
                klassName = firstObj.getClass().getName();
            }
            throw new BadRequestResponse(String.format("Format %s not implemented for data of class:%s", getContentType(), klassName));
        }
    }
    return retval;
}
Also used : Office(cwms.radar.data.dto.Office) CwmsDTO(cwms.radar.data.dto.CwmsDTO) OfficeFormatV1(cwms.radar.formatters.OfficeFormatV1) BadRequestResponse(io.javalin.http.BadRequestResponse)

Example 2 with BadRequestResponse

use of io.javalin.http.BadRequestResponse in project cwms-radar-api by USACE.

the class JsonV1 method buildFormatting.

private Object buildFormatting(CwmsDTO dao) {
    Object retval = null;
    if (dao instanceof Office) {
        List<Office> offices = Arrays.asList((Office) dao);
        retval = new OfficeFormatV1(offices);
    } else if (dataTypesContains(dao.getClass())) {
        // Any types that have to be handle as special cases
        // should be in else if branches before this
        // If the class is in the annotation assume we can just return it.
        retval = dao;
    }
    if (retval == null) {
        String klassName = "unknown";
        if (dao != null) {
            klassName = dao.getClass().getName();
        }
        throw new BadRequestResponse(String.format("Format %s not implemented for data of class:%s", getContentType(), klassName));
    }
    return retval;
}
Also used : Office(cwms.radar.data.dto.Office) OfficeFormatV1(cwms.radar.formatters.OfficeFormatV1) BadRequestResponse(io.javalin.http.BadRequestResponse)

Aggregations

Office (cwms.radar.data.dto.Office)2 OfficeFormatV1 (cwms.radar.formatters.OfficeFormatV1)2 BadRequestResponse (io.javalin.http.BadRequestResponse)2 CwmsDTO (cwms.radar.data.dto.CwmsDTO)1