@Test public void changeResponseWhenCallbackParamNameIsSavedInContext() throws Exception { JsonpConfigBean config = new JsonpConfigBean(); String functionName = "testFunction"; sContext.setAttribute(JsonpPolicy.CALLBACK_FUNCTION_NAME, functionName); ApiResponse response = new ApiResponse(); String json = "{\"name\": \"test\"}"; IApimanBuffer chunk = new ByteBuffer(json.getBytes().length); chunk.append(json); IAsyncHandler<IApimanBuffer> bodyHandler = mock(IAsyncHandler.class); IReadWriteStream<ApiResponse> responseDataHandler = jsonpPolicy.getResponseDataHandler(response, sContext, config); ApiResponse head = responseDataHandler.getHead(); responseDataHandler.bodyHandler(bodyHandler); responseDataHandler.write(chunk); responseDataHandler.end(); assertSame(response, head); verify(bodyHandler).handle(refEq(new ByteBuffer("testFunction("))); verify(bodyHandler).handle(refEq(new ByteBuffer(json))); verify(bodyHandler).handle(refEq(new ByteBuffer(")"))); }
@Override public IReadWriteStream<ApiResponse> getResponseDataHandler(final ApiResponse response, IPolicyContext context, Object policyConfiguration) { final String callbackFunctionName = (String) context.getAttribute(CALLBACK_FUNCTION_NAME, null); if (callbackFunctionName != null) { HttpHeaders httpHeaders = new HttpHeaders(response.getHeaders()); final String encoding = httpHeaders.getCharsetFromContentType(StandardCharsets.UTF_8.name()); final int additionalContentLength = callbackFunctionName.length() + OPEN_PARENTHESES.length() + CLOSE_PARENTHESES.length(); httpHeaders.setContentType(APPLICATION_JAVASCRIPT); httpHeaders.incrementContentLength(additionalContentLength); final IBufferFactoryComponent bufferFactory = context.getComponent(IBufferFactoryComponent.class); return new AbstractStream<ApiResponse>() { private boolean firstChunk = true; @Override public ApiResponse getHead() { return response; } @Override protected void handleHead(ApiResponse head) { } @Override public void write(IApimanBuffer chunk) { if (firstChunk) { IApimanBuffer buffer = bufferFactory.createBuffer(callbackFunctionName.length() + OPEN_PARENTHESES.length()); try { buffer.append(callbackFunctionName, encoding); buffer.append(OPEN_PARENTHESES, encoding); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } super.write(buffer); firstChunk = false; } super.write(chunk); } @Override public void end() { if (!firstChunk) { IApimanBuffer buffer = bufferFactory.createBuffer(CLOSE_PARENTHESES.length()); try { buffer.append(CLOSE_PARENTHESES, encoding); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } super.write(buffer); } super.end(); } }; } return null; }
JsonpPolicy extends AbstractMappedPolicy<JsonpConfigBean> implements IDataPolicy { @Override public IReadWriteStream<ApiResponse> getResponseDataHandler(final ApiResponse response, IPolicyContext context, Object policyConfiguration) { final String callbackFunctionName = (String) context.getAttribute(CALLBACK_FUNCTION_NAME, null); if (callbackFunctionName != null) { HttpHeaders httpHeaders = new HttpHeaders(response.getHeaders()); final String encoding = httpHeaders.getCharsetFromContentType(StandardCharsets.UTF_8.name()); final int additionalContentLength = callbackFunctionName.length() + OPEN_PARENTHESES.length() + CLOSE_PARENTHESES.length(); httpHeaders.setContentType(APPLICATION_JAVASCRIPT); httpHeaders.incrementContentLength(additionalContentLength); final IBufferFactoryComponent bufferFactory = context.getComponent(IBufferFactoryComponent.class); return new AbstractStream<ApiResponse>() { private boolean firstChunk = true; @Override public ApiResponse getHead() { return response; } @Override protected void handleHead(ApiResponse head) { } @Override public void write(IApimanBuffer chunk) { if (firstChunk) { IApimanBuffer buffer = bufferFactory.createBuffer(callbackFunctionName.length() + OPEN_PARENTHESES.length()); try { buffer.append(callbackFunctionName, encoding); buffer.append(OPEN_PARENTHESES, encoding); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } super.write(buffer); firstChunk = false; } super.write(chunk); } @Override public void end() { if (!firstChunk) { IApimanBuffer buffer = bufferFactory.createBuffer(CLOSE_PARENTHESES.length()); try { buffer.append(CLOSE_PARENTHESES, encoding); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } super.write(buffer); } super.end(); } }; } return null; } }
JsonpPolicy extends AbstractMappedPolicy<JsonpConfigBean> implements IDataPolicy { @Override public IReadWriteStream<ApiResponse> getResponseDataHandler(final ApiResponse response, IPolicyContext context, Object policyConfiguration) { final String callbackFunctionName = (String) context.getAttribute(CALLBACK_FUNCTION_NAME, null); if (callbackFunctionName != null) { HttpHeaders httpHeaders = new HttpHeaders(response.getHeaders()); final String encoding = httpHeaders.getCharsetFromContentType(StandardCharsets.UTF_8.name()); final int additionalContentLength = callbackFunctionName.length() + OPEN_PARENTHESES.length() + CLOSE_PARENTHESES.length(); httpHeaders.setContentType(APPLICATION_JAVASCRIPT); httpHeaders.incrementContentLength(additionalContentLength); final IBufferFactoryComponent bufferFactory = context.getComponent(IBufferFactoryComponent.class); return new AbstractStream<ApiResponse>() { private boolean firstChunk = true; @Override public ApiResponse getHead() { return response; } @Override protected void handleHead(ApiResponse head) { } @Override public void write(IApimanBuffer chunk) { if (firstChunk) { IApimanBuffer buffer = bufferFactory.createBuffer(callbackFunctionName.length() + OPEN_PARENTHESES.length()); try { buffer.append(callbackFunctionName, encoding); buffer.append(OPEN_PARENTHESES, encoding); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } super.write(buffer); firstChunk = false; } super.write(chunk); } @Override public void end() { if (!firstChunk) { IApimanBuffer buffer = bufferFactory.createBuffer(CLOSE_PARENTHESES.length()); try { buffer.append(CLOSE_PARENTHESES, encoding); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } super.write(buffer); } super.end(); } }; } return null; }  }
JsonpPolicy extends AbstractMappedPolicy<JsonpConfigBean> implements IDataPolicy { @Override public IReadWriteStream<ApiResponse> getResponseDataHandler(final ApiResponse response, IPolicyContext context, Object policyConfiguration) { final String callbackFunctionName = (String) context.getAttribute(CALLBACK_FUNCTION_NAME, null); if (callbackFunctionName != null) { HttpHeaders httpHeaders = new HttpHeaders(response.getHeaders()); final String encoding = httpHeaders.getCharsetFromContentType(StandardCharsets.UTF_8.name()); final int additionalContentLength = callbackFunctionName.length() + OPEN_PARENTHESES.length() + CLOSE_PARENTHESES.length(); httpHeaders.setContentType(APPLICATION_JAVASCRIPT); httpHeaders.incrementContentLength(additionalContentLength); final IBufferFactoryComponent bufferFactory = context.getComponent(IBufferFactoryComponent.class); return new AbstractStream<ApiResponse>() { private boolean firstChunk = true; @Override public ApiResponse getHead() { return response; } @Override protected void handleHead(ApiResponse head) { } @Override public void write(IApimanBuffer chunk) { if (firstChunk) { IApimanBuffer buffer = bufferFactory.createBuffer(callbackFunctionName.length() + OPEN_PARENTHESES.length()); try { buffer.append(callbackFunctionName, encoding); buffer.append(OPEN_PARENTHESES, encoding); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } super.write(buffer); firstChunk = false; } super.write(chunk); } @Override public void end() { if (!firstChunk) { IApimanBuffer buffer = bufferFactory.createBuffer(CLOSE_PARENTHESES.length()); try { buffer.append(CLOSE_PARENTHESES, encoding); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } super.write(buffer); } super.end(); } }; } return null; }  @Override IReadWriteStream<ApiRequest> getRequestDataHandler(ApiRequest request,
            IPolicyContext context, Object policyConfiguration); @Override IReadWriteStream<ApiResponse> getResponseDataHandler(final ApiResponse response,
            IPolicyContext context, Object policyConfiguration); }
JsonpPolicy extends AbstractMappedPolicy<JsonpConfigBean> implements IDataPolicy { @Override public IReadWriteStream<ApiResponse> getResponseDataHandler(final ApiResponse response, IPolicyContext context, Object policyConfiguration) { final String callbackFunctionName = (String) context.getAttribute(CALLBACK_FUNCTION_NAME, null); if (callbackFunctionName != null) { HttpHeaders httpHeaders = new HttpHeaders(response.getHeaders()); final String encoding = httpHeaders.getCharsetFromContentType(StandardCharsets.UTF_8.name()); final int additionalContentLength = callbackFunctionName.length() + OPEN_PARENTHESES.length() + CLOSE_PARENTHESES.length(); httpHeaders.setContentType(APPLICATION_JAVASCRIPT); httpHeaders.incrementContentLength(additionalContentLength); final IBufferFactoryComponent bufferFactory = context.getComponent(IBufferFactoryComponent.class); return new AbstractStream<ApiResponse>() { private boolean firstChunk = true; @Override public ApiResponse getHead() { return response; } @Override protected void handleHead(ApiResponse head) { } @Override public void write(IApimanBuffer chunk) { if (firstChunk) { IApimanBuffer buffer = bufferFactory.createBuffer(callbackFunctionName.length() + OPEN_PARENTHESES.length()); try { buffer.append(callbackFunctionName, encoding); buffer.append(OPEN_PARENTHESES, encoding); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } super.write(buffer); firstChunk = false; } super.write(chunk); } @Override public void end() { if (!firstChunk) { IApimanBuffer buffer = bufferFactory.createBuffer(CLOSE_PARENTHESES.length()); try { buffer.append(CLOSE_PARENTHESES, encoding); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } super.write(buffer); } super.end(); } }; } return null; }  @Override IReadWriteStream<ApiRequest> getRequestDataHandler(ApiRequest request,
            IPolicyContext context, Object policyConfiguration); @Override IReadWriteStream<ApiResponse> getResponseDataHandler(final ApiResponse response,
            IPolicyContext context, Object policyConfiguration);  }