public static String getAcceptPostHeader(String extraFormats) { final Collection<RDFFormat> rdfFormats = filterAvailableParsers(LdpService.SERVER_PREFERED_RDF_FORMATS); final StringBuilder sb = new StringBuilder(); for (RDFFormat rdfFormat : rdfFormats) { sb.append(rdfFormat.getDefaultMIMEType()); sb.append(", "); } if (StringUtils.isNotBlank(extraFormats)) { sb.append(extraFormats); } else { sb.delete(sb.length() - 2, sb.length()); } return sb.toString(); }
/** * Loads RDF contents from the specified file into the repository. * * @param filePath Path to data file. * @throws RepositoryException If no connection could be established. */ public void loadFromFile(String filePath) throws RepositoryException { RepositoryConnection conn = repo.getConnection(); try { conn.add( new File(filePath), null, RDFFormat.forFileName(filePath), ValueFactoryImpl.getInstance().createURI("urn:defaultContext")); } catch (RDFParseException | IOException e) { throw new RuntimeException(e); } finally { conn.close(); } }
/** * Exports all data into the specified file (as RDF/XML). * * @param filePath Path to file. * @throws RepositoryException * @throws FileNotFoundException */ public void exportToFile(String filePath) throws RepositoryException, FileNotFoundException { OutputStream fileStream = new FileOutputStream(new File(filePath)); RDFWriter writer = Rio.createWriter(RDFFormat.forFileName(filePath, RDFFormat.RDFXML), fileStream); RepositoryConnection conn = repo.getConnection(); try { conn.export(writer); } catch (RDFHandlerException e) { throw new RuntimeException("Error during export: " + e); } finally { conn.close(); } }
/** * Import data from URI source Request is made with proper HTTP ACCEPT header and will follow * redirects for proper LOD source negotiation * * @param urlstring absolute URI of the data source * @param format RDF format to request/parse from data source */ public void addURI(String urlstring, RDFFormat format) { try { RepositoryConnection con = currentRepository.getConnection(); try { URL url = new URL(urlstring); URLConnection uricon = (URLConnection) url.openConnection(); uricon.addRequestProperty("accept", format.getDefaultMIMEType()); InputStream instream = uricon.getInputStream(); con.add(instream, urlstring, format); } finally { con.close(); } } catch (Exception e) { e.printStackTrace(); } }
public RDFFormat getRDFFormat( final File file, final RDFFormat defaultValue, final String... alternatives) { String s = getOption(null, alternatives); RDFFormat format; // If they specified an option, try to find it out of the non-standard // list of descriptors in Sesamize.rdfFormatByName if (null != s) { format = findRDFFormat(s); } else { // otherwise try to find the format based on the file name extension, // using the specified default value as a fallback Optional<RDFFormat> f = RDFFormat.matchFileName(file.getName(), null); format = f.isPresent() ? f.get() : defaultValue; } return format; }
// add ontologies to repository public Model(Collection<File> rdfFilesToImport) { this(); this.schemaContext = this.repository.getValueFactory().createURI("http://localhost/context"); // this.schemaContext=this.repository.getValueFactory().createURI(TestDriver.getConfigurations().getString(Configurations.ENDPOINT_URL)); try { RepositoryConnection repoConn = this.repository.getConnection(); for (File file : rdfFilesToImport) { System.out.println("Adding file: " + file.getName()); repoConn.add( file, this.schemaContext.toString(), RDFFormat.forMIMEType(file.getName()), this .schemaContext); // forMIMEType finds the file format in order to read more than one // rdfformats System.out.println("FILE ADDED"); } repoConn.close(); } catch (IOException | RDFParseException | RepositoryException ex) { ex.printStackTrace(); } }
@Test public void testPostRdfBasic() throws Exception { // prepare: add an artifact final InferredOWLOntologyID testArtifact = this.loadTestArtifact( TestConstants.TEST_ARTIFACT_20130206, MediaType.APPLICATION_RDF_TURTLE); final ClientResource searchClientResource = new ClientResource(this.getUrl(PoddWebConstants.PATH_SEARCH)); try { searchClientResource.addQueryParameter( PoddWebConstants.KEY_ARTIFACT_IDENTIFIER, testArtifact.getOntologyIRI().toString()); // prepare: the test input final String[] objectUris = { "http://purl.org/podd/basic-1-20130206/object:2966", "http://purl.org/podd/basic-2-20130206/artifact:1#Demo-Genotype", "http://purl.org/podd/basic-2-20130206/artifact:1#SqueekeeMaterial", "http://purl.org/podd/ns/poddScience#WildType_NotApplicable", "http://purl.org/podd/ns/poddPlant#DeltaTporometer-63", "http://purl.org/podd/ns/poddBase#DisplayType_LongText" }; final String[] expectedLabels = { "Project#2012-0006_ Cotton Leaf Morphology", "Demo genotype", "Squeekee material", "Not Applicable", "Delta-T porometer", null }; final Model testModel = new LinkedHashModel(); for (final String s : objectUris) { testModel.add(PODD.VF.createURI(s), RDFS.LABEL, PODD.VF.createLiteral("?blank")); } final RDFFormat inputFormat = RDFFormat.RDFXML; final MediaType inputMediaType = MediaType.valueOf(inputFormat.getDefaultMIMEType()); // build input representation final ByteArrayOutputStream output = new ByteArrayOutputStream(8096); Rio.write(testModel, output, inputFormat); final Representation input = new StringRepresentation(output.toString(), inputMediaType); // invoke service final Representation results = this.doTestAuthenticatedRequest( searchClientResource, Method.POST, input, inputMediaType, Status.SUCCESS_OK, AbstractResourceImplTest.WITH_ADMIN); // verify: response final Model resultModel = this.assertRdf(results, RDFFormat.RDFXML, 5); // verify: each URI has the expected label for (int i = 0; i < objectUris.length; i++) { final String objectString = resultModel.filter(PODD.VF.createURI(objectUris[i]), RDFS.LABEL, null).objectString(); Assert.assertEquals("Not the expected label", expectedLabels[i], objectString); } } finally { this.releaseClient(searchClientResource); } }