use of io.clownfish.clownfish.datamodels.RestKeywordListOutput in project Clownfish by rawdog71.
the class GetKeywordLibraries 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")) {
CfKeywordlist keywordlist = null;
List<CfKeywordlist> keywordlistList = new ArrayList<>();
String keywordlistid = request.getParameter("id");
if (keywordlistid != null) {
keywordlist = cfkeywordlistService.findById(Long.parseLong(keywordlistid));
keywordlistList.add(keywordlist);
}
String keywordlistname = request.getParameter("name");
if (keywordlistname != null) {
keywordlist = cfkeywordlistService.findByName(keywordlistname);
keywordlistList.clear();
keywordlistList.add(keywordlist);
}
if ((null == keywordlistid) && (null == keywordlistname)) {
keywordlistList = cfkeywordlistService.findAll();
}
ArrayList<RestKeywordListOutput> keywordlistoutputList = new ArrayList<>();
for (CfKeywordlist keywordlistItem : keywordlistList) {
List<CfKeyword> keywordList = new ArrayList<>();
List<CfKeywordlistcontent> keywordlistcontentList = cfkeywordlistcontentService.findByKeywordlistref(keywordlistItem.getId());
for (CfKeywordlistcontent keywordlistcontent : keywordlistcontentList) {
keywordList.add(cfkeywordService.findById(keywordlistcontent.getCfKeywordlistcontentPK().getKeywordref()));
}
RestKeywordListOutput keywordlistoutput = new RestKeywordListOutput();
keywordlistoutput.setKeywordlist(keywordlistItem);
keywordlistoutput.setKeywords(keywordList);
keywordlistoutputList.add(keywordlistoutput);
}
Gson gson = new Gson();
String json = gson.toJson(keywordlistoutputList);
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 keyword lists");
} catch (IOException ex1) {
LOGGER.error(ex1.getMessage());
}
} catch (IOException ex) {
LOGGER.error(ex.getMessage());
}
}
Aggregations