protected Resources<?> entitiesToResources(Iterable<Object> entities, PersistentEntityResourceAssembler assembler, Class<?> domainType) { if (!entities.iterator().hasNext()) { List<Object> content = Arrays.<Object> asList(WRAPPERS.emptyCollectionOf(domainType)); return new Resources<Object>(content, getDefaultSelfLink()); } List<Resource<Object>> resources = new ArrayList<Resource<Object>>(); for (Object obj : entities) { resources.add(obj == null ? null : assembler.toResource(obj)); } return new Resources<Resource<Object>>(resources, getDefaultSelfLink()); }
/** * <code>GET /{repository}/{id}</code> - Returns a single entity. * * @param resourceInformation * @param id * @return * @throws HttpRequestMethodNotSupportedException */ @RequestMapping(value = BASE_MAPPING + "/{id}", method = RequestMethod.GET) public ResponseEntity<Resource<?>> getItemResource(RootResourceInformation resourceInformation, @BackendId Serializable id, final PersistentEntityResourceAssembler assembler, @RequestHeader HttpHeaders headers) throws HttpRequestMethodNotSupportedException { return getItemResource(resourceInformation, id).map(it -> { PersistentEntity<?, ?> entity = resourceInformation.getPersistentEntity(); return resourceStatus.getStatusAndHeaders(headers, it, entity).toResponseEntity(// () -> assembler.toFullResource(it)); }).orElseGet(() -> new ResponseEntity<Resource<?>>(HttpStatus.NOT_FOUND)); }
@Override public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { String projectionParameter = webRequest.getParameter(projectionDefinitions.getParameterName()); PersistentEntityProjector projector = new PersistentEntityProjector(projectionDefinitions, projectionFactory, projectionParameter, links.getMappings()); return new PersistentEntityResourceAssembler(entities, projector, links, linkProvider); } }
private Builder wrap(Object instance, Object source) { PersistentEntity<?, ?> entity = entities.getRequiredPersistentEntity(source.getClass()); return PersistentEntityResource.build(instance, entity).// withEmbedded(getEmbeddedResources(source)).// withLink(getSelfLinkFor(source)).// withLink(linkProvider.createSelfLinkFor(source)); }
@Override public PersistentEntityResource toResource(Object instance) { Assert.notNull(instance, "Entity instance must not be null!"); return wrap(projector.projectExcerpt(instance), instance).build(); }
/** * Sets the location header pointing to the resource representing the given instance. Will make sure we properly * expand the URI template potentially created as self link. * * @param headers must not be {@literal null}. * @param assembler must not be {@literal null}. * @param source must not be {@literal null}. */ private void addLocationHeader(HttpHeaders headers, PersistentEntityResourceAssembler assembler, Object source) { String selfLink = assembler.getSelfLinkFor(source).getHref(); headers.setLocation(new UriTemplate(selfLink).expand()); }
private Builder wrap(Object instance, Object source) { PersistentEntity<?, ?> entity = entities.getRequiredPersistentEntity(source.getClass()); return PersistentEntityResource.build(instance, entity).// withEmbedded(getEmbeddedResources(source)).// withLink(getSelfLinkFor(source)).// withLink(linkProvider.createSelfLinkFor(source)); }
/** * Returns the full object as {@link PersistentEntityResource} using the underlying {@link Projector}. * * @param instance must not be {@literal null}. * @return */ public PersistentEntityResource toFullResource(Object instance) { Assert.notNull(instance, "Entity instance must not be null!"); return wrap(projector.project(instance), instance).build(); }
/** * Sets the location header pointing to the resource representing the given instance. Will make sure we properly * expand the URI template potentially created as self link. * * @param headers must not be {@literal null}. * @param assembler must not be {@literal null}. * @param source must not be {@literal null}. */ private void addLocationHeader(HttpHeaders headers, PersistentEntityResourceAssembler assembler, Object source) { String selfLink = assembler.getSelfLinkFor(source).getHref(); headers.setLocation(new UriTemplate(selfLink).expand()); }
@RequestMapping(value = BASE_MAPPING, method = GET) public ResponseEntity<ResourceSupport> followPropertyReference(final RootResourceInformation repoRequest, @BackendId Serializable id, final @PathVariable String property, final PersistentEntityResourceAssembler assembler) throws Exception { HttpHeaders headers = new HttpHeaders(); Function<ReferencedProperty, ResourceSupport> handler = prop -> prop.mapValue(it -> { if (prop.property.isCollectionLike()) { return toResources((Iterable<?>) it, assembler, prop.propertyType, Optional.empty()); } else if (prop.property.isMap()) { Map<Object, Resource<?>> resources = new HashMap<Object, Resource<?>>(); for (Map.Entry<Object, Object> entry : ((Map<Object, Object>) it).entrySet()) { resources.put(entry.getKey(), assembler.toResource(entry.getValue())); } return new Resource<Object>(resources); } else { PersistentEntityResource resource = assembler.toResource(it); headers.set("Content-Location", resource.getId().getHref()); return resource; } }).orElseThrow(() -> new ResourceNotFoundException()); return ControllerUtils.toResponseEntity(HttpStatus.OK, headers, // doWithReferencedProperty(repoRequest, id, property, handler, HttpMethod.GET)); }
/** * Turns the given source into a {@link ResourceSupport} if needed and possible. Uses the given * {@link PersistentEntityResourceAssembler} for the actual conversion. * * @param source can be must not be {@literal null}. * @param assembler must not be {@literal null}. * @param domainType the domain type in case the source is an empty iterable, must not be {@literal null}. * @param baseLink can be {@literal null}. * @return */ protected ResponseEntity<?> toResource(Optional<Object> source, final PersistentEntityResourceAssembler assembler, Class<?> domainType, Optional<Link> baseLink, HttpHeaders headers, RootResourceInformation information) { return source.map(it -> { if (it instanceof Iterable) { return ResponseEntity.ok(toResources((Iterable<?>) it, assembler, domainType, baseLink)); } else if (ClassUtils.isPrimitiveOrWrapper(it.getClass())) { return ResponseEntity.ok(it); } PersistentEntity<?, ?> entity = information.getPersistentEntity(); // Returned value is not of the aggregates type - probably some projection if (!entity.getType().isInstance(it)) { return ResponseEntity.ok(it); } return resourceStatus.getStatusAndHeaders(headers, it, entity).toResponseEntity(// () -> assembler.toFullResource(it)); }).orElseThrow(() -> new ResourceNotFoundException()); }
@Override public PersistentEntityResource toResource(Object instance) { Assert.notNull(instance, "Entity instance must not be null!"); return wrap(projector.projectExcerpt(instance), instance).build(); }
@Override public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { String projectionParameter = webRequest.getParameter(projectionDefinitions.getParameterName()); PersistentEntityProjector projector = new PersistentEntityProjector(projectionDefinitions, projectionFactory, projectionParameter, links.getMappings()); return new PersistentEntityResourceAssembler(entities, projector, links, linkProvider); } }
/** * <code>HEAD /{repository}/{id}</code> * * @param resourceInformation * @param id * @return * @throws HttpRequestMethodNotSupportedException * @since 2.2 */ @RequestMapping(value = BASE_MAPPING + "/{id}", method = RequestMethod.HEAD) public ResponseEntity<?> headForItemResource(RootResourceInformation resourceInformation, @BackendId Serializable id, PersistentEntityResourceAssembler assembler) throws HttpRequestMethodNotSupportedException { return getItemResource(resourceInformation, id).map(it -> { Links links = new Links(assembler.toResource(it).getLinks()); HttpHeaders headers = headersPreparer.prepareHeaders(resourceInformation.getPersistentEntity(), it); headers.add(LINK_HEADER, links.toString()); return new ResponseEntity<Object>(headers, HttpStatus.NO_CONTENT); }).orElseThrow(() -> new ResourceNotFoundException()); }
/** * Triggers the creation of the domain object and renders it into the response if needed. * * @param domainObject * @param invoker * @return */ private ResponseEntity<ResourceSupport> createAndReturn(Object domainObject, RepositoryInvoker invoker, PersistentEntityResourceAssembler assembler, boolean returnBody) { publisher.publishEvent(new BeforeCreateEvent(domainObject)); Object savedObject = invoker.invokeSave(domainObject); publisher.publishEvent(new AfterCreateEvent(savedObject)); Optional<PersistentEntityResource> resource = Optional .ofNullable(returnBody ? assembler.toFullResource(savedObject) : null); HttpHeaders headers = headersPreparer.prepareHeaders(resource); addLocationHeader(headers, assembler, savedObject); return ControllerUtils.toResponseEntity(HttpStatus.CREATED, headers, resource); }
/** * Returns the full object as {@link PersistentEntityResource} using the underlying {@link Projector}. * * @param instance must not be {@literal null}. * @return */ public PersistentEntityResource toFullResource(Object instance) { Assert.notNull(instance, "Entity instance must not be null!"); return wrap(projector.project(instance), instance).build(); }
if (propertyId.equals(accessor1.getIdentifier().toString())) { PersistentEntityResource resource1 = assembler.toResource(obj); headers.set("Content-Location", resource1.getId().getHref()); return resource1; if (propertyId.equals(accessor2.getIdentifier().toString())) { PersistentEntityResource resource2 = assembler.toResource(entry.getValue()); headers.set("Content-Location", resource2.getId().getHref()); return resource2;
/** * Merges the given incoming object into the given domain object. * * @param domainObject * @param invoker * @param httpMethod * @return */ private ResponseEntity<ResourceSupport> saveAndReturn(Object domainObject, RepositoryInvoker invoker, HttpMethod httpMethod, PersistentEntityResourceAssembler assembler, boolean returnBody) { publisher.publishEvent(new BeforeSaveEvent(domainObject)); Object obj = invoker.invokeSave(domainObject); publisher.publishEvent(new AfterSaveEvent(obj)); PersistentEntityResource resource = assembler.toFullResource(obj); HttpHeaders headers = headersPreparer.prepareHeaders(Optional.of(resource)); if (PUT.equals(httpMethod)) { addLocationHeader(headers, assembler, obj); } if (returnBody) { return ControllerUtils.toResponseEntity(HttpStatus.OK, headers, resource); } else { return ControllerUtils.toEmptyResponse(HttpStatus.NO_CONTENT, headers); } }
/** * 데이터 소스의 상세정보를 가져옵니다. 임시 데이터 소스가 존재할 경우 */ @Transactional(readOnly = true) @RequestMapping(value = "/datasources/{dataSourceId}", method = RequestMethod.GET) public ResponseEntity<?> findDataSources(@PathVariable("dataSourceId") String dataSourceId, @RequestParam(value = "includeUnloadedField", required = false) Boolean includeUnloadedField, PersistentEntityResourceAssembler resourceAssembler) { DataSource resultDataSource = dataSourceService.findDataSourceIncludeTemporary(dataSourceId, includeUnloadedField); return ResponseEntity.ok(resourceAssembler.toResource(resultDataSource)); }
protected Object toResource(Object source, PersistentEntityResourceAssembler assembler, Class<?> domainType, Link baseLink) { if (source instanceof Iterable) { return toResources((Iterable<?>) source, assembler, domainType, baseLink); } else if (source == null) { throw new ResourceNotFoundException(); } else if (ClassUtils.isPrimitiveOrWrapper(source.getClass())) { return source; } return assembler.toFullResource(source); }