Zhiqim Console(管理控制台)是知启蒙框架中最简洁的管理控制台组件,没有数据库,只保留一个账号,非常适用于后端程序嵌入WEB控制台模式,包括首页、登录、主界面、左侧菜单和欢迎页功能,依赖该组件实现基本的账号验证,通过覆盖原则增加自有功能。

森中灵 最后提交于5月前 修改版本号
AvatarAction.java3KB
/*
 * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。[遇见知启蒙,邂逅框架梦,本文采用木兰宽松许可证第2版]
 * 
 * https://zhiqim.org/project/zhiqim_components/zhiqim_console.htm
 *
 * Zhiqim Console is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
package org.zhiqim.manager.action;

import java.util.List;

import org.zhiqim.httpd.HttpRequest;
import org.zhiqim.httpd.HttpResponse;
import org.zhiqim.httpd.context.annotation.AnLogNot;
import org.zhiqim.httpd.context.core.Action;
import org.zhiqim.kernel.util.DateTimes;
import org.zhiqim.kernel.util.Ints;
import org.zhiqim.kernel.util.Longs;
import org.zhiqim.kernel.util.Validates;
import org.zhiqim.manager.ZmrBootstrap;
import org.zhiqim.manager.ZmrConstants;
import org.zhiqim.manager.dbo.ZmrAvatar;

/**
 * 操作员/系统头像管理
 */
public class AvatarAction implements Action, ZmrConstants
{
    @AnLogNot
    public void execute(HttpRequest request) throws Exception
    {
        HttpResponse response = request.getResponse();
        
        //1.判断参数
        List<String> paramList = request.getParameterMatch();
        if (paramList == null || paramList.size() != 2 || 
            !Validates.isIntegerNonnegative(paramList.get(0)) || !Validates.isIntegerPositive(paramList.get(1)))
        {//匹配参数失败
            response.sendError(_400_BAD_REQUEST_);
            return;
        }
        
        int avatarSize = Ints.toInt(paramList.get(1));
        if (avatarSize != 50 && avatarSize != 100 && avatarSize != 150)
        {//不是50/100/150
            response.sendError(_400_BAD_REQUEST_);
            return;
        }
        
        long avatarId = Longs.toLong(paramList.get(0));
        if (avatarId == 0)
        {//没有头像
            response.sendRedirect(request.getRootPath(_PATH_SERVICE_RES_AVATAR_));
            return;
        }
        
        //2.查出记录
        ZmrAvatar avatar = ZmrBootstrap.getAvatar(avatarId);
        if (avatar == null)
        {
            response.sendError(_404_NOT_FOUND_);
            return;
        }
        
        //3.清除缓存设置
        response.removeHeader(_PRAGMA_);
        response.removeHeader(_P3P_);
        response.removeHeader(_CACHE_CONTROL_);
        
        //4.判断是否未修改
        long lastModified = avatar.getAvatarTime();
        String lastModifiedTime = DateTimes.getDateTimeHttp(lastModified);
        String ifModifiedSince = request.getHeader(_IF_MODIFIED_SINCE_);
        if (ifModifiedSince != null && ifModifiedSince.indexOf(lastModifiedTime) != -1)
        {//时间相等
            response.setHeader(_EXPIRES_, DateTimes.getDateTimeHttpNextHour());
            response.sendError(_304_NOT_MODIFIED_);
            return;
        }
        
        //5.提交修改
        response.setContentType("image/png");
        response.setHeader(_ACCEPT_RANGES_, _BYTES_);
        response.setHeader(_LAST_MODIFIED_, lastModifiedTime);
        response.setHeader(_EXPIRES_, DateTimes.getDateTimeHttpNextHour());
        
        response.write(avatar.getAvatarData());
        response.commit();
    }
}