com.yunmel.syncretic.core.BaseController.java Source code

Java tutorial

Introduction

Here is the source code for com.yunmel.syncretic.core.BaseController.java

Source

/*
 * Copyright (c) 2016 yunmle.com(?).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.yunmel.syncretic.core;

import java.beans.PropertyEditorSupport;
import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.yunmel.syncretic.bean.Result;
import com.yunmel.syncretic.exception.BaseException;
import com.yunmel.syncretic.utils.io.IOUtils;
import com.yunmel.syncretic.utils.jse.RandomUtils;

/**
 * 
 * @description  ?XSS
 *
 * @author xuyq - chainisit@126.com
 * @since 1.0 - 2016715
 */
public abstract class BaseController {
    private final static Logger LOG = LoggerFactory.getLogger(BaseController.class);

    @InitBinder
    public 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) {
                try {
                    setValue(DateUtils.parseDate(text, "yyyy-MM-dd HH:mm:ss"));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }

        });
        // Timestamp ?
        binder.registerCustomEditor(Timestamp.class, new PropertyEditorSupport() {
            @Override
            public void setAsText(String text) {
                Date date = null;
                try {
                    date = DateUtils.parseDate(text, "yyyy-MM-dd HH:mm:ss");
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                setValue(date == null ? null : new Timestamp(date.getTime()));
            }
        });

    }

    /**
     * ?
     * 
     * @param data
     * @return
     */
    protected ResponseEntity<Result> ok(Object data) {
        return ResponseEntity.ok(Result.build(0, "?", data));
    }

    /**
     * 
     * 
     * @param e
     * @return
     */
    protected ResponseEntity<Result> error(BaseException e) {
        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(Result.build(e.getCode(), e.getMessage()));
    }

    /**
     * 
     * 
     * @param e
     * @return
     */
    protected ResponseEntity<Result> refuse(Result result) {
        return ResponseEntity.status(HttpStatus.FORBIDDEN).body(result);
    }

    /**
     * ?
     * 
     * @param e
     * @return
     */
    protected ResponseEntity<Result> unauthorized(Result result) {
        return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(result);
    }

    protected void uploadFile(HttpServletRequest request, String field, File file) {
        try {
            if (request instanceof MultipartHttpServletRequest) {
                MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
                MultiValueMap<String, MultipartFile> map = multipartRequest.getMultiFileMap();
                List<MultipartFile> files = map.get(field);
                MultipartFile _file = files.get(0);
                IOUtils.copyInputStreamToFile(_file.getInputStream(), file);
            }
        } catch (IOException e) {
            LOG.error("upload file error.", e);
        }
    }

    protected File uploadFile(HttpServletRequest request, String field) {
        try {
            if (request instanceof MultipartHttpServletRequest) {
                MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
                MultiValueMap<String, MultipartFile> map = multipartRequest.getMultiFileMap();
                List<MultipartFile> files = map.get(field);
                MultipartFile _file = files.get(0);
                File file = new File(RandomUtils.genRandom32Hex());
                IOUtils.copyInputStreamToFile(_file.getInputStream(), file);
                return file;
            }
        } catch (IOException e) {
            LOG.error("upload file error.", e);
        }

        return null;
    }

    protected MultipartFile getMultipartFile(HttpServletRequest request, String field) {
        try {
            if (request instanceof MultipartHttpServletRequest) {
                MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
                MultiValueMap<String, MultipartFile> map = multipartRequest.getMultiFileMap();
                List<MultipartFile> files = map.get(field);
                return files.get(0);
            }
        } catch (Exception e) {
            LOG.error("upload file error.", e);
        }

        return null;
    }

    protected List<MultipartFile> getMultipartFiles(HttpServletRequest request, String field) {
        try {
            if (request instanceof MultipartHttpServletRequest) {
                MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
                MultiValueMap<String, MultipartFile> map = multipartRequest.getMultiFileMap();
                return map.get(field);
            }
        } catch (Exception e) {
            LOG.error("upload file error.", e);
        }

        return null;
    }
}