Search in sources :

Example 1 with TemplateDataOutput

use of io.clownfish.clownfish.datamodels.TemplateDataOutput in project Clownfish by rawdog71.

the class GetTemplates method doGet.

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
 * Handles the HTTP <code>GET</code> method.
 *
 * @param request servlet request
 * @param response servlet response
 */
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
    try {
        String token = request.getParameter("token");
        if (authtokenlist.checkValidToken(token)) {
            String apikey = request.getParameter("apikey");
            if (apikeyutil.checkApiKey(apikey, "RestService")) {
                CfTemplate template = null;
                List<CfTemplate> templateList = new ArrayList<>();
                String templateid = request.getParameter("id");
                if (templateid != null) {
                    template = cftemplateService.findById(Long.parseLong(templateid));
                    templateList.add(template);
                }
                String templatename = request.getParameter("name");
                if (templatename != null) {
                    template = cftemplateService.findByName(templatename);
                    templateList.clear();
                    templateList.add(template);
                }
                if ((null == templateid) && (null == templatename)) {
                    templateList = cftemplateService.findAll();
                }
                ArrayList<TemplateDataOutput> templatedataoutputList = new ArrayList<>();
                for (CfTemplate templateItem : templateList) {
                    TemplateDataOutput templatedataoutput = new TemplateDataOutput();
                    templatedataoutput.setTemplate(templateItem);
                    templatedataoutputList.add(templatedataoutput);
                }
                Gson gson = new Gson();
                String json = gson.toJson(templatedataoutputList);
                response.setContentType("application/json;charset=UTF-8");
                try (PrintWriter out = response.getWriter()) {
                    out.print(json);
                } catch (IOException ex) {
                    LOGGER.error(ex.getMessage());
                }
            } else {
                PrintWriter out = response.getWriter();
                out.print("Wrong API KEY");
            }
        } else {
            PrintWriter out = response.getWriter();
            out.print("Invalid Token");
        }
    } catch (javax.persistence.NoResultException | java.lang.IllegalArgumentException ex) {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            out.print("No class");
        } catch (IOException ex1) {
            LOGGER.error(ex1.getMessage());
        }
    } catch (IOException ex) {
        LOGGER.error(ex.getMessage());
    }
}
Also used : ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) IOException(java.io.IOException) CfTemplate(io.clownfish.clownfish.dbentities.CfTemplate) TemplateDataOutput(io.clownfish.clownfish.datamodels.TemplateDataOutput) PrintWriter(java.io.PrintWriter)

Aggregations

Gson (com.google.gson.Gson)1 TemplateDataOutput (io.clownfish.clownfish.datamodels.TemplateDataOutput)1 CfTemplate (io.clownfish.clownfish.dbentities.CfTemplate)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 ArrayList (java.util.ArrayList)1