Search in sources :

Example 6 with HttpResponse

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;
}
Also used : RequestHeader(com.github.davidmoten.odata.client.RequestHeader) HttpResponse(com.github.davidmoten.odata.client.HttpResponse)

Example 7 with HttpResponse

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);
    }
}
Also used : ProtocolException(java.net.ProtocolException) HttpURLConnection(java.net.HttpURLConnection) OutputStream(java.io.OutputStream) RequestHeader(com.github.davidmoten.odata.client.RequestHeader) HttpResponse(com.github.davidmoten.odata.client.HttpResponse) IOException(java.io.IOException) ClientException(com.github.davidmoten.odata.client.ClientException) URL(java.net.URL)

Example 8 with HttpResponse

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);
}
Also used : ContextPath(com.github.davidmoten.odata.client.ContextPath) RequestHeader(com.github.davidmoten.odata.client.RequestHeader) HttpResponse(com.github.davidmoten.odata.client.HttpResponse)

Example 9 with HttpResponse

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);
}
Also used : ArrayList(java.util.ArrayList) RequestHeader(com.github.davidmoten.odata.client.RequestHeader) HttpResponse(com.github.davidmoten.odata.client.HttpResponse)

Example 10 with HttpResponse

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);
}
Also used : ContextPath(com.github.davidmoten.odata.client.ContextPath) RequestHeader(com.github.davidmoten.odata.client.RequestHeader) HttpResponse(com.github.davidmoten.odata.client.HttpResponse)

Aggregations

HttpResponse (com.github.davidmoten.odata.client.HttpResponse)13 RequestHeader (com.github.davidmoten.odata.client.RequestHeader)12 ContextPath (com.github.davidmoten.odata.client.ContextPath)7 ClientException (com.github.davidmoten.odata.client.ClientException)3 HttpService (com.github.davidmoten.odata.client.HttpService)3 IOException (java.io.IOException)2 URL (java.net.URL)2 ArrayList (java.util.ArrayList)2 OutputStream (java.io.OutputStream)1 HttpURLConnection (java.net.HttpURLConnection)1 MalformedURLException (java.net.MalformedURLException)1 ProtocolException (java.net.ProtocolException)1 List (java.util.List)1 GraphService (odata.msgraph.client.container.GraphService)1 HttpEntity (org.apache.http.HttpEntity)1 HttpEntityEnclosingRequest (org.apache.http.HttpEntityEnclosingRequest)1 RequestConfig (org.apache.http.client.config.RequestConfig)1 Builder (org.apache.http.client.config.RequestConfig.Builder)1 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)1 InputStreamEntity (org.apache.http.entity.InputStreamEntity)1