@Override /* Callable */ public String call() throws Exception { return getResponseAsString(); } }
/** * Adds a response pattern finder to look for regular expression matches in the response output. * * <p> * This method can be called multiple times to add multiple response pattern finders. * * <p> * {@link ResponsePattern ResponsePatterns} use the {@link #getCapturedResponse()} to read the response text and so * does not affect the other output methods such as {@link #getResponseAsString()}. * * @param responsePattern The response pattern finder. * @return This object (for method chaining). */ public RestCall responsePattern(final ResponsePattern responsePattern) { captureResponse(); interceptor( new RestCallInterceptor() { @Override public void onClose(RestCall restCall) throws RestCallException { responsePattern.match(RestCall.this); } } ); return this; }
void forceClose() throws RestCallException { softClose = false; close(); }
<T> T getResponseInner(ClassMeta<T> type) throws IOException, ParseException { try { if (response == null) connect(type); softClose(); return (T)response; return (T)getReader(); if (ic.equals(InputStream.class)) return (T)getInputStream(); if (type.isType(ReaderResource.class) || type.isType(StreamResource.class)) { String mediaType = null; return (T)ReaderResource.create().headers(headers).mediaType(mediaType).contents(getReader()).build(); return (T)StreamResource.create().headers(headers).mediaType(mediaType).contents(getInputStream()).build(); connect(type); Header h = response.getFirstHeader("Content-Type"); int sc = response.getStatusLine().getStatusCode(); return type.getStringTransform().transform(getResponseAsString()); try (Closeable in = parser.isReaderParser() ? getReader() : getInputStream()) { ParserSessionArgs pArgs = new ParserSessionArgs(this.getProperties(), null, response.getLocale(), null, mt, responseBodySchema, false, null); return parser.createSession(pArgs).parse(in, type); return type.getReaderTransform().transform(getReader());
boolean isMulti = isEmpty(name) || "*".equals(name) || value instanceof NameValuePairs; if (! isMulti) { if (canAdd(value, schema, skipIfEmpty)) formData.add(new SerializedNameValuePair(name, value, serializer, schema)); } else if (value instanceof NameValuePairs) { String v = p.getValue(); HttpPartSchema s = schema.getProperty(n); if (canAdd(v, s, skipIfEmpty)) formData.add(p); Object v = p.getValue(); HttpPartSchema s = schema.getProperty(n); if (canAdd(v, s, skipIfEmpty)) formData(n, v, skipIfEmpty, serializer, s); } else if (isBean(value)) { return formData(name, toBeanMap(value), skipIfEmpty, serializer, schema); } else if (value instanceof Reader || value instanceof InputStream) { contentType("application/x-www-form-urlencoded"); body(value); } else if (value instanceof CharSequence) { try { contentType("application/x-www-form-urlencoded"); body(new StringEntity(value.toString())); } catch (UnsupportedEncodingException e) {} } else {
connect(); try { StatusLine status = response.getStatusLine(); int sc = status.getStatusCode(); if (sc >= 400 && ! ignoreErrors) throw new RestCallException(sc, status.getReasonPhrase(), request.getMethod(), request.getURI(), getResponseAsString()).setHttpResponse(response); if (outputStreams.size() > 0 || writers.size() > 0) getReader(); return sc; } catch (RestCallException e) { throw new RestCallException(e).setHttpResponse(response); } finally { close();
@Override /* InvocationHandler */ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { RemoteInterfaceMethod rim = rm.getMethodMeta(method); if (rim == null) throw new RuntimeException("Method is not exposed as a remote method."); String url = rim.getUrl(); try (RestCall rc = doCall("POST", url, true)) { rc.serializer(serializer).parser(parser).body(args); Object v = rc.getResponse(method.getGenericReturnType()); if (v == null && method.getReturnType().isPrimitive()) v = ClassUtils.getPrimitiveDefault(method.getReturnType()); return v; } catch (RestCallException e) { // Try to throw original exception if possible. e.throwServerException(interfaceClass.getClassLoader(), method.getExceptionTypes()); throw new RuntimeException(e); } catch (Exception e) { throw new RuntimeException(e); } } });
boolean isMulti = isEmpty(name) || "*".equals(name) || value instanceof NameValuePairs; if (! isMulti) { if (canAdd(value, schema, skipIfEmpty)) try { request.setHeader(name, serializer.serialize(HEADER, schema, value)); String v = p.getValue(); HttpPartSchema s = schema.getProperty(n); if (canAdd(v, s, skipIfEmpty)) header(n, v, skipIfEmpty, serializer, s); Object v = p.getValue(); HttpPartSchema s = schema.getProperty(n); if (canAdd(v, s, skipIfEmpty)) header(n, v, skipIfEmpty, serializer, s); } else if (isBean(value)) { return header(name, toBeanMap(value), skipIfEmpty, serializer, schema); } else { throw new RestCallException("Invalid name ''{0}'' passed to header(name,value,skipIfEmpty) for data type ''{1}''", name, getReadableClassNameForObject(value));
boolean isMulti = isEmpty(name) || "*".equals(name) || value instanceof NameValuePairs; if (! isMulti) { if (canAdd(value, schema, skipIfEmpty)) try { uriBuilder.addParameter(name, serializer.serialize(QUERY, schema, value)); String v = p.getValue(); HttpPartSchema s = schema.getProperty(n); if (canAdd(v, s, skipIfEmpty)) query(n, v, skipIfEmpty, serializer, s); Object v = p.getValue(); HttpPartSchema s = schema.getProperty(n); if (canAdd(v, s, skipIfEmpty)) query(n, v, skipIfEmpty, serializer, s); } else if (isBean(value)) { return query(name, toBeanMap(value), skipIfEmpty, serializer, schema); } else if (value instanceof Reader || value instanceof InputStream) { try {
/** * Connects to the remote resource (if {@code connect()} hasn't already been called) and returns the value of * an HTTP header on the response. * * <p> * Useful if you're only interested in a particular header value from the response and not the body of the response. * * <p> * The response entity is discarded unless one of the pipe methods have been specified to pipe the output to an * output stream or writer. * * @param name The header name. * @return The response header as a string, or <jk>null</jk> if the header was not found. * @throws RestCallException If an exception or non-200 response code occurred during the connection attempt. * @throws IOException If an exception occurred while streaming was already occurring. */ public String getResponseHeader(String name) throws IOException { try { HttpResponse r = getResponse(); Header h = r.getFirstHeader(name); return h == null ? null : h.getValue(); } catch (IOException e) { isFailed = true; close(); throw e; } }
/** * Adds form data pairs to this request to perform a URL-encoded form post. * * @param nameValuePairs The name-value pairs of the request. * @return This object (for method chaining). * @throws RestCallException */ public RestCall formData(NameValuePairs nameValuePairs) throws RestCallException { return formData(null, nameValuePairs); }
@Override public CompanySummary enrichCompany(EnrichCompanyRequest request) { try { RestCall call = restClient .doPost(baseUrl() + "company.enrich") .input(request); String responseJson = call.getResponseAsString(); CompanySummary result = parser.parse(responseJson, CompanySummary.class); return result; } catch( Exception e ) { LOGGER.error("Exception", e); return new CompanySummary(); } finally { Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS); } }
/** * Perform a <code>PUT</code> request against the specified URL. * * @param url * The URL of the remote REST resource. * Can be any of the following: {@link String}, {@link URI}, {@link URL}. * @param o * The object to serialize and transmit to the URL as the body of the request. * Can be of the following types: * <ul class='spaced-list'> * <li> * {@link Reader} - Raw contents of {@code Reader} will be serialized to remote resource. * <li> * {@link InputStream} - Raw contents of {@code InputStream} will be serialized to remote resource. * <li> * {@link Object} - POJO to be converted to text using the {@link Serializer} registered with the * {@link RestClient}. * <li> * {@link HttpEntity} - Bypass Juneau serialization and pass HttpEntity directly to HttpClient. * </ul> * @return * A {@link RestCall} object that can be further tailored before executing the request * and getting the response as a parsed object. * @throws RestCallException If any authentication errors occurred. */ public RestCall doPut(Object url, Object o) throws RestCallException { return doCall("PUT", url, true).body(o); }
/** * Connects to the remote resource (if {@code connect()} hasn't already been called) and returns the HTTP response * message body as plain text. * * <p> * The response entity is discarded unless one of the pipe methods have been specified to pipe the output to an * output stream or writer. * * @return The response as a string. * @throws RestCallException If an exception or non-200 response code occurred during the connection attempt. * @throws IOException If an exception occurred while streaming was already occurring. */ public String getResponseAsString() throws IOException { try (Reader r = getReader()) { return read(r).toString(); } catch (IOException e) { isFailed = true; close(); throw e; } }
@Override public boolean streamAbort(InputStream wrapped) throws IOException { RestCall.this.forceClose(); return true; } });
/** * Returns access to the {@link HttpResponse} returned by {@link HttpClient#execute(HttpUriRequest)}. * * <p> * Returns <jk>null</jk> if {@link #connect()} has not yet been called. * * @return The HTTP response object. * @throws IOException */ public HttpResponse getResponse() throws IOException { connect(); return response; }
@Override /* RestCallInterceptor */ public void onInit(RestCall restCall) { if (log.isLoggable(level)) restCall.captureResponse(); }
boolean isMulti = isEmpty(name) || "*".equals(name) || value instanceof NameValuePairs; if (! isMulti) { if (canAdd(value, schema, skipIfEmpty)) formData.add(new SerializedNameValuePair(name, value, serializer, schema)); } else if (value instanceof NameValuePairs) { String v = p.getValue(); HttpPartSchema s = schema.getProperty(n); if (canAdd(v, s, skipIfEmpty)) formData.add(p); Object v = p.getValue(); HttpPartSchema s = schema.getProperty(n); if (canAdd(v, s, skipIfEmpty)) formData(n, v, skipIfEmpty, serializer, s); } else if (isBean(value)) { return formData(name, toBeanMap(value), skipIfEmpty, serializer, schema); } else if (value instanceof Reader || value instanceof InputStream) { contentType("application/x-www-form-urlencoded"); body(value); } else if (value instanceof CharSequence) { try { contentType("application/x-www-form-urlencoded"); body(new StringEntity(value.toString())); } catch (UnsupportedEncodingException e) {} } else {