com.sccl.attech.common.web.BaseController.java Source code

Java tutorial

Introduction

Here is the source code for com.sccl.attech.common.web.BaseController.java

Source

/**
 * Copyright &copy; 2012-2013 <a href="https://github.com/sccl/attech">attech</a> All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.sccl.attech.common.web;

import java.beans.PropertyEditorSupport;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;
import javax.validation.Validator;

import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringEscapeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.sccl.attech.common.beanvalidator.BeanValidators;
import com.sccl.attech.common.mapper.JsonMapper;
import com.sccl.attech.common.utils.DateUtils;
import com.sccl.attech.common.utils.Md5Util;
import com.sccl.attech.common.utils.StringUtils;

/**
 * ?
 * 
 * @author sccl
 * @version 2013-3-23
 */
public abstract class BaseController {

    /**
     * 
     */
    protected Logger logger = LoggerFactory.getLogger(getClass());

    protected static String PHONE_REST_SALT = "p112er4323e";// assistant

    public static final String PAGE_CHARSET = "text/html; charset=UTF-8";// ??
    public static final String APP_KEY = "web";// appKey
    public static final String SUCCESS = "success";// ?
    public static final String ERROR = "error";// 

    public static final String RETURN_STATUS = "status";// json?
    public static final String RETURN_MESSAGE = "message";// json?

    /**
     * ?Bean
     */
    @Autowired
    protected Validator validator;

    /**
     * ???
     * 
     * @param object
     *            ?
     * @param groups
     *            ?
     * @return ??true?? message 
     */
    protected boolean beanValidator(Model model, Object object, Class<?>... groups) {
        try {
            BeanValidators.validateWithException(validator, object, groups);
        } catch (ConstraintViolationException ex) {
            List<String> list = BeanValidators.extractPropertyAndMessageAsList(ex, ": ");
            list.add(0, "??");
            addMessage(model, list.toArray(new String[] {}));
            return false;
        }
        return true;
    }

    /**
     * ???
     * 
     * @param object
     *            ?
     * @param groups
     *            ?
     * @return ??true?? flash message 
     */
    protected boolean beanValidator(RedirectAttributes redirectAttributes, Object object, Class<?>... groups) {
        try {
            BeanValidators.validateWithException(validator, object, groups);
        } catch (ConstraintViolationException ex) {
            List<String> list = BeanValidators.extractPropertyAndMessageAsList(ex, ": ");
            list.add(0, "??");
            addMessage(redirectAttributes, list.toArray(new String[] {}));
            return false;
        }
        return true;
    }

    /**
     * Model?
     * 
     * @param messages
     *            ?
     */
    protected void addMessage(Model model, String... messages) {
        StringBuilder sb = new StringBuilder();
        for (String message : messages) {
            sb.append(message).append(messages.length > 1 ? "<br/>" : "");
        }
        model.addAttribute("message", sb.toString());
    }

    /**
     * Flash?
     * 
     * @param messages
     *            ?
     */
    protected void addMessage(RedirectAttributes redirectAttributes, String... messages) {
        StringBuilder sb = new StringBuilder();
        for (String message : messages) {
            sb.append(message).append(messages.length > 1 ? "<br/>" : "");
        }
        redirectAttributes.addFlashAttribute("message", sb.toString());
    }

    /**
     * ?? 1. ?StringHTML?XSS 2. Date?String
     */
    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        // String??StringHTML?XSS
        binder.registerCustomEditor(String.class, new PropertyEditorSupport() {
            @Override
            public void setAsText(String text) {
                setValue(text == null ? null : StringEscapeUtils.escapeHtml4(text.trim()));
            }

            @Override
            public String getAsText() {
                Object value = getValue();
                return value != null ? value.toString() : "";
            }
        });
        // Date ?
        binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
            @Override
            public void setAsText(String text) {
                setValue(DateUtils.parseDate(text));
            }
        });
    }

    /**
     * ?response?
     * 
     * @param message
     * @throws IOException
     */
    protected void sendObjectToJson(Object object, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        PrintWriter pw = response.getWriter();
        String message = JsonMapper.getInstance().toJson(object);
        pw.write(message);
        pw.flush();
        pw.close();
    }

    protected String validREST(String key, String salt, String license) {
        if (!StringUtils.equals(Md5Util.md5Encoder(key + salt), license))
            return "0";
        return null;
    }

    // JSON??null
    public String actionSuccess(String message) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(RETURN_STATUS, SUCCESS);
        jsonObject.put(RETURN_MESSAGE, message);
        return jsonObject.toString();
    }

    // JSON?null
    public String actionError(String message) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(RETURN_STATUS, ERROR);
        jsonObject.put(RETURN_MESSAGE, message);
        return jsonObject.toString();
    }

    //?request?array
    public String getJsonString(String jsonString, String key) throws IOException {
        String retJson = "";
        HashMap<String, LinkedHashMap<String, Object>> jsonBean = null;
        if (StringUtils.isNotBlank(jsonString) && !("false").equals(jsonString)) {
            jsonBean = JsonMapper.getInstance().readValue(jsonString, HashMap.class);
        }
        try {
            retJson = String.valueOf(jsonBean.get(key));
        } catch (Exception e) {
        }
        return retJson;
    }

}