JVoiceXmlDocumentServer implements DocumentServer { @Override public Object getObject(final String sessionId, final DocumentDescriptor descriptor, final String type) throws BadFetchError { final URI uri = descriptor.getUri(); LOGGER.info("retrieving object with type '" + type + "' from '" + uri + "'"); final RequestMethod method = descriptor.getMethod(); final Collection<KeyValuePair> parameters = descriptor.getParameters(); final FetchAttributes attrs = descriptor.getAttributes(); final FetchAttributes mergedAttrs = mergeFetchAttributes(attrs); final long timeout = mergedAttrs.getFetchTimeout(); final SchemeStrategy strategy = getSchemeStrategy(uri); InputStream input = null; final Object object; try { input = strategy.getInputStream(sessionId, uri, method, timeout, parameters); if (type == null) { final ReadBuffer buffer = new ReadBuffer(); buffer.read(input); return buffer; } else if (type.equals(TEXT_PLAIN)) { final ReadBuffer buffer = new ReadBuffer(); buffer.read(input); return buffer.toString(); } else if (type.equals(TEXT_XML)) { object = readXml(input); } else { throw new BadFetchError("unknown type '" + type + "'"); } } catch (IOException e) { throw new BadFetchError(e.getMessage(), e); } catch (UnsupportedElementError e) { throw new BadFetchError(e.getMessage(), e); } finally { if (input != null) { try { input.close(); } catch (IOException e) { throw new BadFetchError(e); } } } return object; } JVoiceXmlDocumentServer(); void setDocumentStorage(final DocumentStorage documentStorage); @Override void start(); void setSchemeStrategies(final List<SchemeStrategy> schemeStrategies); void setFetchAttributes(final FetchAttributes attrs); @Override VoiceXmlDocument getDocument(final String sessionId,
            final DocumentDescriptor descriptor); void addSchemeStrategy(final SchemeStrategy strategy); @Override URI resolveBuiltinUri(final URI uri); @Override URI addGrammarDocument(final String sessionId,
            final GrammarDocument document); @Override GrammarDocument getGrammarDocument(final String sessionId,
            final URI uri, final FetchAttributes attrs); @Override AudioInputStream getAudioInputStream(final String sessionId,
            final URI uri); @Override Object getObject(final String sessionId,
            final DocumentDescriptor descriptor, final String type); @Override URI storeAudio(final AudioInputStream in); @Override void sessionClosed(final String sessionId); @Override void stop();  }
@Test public void testGetObjectTextPlain() throws JVoiceXMLEvent, Exception { String test = "Pinocchio"; final URI uri = map.getUri("/test"); map.addDocument(uri, test); final DocumentDescriptor descriptor = new DocumentDescriptor(uri); Object object = server.getObject(null, descriptor, DocumentServer.TEXT_PLAIN); Assert.assertEquals(test, object); }
@Test public void testGetObjectTextXml() throws JVoiceXMLEvent, Exception { VoiceXmlDocument document = new VoiceXmlDocument(); Vxml vxml = document.getVxml(); vxml.appendChild(Form.class); final URI uri = map.getUri("/test"); map.addDocument(uri, document); final DocumentDescriptor descriptor = new DocumentDescriptor(uri); Object object = server.getObject(null, descriptor, DocumentServer.TEXT_XML); Assert.assertTrue("object should be a document", object instanceof Document); final Document other = (Document) object; final Node otherVxml = other.getFirstChild(); Assert.assertEquals(Vxml.TAG_NAME, otherVxml.getNodeName()); final Node otherForm = otherVxml.getFirstChild(); Assert.assertEquals(Form.TAG_NAME, otherForm.getNodeName()); }
@Test public void testGetObjectBinary() throws JVoiceXMLEvent, Exception { final URL file = this.getClass().getResource("/test.wav"); final URI uri = file.toURI(); final DocumentDescriptor descriptor = new DocumentDescriptor(uri); final Object object = server.getObject(null, descriptor, null); Assert.assertTrue(object instanceof ReadBuffer); final ReadBuffer buffer = (ReadBuffer) object; Assert.assertFalse(buffer.isAscii()); }