no.dusken.aranea.admin.control.VcardController.java Source code

Java tutorial

Introduction

Here is the source code for no.dusken.aranea.admin.control.VcardController.java

Source

/*
 Copyright 2006 - 2010 Under Dusken
    
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at
    
 http://www.apache.org/licenses/LICENSE-2.0
    
 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

package no.dusken.aranea.admin.control;

import no.dusken.aranea.model.Person;
import no.dusken.aranea.model.Role;
import no.dusken.aranea.service.PersonService;
import no.dusken.aranea.service.RoleService;
import no.dusken.aranea.util.ImageUtils;
import no.dusken.common.exception.PageNotFoundException;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class VcardController extends AbstractController {

    private PersonService personService;

    private ImageUtils imageUtils;

    private RoleService roleService;

    private int imageWidth = 200;

    private int imageHeight = 200;

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

    @Required
    public void setPersonService(PersonService personService) {
        this.personService = personService;
    }

    public void setImageWidth(int imageWidth) {
        this.imageWidth = imageWidth;
    }

    public void setImageHeight(int imageHeight) {
        this.imageHeight = imageHeight;
    }

    @Required
    public void setImageUtils(ImageUtils imageUtils) {
        this.imageUtils = imageUtils;
    }

    @Required
    public void setRoleService(RoleService roleService) {
        this.roleService = roleService;
    }
}