Skip to content

Commit

Permalink
fix: always auto generate URI based on incoming request
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominick Leppich committed Aug 28, 2024
1 parent 5926bca commit 721fa1a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@
import org.apache.jena.vocabulary.RDF;
import org.apache.jena.vocabulary.SKOS;
import org.apache.jena.vocabulary.XSD;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import org.springframework.web.util.UriComponents;

import java.io.StringWriter;
import java.lang.reflect.Method;
Expand All @@ -36,12 +37,6 @@

@Service
public class RDFMapperImpl implements RDFMapper {
@Value("${vocabulary-server.base-url}")
private String host;

@Value("${server.port}")
private int port;

public static final RDFFormat RDF_XML_SYNTAX = RDFFormat.RDFXML;
public static final RDFFormat RDF_TURTLE_SYNTAX = RDFFormat.TURTLE_BLOCKS;

Expand All @@ -53,15 +48,24 @@ private <T> String transform(T entity, EntityToModelMappingFunction<T> function,

private String generateURIForId(Class<?> clazz, long id) {
try {
String baseUrl = extractBaseURI();
String classRoute = extractClassEndpoint(clazz);
String methodRoute = extractMethodEndpoint(clazz.getMethod("one", long.class));
String endpoint = classRoute + methodRoute.replace("{id}", Long.toString(id));
return host + ':' + port + endpoint;
return baseUrl + endpoint;
} catch (NoSuchMethodException e) {
throw new MappingException(clazz, String.class, e);
}
}

private String extractBaseURI() {
UriComponents uriComponents = ServletUriComponentsBuilder.fromCurrentRequest().build();
String scheme = uriComponents.getScheme();
String host = uriComponents.getHost();
String port = String.valueOf(uriComponents.getPort());
return scheme + "://" + host + ':' + port;
}

private static String extractClassEndpoint(Class<?> clazz) throws NoSuchMethodException {
String[] values = clazz.getAnnotation(RequestMapping.class).value();
assert (values.length == 1);
Expand Down
2 changes: 0 additions & 2 deletions module-core/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
server.address=127.0.0.1

# Basic configuration
# The base url of the vocabulary server (which depends on external factors), it is used it generate valid API reference links
vocabulary-server.base-url=http://localhost
# The port the vocabulary server should listen on
server.port=8081
# The name of the application, can be customized but shouldn't affect anything
Expand Down

0 comments on commit 721fa1a

Please sign in to comment.