annotate 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
rev   line source
luis@16 1 /*
luis@28 2 * HumanEchoServlet
luis@16 3 *
luis@16 4 * Version information
luis@16 5 *
luis@16 6 * 3 December 2013
luis@16 7 *
luis@16 8 * Copyright notice
luis@16 9 */
luis@15 10
luis@15 11 import java.util.Properties;
luis@15 12 import java.io.IOException;
luis@15 13
luis@28 14 import java.io.FileInputStream;
luis@28 15 import java.io.BufferedInputStream;
luis@28 16 import java.io.File;
luis@28 17
luis@21 18 import org.apache.log4j.Logger;
luis@16 19
luis@28 20 import javax.servlet.*;
luis@28 21 import javax.servlet.http.*;
luis@0 22
luis@0 23 import com.mathworks.toolbox.javabuilder.MWJavaObjectRef;
luis@0 24 import com.mathworks.toolbox.javabuilder.MWNumericArray;
luis@11 25 import com.mathworks.toolbox.javabuilder.MWStructArray;
luis@0 26 import com.mathworks.toolbox.javabuilder.MWException;
luis@15 27 import com.mathworks.toolbox.javabuilder.internal.MCRConfiguration;
luis@15 28
luis@15 29 import uk.ac.soton.isvr.*;
luis@0 30
luis@0 31 public class HumanEchoServlet extends HttpServlet {
luis@0 32 private HumanEcho echo;
luis@28 33 private static Logger logger = Logger.getLogger(HumanEchoServlet.class);
luis@0 34
luis@28 35 private String tempdir = "";
luis@28 36 private String wavdir = "";
luis@16 37
luis@0 38 public void init(ServletConfig config) throws ServletException {
luis@0 39 super.init(config);
luis@0 40
luis@15 41 // reading properties file
luis@15 42 try {
luis@15 43 Properties properties = new Properties();
luis@15 44 properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("myapp.properties"));
luis@15 45
luis@15 46 //get the property value and print it out
luis@28 47 tempdir = properties.getProperty("tmpdir");
luis@28 48 wavdir = properties.getProperty("outdir");
luis@15 49
luis@15 50 } catch (IOException e) {
luis@15 51 e.printStackTrace();
luis@15 52 }
luis@15 53
luis@0 54 try {
luis@0 55 echo = new HumanEcho();
luis@0 56 }
luis@0 57 catch(MWException e) {
luis@0 58 e.printStackTrace();
luis@0 59 }
luis@0 60 }
luis@0 61
luis@0 62 public void destroy() {
luis@0 63 super.destroy();
luis@0 64
luis@0 65 if(echo!=null) {
luis@0 66 echo.dispose();
luis@0 67 }
luis@0 68 }
luis@0 69
luis@11 70 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
luis@0 71
luis@11 72 // todo: validate/normalise distance
luis@11 73 int dist = Integer.parseInt(request.getParameter("dist"));
luis@11 74 MWStructArray Input = null;
luis@11 75 HumanEcho echo;
luis@0 76
luis@28 77 String outputfname = new String();
luis@28 78
luis@11 79 try {
luis@11 80 echo = new HumanEcho();
luis@0 81
luis@16 82 logger.error("We are logging!");
luis@28 83 logger.warn(System.getProperty("catalina.base"));
luis@28 84
luis@16 85
luis@11 86 // Matlab structure:
luis@11 87 // Input = struct('dist', 0.9, 'azim', 0, 'orient', 'horz', 'dirweight', 0.2, 'outputfname', 'foo.wav')
luis@0 88
luis@11 89 String[] InputStructFields = {"dist", "azim", "orient", "dirweight", "outputfname"};
luis@11 90 Input = new MWStructArray(1, 1, InputStructFields);
luis@11 91 Input.set("dist", 1, Double.valueOf(dist));
luis@11 92 Input.set("azim", 1, Double.valueOf(0));
luis@11 93 Input.set("orient", 1, "horz");
luis@11 94 Input.set("dirweight", 1, Double.valueOf(0.2));
luis@0 95
luis@11 96 // the ofname should depend on the parameters
luis@11 97 StringBuilder sb = new StringBuilder();
luis@17 98 sb.append("e_d");
luis@11 99 sb.append(dist);
luis@11 100 sb.append(".wav");
luis@28 101 outputfname = sb.toString();
luis@11 102
luis@11 103 Input.set("outputfname", 1, outputfname);
luis@11 104
luis@11 105 // todo: before calling should test if wav already exists..
luis@11 106 Object[] result = echo.simulateBinauralSignals(Input);
luis@0 107 }
luis@0 108 catch(MWException e) {
luis@0 109 e.printStackTrace();
luis@0 110 }
luis@0 111
luis@28 112 // Creating the stream
luis@28 113 ServletOutputStream stream = null;
luis@28 114 BufferedInputStream buf = null;
luis@28 115
luis@28 116 try{
luis@28 117 stream = response.getOutputStream();
luis@28 118 File wavfile = new File(outputfname);
luis@28 119
luis@28 120 //set response headers
luis@28 121 response.setContentType("audio/wav");
luis@28 122 response.addHeader("Content-Disposition","attachment; filename=" + outputfname );
luis@28 123 response.setContentLength( (int) wavfile.length( ) );
luis@28 124
luis@28 125 FileInputStream input = new FileInputStream(wavfile);
luis@28 126 buf = new BufferedInputStream(input);
luis@28 127 int readBytes = 0;
luis@28 128
luis@28 129 //read from the file; write to the ServletOutputStream
luis@28 130 while((readBytes = buf.read( )) != -1)
luis@28 131 stream.write(readBytes);
luis@28 132
luis@28 133 } catch (IOException ioe){
luis@28 134 throw new ServletException(ioe.getMessage( ));
luis@28 135 } finally {
luis@28 136 //close the input/output streams
luis@28 137 if(stream != null)
luis@28 138 stream.close( );
luis@28 139
luis@28 140 if(buf != null)
luis@28 141 buf.close( );
luis@28 142 }
luis@28 143
luis@21 144 System.out.println("MCRROOT: " + System.getenv("MCRROOT"));
luis@21 145 System.out.println("PATH: " + System.getenv("PATH"));
luis@21 146 System.out.println(MCRConfiguration.isInstalledMCR());
luis@15 147
luis@28 148 logger.warn("tmpdir " + tempdir);
luis@0 149 }
luis@0 150 }