package com.asascience.ncsos.controller;

import com.asascience.ncsos.outputformatter.OutputFormatter;
import com.asascience.ncsos.service.Parser;
import com.asascience.ncsos.util.DatasetHandlerAdapter;
import org.apache.log4j.BasicConfigurator;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import ucar.nc2.dataset.NetcdfDataset;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;

@Controller
@RequestMapping("/sos")
public class SosController implements ISosContoller {

    private static org.slf4j.Logger _log = org.slf4j.LoggerFactory.getLogger(SosController.class);
    private static org.slf4j.Logger _logServerStartup = org.slf4j.LoggerFactory.getLogger("serverStartup");
    
    private HashMap<String, Object> respMap;

    protected String getPath() {
        return "Sos/";
    }

    public void init() throws ServletException {
        BasicConfigurator.configure();
        _logServerStartup.info("SOS Service - initialization start");
    }

    public void destroy() {
        NetcdfDataset.shutdown();
        _logServerStartup.info("SOS Service - destroy done");
    }

    /** 
     * Generate SOS for the underlying NetcdfDataset
     * 
     * @param request incoming url request 
     * @param response outgoing web based response
     * @throws ServletException if ServletException occurred
     * @throws IOException if IOException occurred 
     */
    @RequestMapping(params = {})
    @Override
    public void handleSOSRequest(final HttpServletRequest req, final HttpServletResponse res) throws ServletException, IOException {

        NetcdfDataset dataset = null;
        
        respMap = new HashMap<String, Object>();

        try {
            //see http://tomcat.apache.org/tomcat-5.5-doc/config/context.html ----- workdir    
            String tempdir = System.getProperty("java.io.tmpdir");
         
            dataset = DatasetHandlerAdapter.openDataset(req, res);
         
            Parser md = new Parser();
            respMap = md.enhanceGETRequest(dataset, req.getQueryString(), req.getRequestURL().toString(),tempdir);            
            
            Writer writer = res.getWriter();
            OutputFormatter output = (OutputFormatter)respMap.get("outputFormatter");
            res.setContentType(output.getContentType().toString());            
            output.writeOutput(writer);            
            writer.flush();
            writer.close();

        } catch (Exception e) {
            _log.error("Something went wrong", e);
            //close the dataset remove memory hang
        } finally {  
            // This is a workaround for a bug in thredds. On the second request 
            // for a ncml object the request will fail due to an error
            // with the cached object. In order to get around this, the
            // cache for the ncml files must be cleared.
            String location = dataset.getReferencedFile().getLocation().toLowerCase();
            if(location.endsWith("xml") ||
                    location.endsWith("ncml") ||
                    location.endsWith("nc")) {
                dataset.getReferencedFile().setFileCache(null);
            }
            DatasetHandlerAdapter.closeDataset(dataset);
            
        }

    }
}

@@ -2128,25 +2128,16 @@
 , res);%0A
-         
 %0A       
@@ -3163,16 +3163,116 @@
         
+try%7B%0A            %09NetcdfDataset dset =  ((NetcdfDataset)dataset.getReferencedFile());%0A%0A            %09
 if(locat
@@ -3302,32 +3302,27 @@
             
-     
-   
+%09%09%09
 location.end
@@ -3354,39 +3354,40 @@
     
-        location.endsWith(%22nc%22)
+%09%09%09dset.getAggregation() != null
 ) %7B%0A
@@ -3394,28 +3394,26 @@
             
-    
+%09%09
 dataset.getR
@@ -3460,16 +3460,64 @@
         
+%09%7D%0A            %7D%0A            catch(Exception e)%7B
 %7D%0A      
@@ -3572,32 +3572,58 @@
 );%0A            %0A
+            %0A            %0A
         %7D%0A%0A    %7D

Check to see if the original dataset is an aggregation and if it is clear the cache.