@Test public void handleErrorWhenGettingEngine() throws CaptchaException { JCaptchaResourceReference jCaptchaResourceReference = new JCaptchaResourceReference("text", "foobar"); when(captchaServiceManager.getCaptchaService("foobar")) .thenThrow(new CaptchaException("Cannot found foobar service", new RuntimeException())); ResourceReferenceHandlerException resourceReferenceHandlerException = assertThrows(ResourceReferenceHandlerException.class, () -> { jCaptchaResourceReferenceHandler.handle(jCaptchaResourceReference, resourceReferenceHandlerChain); }); assertEquals("Failed to handle resource [jcaptcha]", resourceReferenceHandlerException.getMessage()); assertEquals(CaptchaException.class, resourceReferenceHandlerException.getCause().getClass()); assertEquals("Cannot found foobar service", resourceReferenceHandlerException.getCause().getMessage()); }
@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);  }