@Test public void handleCaptchaSound() throws ResourceReferenceHandlerException, CaptchaException, IOException { SoundCaptchaService soundCaptchaService = mock(SoundCaptchaService.class); when(captchaServiceManager.getCaptchaService("customSoundEngine")).thenReturn(soundCaptchaService); AudioInputStream audioInputStream = mock(AudioInputStream.class); when(soundCaptchaService.getSoundChallengeForID(SESSION_ID, LOCALE)).thenReturn(audioInputStream); when(audioInputStream.getFormat()).thenReturn(mock(AudioFormat.class)); JCaptchaResourceReference jCaptchaResourceReference = new JCaptchaResourceReference("sound", "customSoundEngine"); jCaptchaResourceReferenceHandler.handle(jCaptchaResourceReference, resourceReferenceHandlerChain); verify(captchaServiceManager, times(1)).getCaptchaService("customSoundEngine"); verify(httpSession, times(1)).getId(); verify(soundCaptchaService, times(1)).getSoundChallengeForID(SESSION_ID, LOCALE); verify(response, times(1)).setContentType("audio/x-wav"); verify(response, times(1)).getOutputStream(); verify(outputStream, times(1)).write(any()); verify(resourceReferenceHandlerChain, times(1)).handleNext(jCaptchaResourceReference); }
@Override public void handle(ResourceReference reference, ResourceReferenceHandlerChain chain) throws ResourceReferenceHandlerException { try { HttpServletRequest request = ((ServletRequest) this.container.getRequest()).getHttpServletRequest(); HttpServletResponse response = ((ServletResponse) this.container.getResponse()).getHttpServletResponse(); JCaptchaResourceReference jCaptchaResourceReference = (JCaptchaResourceReference) reference; if (System.getProperty(FREETTS_PROPERTIES_KEY) == null) { System.setProperty(FREETTS_PROPERTIES_KEY, AbstractFreeTTSWordToSound.defaultVoicePackage); } CaptchaService captchaService = captchaServiceManager.getCaptchaService(jCaptchaResourceReference.getEngine()); String type = jCaptchaResourceReference.getCaptchaType(); String id = request.getSession().getId(); Locale locale = request.getLocale(); if (type == null) { throw new IllegalArgumentException("Missing [type] parameter"); } switch (type) { case TYPE_IMAGE: ImageToJpegHelper.flushNewCaptchaToResponse(request, response, null, (ImageCaptchaService) captchaService, id, locale); break; case TYPE_SOUND: SoundToWavHelper.flushNewCaptchaToResponse(request, response, null, (SoundCaptchaService) captchaService, id, locale); break; case TYPE_TEXT: response.setContentType("text/plain"); response.setCharacterEncoding("UTF-8"); response.setHeader("Cache-Control", "no-store"); response.setHeader("Pragma", "no-cache"); response.setDateHeader("Expires", 0); String challenge = (String) captchaService.getChallengeForID(id, locale); try (OutputStream responseOutput = response.getOutputStream()) { IOUtils.write(challenge, responseOutput, Charset.defaultCharset()); } break; default: throw new UnsupportedOperationException( String.format("Unsupported value [%s] for the [type] parameter", type)); } } catch (Exception e) { throw new ResourceReferenceHandlerException( String.format("Failed to handle resource [%s]", JCaptchaResourceReference.TYPE), e); } chain.handleNext(reference); }
JCaptchaResourceReferenceHandler extends AbstractResourceReferenceHandler<ResourceType> { @Override public void handle(ResourceReference reference, ResourceReferenceHandlerChain chain) throws ResourceReferenceHandlerException { try { HttpServletRequest request = ((ServletRequest) this.container.getRequest()).getHttpServletRequest(); HttpServletResponse response = ((ServletResponse) this.container.getResponse()).getHttpServletResponse(); JCaptchaResourceReference jCaptchaResourceReference = (JCaptchaResourceReference) reference; if (System.getProperty(FREETTS_PROPERTIES_KEY) == null) { System.setProperty(FREETTS_PROPERTIES_KEY, AbstractFreeTTSWordToSound.defaultVoicePackage); } CaptchaService captchaService = captchaServiceManager.getCaptchaService(jCaptchaResourceReference.getEngine()); String type = jCaptchaResourceReference.getCaptchaType(); String id = request.getSession().getId(); Locale locale = request.getLocale(); if (type == null) { throw new IllegalArgumentException("Missing [type] parameter"); } switch (type) { case TYPE_IMAGE: ImageToJpegHelper.flushNewCaptchaToResponse(request, response, null, (ImageCaptchaService) captchaService, id, locale); break; case TYPE_SOUND: SoundToWavHelper.flushNewCaptchaToResponse(request, response, null, (SoundCaptchaService) captchaService, id, locale); break; case TYPE_TEXT: response.setContentType("text/plain"); response.setCharacterEncoding("UTF-8"); response.setHeader("Cache-Control", "no-store"); response.setHeader("Pragma", "no-cache"); response.setDateHeader("Expires", 0); String challenge = (String) captchaService.getChallengeForID(id, locale); try (OutputStream responseOutput = response.getOutputStream()) { IOUtils.write(challenge, responseOutput, Charset.defaultCharset()); } break; default: throw new UnsupportedOperationException( String.format("Unsupported value [%s] for the [type] parameter", type)); } } catch (Exception e) { throw new ResourceReferenceHandlerException( String.format("Failed to handle resource [%s]", JCaptchaResourceReference.TYPE), e); } chain.handleNext(reference); } }
JCaptchaResourceReferenceHandler extends AbstractResourceReferenceHandler<ResourceType> { @Override public void handle(ResourceReference reference, ResourceReferenceHandlerChain chain) throws ResourceReferenceHandlerException { try { HttpServletRequest request = ((ServletRequest) this.container.getRequest()).getHttpServletRequest(); HttpServletResponse response = ((ServletResponse) this.container.getResponse()).getHttpServletResponse(); JCaptchaResourceReference jCaptchaResourceReference = (JCaptchaResourceReference) reference; if (System.getProperty(FREETTS_PROPERTIES_KEY) == null) { System.setProperty(FREETTS_PROPERTIES_KEY, AbstractFreeTTSWordToSound.defaultVoicePackage); } CaptchaService captchaService = captchaServiceManager.getCaptchaService(jCaptchaResourceReference.getEngine()); String type = jCaptchaResourceReference.getCaptchaType(); String id = request.getSession().getId(); Locale locale = request.getLocale(); if (type == null) { throw new IllegalArgumentException("Missing [type] parameter"); } switch (type) { case TYPE_IMAGE: ImageToJpegHelper.flushNewCaptchaToResponse(request, response, null, (ImageCaptchaService) captchaService, id, locale); break; case TYPE_SOUND: SoundToWavHelper.flushNewCaptchaToResponse(request, response, null, (SoundCaptchaService) captchaService, id, locale); break; case TYPE_TEXT: response.setContentType("text/plain"); response.setCharacterEncoding("UTF-8"); response.setHeader("Cache-Control", "no-store"); response.setHeader("Pragma", "no-cache"); response.setDateHeader("Expires", 0); String challenge = (String) captchaService.getChallengeForID(id, locale); try (OutputStream responseOutput = response.getOutputStream()) { IOUtils.write(challenge, responseOutput, Charset.defaultCharset()); } break; default: throw new UnsupportedOperationException( String.format("Unsupported value [%s] for the [type] parameter", type)); } } catch (Exception e) { throw new ResourceReferenceHandlerException( String.format("Failed to handle resource [%s]", JCaptchaResourceReference.TYPE), e); } chain.handleNext(reference); }  }
JCaptchaResourceReferenceHandler extends AbstractResourceReferenceHandler<ResourceType> { @Override public void handle(ResourceReference reference, ResourceReferenceHandlerChain chain) throws ResourceReferenceHandlerException { try { HttpServletRequest request = ((ServletRequest) this.container.getRequest()).getHttpServletRequest(); HttpServletResponse response = ((ServletResponse) this.container.getResponse()).getHttpServletResponse(); JCaptchaResourceReference jCaptchaResourceReference = (JCaptchaResourceReference) reference; if (System.getProperty(FREETTS_PROPERTIES_KEY) == null) { System.setProperty(FREETTS_PROPERTIES_KEY, AbstractFreeTTSWordToSound.defaultVoicePackage); } CaptchaService captchaService = captchaServiceManager.getCaptchaService(jCaptchaResourceReference.getEngine()); String type = jCaptchaResourceReference.getCaptchaType(); String id = request.getSession().getId(); Locale locale = request.getLocale(); if (type == null) { throw new IllegalArgumentException("Missing [type] parameter"); } switch (type) { case TYPE_IMAGE: ImageToJpegHelper.flushNewCaptchaToResponse(request, response, null, (ImageCaptchaService) captchaService, id, locale); break; case TYPE_SOUND: SoundToWavHelper.flushNewCaptchaToResponse(request, response, null, (SoundCaptchaService) captchaService, id, locale); break; case TYPE_TEXT: response.setContentType("text/plain"); response.setCharacterEncoding("UTF-8"); response.setHeader("Cache-Control", "no-store"); response.setHeader("Pragma", "no-cache"); response.setDateHeader("Expires", 0); String challenge = (String) captchaService.getChallengeForID(id, locale); try (OutputStream responseOutput = response.getOutputStream()) { IOUtils.write(challenge, responseOutput, Charset.defaultCharset()); } break; default: throw new UnsupportedOperationException( String.format("Unsupported value [%s] for the [type] parameter", type)); } } catch (Exception e) { throw new ResourceReferenceHandlerException( String.format("Failed to handle resource [%s]", JCaptchaResourceReference.TYPE), e); } chain.handleNext(reference); }  @Override List<ResourceType> getSupportedResourceReferences(); @Override void handle(ResourceReference reference, ResourceReferenceHandlerChain chain); }
JCaptchaResourceReferenceHandler extends AbstractResourceReferenceHandler<ResourceType> { @Override public void handle(ResourceReference reference, ResourceReferenceHandlerChain chain) throws ResourceReferenceHandlerException { try { HttpServletRequest request = ((ServletRequest) this.container.getRequest()).getHttpServletRequest(); HttpServletResponse response = ((ServletResponse) this.container.getResponse()).getHttpServletResponse(); JCaptchaResourceReference jCaptchaResourceReference = (JCaptchaResourceReference) reference; if (System.getProperty(FREETTS_PROPERTIES_KEY) == null) { System.setProperty(FREETTS_PROPERTIES_KEY, AbstractFreeTTSWordToSound.defaultVoicePackage); } CaptchaService captchaService = captchaServiceManager.getCaptchaService(jCaptchaResourceReference.getEngine()); String type = jCaptchaResourceReference.getCaptchaType(); String id = request.getSession().getId(); Locale locale = request.getLocale(); if (type == null) { throw new IllegalArgumentException("Missing [type] parameter"); } switch (type) { case TYPE_IMAGE: ImageToJpegHelper.flushNewCaptchaToResponse(request, response, null, (ImageCaptchaService) captchaService, id, locale); break; case TYPE_SOUND: SoundToWavHelper.flushNewCaptchaToResponse(request, response, null, (SoundCaptchaService) captchaService, id, locale); break; case TYPE_TEXT: response.setContentType("text/plain"); response.setCharacterEncoding("UTF-8"); response.setHeader("Cache-Control", "no-store"); response.setHeader("Pragma", "no-cache"); response.setDateHeader("Expires", 0); String challenge = (String) captchaService.getChallengeForID(id, locale); try (OutputStream responseOutput = response.getOutputStream()) { IOUtils.write(challenge, responseOutput, Charset.defaultCharset()); } break; default: throw new UnsupportedOperationException( String.format("Unsupported value [%s] for the [type] parameter", type)); } } catch (Exception e) { throw new ResourceReferenceHandlerException( String.format("Failed to handle resource [%s]", JCaptchaResourceReference.TYPE), e); } chain.handleNext(reference); }  @Override List<ResourceType> getSupportedResourceReferences(); @Override void handle(ResourceReference reference, ResourceReferenceHandlerChain chain);  }