Search in sources :

Example 1 with HttpAdapter

use of io.crnk.client.http.HttpAdapter 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 HttpAdapter

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

the class QuerySpecClientTest method testUpdate.

public void testUpdate(boolean pushAlways) {
    final List<String> methods = new ArrayList<>();
    final List<String> paths = new ArrayList<>();
    final Interceptor interceptor = new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            methods.add(request.method());
            paths.add(request.url().encodedPath());
            return chain.proceed(request);
        }
    };
    HttpAdapter httpAdapter = client.getHttpAdapter();
    if (httpAdapter instanceof OkHttpAdapter) {
        ((OkHttpAdapter) httpAdapter).addListener(new OkHttpAdapterListener() {

            @Override
            public void onBuild(Builder builder) {
                builder.addInterceptor(interceptor);
            }
        });
    }
    Task task = new Task();
    task.setId(1L);
    task.setName("test");
    taskRepo.create(task);
    Task savedTask = taskRepo.findOne(1L, new QuerySpec(Task.class));
    Assert.assertNotNull(savedTask);
    // perform update
    task.setName("updatedName");
    taskRepo.save(task);
    // check updated
    savedTask = taskRepo.findOne(1L, new QuerySpec(Task.class));
    Assert.assertNotNull(savedTask);
    Assert.assertEquals("updatedName", task.getName());
    if (httpAdapter instanceof OkHttpAdapter) {
        // check HTTP handling
        Assert.assertEquals(4, methods.size());
        Assert.assertEquals(4, paths.size());
        Assert.assertEquals("POST", methods.get(0));
        Assert.assertEquals("GET", methods.get(1));
        if (pushAlways) {
            Assert.assertEquals("POST", methods.get(2));
            Assert.assertEquals("/tasks", paths.get(2));
        } else {
            Assert.assertEquals("PATCH", methods.get(2));
            Assert.assertEquals("/tasks/1", paths.get(2));
        }
        Assert.assertEquals("GET", methods.get(3));
        Assert.assertEquals("/tasks", paths.get(0));
        Assert.assertEquals("/tasks/1", paths.get(1));
        Assert.assertEquals("/tasks/1", paths.get(3));
    }
}
Also used : Task(io.crnk.test.mock.models.Task) HttpAdapter(io.crnk.client.http.HttpAdapter) OkHttpAdapter(io.crnk.client.http.okhttp.OkHttpAdapter) Builder(okhttp3.OkHttpClient.Builder) ArrayList(java.util.ArrayList) Request(okhttp3.Request) OkHttpAdapterListener(io.crnk.client.http.okhttp.OkHttpAdapterListener) OkHttpAdapter(io.crnk.client.http.okhttp.OkHttpAdapter) QuerySpec(io.crnk.core.queryspec.QuerySpec) Interceptor(okhttp3.Interceptor)

Example 3 with HttpAdapter

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

the class ApprovalIntTest method setUp.

@Before
@Override
public void setUp() throws Exception {
    super.setUp();
    client = new CrnkClient(getBaseUri().toString());
    client.getObjectMapper().registerModule(new JavaTimeModule());
    HttpAdapter httpAdapter = client.getHttpAdapter();
    httpAdapter.setReceiveTimeout(10000, TimeUnit.SECONDS);
    scheduleRepo = client.getRepositoryForInterface(ScheduleRepository.class);
    approvalRepo = client.getRepositoryForType(ScheduleApprovalProcessInstance.class);
    processTaskRepo = client.getRepositoryForType(ScheduleApprovalProcessInstance.class, ApproveTask.class);
    taskRepo = client.getRepositoryForType(ApproveTask.class);
    approvalRelRepo = client.getRepositoryForType(Schedule.class, ScheduleApprovalProcessInstance.class);
    formRepo = client.getRepositoryForType(ApproveForm.class);
}
Also used : CrnkClient(io.crnk.client.CrnkClient) HttpAdapter(io.crnk.client.http.HttpAdapter) JavaTimeModule(com.fasterxml.jackson.datatype.jsr310.JavaTimeModule) Schedule(io.crnk.test.mock.models.Schedule) ScheduleRepository(io.crnk.test.mock.repository.ScheduleRepository) ScheduleApprovalProcessInstance(io.crnk.activiti.example.model.ScheduleApprovalProcessInstance) ApproveForm(io.crnk.activiti.example.model.ApproveForm) ApproveTask(io.crnk.activiti.example.model.ApproveTask) Before(org.junit.Before)

Example 4 with HttpAdapter

use of io.crnk.client.http.HttpAdapter 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

HttpAdapter (io.crnk.client.http.HttpAdapter)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 HttpAdapterRequest (io.crnk.client.http.HttpAdapterRequest)2 HttpAdapterResponse (io.crnk.client.http.HttpAdapterResponse)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 JavaTimeModule (com.fasterxml.jackson.datatype.jsr310.JavaTimeModule)1 ApproveForm (io.crnk.activiti.example.model.ApproveForm)1 ApproveTask (io.crnk.activiti.example.model.ApproveTask)1 ScheduleApprovalProcessInstance (io.crnk.activiti.example.model.ScheduleApprovalProcessInstance)1 CrnkClient (io.crnk.client.CrnkClient)1 TransportException (io.crnk.client.TransportException)1 OkHttpAdapter (io.crnk.client.http.okhttp.OkHttpAdapter)1 OkHttpAdapterListener (io.crnk.client.http.okhttp.OkHttpAdapterListener)1 Document (io.crnk.core.engine.document.Document)1 Resource (io.crnk.core.engine.document.Resource)1 InternalServerErrorException (io.crnk.core.exception.InternalServerErrorException)1 QuerySpec (io.crnk.core.queryspec.QuerySpec)1 Operation (io.crnk.operations.Operation)1 OperationResponse (io.crnk.operations.OperationResponse)1