in juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/RestRequest.java [1847:2001]
public RestResponse run() throws RestCallException {
if (response != null)
throw new RestCallException(response, null, "run() already called.");
try {
queryData.stream().map(SimpleQuery::new).filter(SimplePart::isValid).forEach(
x -> uriBuilder.addParameter(x.name, x.value)
);
pathData.stream().map(SimplePath::new).forEach(x ->
{
String path = uriBuilder.getPath();
String name = x.name, value = x.value;
String var = "{" + name + "}";
if (path.indexOf(var) == -1 && ! name.equals("/*"))
throw new IllegalStateException("Path variable {" + name + "} was not found in path.");
if (name.equals("/*"))
path = path.replaceAll("\\/\\*$", "/" + value);
else
path = path.replace(var, String.valueOf(value));
uriBuilder.setPath(path);
}
);
HttpEntityEnclosingRequestBase request2 = request instanceof HttpEntityEnclosingRequestBase ? (HttpEntityEnclosingRequestBase)request : null;
request.setURI(uriBuilder.build());
// Pick the serializer if it hasn't been overridden.
HeaderList hl = headerData;
Optional<Header> h = hl.getLast("Content-Type");
String contentType = h.isPresent() ? h.get().getValue() : null;
Serializer serializer = this.serializer;
if (serializer == null)
serializer = client.getMatchingSerializer(contentType);
if (contentType == null && serializer != null)
contentType = serializer.getPrimaryMediaType().toString();
// Pick the parser if it hasn't been overridden.
h = hl.getLast("Accept");
String accept = h.isPresent() ? h.get().getValue() : null;
Parser parser = this.parser;
if (parser == null)
parser = client.getMatchingParser(accept);
if (accept == null && parser != null)
hl.set(Accept.of( parser.getPrimaryMediaType()));
headerData.stream().map(SimpleHeader::new).filter(SimplePart::isValid).forEach(x -> request.addHeader(x));
if (request2 == null && content != NO_BODY)
throw new RestCallException(null, null, "Method does not support content entity. Method={0}, URI={1}", getMethod(), getURI());
if (request2 != null) {
Object input2 = null;
if (content != NO_BODY) {
input2 = content;
} else {
input2 = new UrlEncodedFormEntity(formData.stream().map(SimpleFormData::new).filter(SimplePart::isValid).collect(toList()));
}
if (input2 instanceof Supplier)
input2 = ((Supplier<?>)input2).get();
HttpEntity entity = null;
if (input2 instanceof PartList)
entity = new UrlEncodedFormEntity(((PartList)input2).stream().map(SimpleFormData::new).filter(SimplePart::isValid).collect(toList()));
else if (input2 instanceof HttpResource) {
HttpResource r = (HttpResource)input2;
r.getHeaders().forEach(x -> request.addHeader(x));
entity = (HttpEntity)input2;
}
else if (input2 instanceof HttpEntity) {
if (input2 instanceof SerializedEntity) {
entity = ((SerializedEntity)input2).copyWith(serializer, contentSchema);
} else {
entity = (HttpEntity)input2;
}
}
else if (input2 instanceof Reader)
entity = readerEntity((Reader)input2, getRequestContentType(TEXT_PLAIN));
else if (input2 instanceof InputStream)
entity = streamEntity((InputStream)input2, -1, getRequestContentType(ContentType.APPLICATION_OCTET_STREAM));
else if (serializer != null)
entity = serializedEntity(input2, serializer, contentSchema).setContentType(contentType);
else {
if (client.hasSerializers()) {
if (contentType == null)
throw new RestCallException(null, null, "Content-Type not specified on request. Cannot match correct serializer. Use contentType(String) or mediaType(String) to specify transport language.");
throw new RestCallException(null, null, "No matching serializer for media type ''{0}''", contentType);
}
entity = stringEntity(input2 == null ? "" : BeanContext.DEFAULT.getClassMetaForObject(input2).toString(input2), getRequestContentType(TEXT_PLAIN));
}
request2.setEntity(entity);
}
try {
response = client.createResponse(this, client.run(target, request, context), parser);
} catch (Exception e) {
throw e;
}
if (isDebug() || client.logRequests == DetailLevel.FULL)
response.cacheContent();
for (RestCallInterceptor rci : interceptors)
rci.onConnect(this, response);
client.onCallConnect(this, response);
String method = getMethod();
int sc = response.getStatusCode();
Thrown thrown = response.getHeader("Thrown").asHeader(Thrown.class);
if (thrown.isPresent() && rethrow != null) {
Thrown.Part thrownPart = thrown.asParts().get().get(0);
String className = thrownPart.getClassName();
String message = thrownPart.getMessage();
for (Class<? extends Throwable> t : rethrow) {
if (t.getName().equals(className)) {
ConstructorInfo c = null;
ClassInfo ci = ClassInfo.of(t);
c = ci.getPublicConstructor(x -> x.hasParamTypes(HttpResponse.class));
if (c != null)
throw c.<Throwable>invoke(response);
c = ci.getPublicConstructor(x -> x.hasParamTypes(String.class));
if (c != null)
throw c.<Throwable>invoke(message != null ? message : response.getContent().asString());
c = ci.getPublicConstructor(x -> x.hasParamTypes(String.class,Throwable.class));
if (c != null)
throw c.<Throwable>invoke(message != null ? message : response.getContent().asString(), null);
c = ci.getPublicConstructor(ConstructorInfo::hasNoParams);
if (c != null)
throw c.<Throwable>invoke();
}
}
}
if (errorCodes.test(sc) && ! ignoreErrors) {
throw new RestCallException(response, null, "HTTP method ''{0}'' call to ''{1}'' caused response code ''{2}, {3}''.\nResponse: \n{4}",
method, getURI(), sc, response.getReasonPhrase(), response.getContent().asAbbreviatedString(1000));
}
} catch (RuntimeException | RestCallException e) {
if (response != null)
response.close();
throw e;
} catch (Throwable e) {
if (response != null)
response.close();
throw new RestCallException(response, e, "Call failed.");
}
return this.response;
}