RelationController.java

/*
 * Copyright (c) 2007-2026 MetaSolutions AB
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.entrystore.rest.springboot.controller;

import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.entrystore.Entry;
import org.entrystore.rest.springboot.service.EntryService;
import org.entrystore.rest.springboot.service.RelationService;
import org.entrystore.rest.springboot.util.GraphUtil;
import org.entrystore.rest.springboot.util.HttpUtil;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequiredArgsConstructor
public class RelationController {

	private final EntryService entryService;
	private final RelationService relationService;

	@Operation(summary = "Returns an RDF-graph with an entry's relations with other entries")
	@GetMapping(
			path = "/{context-id}/relations/{entry-id}",
			produces = {
					MediaType.APPLICATION_JSON_VALUE,
					MediaType.APPLICATION_OCTET_STREAM_VALUE,
					"application/rdf+xml", "text/n3", "text/rdf+n3", "text/turtle",
					"application/trix", "application/n-triples", "application/trig",
					"application/ld+json", "application/rdf+json"
			}
	)
	public ResponseEntity<String> getRelations(
			@PathVariable("context-id") String contextId,
			@PathVariable("entry-id") String entryId,
			@RequestParam(required = false) MediaType format,
			@RequestHeader(value = "Accept", defaultValue = GraphUtil.DEFAULT_RDF_MEDIA_TYPE) String acceptHeader
	) {
		String mediaType = GraphUtil.resolveRdfMediaType(format, acceptHeader);

		Entry entry = entryService.getEntryByContextIdAndEntryId(contextId, entryId);
		String relationsGraph = relationService.getEntryRelations(entry, mediaType);

		ResponseEntity.BodyBuilder bodyBuilder = ResponseEntity
				.ok()
				.contentType(MediaType.parseMediaType(mediaType));

		HttpUtil.updateResponseWithModificationDateAndETag(bodyBuilder, entry.getModifiedDate());

		return bodyBuilder
				.body(relationsGraph);
	}
}