//
// return content of xml 
// (used for VOSI capabilityVOSI and availability.xml)
//

import java.io.IOException;
import java.io.PrintWriter;
import java.io.File;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.*; // ArrayList<String>

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletOutputStream; // for SOOA

// from vlkb_mergefiles.java - dir & file handling
import java.io.*;

import java.nio.file.*;
import static java.nio.file.StandardCopyOption.*;


// serve VOSI resources from xml files (for now implemented as strings, not files FIXME)

public class VlkbServletFile
    extends javax.servlet.http.HttpServlet
{
    // for logs and debug
    String className = this.getClass().getSimpleName();

// VOSI
// String accessURL = null; // FIXME now read from MERGEURL later introduce own param
// String funcName = "vlkb_cutout"; // FIXME read from config file

	private static final String availStr = 
		  "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
		+ "<vosi:availability xmlns:vosi=\"http://www.ivoa.net/xml/VOSIAvailability/v1.0\">"
		+ " <vosi:available>true</vosi:available>"
		+ " <vosi:note> VLKB-SODA " + Version.asString + " is accepting queries </vosi:note>"
		+ "</vosi:availability>";

	private String capsStr = null;


	protected void SetCapsStr(String URL, String funcName)
	{
		if(URL != null)
		{
		
            String accessURL = stripTrailingSlash(URL);

    capsStr =
		  "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"

		+ "<vosi:capabilities "
		+    "xmlns:vosi=\"http://www.ivoa.net/xml/VOSICapabilities/v1.0\" "
		+    "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
		+    "xmlns:vod=\"http://www.ivoa.net/xml/VODataService/v1.1\">"

		+ " <capability standardID=\"ivo://ivoa.net/std/VOSI#capabilities\">"
		+ "   <interface xsi:type=\"vod:ParamHTTP\" version=\"1.0\">"
		+ "     <accessURL use=\"full\">"
		+          accessURL + "/capabilities"
		+ "     </accessURL>"
		+ "   </interface>"
		+ " </capability>"

		+ " <capability standardID=\"ivo://ivoa.net/std/VOSI#availability\">"
		+ "   <interface xsi:type=\"vod:ParamHTTP\" version=\"1.0\">"
		+ "     <accessURL use=\"full\">"
		+          accessURL + "/availability"
		+ "     </accessURL>"
		+ "   </interface>"
		+ " </capability>"

		+ " <capability standardID=\"ivo://ivoa.net/std/SODA#sync-1.0\">"
		+ "   <interface xsi:type=\"vod:ParamHTTP\" role=\"std\" version=\"1.0\">"
		+ "     <accessURL use=\"full\">"
		+          accessURL + "/" + funcName
		+ "     </accessURL>"
		+ "   </interface>"
		+ " </capability>"

		+ " <capability standardID=\"ivo://ivoa.net/std/SODA#async-1.0\">"
		+ "   <interface xsi:type=\"vod:ParamHTTP\" role=\"std\" version=\"1.0\">"
		+ "     <accessURL use=\"full\">"
		+          accessURL + "/" + funcName + "_uws/soda_cuts"
		+ "     </accessURL>"
		+ "   </interface>"
		+ " </capability>"

	+ "</vosi:capabilities>";
		}
	}


	String stripTrailingSlash(String path)
	{
       		if (path.endsWith("/"))
           		return path.substring(0,path.length()-1);
       		else
           		return path;
    	}


    protected void doGet(HttpServletRequest request,
                         HttpServletResponse response)
        throws ServletException, IOException {

            doPost(request, response);
        }



    protected void doPost(HttpServletRequest request,
                          HttpServletResponse response)
        throws ServletException, IOException
	{
		StringBuffer requestURL = request.getRequestURL();
	
   	System.out.println(className + " vlkb req from: " + request.getRemoteAddr()
                               + " doGet: " + requestURL.toString());

		PrintWriter writer = response.getWriter();
		response.setContentType("text/xml");
	
		if(-1 != requestURL.lastIndexOf("/capabilities"))
		{
            String fullURL = request.getRequestURL().toString();
            String baseURL = fullURL.substring(0,requestURL.lastIndexOf("/"));

            SetCapsStr(baseURL, "soda");
			writer.println(capsStr);	

		}
		else if(-1 != requestURL.lastIndexOf("/availability"))
		{
			writer.println(availStr);	
		}
		// error FIXME what to do if none of above given ? e.g. misconfigured web.xml
		
		writer.close();
		return;
        }
}

