Search in sources :

Example 1 with HttpAdapterRequest

use of io.crnk.client.http.HttpAdapterRequest in project crnk-framework by crnk-project.

the class OperationsCall method execute.

public void execute() {
    List<Operation> operations = new ArrayList<>();
    for (QueuedOperation queuedOperation : queuedOperations) {
        operations.add(queuedOperation.operation);
    }
    HttpAdapter adapter = client.getCrnk().getHttpAdapter();
    ObjectMapper mapper = client.getCrnk().getObjectMapper();
    try {
        String operationsJson = mapper.writer().writeValueAsString(operations.toArray(new Operation[operations.size()]));
        String url = client.getCrnk().getServiceUrlProvider().getUrl() + "/operations";
        HttpAdapterRequest request = adapter.newRequest(url, HttpMethod.PATCH, operationsJson);
        request.header(HttpHeaders.HTTP_CONTENT_TYPE, OperationsRequestProcessor.JSONPATCH_CONTENT_TYPE);
        request.header(HttpHeaders.HTTP_HEADER_ACCEPT, OperationsRequestProcessor.JSONPATCH_CONTENT_TYPE);
        HttpAdapterResponse response = request.execute();
        int status = response.code();
        if (status != 200) {
            // general issue, status of individual operations is important.
            throw new InternalServerErrorException("patch execution failed with status " + status);
        }
        String json = response.body();
        responses = Arrays.asList(mapper.readValue(json, OperationResponse[].class));
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
}
Also used : HttpAdapterRequest(io.crnk.client.http.HttpAdapterRequest) HttpAdapter(io.crnk.client.http.HttpAdapter) HttpAdapterResponse(io.crnk.client.http.HttpAdapterResponse) ArrayList(java.util.ArrayList) Operation(io.crnk.operations.Operation) IOException(java.io.IOException) InternalServerErrorException(io.crnk.core.exception.InternalServerErrorException) OperationResponse(io.crnk.operations.OperationResponse) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with HttpAdapterRequest

use of io.crnk.client.http.HttpAdapterRequest in project crnk-framework by crnk-project.

the class ClientStubBase method execute.

protected Object execute(String url, ResponseType responseType, HttpMethod method, String requestBody) {
    try {
        HttpAdapter httpAdapter = client.getHttpAdapter();
        HttpAdapterRequest request = httpAdapter.newRequest(url, method, requestBody);
        LOGGER.debug("requesting {} {}", method, url);
        if (requestBody != null) {
            LOGGER.debug("request body: {}", requestBody);
        }
        if (method == HttpMethod.POST || method == HttpMethod.PATCH) {
            request.header("Content-Type", HttpHeaders.JSONAPI_CONTENT_TYPE + "; charset=" + HttpHeaders.DEFAULT_CHARSET);
        }
        request.header("Accept", HttpHeaders.JSONAPI_CONTENT_TYPE);
        HttpAdapterResponse response = request.execute();
        if (!response.isSuccessful()) {
            throw handleError(response);
        }
        String body = response.body();
        LOGGER.debug("response body: {}", body);
        ObjectMapper objectMapper = client.getObjectMapper();
        if (responseType != ResponseType.NONE && Resource.class.equals(resourceClass)) {
            Document document = objectMapper.readValue(body, Document.class);
            return toResourceResponse(document, objectMapper);
        } else if (responseType != ResponseType.NONE) {
            Document document = objectMapper.readValue(body, Document.class);
            ClientDocumentMapper documentMapper = client.getDocumentMapper();
            return documentMapper.fromDocument(document, responseType == ResponseType.RESOURCES);
        }
        return null;
    } catch (IOException e) {
        throw new TransportException(e);
    }
}
Also used : HttpAdapterRequest(io.crnk.client.http.HttpAdapterRequest) HttpAdapter(io.crnk.client.http.HttpAdapter) HttpAdapterResponse(io.crnk.client.http.HttpAdapterResponse) Resource(io.crnk.core.engine.document.Resource) IOException(java.io.IOException) Document(io.crnk.core.engine.document.Document) TransportException(io.crnk.client.TransportException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 HttpAdapter (io.crnk.client.http.HttpAdapter)2 HttpAdapterRequest (io.crnk.client.http.HttpAdapterRequest)2 HttpAdapterResponse (io.crnk.client.http.HttpAdapterResponse)2 IOException (java.io.IOException)2 TransportException (io.crnk.client.TransportException)1 Document (io.crnk.core.engine.document.Document)1 Resource (io.crnk.core.engine.document.Resource)1 InternalServerErrorException (io.crnk.core.exception.InternalServerErrorException)1 Operation (io.crnk.operations.Operation)1 OperationResponse (io.crnk.operations.OperationResponse)1 ArrayList (java.util.ArrayList)1