Package cn.aprilsoft.TinyAppServer.processor.impl

Source Code of cn.aprilsoft.TinyAppServer.processor.impl.CommonProcessor

package cn.aprilsoft.TinyAppServer.processor.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.SocketException;

import org.apache.log4j.Logger;

import cn.aprilsoft.TinyAppServer.configuare.Configuare;
import cn.aprilsoft.TinyAppServer.error.HttpErrorMessage;
import cn.aprilsoft.TinyAppServer.exception.ClientAbortException;
import cn.aprilsoft.TinyAppServer.exception.HttpException;
import cn.aprilsoft.TinyAppServer.processor.Processor;
import cn.aprilsoft.TinyAppServer.reqNrep.Request;
import cn.aprilsoft.TinyAppServer.reqNrep.Response;
import cn.aprilsoft.io.FileUtil;
import cn.aprilsoft.io.IOUtil;
import cn.aprilsoft.mime.ContentType;

public class CommonProcessor extends Processor {

    private static final Logger log = Logger.getLogger(CommonProcessor.class);

    public CommonProcessor() {
    }

    @Override
    protected void doGet(Request request, Response response) {
        doPost(request, response);
    }

    @Override
    protected void doPost(Request request, Response response) {
        String url = request.getUrl();

        String path = FileUtil.combilePath(Configuare.BASE_PATH, url);

        File file = new File(path);

        if (file.exists() && file.isFile()) {
            if ((!file.canRead()) || file.isHidden()) {
                log.warn("File '" + file.getName() + "' access denied.");
                HttpErrorMessage.error403(response);
            } else {
                try {
                    outputFileToResponse(response, file);
                } catch (SocketException e) {
                    throw new ClientAbortException(e);
                } catch (IOException e) {
                    throw new HttpException(e);
                }
            }
        } else {
            log.warn("File '" + file.getName() + "' not exist.");
            HttpErrorMessage.error404(response);
        }
    }

    private void outputFileToResponse(Response response, File file) throws IOException {
        String fileName = file.getName();
        int lastDot = fileName.lastIndexOf('.');
        String ext = null;
        if (lastDot >= 0) {
            ext = fileName.substring(lastDot + 1);
        }
        response.setContentType(ContentType.getContentTypeByExt(ext));

        FileInputStream fis = new FileInputStream(file);
        response.setContentSize(fis.available());
        OutputStream os = response.getOutputStream();
        try {
            IOUtil.copy(fis, os);
        } finally {
            fis.close();
        }
        os.flush();
    }
}
TOP

Related Classes of cn.aprilsoft.TinyAppServer.processor.impl.CommonProcessor

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.