Example usage for org.springframework.web.bind ServletRequestUtils getRequiredBooleanParameter

List of usage examples for org.springframework.web.bind ServletRequestUtils getRequiredBooleanParameter

Introduction

In this page you can find the example usage for org.springframework.web.bind ServletRequestUtils getRequiredBooleanParameter.

Prototype

public static boolean getRequiredBooleanParameter(ServletRequest request, String name)
        throws ServletRequestBindingException 

Source Link

Document

Get a boolean parameter, throwing an exception if it isn't found or isn't a boolean.

Usage

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);
}