Search in sources :

Example 1 with HttpMethod

use of io.crnk.core.engine.http.HttpMethod in project crnk-framework by crnk-project.

the class ResourceUpsert method canModifyField.

/**
 * Allows to check whether the given field can be written.
 *
 * @param field from the information model or null if is a dynamic field (like JsonAny).
 */
protected boolean canModifyField(ResourceInformation resourceInformation, String fieldName, ResourceField field) {
    if (field == null) {
        return true;
    }
    HttpMethod method = getHttpMethod();
    ResourceFieldAccess access = field.getAccess();
    boolean modifiable = method == HttpMethod.POST ? access.isPostable() : access.isPatchable();
    FilterBehavior filterBehavior = modifiable ? FilterBehavior.NONE : getDefaultFilterBehavior();
    filterBehavior = filterBehavior.merge(resourceFilterDirectory.get(field, method));
    if (filterBehavior == FilterBehavior.NONE) {
        return true;
    } else if (filterBehavior == FilterBehavior.FORBIDDEN) {
        throw new ForbiddenException("field '" + fieldName + "' cannot be modified");
    } else {
        PreconditionUtil.assertEquals("unknown behavior", FilterBehavior.IGNORED, filterBehavior);
        return false;
    }
}
Also used : ForbiddenException(io.crnk.core.exception.ForbiddenException) FilterBehavior(io.crnk.core.engine.filter.FilterBehavior) HttpMethod(io.crnk.core.engine.http.HttpMethod)

Example 2 with HttpMethod

use of io.crnk.core.engine.http.HttpMethod in project crnk-framework by crnk-project.

the class ResourceRepositoryStubImpl method executeUpdate.

private Object executeUpdate(String requestUrl, T resource, boolean create) {
    JsonApiResponse response = new JsonApiResponse();
    response.setEntity(resource);
    ClientDocumentMapper documentMapper = client.getDocumentMapper();
    final Document requestDocument = documentMapper.toDocument(response, null);
    final ObjectMapper objectMapper = client.getObjectMapper();
    String requestBodyValue = ExceptionUtil.wrapCatchedExceptions(new Callable<String>() {

        @Override
        public String call() throws Exception {
            return objectMapper.writeValueAsString(requestDocument);
        }
    });
    HttpMethod method = create || client.getPushAlways() ? HttpMethod.POST : HttpMethod.PATCH;
    return execute(requestUrl, ResponseType.RESOURCE, method, requestBodyValue);
}
Also used : JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) Document(io.crnk.core.engine.document.Document) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HttpMethod(io.crnk.core.engine.http.HttpMethod)

Aggregations

HttpMethod (io.crnk.core.engine.http.HttpMethod)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Document (io.crnk.core.engine.document.Document)1 FilterBehavior (io.crnk.core.engine.filter.FilterBehavior)1 ForbiddenException (io.crnk.core.exception.ForbiddenException)1 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)1