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