Repository navigation
Expand file tree
/
Copy pathRDFSource.java
More file actions
168 lines (150 loc) · 5.9 KB
/
Copy pathRDFSource.java
File metadata and controls
168 lines (150 loc) · 5.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
* Copyright Inrupt Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.inrupt.client;
import com.inrupt.client.spi.RDFFactory;
import com.inrupt.client.spi.ServiceProvider;
import com.inrupt.rdf.wrapping.commons.WrapperDataset;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.util.Objects;
import org.apache.commons.rdf.api.Dataset;
import org.apache.commons.rdf.api.RDF;
import org.apache.commons.rdf.api.RDFSyntax;
/**
* A base class for RDF-based resource mapping.
*
* <p>This class can be used as a basis for object mapping with higher-level client applications.
*
* @deprecated This class will be removed in version 3.0.
*/
@Deprecated(since = "2.1.0", forRemoval = true)
public class RDFSource extends WrapperDataset implements Resource {
/**
* The RDF Factory instance.
*/
protected static final RDF rdf = RDFFactory.getInstance();
private final URI identifier;
private final RDFSyntax syntax;
private final Headers headers;
/**
* Create a new RDF-bearing resource.
*
* <p>Subclasses should have the same constructor signature to work with the provided object mapping mechanism.
*
* @param identifier the resource identifier
* @param dataset the dataset corresponding to this resource, may be {@code null}
*/
protected RDFSource(final URI identifier, final Dataset dataset) {
this(identifier, RDFSyntax.TURTLE, dataset);
}
/**
* Create a new RDF-bearing resource.
*
* <p>Subclasses should have the same constructor signature to work with the provided object mapping mechanism.
*
* @param identifier the resource identifier
* @param dataset the dataset corresponding to this resource, may be {@code null}
* @param headers header values associated with the resource, may be {@code null}
*/
protected RDFSource(final URI identifier, final Dataset dataset, final Headers headers) {
this(identifier, RDFSyntax.TURTLE, dataset, headers);
}
/**
* Create a new RDF-bearing resource.
*
* <p>Subclasses should have the same constructor signature to work with the provided object mapping mechanism.
*
* @param identifier the resource identifier
* @param syntax the original RDF syntax in use
* @param dataset the dataset corresponding to this resource, may be {@code null}
*/
protected RDFSource(final URI identifier, final RDFSyntax syntax, final Dataset dataset) {
this(identifier, syntax, dataset, null);
}
/**
* Create a new RDF-bearing resource.
*
* <p>Subclasses should have the same constructor signature to work with the provided object mapping mechanism.
*
* @param identifier the resource identifier
* @param syntax the original RDF syntax in use
* @param dataset the dataset corresponding to this resource, may be {@code null}
* @param headers header values associated with the resource, may be {@code null}
*/
protected RDFSource(final URI identifier, final RDFSyntax syntax, final Dataset dataset, final Headers headers) {
super(dataset == null ? rdf.createDataset() : dataset);
this.headers = headers == null ? Headers.empty() : headers;
this.identifier = Objects.requireNonNull(identifier, "identifier may not be null!").normalize();
this.syntax = Objects.requireNonNull(syntax);
}
@Override
public URI getIdentifier() {
return identifier;
}
@Override
public String getContentType() {
return syntax.mediaType();
}
@Override
public Headers getHeaders() {
return headers;
}
@Override
public InputStream getEntity() throws IOException {
try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
serialize(syntax, out);
return new ByteArrayInputStream(out.toByteArray());
}
}
/**
* Serialize this object with a defined RDF syntax.
*
* @param syntax the RDF syntax
* @param out the output stream
* @throws IOException in the case of an I/O error
*/
public void serialize(final RDFSyntax syntax, final OutputStream out) throws IOException {
ServiceProvider.getRdfService().fromDataset(this, syntax, out);
}
/**
* Validate the dataset for this object.
*
* <p>Subclasses may override this method to perform validation on the provided dataset.
* By default, this method is a no-op.
*
* @return the validation result
*/
public ValidationResult validate() {
return new ValidationResult(true);
}
@Override
public void close() {
try {
super.close();
} catch (final Exception ex) {
throw new InruptClientException("Error closing dataset", ex);
}
}
}