JVoiceXmlDocumentServer implements DocumentServer { @Override public VoiceXmlDocument getDocument(final String sessionId, final DocumentDescriptor descriptor) throws BadFetchError { final URI uri = descriptor.getUri(); final SchemeStrategy strategy = getSchemeStrategy(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(); LOGGER.info("loading document with URI '" + uri + "..."); InputStream input = null; final VoiceXmlDocument document; try { input = strategy.getInputStream(sessionId, uri, method, timeout, parameters); document = readDocument(input); } catch (UnsupportedElementError e) { throw new BadFetchError(e.getMessage(), e); } catch (IOException e) { throw new BadFetchError(e.getMessage(), e); } finally { if (input != null) { try { input.close(); } catch (IOException e) { throw new BadFetchError(e); } } } if (LOGGER.isDebugEnabled()) { LOGGER.debug("...read document"); LOGGER.debug(document); } final Vxml vxml = document.getVxml(); final String version = vxml.getVersion(); if (version == null) { throw new BadFetchError("The document at '" + uri + "' does not provide a version attribute!"); } return document; } 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 testGetDocument() throws Exception, JVoiceXMLEvent { final VoiceXmlDocument document = new VoiceXmlDocument(); final URI uri = map.getUri("/test"); map.addDocument(uri, document); final DocumentDescriptor descriptor = new DocumentDescriptor(uri); final Session session = Mockito.mock(Session.class); Mockito.when(session.getSessionId()).thenReturn( UUID.randomUUID().toString()); final String sessionId = session.getSessionId(); final VoiceXmlDocument retrievedDocument = server.getDocument( sessionId, descriptor); Assert.assertEquals(document.toString(), retrievedDocument.toString()); }
@Test public void testGetDocumentFragment() throws Exception, JVoiceXMLEvent { final VoiceXmlDocument document = new VoiceXmlDocument(); final URI uri = map.getUri("/test"); map.addDocument(uri, document); final URI fragmentUri = new URI(uri.toString() + "#fragment"); final DocumentDescriptor descriptor = new DocumentDescriptor( fragmentUri); final Session session = Mockito.mock(Session.class); Mockito.when(session.getSessionId()).thenReturn( UUID.randomUUID().toString()); final String sessionId = session.getSessionId(); final VoiceXmlDocument retrievedDocument = server.getDocument( sessionId, descriptor); Assert.assertEquals(document.toString(), retrievedDocument.toString()); }
@Test(expected = BadFetchError.class) public void testGetInvalidDocument() throws Exception, JVoiceXMLEvent { final String str = "<vxml><form><block><prompt>test</prompt>" + "</block></form></vxml>"; final StringReader reader = new StringReader(str); final InputSource input = new InputSource(reader); final VoiceXmlDocument document = new VoiceXmlDocument(input); final URI uri = map.getUri("/test"); map.addDocument(uri, document); final DocumentDescriptor descriptor = new DocumentDescriptor(uri); final Session session = Mockito.mock(Session.class); Mockito.when(session.getSessionId()).thenReturn( UUID.randomUUID().toString()); final String sessionId = session.getSessionId(); final VoiceXmlDocument retrievedDocument = server.getDocument( sessionId, descriptor); Assert.assertEquals(document, retrievedDocument); }