Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Daylight is seen. FedoraVersions is now fully streaming, HTTP to JCR
- Loading branch information
Showing
7 changed files
with
260 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
...ttp-commons/src/main/java/org/fcrepo/http/commons/responses/RdfStreamStreamingOutput.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
/** | ||
* Copyright 2013 DuraSpace, Inc. | ||
* | ||
* 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.fcrepo.http.commons.responses; | ||
|
||
import static org.openrdf.model.impl.ValueFactoryImpl.getInstance; | ||
import static org.openrdf.model.util.Literals.createLiteral; | ||
import static org.openrdf.rio.RDFFormat.RDFXML; | ||
import static org.openrdf.rio.RDFFormat.forMIMEType; | ||
import static org.slf4j.LoggerFactory.getLogger; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.util.Iterator; | ||
|
||
import javax.jcr.Session; | ||
import javax.ws.rs.WebApplicationException; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.StreamingOutput; | ||
|
||
import org.fcrepo.kernel.utils.iterators.RdfStream; | ||
import org.openrdf.model.Statement; | ||
import org.openrdf.model.Value; | ||
import org.openrdf.model.ValueFactory; | ||
import org.openrdf.rio.RDFFormat; | ||
import org.openrdf.rio.RDFHandlerException; | ||
import org.openrdf.rio.Rio; | ||
import org.openrdf.rio.WriterConfig; | ||
import org.slf4j.Logger; | ||
|
||
import com.google.common.base.Function; | ||
import com.google.common.util.concurrent.AbstractFuture; | ||
import com.hp.hpl.jena.graph.Node; | ||
import com.hp.hpl.jena.graph.Triple; | ||
|
||
/** | ||
* Serializes an {@link RdfStream}. | ||
* | ||
* @author ajs6f | ||
* @date Oct 30, 2013 | ||
*/ | ||
public class RdfStreamStreamingOutput extends AbstractFuture<Session> implements | ||
StreamingOutput { | ||
|
||
private static final Logger LOGGER = | ||
getLogger(RdfStreamStreamingOutput.class); | ||
|
||
private static ValueFactory vfactory = getInstance(); | ||
|
||
private final RDFFormat format; | ||
|
||
private final RdfStream rdfStream; | ||
|
||
/** | ||
* This should be passed in as the {@link Session} in which the | ||
* {@link RdfStream} to be serialized is rooted. It will be made available | ||
* again after serialization for further operation via the {@link Future} | ||
* methods. | ||
*/ | ||
private final Session session; | ||
|
||
/** | ||
* Normal constructor | ||
* | ||
* @param rdfStream | ||
* @param mediaType | ||
*/ | ||
public RdfStreamStreamingOutput(final RdfStream rdfStream, | ||
final MediaType mediaType, final Session session) { | ||
super(); | ||
this.format = forMIMEType(mediaType.toString(), RDFXML); | ||
this.rdfStream = rdfStream; | ||
this.session = session; | ||
} | ||
|
||
@Override | ||
public void write(final OutputStream output) throws IOException, | ||
WebApplicationException { | ||
LOGGER.debug("Serializing RDF stream in: {}", format); | ||
try { | ||
Rio.write(asStatements(), output, format, new WriterConfig()); | ||
} catch (final RDFHandlerException e) { | ||
setException(e); | ||
} finally { | ||
// we're done. | ||
set(session); | ||
} | ||
} | ||
|
||
private Iterable<Statement> asStatements() { | ||
return new Iterable<Statement>() { | ||
|
||
@Override | ||
public Iterator<Statement> iterator() { | ||
return rdfStream.transform(toStatement); | ||
} | ||
}; | ||
} | ||
|
||
protected static final Function<? super Triple, Statement> toStatement = | ||
new Function<Triple, Statement>() { | ||
|
||
@Override | ||
public Statement apply(final Triple t) { | ||
final Value object = getValueForObject(t.getObject()); | ||
return vfactory.createStatement(vfactory.createURI(t | ||
.getSubject().getURI()), vfactory.createURI(t | ||
.getPredicate().getURI()), object); | ||
} | ||
|
||
}; | ||
|
||
protected static Value getValueForObject(final Node object) { | ||
if (object.isURI()) { | ||
return vfactory.createURI(object.getURI()); | ||
} else { | ||
if (object.isLiteral()) { | ||
return createLiteral(vfactory, object.getLiteralValue()); | ||
} | ||
} | ||
throw new UnsupportedOperationException( | ||
"We do not serialize blank nodes!"); | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.