jp.co.opentone.bsol.linkbinder.view.logo.ProjectLogoManager.java Source code

Java tutorial

Introduction

Here is the source code for jp.co.opentone.bsol.linkbinder.view.logo.ProjectLogoManager.java

Source

/*
 * Copyright 2016 OPEN TONE Inc.
 *
 * 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 jp.co.opentone.bsol.linkbinder.view.logo;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Serializable;
import java.util.Date;

import org.apache.commons.httpclient.util.DateParseException;
import org.apache.commons.httpclient.util.DateUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import jp.co.opentone.bsol.framework.core.config.SystemConfig;
import jp.co.opentone.bsol.linkbinder.Constants;

/**
 * ?.
 * @author opentone
 */
public class ProjectLogoManager implements Serializable {

    /**
     * serialVersionUID.
     */
    private static final long serialVersionUID = 2715763431053922629L;

    /**
     * logger.
     */
    private static Logger log = LoggerFactory.getLogger(ProjectLogoManager.class);

    /**
     *  .
     */
    private static final String DIR_SEPARETOR = "/";

    /**
     * .
     */
    public ProjectLogoManager() {
    }

    /**
     * ??projectId??.
     *
     * @param projectId ID
     * @return (??)
     */
    public ProjectLogo get(String projectId) {
        String logoFile = detectLogoFile(projectId);
        if (log.isDebugEnabled()) {
            log.debug("logoFile[" + logoFile + "]");
        }

        return getLogoData(logoFile);
    }

    /**
     * ???????.
     *
     * @param projectId ID
     * @param ifModifiedSince If-Modified-Since
     * @return true:?? false:?????
     */
    public boolean isModified(String projectId, String ifModifiedSince) {
        if (log.isDebugEnabled()) {
            log.debug("projectId[" + projectId + "]");
            log.debug("ifModifiedSince[" + ifModifiedSince + "]");
        }

        if (StringUtils.isEmpty(ifModifiedSince)) {
            // If-Modified-Since()????true
            return true;
        }

        long ifModifiedSinceLongValue = 0;
        try {
            Date ifModifiedSinceDateValue = DateUtil.parseDate(ifModifiedSince);
            ifModifiedSinceLongValue = ifModifiedSinceDateValue.getTime();
            if (log.isDebugEnabled()) {
                log.debug("ifModifiedSinceDateValue[" + ifModifiedSinceDateValue + "]");
                log.debug("ifModifiedSinceLongValue[" + ifModifiedSinceLongValue + "]");
            }
        } catch (DateParseException e) {
            // ?????????
            log.warn("If-Modified-Since parse error[" + ifModifiedSince + "]", e);
            return true;
        }

        String logoFile = detectLogoFile(projectId);
        File f = new File(logoFile);
        if (log.isDebugEnabled()) {
            log.debug("f.exists[" + f.exists() + "]");
            log.debug("f.lastModified[" + f.lastModified() + "]");
        }

        // ???????1/1000?(12:34:56.789?12:34:56.000??????)
        //CHECKSTYLE:OFF
        if (!f.exists() || f.lastModified() / 1000 != ifModifiedSinceLongValue / 1000) {
            return true;
        }
        //CHECKSTYLE:ON

        if (log.isDebugEnabled()) {
            log.debug("return false");
        }
        return false;
    }

    /**
     * ??projectId?.
     *
     * @param projectId ID
     * @return (??)
     */
    private String detectLogoFile(String projectId) {
        String projectLogoDir = SystemConfig.getValue(Constants.KEY_PROJECT_LOGO_DIR);
        String projectLogoExtension = SystemConfig.getValue(Constants.KEY_PROJECT_LOGO_EXTENSION);
        String projectLogoDefault = SystemConfig.getValue(Constants.KEY_PROJECT_LOGO_DEFAULT);
        if (log.isDebugEnabled()) {
            log.debug("projectId[" + projectId + "]");
            log.debug("projectLogoDir[" + projectLogoDir + "]");
            log.debug("projectLogoExtension[" + projectLogoExtension + "]");
            log.debug("projectLogoDefault[" + projectLogoDefault + "]");
        }

        String result;
        if (StringUtils.isEmpty(projectId)) {
            // projectId??????Logo
            result = projectLogoDir + DIR_SEPARETOR + projectLogoDefault;
        } else {
            // projectId?????${projectId}.png
            result = projectLogoDir + DIR_SEPARETOR + projectId + projectLogoExtension;

            // ????${projectId}.png????????Logo?
            File f = new File(result);
            if (!f.exists()) {
                if (log.isDebugEnabled()) {
                    log.debug("File not found[" + result + "]");
                }
                result = projectLogoDir + DIR_SEPARETOR + projectLogoDefault;
            }
        }
        if (log.isDebugEnabled()) {
            log.debug("result[" + result + "]");
        }
        return result;
    }

    /**
     * ??(??)??.
     *
     * @param logoFile ??
     * @return (??)
     */
    private ProjectLogo getLogoData(String logoFile) {
        ProjectLogo result = null;
        BufferedInputStream bis = null;

        try {
            File f = new File(logoFile);
            long lastModifiled = f.lastModified();
            byte[] imageData = new byte[(int) f.length()];
            bis = new BufferedInputStream(new FileInputStream(logoFile));
            bis.read(imageData);

            result = new ProjectLogo();
            result.setImage(imageData);
            result.setLastModified(lastModifiled);
        } catch (FileNotFoundException e) {
            log.warn("????? [" + logoFile + "]");
        } catch (IOException e) {
            log.error("File I/O error[" + logoFile + "]", e);
        } finally {
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    log.error("File close error.[" + logoFile + "]", e);
                }
            }
        }
        return result;
    }
}