use of com.github.davidmoten.odata.client.HttpResponse in project odata-client by davidmoten.
the class DefaultHttpService method getResponsePatchOverride.
private HttpResponse getResponsePatchOverride(String url, List<RequestHeader> requestHeaders, InputStream content, int length) {
List<RequestHeader> list = Lists.newArrayList(requestHeaders);
list.add(new RequestHeader("X-HTTP-Method-Override", "PATCH"));
HttpResponse result = getResponse(url, list, HttpMethod.POST, true, content, length);
// only indicate patch not supported if the result is returned ok
patchSupported = false;
return result;
}
use of com.github.davidmoten.odata.client.HttpResponse in project odata-client by davidmoten.
the class DefaultHttpService method getResponse.
private HttpResponse getResponse(String url, List<RequestHeader> requestHeaders, HttpMethod method, boolean doInput, InputStream content, int length) {
try {
URL u = new URL(url);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setInstanceFollowRedirects(true);
c.setRequestMethod(method.toString());
for (RequestHeader header : requestHeadersModifier.apply(requestHeaders)) {
c.setRequestProperty(header.name(), header.value());
}
if (length != HttpService.LENGTH_UNKNOWN) {
c.setRequestProperty("Content-Length", length + "");
}
c.setDoInput(doInput);
c.setDoOutput(content != null);
// apply just before connection established so further configuration can take
// place like timeouts
consumer.accept(c);
if (content != null) {
try (OutputStream out = c.getOutputStream()) {
byte[] b = new byte[8192];
int len;
while ((len = content.read(b)) != -1) {
out.write(b, 0, len);
}
}
}
final byte[] bytes;
if (doInput) {
bytes = Util.read(c.getInputStream());
} else {
bytes = null;
}
return new HttpResponse(c.getResponseCode(), bytes);
} catch (ProtocolException e) {
throw new ProtocolRuntimeException(e);
} catch (IOException e) {
throw new ClientException(e);
}
}
use of com.github.davidmoten.odata.client.HttpResponse in project odata-client by davidmoten.
the class RequestHelper method getWithParametricType.
public static <T, S> T getWithParametricType(ContextPath contextPath, Class<T> cls, Class<S> parametricTypeClass, RequestOptions options) {
// build the url
ContextPath cp = contextPath.addQueries(options.getQueries());
List<RequestHeader> h = cleanAndSupplementRequestHeaders(options, "minimal", false);
// get the response
HttpResponse response = cp.context().service().get(cp.toUrl(), h, options);
checkResponseCode(cp, response, HttpURLConnection.HTTP_OK);
// deserialize
String text = response.getText();
Class<? extends T> c = getSubClass(cp, contextPath.context().schemas(), cls, text);
// FileAttachment which is a subclass of Attachment)
return cp.context().serializer().deserializeWithParametricType(text, c, parametricTypeClass, contextPath, false);
}
use of com.github.davidmoten.odata.client.HttpResponse in project odata-client by davidmoten.
the class RequestHelper method sendChunk.
public static void sendChunk(HttpMethod method, HttpService service, String url, InputStream in, List<RequestHeader> requestHeaders, long startByte, long finishByte, long size, HttpRequestOptions options) {
List<RequestHeader> h = new ArrayList<RequestHeader>(requestHeaders);
h.add(RequestHeader.create("Content-Range", "bytes " + startByte + "-" + (finishByte - 1) + "/" + size));
HttpResponse response = service.put(url, h, in, (int) (finishByte - startByte), options);
checkResponseCode(url, response, 200, 204);
}
use of com.github.davidmoten.odata.client.HttpResponse in project odata-client by davidmoten.
the class RequestHelper method submitAny.
public static <T> T submitAny(HttpMethod method, Object object, ContextPath contextPath, Class<T> responseClass, RequestOptions options) {
// build the url
ContextPath cp = contextPath.addQueries(options.getQueries());
String json;
if (object == null) {
json = "";
} else {
json = Serializer.INSTANCE.serialize(object);
}
List<RequestHeader> h = cleanAndSupplementRequestHeaders(options, "minimal", true);
// get the response
HttpResponse response = cp.context().service().post(cp.toUrl(), h, json, options);
// TODO could be tightened to 201 for POST create but POST Action calls need to
// accept any successful code
checkResponseCodeOk(cp, response);
String text = response.getText();
// deserialize
Class<? extends T> c = getSubClass(cp, contextPath.context().schemas(), responseClass, text);
// FileAttachment which is a subclass of Attachment)
return cp.context().serializer().deserialize(text, c, contextPath, false);
}
Aggregations