List of usage examples for org.springframework.web.bind ServletRequestUtils getRequiredBooleanParameter
public static boolean getRequiredBooleanParameter(ServletRequest request, String name) throws ServletRequestBindingException
From source file:no.dusken.aranea.admin.control.VcardController.java
public ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws PageNotFoundException, ServletRequestBindingException { response.setContentType("text/x-vcard"); response.setCharacterEncoding("UTF-8"); Map<String, Object> map = new HashMap<String, Object>(); Set<Person> persons = new HashSet<Person>(); if (request.getParameter("username") != null) { Person person;/* w ww. ja va 2 s.c o m*/ String username = ServletRequestUtils.getRequiredStringParameter(request, "username"); person = personService.getPersonByUsername(username); if (person == null) { //response.sendError(HttpServletResponse.SC_NOT_FOUND, // "No such person: " + username); //Throwing exception so this can be threated as a 404 error on a higher level throw new PageNotFoundException("Person with username: " + username + " not found."); } persons.add(person); response.setHeader("Content-Disposition", "inline; filename=" + person.getUsername() + ".vcf"); } else if (request.getParameter("active") != null) { boolean active = ServletRequestUtils.getRequiredBooleanParameter(request, "active"); response.setHeader("Content-Disposition", "inline; filename=contacts.vcf"); persons.addAll(personService.getPersonsByActive(active)); } else if (request.getParameter("role") != null) { String r = ServletRequestUtils.getRequiredStringParameter(request, "role"); Role role = roleService.getRolesByName(r); response.setHeader("Content-Disposition", "inline; filename=" + r + "_contacts.vcf"); // only get active members having that role persons.addAll(personService.getPersons(role, true)); } else { throw new PageNotFoundException("Missing parameter"); } map.put("persons", persons); // add images <username, base64image> Map<String, String> images = new HashMap<String, String>(); for (Person p : persons) { if (p.getPortrait() != null) { try { String imageString = imageUtils.getBase64Image(p.getPortrait(), imageWidth, imageHeight); // have to add spaces, vcard demands intendation imageString = imageString.replaceAll("\n", "\n "); images.put(p.getUsername(), imageString); } catch (Exception e) { // ignore, no image added } } } map.put("images", images); map.put("formatter", new SimpleDateFormat("yyyy-MM-dd")); return new ModelAndView("no/dusken/aranea/base/admin/person/vcard", map); }