Mercurial > hg > human-echolocation-java-webapp
view src/HumanEchoServlet.java @ 28:52adafab20c1
The Servlet now sends the audio file to the browser;
author | luisf <luis.figueira@eecs.qmul.ac.uk> |
---|---|
date | Tue, 10 Dec 2013 12:02:46 +0000 |
parents | 1d1122584af9 |
children | 0c66cff0d1cc |
line wrap: on
line source
/* * HumanEchoServlet * * Version information * * 3 December 2013 * * Copyright notice */ import java.util.Properties; import java.io.IOException; import java.io.FileInputStream; import java.io.BufferedInputStream; import java.io.File; import org.apache.log4j.Logger; import javax.servlet.*; import javax.servlet.http.*; import com.mathworks.toolbox.javabuilder.MWJavaObjectRef; import com.mathworks.toolbox.javabuilder.MWNumericArray; import com.mathworks.toolbox.javabuilder.MWStructArray; import com.mathworks.toolbox.javabuilder.MWException; import com.mathworks.toolbox.javabuilder.internal.MCRConfiguration; import uk.ac.soton.isvr.*; public class HumanEchoServlet extends HttpServlet { private HumanEcho echo; private static Logger logger = Logger.getLogger(HumanEchoServlet.class); private String tempdir = ""; private String wavdir = ""; public void init(ServletConfig config) throws ServletException { super.init(config); // reading properties file try { Properties properties = new Properties(); properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("myapp.properties")); //get the property value and print it out tempdir = properties.getProperty("tmpdir"); wavdir = properties.getProperty("outdir"); } catch (IOException e) { e.printStackTrace(); } try { echo = new HumanEcho(); } catch(MWException e) { e.printStackTrace(); } } public void destroy() { super.destroy(); if(echo!=null) { echo.dispose(); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // todo: validate/normalise distance int dist = Integer.parseInt(request.getParameter("dist")); MWStructArray Input = null; HumanEcho echo; String outputfname = new String(); try { echo = new HumanEcho(); logger.error("We are logging!"); logger.warn(System.getProperty("catalina.base")); // Matlab structure: // Input = struct('dist', 0.9, 'azim', 0, 'orient', 'horz', 'dirweight', 0.2, 'outputfname', 'foo.wav') String[] InputStructFields = {"dist", "azim", "orient", "dirweight", "outputfname"}; Input = new MWStructArray(1, 1, InputStructFields); Input.set("dist", 1, Double.valueOf(dist)); Input.set("azim", 1, Double.valueOf(0)); Input.set("orient", 1, "horz"); Input.set("dirweight", 1, Double.valueOf(0.2)); // the ofname should depend on the parameters StringBuilder sb = new StringBuilder(); sb.append("e_d"); sb.append(dist); sb.append(".wav"); outputfname = sb.toString(); Input.set("outputfname", 1, outputfname); // todo: before calling should test if wav already exists.. Object[] result = echo.simulateBinauralSignals(Input); } catch(MWException e) { e.printStackTrace(); } // Creating the stream ServletOutputStream stream = null; BufferedInputStream buf = null; try{ stream = response.getOutputStream(); File wavfile = new File(outputfname); //set response headers response.setContentType("audio/wav"); response.addHeader("Content-Disposition","attachment; filename=" + outputfname ); response.setContentLength( (int) wavfile.length( ) ); FileInputStream input = new FileInputStream(wavfile); buf = new BufferedInputStream(input); int readBytes = 0; //read from the file; write to the ServletOutputStream while((readBytes = buf.read( )) != -1) stream.write(readBytes); } catch (IOException ioe){ throw new ServletException(ioe.getMessage( )); } finally { //close the input/output streams if(stream != null) stream.close( ); if(buf != null) buf.close( ); } System.out.println("MCRROOT: " + System.getenv("MCRROOT")); System.out.println("PATH: " + System.getenv("PATH")); System.out.println(MCRConfiguration.isInstalledMCR()); logger.warn("tmpdir " + tempdir); } }