All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.katharsis.dispatcher.controller.resource.ResourcePost Maven / Gradle / Ivy

There is a newer version: 3.0.2
Show newest version
package io.katharsis.dispatcher.controller.resource;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.katharsis.dispatcher.controller.HttpMethod;
import io.katharsis.queryParams.QueryParams;
import io.katharsis.repository.RepositoryMethodParameterProvider;
import io.katharsis.repository.ResourceRepository;
import io.katharsis.request.dto.DataBody;
import io.katharsis.request.dto.RequestBody;
import io.katharsis.request.path.JsonPath;
import io.katharsis.request.path.ResourcePath;
import io.katharsis.resource.exception.RequestBodyException;
import io.katharsis.resource.exception.RequestBodyNotFoundException;
import io.katharsis.resource.exception.ResourceNotFoundException;
import io.katharsis.resource.registry.RegistryEntry;
import io.katharsis.resource.registry.ResourceRegistry;
import io.katharsis.response.HttpStatus;
import io.katharsis.response.LinksInformation;
import io.katharsis.response.MetaInformation;
import io.katharsis.response.ResourceResponse;
import io.katharsis.utils.PropertyUtils;
import io.katharsis.utils.parser.TypeParser;

import java.io.IOException;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.util.Collections;

public class ResourcePost extends ResourceUpsert {

    public ResourcePost(ResourceRegistry resourceRegistry, TypeParser typeParser, ObjectMapper objectMapper) {
        super(resourceRegistry, typeParser, objectMapper);
    }

    /**
     * {@inheritDoc}
     * 

* Check if it is a POST request for a resource. */ @Override public boolean isAcceptable(JsonPath jsonPath, String requestType) { return jsonPath.isCollection() && jsonPath instanceof ResourcePath && HttpMethod.POST.name().equals(requestType); } @Override public ResourceResponse handle(JsonPath jsonPath, QueryParams queryParams, RepositoryMethodParameterProvider parameterProvider, RequestBody requestBody) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException, IOException { String resourceEndpointName = jsonPath.getResourceName(); RegistryEntry endpointRegistryEntry = resourceRegistry.getEntry(resourceEndpointName); if (endpointRegistryEntry == null) { throw new ResourceNotFoundException(resourceEndpointName); } if (requestBody == null) { throw new RequestBodyNotFoundException(HttpMethod.POST, resourceEndpointName); } if (requestBody.isMultiple()) { throw new RequestBodyException(HttpMethod.POST, resourceEndpointName, "Multiple data in body"); } DataBody dataBody = requestBody.getSingleData(); if (dataBody == null) { throw new RequestBodyException(HttpMethod.POST, resourceEndpointName, "No data field in the body."); } RegistryEntry bodyRegistryEntry = resourceRegistry.getEntry(dataBody.getType()); verifyTypes(HttpMethod.POST, resourceEndpointName, endpointRegistryEntry, bodyRegistryEntry); Object newResource = bodyRegistryEntry.getResourceInformation().getResourceClass().newInstance(); setId(dataBody, newResource, bodyRegistryEntry.getResourceInformation()); setAttributes(dataBody, newResource, bodyRegistryEntry.getResourceInformation()); ResourceRepository resourceRepository = endpointRegistryEntry.getResourceRepository(parameterProvider); setRelations(newResource, bodyRegistryEntry, dataBody, queryParams, parameterProvider); Object savedResource = resourceRepository.save(newResource); Serializable resourceId = (Serializable) PropertyUtils .getProperty(savedResource, bodyRegistryEntry.getResourceInformation().getIdField().getName()); @SuppressWarnings("unchecked") Object savedResourceWithRelations = resourceRepository.findOne(resourceId, queryParams); MetaInformation metaInformation = getMetaInformation(resourceRepository, Collections.singletonList(savedResourceWithRelations), queryParams); LinksInformation linksInformation = getLinksInformation(resourceRepository, Collections.singletonList(savedResourceWithRelations), queryParams); return new ResourceResponse(savedResourceWithRelations, jsonPath, queryParams, metaInformation, linksInformation, HttpStatus.CREATED_201); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy