Mercurial > hg > human-echolocation-java-webapp
comparison src/HumanEchoServlet.java @ 54:b27106b380d8
Populate and pass location of input dir; handle all parameters from page; use only one echo instance (??)
author | Chris Cannam |
---|---|
date | Mon, 03 Mar 2014 15:31:14 +0000 |
parents | ebfb3b91fb06 |
children | b3f3b42b9933 |
comparison
equal
deleted
inserted
replaced
53:f69b33d895a6 | 54:b27106b380d8 |
---|---|
27 import com.mathworks.toolbox.javabuilder.internal.MCRConfiguration; | 27 import com.mathworks.toolbox.javabuilder.internal.MCRConfiguration; |
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 | |
32 private HumanEcho echo; | 33 private HumanEcho echo; |
33 private static Logger logger = Logger.getLogger(HumanEchoServlet.class); | 34 private static Logger logger = Logger.getLogger(HumanEchoServlet.class); |
34 | 35 |
35 private String wavdir = ""; | 36 private String outdir = ""; |
37 private String indir = ""; | |
36 | 38 |
37 public void init(ServletConfig config) throws ServletException { | 39 public void init(ServletConfig config) throws ServletException { |
38 super.init(config); | 40 super.init(config); |
39 | 41 |
40 logger.info("In init"); | 42 logger.info("In init"); |
41 | 43 |
42 logger.info("java.library.path is " + | 44 logger.info("java.library.path is " + |
43 System.getProperty("java.library.path")); | 45 System.getProperty("java.library.path")); |
44 | 46 |
45 try { | 47 try { |
46 // Read directory properties | |
47 Properties properties = new Properties(); | 48 Properties properties = new Properties(); |
48 properties.load | 49 properties.load |
49 (Thread.currentThread().getContextClassLoader(). | 50 (Thread.currentThread().getContextClassLoader(). |
50 getResourceAsStream("HumanEcho.properties")); | 51 getResourceAsStream("HumanEcho.properties")); |
51 | 52 |
52 wavdir = properties.getProperty("outdir"); | 53 outdir = properties.getProperty("outdir"); |
54 indir = properties.getProperty("indir"); | |
53 | 55 |
54 logger.info("WAV file directory is " + wavdir); | 56 logger.info("WAV file directory is " + outdir); |
55 | 57 |
56 } catch (Exception e) { | 58 } catch (Exception e) { |
57 logger.error("Failed to load app properties: " + e.getMessage()); | 59 logger.error("Failed to load app properties: " + e.getMessage()); |
58 e.printStackTrace(); | 60 e.printStackTrace(); |
59 } | 61 } |
83 | 85 |
84 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | 86 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
85 | 87 |
86 logger.info("In doGet"); | 88 logger.info("In doGet"); |
87 | 89 |
90 logger.info("Distance (as string) is: " + request.getParameter("dist")); | |
91 | |
88 // todo: validate/normalise distance | 92 // todo: validate/normalise distance |
89 int dist = Integer.parseInt(request.getParameter("dist")); | 93 Double dist = Double.parseDouble(request.getParameter("dist")); |
90 | 94 |
91 logger.info("String azim is: " + request.getParameter("azim")); | 95 Double azim = Double.parseDouble(request.getParameter("azim")); |
92 Float azim = Float.parseFloat(request.getParameter("azim")); | 96 |
97 String orientation = request.getParameter("orient"); | |
98 | |
99 Double dirweight = Double.parseDouble(request.getParameter("dirweight")); | |
93 | 100 |
94 MWStructArray Input = null; | 101 MWStructArray Input = null; |
95 HumanEcho echo; | |
96 | 102 |
97 String outputfname = new String(); | 103 String outputfname = new String(); |
98 | 104 |
99 logger.info("Got dist and azim"); | 105 logger.info("Got parameters"); |
100 | 106 |
101 try { | 107 try { |
102 //!!! erch, shouldn't be creating a new one on each request as well as a global one | |
103 echo = new HumanEcho(); | |
104 | |
105 logger.info("Property catalina.base is " + System.getProperty("catalina.base")); | 108 logger.info("Property catalina.base is " + System.getProperty("catalina.base")); |
106 | 109 |
107 // Matlab structure: | 110 // Matlab structure: |
108 // Input = struct('dist', 0.9, 'azim', 0, 'orient', 'horz', 'dirweight', 0.2, 'outdir', '/tmp/wav', 'outname', 'foo.wav') | 111 // Input = struct('dist', 0.9, 'azim', 0, 'orient', 'horz', 'dirweight', 0.2, 'outdir', '/tmp/wav', 'outname', 'foo.wav', 'indir', '/path/to/hrir') |
109 | 112 |
110 String[] InputStructFields = {"dist", "azim", "orient", "dirweight", "outdir", "outname"}; | 113 String[] InputStructFields = { |
114 "dist", "azim", "orient", "dirweight", | |
115 "outdir", "outname", "indir" | |
116 }; | |
111 | 117 |
112 Input = new MWStructArray(1, 1, InputStructFields); | 118 Input = new MWStructArray(1, 1, InputStructFields); |
113 Input.set("dist", 1, Double.valueOf(dist)); | 119 Input.set("dist", 1, Double.valueOf(dist)); |
114 Input.set("azim", 1, Double.valueOf(azim)); | 120 Input.set("azim", 1, Double.valueOf(azim)); |
115 Input.set("orient", 1, "horz"); | 121 Input.set("orient", 1, orientation); |
116 Input.set("dirweight", 1, Double.valueOf(0.2)); | 122 Input.set("dirweight", 1, Double.valueOf(dirweight)); |
117 Input.set("outdir", 1, wavdir); | 123 Input.set("outdir", 1, outdir); |
124 Input.set("indir", 1, indir); | |
118 | 125 |
119 // the ofname should depend on the parameters | 126 // the ofname should depend on the parameters |
120 StringBuilder sb = new StringBuilder(); | 127 StringBuilder sb = new StringBuilder(); |
121 sb.append("e_d"); | 128 sb.append("e_d"); |
122 sb.append(dist); | 129 sb.append(dist); |
138 ServletOutputStream stream = null; | 145 ServletOutputStream stream = null; |
139 BufferedInputStream buf = null; | 146 BufferedInputStream buf = null; |
140 | 147 |
141 try{ | 148 try{ |
142 stream = response.getOutputStream(); | 149 stream = response.getOutputStream(); |
143 File wavfile = new File(outputfname); | 150 |
151 String filename = outdir + "/" + outputfname + ".wav"; | |
152 logger.info("About to read WAV data from \"" + filename + "\""); | |
153 | |
154 File wavfile = new File(filename); | |
144 | 155 |
145 //set response headers | 156 //set response headers |
146 response.setContentType("audio/x-wav"); | 157 response.setContentType("audio/x-wav"); |
147 response.addHeader("Content-Disposition","attachment; filename=" + outputfname ); | 158 response.addHeader("Content-Disposition","attachment; filename=" + outputfname ); |
148 response.setContentLength( (int) wavfile.length( ) ); | 159 response.setContentLength( (int) wavfile.length( ) ); |
149 | 160 |
150 FileInputStream input = new FileInputStream(wavfile); | 161 FileInputStream input = new FileInputStream(wavfile); |
151 buf = new BufferedInputStream(input); | 162 buf = new BufferedInputStream(input); |
152 int readBytes = 0; | |
153 | 163 |
154 //read from the file; write to the ServletOutputStream | 164 int b = 0; |
155 while((readBytes = buf.read( )) != -1) | 165 int total = 0; |
156 stream.write(readBytes); | 166 |
167 // read from the file; write to the ServletOutputStream | |
168 while ((b = buf.read()) != -1) { | |
169 stream.write(b); | |
170 total += 1; | |
171 } | |
172 | |
173 logger.info("Successfully wrote " + total + " byte(s) to client"); | |
157 | 174 |
158 } catch (IOException ioe){ | 175 } catch (IOException ioe){ |
159 logger.error("Failed to read wav data: " + ioe.getMessage()); | 176 logger.error("Failed to read wav data: " + ioe.getMessage()); |
160 throw new ServletException(ioe.getMessage( )); | 177 throw new ServletException(ioe.getMessage( )); |
161 } finally { | 178 } finally { |
162 //close the input/output streams | 179 if (stream != null) stream.close(); |
163 if(stream != null) | 180 if (buf != null) buf.close(); |
164 stream.close( ); | 181 } |
165 | |
166 if(buf != null) | |
167 buf.close( ); | |
168 } | |
169 | |
170 logger.info("Wrote file: " + outputfname); | |
171 } | 182 } |
172 } | 183 } |