public ClientRequest cookie(Cookie cookie) { return header(HttpHeaders.COOKIE, cookie); }
public ClientResponse<String> post(String url, String contentType, Object data) throws Exception { ClientRequest request = new ClientRequest(url); request.body(contentType, data); ClientResponse response = request.post(); return response; }
public ClientResponse<String> get(String url, MediaType mediaType) throws Exception { ClientRequest request = new ClientRequest(url); request.accept(mediaType); ClientResponse<String> response = request.get(String.class); return response; }
public ClientResponse<String> delete(String url, MediaType mediaType) throws Exception { ClientRequest request = new ClientRequest(url); request.accept(mediaType); ClientResponse response = request.delete(); return response; }
public ClientResponse<String> put(String url, String contentType, Object data) throws Exception { ClientRequest request = new ClientRequest(url); request.body(contentType, data); ClientResponse response = request.put(); return response; }
public void build(ClientRequest request, Object object) { request.body(mediaType, object, type, genericType, annotations); }
protected void initClientRequest() { if (this.executor == null) { this.clientRequest = new ClientRequest(getUrl()); } else { this.clientRequest = new ClientRequest(getUrl(), this.executor); } for (Cookie cookie : cookies) { clientRequest.cookie(cookie); } for (Map.Entry<String, String> headerEntry : headers.entrySet()) { clientRequest.header(headerEntry.getKey(), headerEntry.getValue()); } }
public ClientRequest request() { if (executor != null) { return new ClientRequest(href, executor); } else { return new ClientRequest(href); } }
public ClientResponse execute(ClientExecutionContext ctx) throws Exception { String encoding = ctx.getRequest().getHeaders().getFirst(HttpHeaders.ACCEPT_ENCODING); if (encoding == null) { ctx.getRequest().header(HttpHeaders.ACCEPT_ENCODING, "gzip, deflate"); } else { if (!encoding.contains("gzip")) { encoding += ", gzip"; ctx.getRequest().header(HttpHeaders.ACCEPT_ENCODING, encoding); } } return ctx.proceed(); } }
protected HttpURLConnection createConnection(ClientRequest request) throws Exception { String uri = request.getUri(); String httpMethod = request.getHttpMethod(); HttpURLConnection connection = (HttpURLConnection) new URL(uri).openConnection(); connection.setRequestMethod(httpMethod); return connection; }
@Override public ClientResponse execute(final ClientExecutionContext ctx) throws Exception { final ClientRequest request = ctx.getRequest(); // ETags can only be used on GET requests, so if it's anything else proceed normally if (!request.getHttpMethod().equals(HttpMethod.GET)) { return ctx.proceed(); } // Load the etag for the request from the cache final String etag = getETag(request); if (etag != null) { request.header(HttpHeaders.IF_NONE_MATCH, etag); } // Get the response and save the etag value final ClientResponse response = ctx.proceed(); cacheIfPossible(request, response); return response; }
@Ignore("TODO") @Test public void follow_get() throws Exception { final LinkRepresentation link = jsonRepresentation.getLink("aLink"); final String href = link.getHref(); // when context.checking(new Expectations() { { one(mockExecutor).createRequest(with(any(UriBuilderImpl.class))); will(returnValue(mockClientRequest)); one(mockExecutor).execute(mockClientRequest); will(returnValue(mockClientResponse)); one(mockClientRequest).accept(MediaType.APPLICATION_JSON_TYPE); atLeast(1).of(mockClientRequest).setHttpMethod("GET"); allowing(mockClientRequest).getHttpMethod(); will(returnValue("GET")); one(mockClientRequest).execute(); will(returnValue(mockClientResponse)); one(mockClientResponse).setReturnType(String.class); allowing(mockClientResponse); final MultiValueMap result = new MultiValueMap(); result.put("Content-Type", "application/json"); allowing(mockClientResponse).getMetadata(); will(returnValue(result)); } }); final RestfulResponse<JsonRepresentation> response = client.follow(link); // then }
public RestEasyClientInProcRequest(ClientRequest clientRequest) { this.clientRequest = clientRequest; try { headers.put("Host", UrlHelper.getRequestHost(new URI(clientRequest.getUri()))); } catch (Exception e) { throw new RuntimeException(e); } if (clientRequest.getBodyContentType() != null) { headers.put("Content-type", clientRequest.getBodyContentType().toString()); } headers.putAll(asMap(clientRequest.getHeaders())); }
String expectedResponseFromJasperServer = "....."; ClientRequest request = mock(ClientRequest.class); ClientResponse<String> response = mock(ClientResponse.class); when(request.get(String.class)).thenReturn(response); when(response.getEntity()).thenReturn(expectedResponseFromJasperServer);
public <T> ClientResponse<T> post(Class<T> returnType) throws Exception { BaseClientResponse response = (BaseClientResponse) post(); response.setReturnType(returnType); return response; }
public <T> T get(String uriTemplate, Class<T> type, Object... params) throws Exception { return createRequest(uriTemplate).followRedirects(true).pathParameters( params).get(type).getEntity(); }
public <T> ClientResponse<T> delete(Class<T> returnType) throws Exception { BaseClientResponse response = (BaseClientResponse) delete(); response.setReturnType(returnType); return response; }
public void commitHeaders(ClientRequest request, HttpRequestBase httpMethod) { MultivaluedMap<String, String> headers = request.getHeaders(); for (Map.Entry<String, List<String>> header : headers.entrySet()) { List<String> values = header.getValue(); for (String value : values) { // System.out.println(String.format("setting %s = %s", header.getKey(), value)); httpMethod.addHeader(header.getKey(), value); } } }
@Override public String getHttpMethod() { return clientRequest.getHttpMethod(); }
@Override public URI getUri() { try { return new URI(clientRequest.getUri()); } catch (Exception e) { throw new RuntimeException(e); } }