public <T> ClientResponse<T> put(Class<T> returnType, Type genericType) throws Exception { BaseClientResponse response = (BaseClientResponse) put(); response.setReturnType(returnType); response.setGenericReturnType(genericType); return response; }
public <T> T postTarget(Class<T> returnType) throws Exception { BaseClientResponse<T> response = (BaseClientResponse<T>) post(returnType); if (response.getStatus() == 204) return null; if (response.getStatus() != 200) throw new ClientResponseFailure(response); T obj = response.getEntity(); if (obj instanceof InputStream) { response.setWasReleased(true); } return obj; }
private BaseClientResponse createClientResponse(ClientRequest request, BrowserCache.Entry entry) { BaseClientResponse response = new BaseClientResponse(new CachedStreamFactory(entry)); response.setStatus(200); response.setHeaders(entry.getHeaders()); response.setProviderFactory(request.getProviderFactory()); return response; }
@Override public <T2> T2 getEntity(Class<T2> type, Type genericType) { return getEntity(type, genericType, getAnnotations(type, genericType)); }
public Object extractEntity(ClientRequestContext context, Object... args) { EntityTypeFactory factory = null; try { factory = entityTypeFactory.newInstance(); } catch (InstantiationException e) { throw context.getClientResponse().createResponseFailure(Messages.MESSAGES.couldNotCreateEntityFactory(entityTypeFactory.getClass().getName())); } catch (IllegalAccessException e) { throw ((BaseClientResponse<?>)context.getClientResponse()) .createResponseFailure(Messages.MESSAGES.couldNotCreateEntityFactoryMessage(entityTypeFactory.getClass().getName(), e.getMessage())); } context.getClientResponse().setReturnType( factory.getEntityType(((BaseClientResponse<?>) context.getClientResponse()).getStatus(), ((BaseClientResponse<?>) context.getClientResponse()).getMetadata())); return context.getClientResponse(); } };
public <T> ClientResponse<T> options(Class<T> returnType) throws Exception { BaseClientResponse response = (BaseClientResponse) options(); response.setReturnType(returnType); return response; }
clientResponse.setAttributeExceptionsTo(_method.toString()); clientResponse.setAnnotations(_method.getAnnotations()); ClientRequestContext clientRequestContext = new ClientRequestContext(request, clientResponse, errorHandler, _extractorFactory, _baseUri); Object response = _extractor.extractEntity(clientRequestContext); restResponse.setHeaders(clientResponse.getHeaders()); restResponse.setStatusCode(clientResponse.getStatus()); if (LOGGER.isTraceEnabled()) { LOGGER.trace("Incoming Headers to SwitchYard through OutboundHandler ["); RESTEasyProxy.traceLog(LOGGER, clientResponse.getHeaders()); LOGGER.trace("]");
try response.checkFailureStatus(); Object obj = response.getEntity(method.getReturnType(), method.getGenericReturnType()); if (obj instanceof InputStream) response.setWasReleased(true); response.releaseConnection();
public ClientResponse cacheIfPossible(ClientRequest request, BaseClientResponse response) throws Exception { String cc = (String) response.getResponseHeaders().getFirst(HttpHeaders.CACHE_CONTROL); String exp = (String) response.getResponseHeaders().getFirst(HttpHeaders.EXPIRES); int expires = -1; if (cc != null) { CacheControl cacheControl = CacheControl.valueOf(cc); if (cacheControl.isNoCache()) return response; expires = cacheControl.getMaxAge(); } else if (exp != null) { Date date = DateUtil.parseDate(exp); expires = (int) ((date.getTime() - System.currentTimeMillis()) / 1000); } String lastModified = (String) response.getResponseHeaders().getFirst(HttpHeaders.LAST_MODIFIED); String etag = (String) response.getResponseHeaders().getFirst(HttpHeaders.ETAG); String contentType = (String) response.getResponseHeaders().getFirst(HttpHeaders.CONTENT_TYPE); byte[] cached = ReadFromStream.readFromStream(1024, response.getStreamFactory().getInputStream()); response.getStreamFactory().performReleaseConnection(); MediaType mediaType = MediaType.valueOf(contentType); final BrowserCache.Entry entry = cache.put(request.getUri(), mediaType, response.getResponseHeaders(), cached, expires, etag, lastModified); response.setStreamFactory(new CachedStreamFactory(entry)); return response; }
@Override public <T2> T2 getEntity(Class<T2> type) { return getEntity(type, null); }
@SuppressWarnings("unchecked") public void clientErrorHandling(BaseClientResponse clientResponse, RuntimeException e) { for (ClientErrorInterceptor handler : interceptors) { try { // attempt to reset the stream in order to provide a fresh stream // to each ClientErrorInterceptor -- failing to reset the stream // could mean that an unusable stream will be passed to the // interceptor InputStream stream = clientResponse.getStreamFactory().getInputStream(); if (stream != null) { stream.reset(); } } catch (IOException e1) { // eat this exception since it's not really relevant for the client // response } handler.handle(clientResponse); } throw e; } }
String fileServiceUrl = "http://localhost:8081/RESTfulDemoApplication/files"; RestEasyFileServiceRestfulClient fileServiceClient = ProxyFactory.create(RestEasyFileServiceRestfulClient.class,fileServiceUrl); BaseClientResponse response = (BaseClientResponse)fileServiceClient.getFile("SpringAnnontationsCheatSheet.pdf"); File s = (File)response.getEntity(File.class); File ff = new File("C:\\RestFileUploadTest\\SpringAnnontationsCheatSheet_Downloaded.pdf"); s.renameTo(ff); FileWriter fr = new FileWriter(s); fr.flush(); System.out.println("FileDownload Response = "+ response.getStatus());
@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 }
/** * Automatically does POST/Create pattern. Will throw a ClientResponseFailure * if status is something other than 201 * * @return Link to created resource * @throws Exception, ClientResponseFailure */ public Link create() throws Exception, ClientResponseFailure { BaseClientResponse response = (BaseClientResponse) post(); if (response.getStatus() != 201) throw new ClientResponseFailure(response); return response.getLocationLink(); }
clientResponse.setAttributeExceptionsTo(method.toString()); clientResponse.setAnnotations(method.getAnnotations()); ClientRequestContext clientRequestContext = new ClientRequestContext(request, clientResponse, errorHandler, extractorFactory, baseUri); return extractor.extractEntity(clientRequestContext);
public Integer extractEntity(ClientRequestContext context, Object... args) { return context.getClientResponse().getStatus(); } };
@SuppressWarnings("deprecation") public static <T> Response copyResponseAndClose(Response source, Class<T> type) { int status = -1; Headers<Object> headers = new Headers<Object>(); Object entity = null; try { status = source.getStatus(); if (source instanceof org.jboss.resteasy.client.core.BaseClientResponse) headers.putAll(((org.jboss.resteasy.client.core.BaseClientResponse<?>)source).getMetadata()); if (source instanceof org.jboss.resteasy.client.ClientResponse) { entity = ((org.jboss.resteasy.client.ClientResponse<?>)source).getEntity(type); } else { entity = source.getEntity(); } return new BuiltResponsePreservingError(status, headers, entity, new Annotation[0], null); } catch (Exception e) { Exceptions.propagateIfFatal(e); return new BuiltResponsePreservingError(status, headers, entity, new Annotation[0], e); } finally { if (source instanceof org.jboss.resteasy.client.core.BaseClientResponse) ((org.jboss.resteasy.client.core.BaseClientResponse<?>)source).close(); } }
protected void setHeaders(final MockHttpResponse mockResponse, BaseClientResponse response) { MultivaluedMapImpl<String, String> responseHeaders = new MultivaluedMapImpl<String, String>(); for (Entry<String, List<Object>> entry : mockResponse.getOutputHeaders().entrySet()) { List<String> values = new ArrayList<String>(entry.getValue().size()); for (Object value : entry.getValue()) { values.add(value.toString()); } responseHeaders.addMultiple(entry.getKey(), values); } response.setHeaders(responseHeaders); }
BaseClientResponse tmp = new BaseClientResponse(new BaseClientResponse.BaseClientResponseStreamFactory() BaseClientResponse tmp = new BaseClientResponse(new BaseClientResponse.BaseClientResponseStreamFactory()
public Object extractEntity(ClientRequestContext context, Object... args) { EntityTypeFactory factory = null; try { factory = entityTypeFactory.newInstance(); } catch (InstantiationException e) { throw context.getClientResponse().createResponseFailure(Messages.MESSAGES.couldNotCreateEntityFactory(entityTypeFactory.getClass().getName())); } catch (IllegalAccessException e) { throw ((BaseClientResponse<?>)context.getClientResponse()) .createResponseFailure(Messages.MESSAGES.couldNotCreateEntityFactoryMessage(entityTypeFactory.getClass().getName(), e.getMessage())); } context.getClientResponse().setReturnType( factory.getEntityType(((BaseClientResponse<?>) context.getClientResponse()).getStatus(), ((BaseClientResponse<?>) context.getClientResponse()).getMetadata())); return context.getClientResponse(); } };