/*!
* Copyright 2002 - 2015 Webdetails, a Pentaho company.  All rights reserved.
*
* This software was developed by Webdetails and is provided under the terms
* of the Mozilla Public License, Version 2.0, or any later version. You may not use
* this file except in compliance with the license. If you need a copy of the license,
* please go to  http://mozilla.org/MPL/2.0/. The Initial Developer is Webdetails.
*
* Software distributed under the Mozilla Public License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or  implied. Please refer to
* the license for the specific language governing your rights and limitations.
*/

package pt.webdetails.cda.exporter;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import pt.webdetails.cpf.Util;
import pt.webdetails.cpf.utils.MimeTypes;

/**
 * Standard cda query result
 */
public abstract class ExportedQueryResult {

  private Exporter exporter;

  public ExportedQueryResult( Exporter exporter ) {
    assert exporter != null;
    this.exporter = exporter;
  }

  public Exporter getExporter() {
    return exporter;
  }

  public void writeHeaders( HttpServletResponse response ) throws IOException {
    setResponseHeaders( response, exporter.getMimeType(), exporter.getAttachmentName() );
  }

  public void writeResponse( HttpServletResponse response ) throws ExporterException, IOException {
    setResponseHeaders( response, exporter.getMimeType(), exporter.getAttachmentName() );
    OutputStream out = response.getOutputStream();
    writeOut( out );
  }

  public abstract void writeOut( OutputStream out ) throws ExporterException;

  public String asString() throws ExporterException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    writeOut( out );
    return Util.toString( out.toByteArray() );
  }

  private static void setResponseHeaders( HttpServletResponse response, String mimeType, String attachmentName ) {
    if ( StringUtils.isEmpty( mimeType ) && !StringUtils.isEmpty( attachmentName ) ) {
      mimeType = MimeTypes.getMimeType( attachmentName );
    }

    response.setContentType( mimeType );

    if ( attachmentName != null ) {
      response.setHeader( "content-disposition", "attachment; filename=" + attachmentName );
    }
  }
}

@@ -2388,24 +2388,848 @@
 mentName );%0A
+    %7D else %7B%0A      final String extension;%0A      switch ( mimeType ) %7B%0A        case MimeTypes.CSV:%0A          extension = %22.%22 + ExporterEngine.OutputType.CSV.toString();%0A          break;%0A        case MimeTypes.JSON:%0A          extension = %22.%22 + ExporterEngine.OutputType.JSON.toString();%0A          break;%0A        case MimeTypes.XLS:%0A          extension = %22.%22 + ExporterEngine.OutputType.XLS.toString();%0A          break;%0A        case MimeTypes.XML:%0A          extension = %22.%22 + ExporterEngine.OutputType.XML.toString();%0A          break;%0A        case MimeTypes.HTML:%0A          extension = %22.%22 + ExporterEngine.OutputType.HTML.toString();%0A          break;%0A        default: // e.g. BINARY%0A          extension = %22%22;%0A          break;%0A      %7D%0A      response.setHeader( %22content-disposition%22, %22inline; filename=doQuery%22 + extension );%0A
     %7D%0A  %7D%0A%7D%0A

Set content-disposition header field to allow MS Excel to import CDA queries using web queries