/////////////////////////////////////////////////////////////////////////////
// This file is part of the "OPeNDAP 4 Data Server (aka Hyrax)" project.
//
//
// Copyright (c) 2011 OPeNDAP, Inc.
// Author: Nathan David Potter  <ndp@opendap.org>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
/////////////////////////////////////////////////////////////////////////////
package opendap.bes.dapResponders;

import opendap.bes.Version;
import opendap.bes.BesDapResponder;
import opendap.coreServlet.ReqInfo;
import opendap.dap.User;
import org.jdom.Document;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import org.slf4j.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * Created by IntelliJ IDEA.
 * User: ndp
 * Date: 1/31/11
 * Time: 4:42 PM
 * To change this template use File | Settings | File Templates.
 */
public class GeoTiff extends BesDapResponder {
    private Logger log;




    private static String defaultRequestSuffixRegex = "\\.geotiff";

    public GeoTiff(String sysPath, BesApi besApi) {
        this(sysPath,null, defaultRequestSuffixRegex,besApi);
    }

    public GeoTiff(String sysPath, String pathPrefix, BesApi besApi) {
        this(sysPath,pathPrefix, defaultRequestSuffixRegex,besApi);
    }



    public GeoTiff(String sysPath, String pathPrefix,  String requestSuffixRegex, BesApi besApi) {
        super(sysPath, pathPrefix, requestSuffixRegex, besApi);
        log = org.slf4j.LoggerFactory.getLogger(this.getClass());
    }



    public void respondToHttpGetRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {




        String relativeUrl = ReqInfo.getLocalUrl(request);
        String dataSource = ReqInfo.getBesDataSourceID(relativeUrl);
        String constraintExpression = ReqInfo.getConstraintExpression(request);
        String xmlBase = getXmlBase(request);

        BesApi besApi = getBesApi();



        log.debug("respondToHttpGetRequest(): Sending XML Data response For: " + dataSource +
                    "    CE: '" + constraintExpression + "'");


        response.setContentType("text/xml");
        Version.setOpendapMimeHeaders(request,response,besApi);
        response.setHeader("Content-Description", "dap_xml");
        // Commented because of a bug in the OPeNDAP C++ stuff...
        //response.setHeader("Content-Encoding", "plain");

        response.setStatus(HttpServletResponse.SC_OK);
        String xdap_accept = request.getHeader("XDAP-Accept");


        User user = new User(request);


        OutputStream os = response.getOutputStream();
        ByteArrayOutputStream erros = new ByteArrayOutputStream();


        boolean result = besApi.writeGeoTiffDataResponse(
                        dataSource,
                        constraintExpression,
                        xdap_accept,
                        user.getMaxResponseSize(),
                        os,
                        erros);


        if(!result){
            String msg = new String(erros.toByteArray());
            log.error("respondToHttpGetRequest() encountered a BESError: "+msg);
            os.write(msg.getBytes());
        }



        os.flush();
        log.info("Sent XML Data response.");


    }
}

@@ -1270,16 +1270,50 @@
 eqInfo;%0A
+import opendap.coreServlet.Scrub;%0A
 import o
@@ -2948,45 +2948,340 @@
     
-response.setContentType(%22text/xml%22
+String downloadFileName = Scrub.fileName(relativeUrl.substring(relativeUrl.lastIndexOf(%22/%22) + 1, relativeUrl.length()));%0A%0A        String contentDisposition = %22 attachment; filename=%5C%22%22 +downloadFileName+%22%5C%22%22;%0A%0A        response.setContentType(%22image/geotiff%22);%0A        response.setHeader(%22Content-Disposition%22, contentDisposition
 );%0A
+%0A
     
@@ -3395,15 +3395,33 @@
 %22, %22
-dap_xml
+geotiff image of DSP data
 %22);%0A

Set content-type and content-disposition headers in GeoTiff