protected ResponseEntity<String> executeRequest(URI url, HttpMethod method, HttpHeaders headers, @Nullable String body) { Request httpRequest = this.httpClient.newRequest(url).method(method); addHttpHeaders(httpRequest, headers); if (body != null) { httpRequest.content(new StringContentProvider(body)); } ContentResponse response; try { response = httpRequest.send(); } catch (Exception ex) { throw new SockJsTransportFailureException("Failed to execute request to " + url, ex); } HttpStatus status = HttpStatus.valueOf(response.getStatus()); HttpHeaders responseHeaders = toHttpHeaders(response.getHeaders()); return (response.getContent() != null ? new ResponseEntity<>(response.getContentAsString(), responseHeaders, status) : new ResponseEntity<>(responseHeaders, status)); }
private boolean sendRegistration(JsonObject request) { try { ContentResponse httpResponse = httpClient.newRequest(getRegistrationUri()). content(new StringContentProvider(request.toString())). method(HttpMethod.PUT).header(HttpHeader.CONTENT_TYPE, "application/json").send(); if (httpResponse.getStatus() == 200) { return true; } } catch (Exception ex) { logger.warn("Caught exception sending registration {}", request.toString(), ex); } return false; }
public String callSynchronous(JsonArray params, OrangeContext orangeContext) throws RpcCallException { HttpClientWrapper clientWrapper = loadBalancer.getHttpClientWrapper(); HttpRequestWrapper balancedPost = clientWrapper.createHttpPost(this); //set custom headers if (orangeContext != null) { orangeContext.getProperties().forEach(balancedPost::setHeader); } balancedPost.setHeader("Content-type", TYPE_JSON); //TODO: fix: Temporary workaround below until go services are more http compliant balancedPost.setHeader("Connection", "close"); JsonRpcRequest jsonRequest = new JsonRpcRequest(null, methodName, params); String json = jsonRequest.toString(); balancedPost.setContentProvider(new StringContentProvider(json)); logger.debug("Sending request of size {}", json.length()); ContentResponse rpcResponse = clientWrapper.execute(balancedPost, new JsonRpcCallExceptionDecoder(), orangeContext); String rawResponse = rpcResponse.getContentAsString(); logger.debug("Json response from the service: {}", rawResponse); return JsonRpcResponse.fromString(rawResponse).getResult().getAsString(); }
protected String sendRequest(String path, String data, HttpMethod method) throws Exception { String url = getServiceUrl(path); Request request = httpClient.newRequest(url).method(method). header(HttpHeader.CONTENT_TYPE, "application/json"); if (data != null) { request.content(new StringContentProvider(data)); } ContentResponse response = request.send(); return response.getContentAsString(); }
public void setRequestContent(String data, String charset) throws UnsupportedEncodingException { StringContentProvider cp = charset != null ? new StringContentProvider(data, charset) : new StringContentProvider(data); this.request.content(cp, this.requestContentType); }
@Override public IRequest setBodyEntity(String content, String encoding, String contentType) { request.content(new StringContentProvider(content, encoding), contentType); return this; }
protected Request getRequestWithoutSessionId(HttpServletRequest clientRequest, Request proxyRequest) throws IOException { String content = IOUtils.toString(clientRequest.getInputStream(), UTF_8); if (!content.isEmpty()) { String remoteHost = getRemoteHost(clientRequest); content = removeSessionIdSafe(content, remoteHost); } return proxyRequest.content( new StringContentProvider(clientRequest.getContentType(), content, UTF_8)); }
ContentResponse contentResponse = client.POST(endpoint) .content(new StringContentProvider("XML envelope")) .cookie(new HttpCookie("JSESSIONID", jSessionIdCookieValue)) .cookie(new HttpCookie("ZM_AUTH_TOKEN", authToken)) .cookie(new HttpCookie("ZM_TEST", "true")) .send();
@Test public void shouldPutEntryWithDefaultTllAndIdleTime() throws Exception { //when ContentResponse response = client .POST(String.format("http://localhost:%d/rest/%s/%s", restServer().getPort(), "expiration", "test")) .content(new StringContentProvider("test")) .send(); InternalCacheEntry<String, byte[]> cacheEntry = getCacheEntry("expiration", "test".getBytes()); Metadata metadata = cacheEntry.getMetadata(); //then ResponseAssertion.assertThat(response).isOk(); Assertions.assertThat(metadata.lifespan()).isEqualTo(DEFAULT_LIFESPAN); Assertions.assertThat(metadata.maxIdle()).isEqualTo(DEFAULT_MAX_IDLE); }
@Test public void shouldPutUnknownFormatValueInCache() throws Exception { //when ContentResponse response = client .POST(String.format("http://localhost:%d/rest/%s/%s", restServer().getPort(), "unknown", "test")) .content(new StringContentProvider("Hey!")) .header("Content-type", "application/unknown") .send(); InternalCacheEntry<String, byte[]> cacheEntry = getCacheEntry("unknown", "test".getBytes()); //then ResponseAssertion.assertThat(response).isOk(); ResponseAssertion.assertThat(response).hasEtag(); Assertions.assertThat(new String(cacheEntry.getValue())).isEqualTo("Hey!"); }
@Test public void shouldPutImmortalEntryWithZeroTtlAndIdleTime() throws Exception { //when ContentResponse response = client .POST(String.format("http://localhost:%d/rest/%s/%s", restServer().getPort(), "expiration", "test")) .content(new StringContentProvider("test")) .header("timeToLiveSeconds", "0") .header("maxIdleTimeSeconds", "0") .send(); InternalCacheEntry<String, byte[]> cacheEntry = getCacheEntry("expiration", "test".getBytes()); Metadata metadata = cacheEntry.getMetadata(); //then ResponseAssertion.assertThat(response).isOk(); Assertions.assertThat(metadata.lifespan()).isEqualTo(DEFAULT_LIFESPAN); Assertions.assertThat(metadata.maxIdle()).isEqualTo(DEFAULT_MAX_IDLE); }
@Test public void shouldPutEntryWithTtlAndIdleTime() throws Exception { //when ContentResponse response = client .POST(String.format("http://localhost:%d/rest/%s/%s", restServer().getPort(), "expiration", "test")) .content(new StringContentProvider("test")) .header("timeToLiveSeconds", "50") .header("maxIdleTimeSeconds", "50") .send(); InternalCacheEntry<String, byte[]> cacheEntry = getCacheEntry("expiration", "test".getBytes()); Metadata metadata = cacheEntry.getMetadata(); //then ResponseAssertion.assertThat(response).isOk(); Assertions.assertThat(metadata.lifespan()).isEqualTo(50 * 1000); Assertions.assertThat(metadata.maxIdle()).isEqualTo(50 * 1000); }
@Test public void shouldUpdateEntryWhenReplacingUsingPut() throws Exception { //given putStringValueInCache("default", "test", "test"); //when ContentResponse response = client .newRequest(String.format("http://localhost:%d/rest/%s/%s", restServer().getPort(), "default", "test")) .content(new StringContentProvider("Hey!")) .header("Content-type", "text/plain;charset=UTF-8") .method(HttpMethod.PUT) .send(); String valueFromCache = new String(getCacheEntry("default", "test".getBytes()).getValue()); //then ResponseAssertion.assertThat(response).isOk(); Assertions.assertThat(valueFromCache).isEqualTo("Hey!"); }
@Test public void shouldPutTextValueInCache() throws Exception { //when ContentResponse response = client .POST(String.format("http://localhost:%d/rest/%s/%s", restServer().getPort(), "default", "test")) .content(new StringContentProvider("Hey!")) .header("Content-type", "text/plain;charset=UTF-8") .send(); InternalCacheEntry<String, byte[]> cacheEntry = getCacheEntry("default", "test".getBytes()); //then ResponseAssertion.assertThat(response).isOk(); ResponseAssertion.assertThat(response).hasEtag(); Assertions.assertThat(new String(cacheEntry.getValue())).isEqualTo("Hey!"); }
protected void registerProtobuf(String protoFileName, String protoFileContents) throws Exception { String protobufMetadataUrl = getProtobufMetadataUrl(protoFileName); ContentResponse response = client .newRequest(protobufMetadataUrl) .content(new StringContentProvider(protoFileContents)) .method(POST) .send(); assertEquals(response.getStatus(), HttpStatus.OK_200); String errorKey = protoFileName.concat(".error"); ContentResponse errorCheck = client.newRequest(getProtobufMetadataUrl(errorKey)).method(GET).send(); assertEquals(errorCheck.getStatus(), HttpStatus.NOT_FOUND_404); }
private ContentResponse writeJsonToCache(String key, String json, String cacheName) throws Exception { return client.newRequest(String.format("http://localhost:%d/rest/%s/%s", restServer().getPort(), cacheName, key)) .content(new StringContentProvider(json)) .header(HttpHeader.CONTENT_TYPE, APPLICATION_JSON_TYPE) .method(HttpMethod.PUT).send(); }
@Test public void shouldConflictWhenTryingToReplaceExistingEntryUsingPost() throws Exception { //given putStringValueInCache("default", "test", "test"); //when ContentResponse response = client .POST(String.format("http://localhost:%d/rest/%s/%s", restServer().getPort(), "default", "test")) .content(new StringContentProvider("Hey!")) .header("Content-type", "text/plain;charset=UTF-8") .send(); //then ResponseAssertion.assertThat(response).isConflicted(); }
public void testRestAccessLog() throws Exception { client.newRequest(String.format("http://localhost:%d/rest/default/key", restServer.getPort())) .content(new StringContentProvider("value")) .header("Content-type", "text/plain; charset=utf-8") .method(HttpMethod.PUT) .send(); restServer.stop(); String logline = logAppender.getLog(0); assertTrue(logline, logline.matches("^127\\.0\\.0\\.1 - \\[\\d+/\\w+/\\d+:\\d+:\\d+:\\d+ [+-]?\\w+\\] \"PUT /rest/default/key HTTP/1\\.1\" 404 \\d+ \\d+ \\d+ Jetty/\\p{Graph}+$")); } }
protected void write(int id, String contents, HttpMethod method, MediaType contentType) throws Exception { ContentResponse response = client .newRequest(String.format("http://localhost:%d/rest/%s/%d", pickServer().getPort(), CACHE_NAME, id)) .method(method) .content(new StringContentProvider(contents)) .header(HttpHeader.CONTENT_TYPE, contentType.toString()) .send(); assertEquals(response.getStatus(), HttpStatus.OK_200); }
private void putInCache(String cacheName, Object key, String keyContentType, String value, String contentType) throws InterruptedException, ExecutionException, TimeoutException { Request request = client .newRequest(String.format("http://localhost:%d/rest/%s/%s", restServer().getPort(), cacheName, key)) .content(new StringContentProvider(value)) .header("Content-type", contentType) .method(HttpMethod.PUT); if (keyContentType != null) request.header("Key-Content-type", keyContentType); ContentResponse response = request.send(); ResponseAssertion.assertThat(response).isOk(); }