/*
 * Copyright
 *   2008 axYus - www.axyus.com
 *   2008 C.Bosquet - charles.bosquet@axyus.com
 *
 * This file is part of XEMELIOS.
 *
 * XEMELIOS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * XEMELIOS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with XEMELIOS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package fr.gouv.finances.dgfip.xemelios.web.utils;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DownloadAction;

/**
 * @author CBO
 * Ajoute la gestion des autorisations et du 404 par rapport � l'impl�mentation standard de Struts
 */
public abstract class StdDownloadAction extends DownloadAction {

  @Override
  public final ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
    if (isRequestAuthorized(request)) {
      StreamInfo info = getStreamInfo(mapping, form, request, response);
      if (info != null) {
        InputStream stream = info.getInputStream();
        try {
          response.setContentType(info.getContentType());
          copy(stream, response.getOutputStream());
        } finally {
          if (stream != null) {
            stream.close();
          }
        }
      } else {
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
      }
    } else {
      response.sendError(HttpServletResponse.SC_FORBIDDEN);
    }
    return null;
  }

  protected boolean isRequestAuthorized(HttpServletRequest request) throws SQLException {
    return true;
  }

  protected String getMessage(HttpServletRequest request, String key) {
    return getResources(request).getMessage(getLocale(request), key);
  }

  protected String getMessage(HttpServletRequest request, String key, Object arg) {
    return getResources(request).getMessage(getLocale(request), key, arg);
  }

  protected String getMessage(HttpServletRequest request, String key, Object arg1, Object arg2) {
    return getResources(request).getMessage(getLocale(request), key, arg1, arg1);
  }

  protected String getMessage(HttpServletRequest request, String key, Object args[]) {
    return getResources(request).getMessage(getLocale(request), key, args);
  }

  static public FileStreamInfo createFileStreamInfo(ServletContext servletContext, File file) {
    String mimeType = servletContext.getMimeType(file.getName());
    if (mimeType == null) {
      mimeType = "application/octet-stream";
    }
    return new FileStreamInfo(mimeType, file);
  }

  static public class ByteArrayStreamInfo implements StreamInfo {

    protected String contentType;
    protected byte[] bytes;

    public ByteArrayStreamInfo(String contentType, byte[] bytes) {
      this.contentType = contentType;
      this.bytes = bytes;
    }

    public String getContentType() {
      return contentType;
    }

    public InputStream getInputStream() throws IOException {
      return new ByteArrayInputStream(bytes);
    }
  }
}
