package gov.nih.nci.calab.service.util;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

public class ActionUtil
{
    public void writeBinaryStream(File file, HttpServletResponse response) throws Exception
    {
        if (file == null || response == null)
        {
            throw new Exception("Unable to write file to HttpServletResponse: " +
                "Either pathName or response is null.");
        }
        try
        {
            // set a non-standard content type to force brower to open Save As dialog
            response.setContentType("application/octet-stream");
            response.setHeader("Content-Disposition", "attachment; filename=" + file.getName());
            ServletOutputStream sos = response.getOutputStream();
            BufferedInputStream bis =  new BufferedInputStream(new FileInputStream(file));
            byte[] buf = new byte[1024 * 10]; // ~= 10KB
            for (int len = -1; (len = bis.read(buf)) != -1; )
            {
                sos.write(buf, 0, len);
            }
            response.setContentLength((int)file.length());
            sos.flush(); // let the Servlet container handle closing of this ServletOutputStream
            bis.close();
        }
        catch (Exception e)
        {
            throw new Exception("Unable to write file to client, exception is " + e.toString());
        }

    }
}

@@ -127,124 +127,660 @@
 am;%0A
-%0Aimport javax.servlet.ServletOutputStream;%0Aimport javax.servlet.http.HttpServletResponse;%0A%0Apublic class ActionUtil%0A%7B
+import java.io.IOException;%0Aimport java.util.Enumeration;%0A%0Aimport javax.servlet.http.HttpServletRequest;%0Aimport javax.servlet.ServletOutputStream;%0Aimport javax.servlet.http.HttpServletResponse;%0Aimport javax.servlet.ServletException;%0A%0Apublic class ActionUtil%0A%7B%0A%09public static String getHostUriString (HttpServletRequest request) %0A%09%09%09throws ServletException, IOException%0A%09%7B%0A%09%09String scheme = request.getScheme();%0A%09%09String host = request.getServerName();%0A%09%09int port = request.getServerPort();%0A%0A%09%09String url = %22%22;%0A%09%09if(host.equals(%22localhost%22))%0A%09%09%09url = scheme + %22://%22 + host + %22:%22 + port;%0A%09%09%0A%09%09else%0A%09%09%09url = scheme + %22://%22 + host;%0A%09%09%0A%09%09return url;%0A%09%09%0A%09%7D%0A%09
 %0A   

add a method to get service url