/** * Get namespace * * @param prefix prefix * @return Response with the mapping, if exists */ @GET @Path("/" + PREFIX_PATTERN) @Produces("application/json") public Response getMapping(@PathParam("prefix") String prefix) { if (prefixService.containsPrefix(prefix)) { Map<String, String> result = new HashMap<>(); result.put(prefix, prefixService.getNamespace(prefix)); return Response.ok().entity(result).build(); } else { log.error("prefix {} mapping not found", prefix); return Response.status(Response.Status.NOT_FOUND).entity("prefix " + prefix + " mapping not found").build(); } }
/** * Reverse prefix lookup * * @param uri namespace * @return Response with the result of the reverse search */ @GET @Path("/reverse") @Produces("application/json") public Response getPrefix(@QueryParam("uri") @NotNull String uri) { if (StringUtils.isNotBlank(uri)) { if (prefixService.containsNamespace(uri)) { Map<String, String> result = new HashMap<>(); result.put(uri, prefixService.getPrefix(uri)); return Response.ok().entity(result).build(); } else { log.error("namespace {} mapping not found", uri); return Response.status(Response.Status.NOT_FOUND).entity("namespace " + uri + " mapping not found").build(); } } else { log.error("Empty namespace requested"); return Response.status(Response.Status.BAD_REQUEST).build(); } }
/** * Add new mapping * * @param prefix prefix * @param namespace uri * @return operation result */ @POST @Path("/" + PREFIX_PATTERN) public Response addMapping(@PathParam("prefix") String prefix, @QueryParam("uri") @NotNull String namespace) { try { prefixService.add(prefix, namespace); return Response.status(Response.Status.CREATED).build(); } catch (URISyntaxException e) { return Response.status(Response.Status.NOT_ACCEPTABLE).entity(e.getMessage()).build(); } catch (IllegalArgumentException e) { return Response.status(Response.Status.CONFLICT).entity(e.getMessage()).build(); } }
final String curie = prefixService.getCurie(sug); suggestions.add(curie != null ? curie : sug); return Collections.emptyList(); } else if (prefixService.containsPrefix(px)) { String resolved = prefixService.getNamespace(px) + (local != null ? local : ""); List<String> suggestions = new ArrayList<String>(); for (String c : getCompletions(resolved, limit, mode)) { continue; final String curie = prefixService.getCurie(c); suggestions.add(curie != null ? curie : c); if (mode.equals(MODE_TRANSFORM)) { for (String s : ldPathService.getTransformableTypes()) { String px = prefixService.getPrefix(UriUtil.getNamespace(s)); if (px != null && px.startsWith(prefix) && !suggestions.contains(px)) { suggestions.add(px); suggestions.add(FUNCTION_PREFIX); for (String px : prefixService.getMappings().keySet()) { if (px.startsWith(prefix)) { suggestions.add(px);
@Test(expected=IllegalArgumentException.class) public void testDoubleSet() throws IllegalArgumentException, URISyntaxException { String prefix = "bar"; String namespace1 = "http://bar1#"; String namespace2 = "http://bar2#"; Assert.assertNull(prefixService.getNamespace(prefix)); prefixService.add(prefix, namespace1); Assert.assertNotNull(prefixService.getNamespace(prefix)); Assert.assertEquals(namespace1, prefixService.getNamespace(prefix)); Assert.assertEquals(prefix, prefixService.getPrefix(namespace1)); prefixService.add(prefix, namespace2); }
@Test public void testForceDoubleSet() throws IllegalArgumentException, URISyntaxException { String prefix = "baz"; String namespace1 = "http://baz1#"; String namespace2 = "http://baz2#"; Assert.assertNull(prefixService.getNamespace(prefix)); prefixService.add(prefix, namespace1); Assert.assertNotNull(prefixService.getNamespace(prefix)); Assert.assertEquals(namespace1, prefixService.getNamespace(prefix)); Assert.assertEquals(prefix, prefixService.getPrefix(namespace1)); prefixService.forceAdd(prefix, namespace2); Assert.assertNotNull(prefixService.getNamespace(prefix)); Assert.assertEquals(namespace2, prefixService.getNamespace(prefix)); Assert.assertEquals(prefix, prefixService.getPrefix(namespace2)); }
String namespace = "http://foo#"; Assert.assertNull(prefixService.getNamespace(prefix)); post("/prefix/" + prefix + "?uri=" + namespace); Assert.assertNotNull(prefixService.getNamespace(prefix)); Assert.assertEquals(namespace, prefixService.getNamespace(prefix)); Assert.assertEquals(prefix, prefixService.getPrefix(namespace));
@Override public Map<String, String> getMappings() { Map<String, String> mappings = new HashMap<String, String>(); mappings.putAll(prefixService.getMappings()); mappings.putAll(localNS); return Collections.unmodifiableMap(mappings); }
String predicateUri = t.getPredicate().stringValue(); predicate.put("uri", predicateUri); String predicateCurie = prefixService.getCurie(predicateUri); predicate.put("curie", StringUtils.isNotBlank(predicateCurie) ? predicateCurie : predicateUri); triple.put("predicate", predicate); if (value instanceof URI) { //http uri object.put("uri", objectValue); String objectCurie = prefixService.getCurie(objectValue); object.put("curie", StringUtils.isNotBlank(objectCurie) ? objectCurie : objectValue); object.put("cache", "true"); objectValue = "\"" + objectValue + "\"@" + lang; if (literal.getDatatype() != null) { String datatype = prefixService.getCurie(literal.getDatatype().stringValue()); object.put("datatype", datatype); objectValue += "^^" + datatype; String datatype = prefixService.getCurie(literal.getDatatype().stringValue()); object.put("datatype", datatype); objectValue = "\"" + objectValue + "\"^^" + datatype; String contextUri = t.getContext().stringValue(); context.put("uri", contextUri); String contextCurie = prefixService.getCurie(contextUri); context.put("curie", StringUtils.isNotBlank(contextCurie) ? contextCurie : contextUri); triple.put("context", context); data.put("baseUri", configurationService.getServerUri());
@Override public String getNamespace(String prefix) { if (localNS.containsKey(prefix)) return localNS.get(prefix); else return prefixService.getNamespace(prefix); }
@Override public String getPrefix(String namespace) { if (localNS.containsValue(namespace)) return localNS.inverse().get(namespace); else return prefixService.getPrefix(namespace); }
@Override public boolean containsPrefix(String prefix) { return localNS.containsKey(prefix) || prefixService.containsPrefix(prefix); }
@Override public boolean containsNamespace(String namespace) { return localNS.containsValue(namespace) || prefixService.containsNamespace(namespace); }
/** * Removes a prefix * * @param prefix prefix * @return Response with the result of the operation */ @DELETE @Path("/" + PREFIX_PATTERN) public Response deleteMapping(@PathParam("prefix") String prefix) { if (prefixService.remove(prefix)) { return Response.status(Status.NO_CONTENT).build(); } else { return Response.status(Response.Status.NOT_FOUND).entity("prefix " + prefix + " mapping not found").build(); } }
@Test public void testSet() throws IllegalArgumentException, URISyntaxException { String prefix = "foo"; String namespace = "http://foo#"; Assert.assertNull(prefixService.getNamespace(prefix)); prefixService.add(prefix, namespace); Assert.assertNotNull(prefixService.getNamespace(prefix)); Assert.assertEquals(namespace, prefixService.getNamespace(prefix)); Assert.assertEquals(prefix, prefixService.getPrefix(namespace)); }
/** * Get all prefixes mappings * * @return all current prefixes mappings */ @GET @Produces("application/json") public Response getMappings() { Map<String, String> mappings = (prefixService != null ? prefixService.getMappings() : new HashMap<String,String>()); return Response.ok().entity(mappings).build(); }
String predicateUri = t.getPredicate().stringValue(); predicate.put("uri", predicateUri); String predicateCurie = prefixService.getCurie(predicateUri); predicate.put("curie", StringUtils.isNotBlank(predicateCurie) ? predicateCurie : predicateUri); triple.put("predicate", predicate); if (value instanceof URI) { //http uri object.put("uri", objectValue); String objectCurie = prefixService.getCurie(objectValue); object.put("curie", StringUtils.isNotBlank(objectCurie) ? objectCurie : objectValue); object.put("cache", "true"); objectValue = "\"" + objectValue + "\"@" + lang; if (literal.getDatatype() != null) { String datatype = prefixService.getCurie(literal.getDatatype().stringValue()); object.put("datatype", datatype); objectValue += "^^" + datatype; String datatype = prefixService.getCurie(literal.getDatatype().stringValue()); object.put("datatype", datatype); objectValue = "\"" + objectValue + "\"^^" + datatype; String contextUri = t.getContext().stringValue(); context.put("uri", contextUri); String contextCurie = prefixService.getCurie(contextUri); context.put("curie", StringUtils.isNotBlank(contextCurie) ? contextCurie : contextUri); triple.put("context", context); data.put("baseUri", configurationService.getServerUri());
/** * Removes a prefix * * @param prefix prefix * @return Response with the result of the operation */ @DELETE @Path("/" + PREFIX_PATTERN) public Response deleteMapping(@PathParam("prefix") String prefix) { if (prefixService.remove(prefix)) { return Response.status(Status.NO_CONTENT).build(); } else { return Response.status(Response.Status.NOT_FOUND).entity("prefix " + prefix + " mapping not found").build(); } }
@GET @Path("/prefix") @Produces(Namespaces.MIME_TYPE_JSON) public Map<String, String> resolvePrefix(@QueryParam("prefix") String prefix, @Context UriInfo info) { final PrefixService prefixService = createLocalPrefixService(info); if (prefixService.containsPrefix(prefix)) return Collections.singletonMap(prefix, prefixService.getNamespace(prefix)); // As a fallback, try prefix.cc if (prefix != null) { final String namespace = prefixCC.getNamespace(prefix); if (namespace != null) return Collections.singletonMap(prefix, namespace); } return Collections.emptyMap(); }
/** * Reverse prefix lookup * * @param uri namespace * @return Response with the result of the reverse search */ @GET @Path("/reverse") @Produces("application/json") public Response getPrefix(@QueryParam("uri") @NotNull String uri) { if (StringUtils.isNotBlank(uri)) { if (prefixService.containsNamespace(uri)) { Map<String, String> result = new HashMap<>(); result.put(uri, prefixService.getPrefix(uri)); return Response.ok().entity(result).build(); } else { log.error("namespace {} mapping not found", uri); return Response.status(Response.Status.NOT_FOUND).entity("namespace " + uri + " mapping not found").build(); } } else { log.error("Empty namespace requested"); return Response.status(Response.Status.BAD_REQUEST).build(); } }