|
| 1 | +/* |
| 2 | + * Copyright 2023 Martynas Jusevičius <martynas@atomgraph.com>. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.atomgraph.linkeddatahub.writer; |
| 17 | + |
| 18 | +import com.atomgraph.client.util.DataManager; |
| 19 | +import com.atomgraph.linkeddatahub.model.auth.Agent; |
| 20 | +import jakarta.inject.Singleton; |
| 21 | +import jakarta.ws.rs.Produces; |
| 22 | +import jakarta.ws.rs.WebApplicationException; |
| 23 | +import jakarta.ws.rs.core.EntityTag; |
| 24 | +import jakarta.ws.rs.core.MediaType; |
| 25 | +import jakarta.ws.rs.core.MultivaluedMap; |
| 26 | +import jakarta.ws.rs.core.Response; |
| 27 | +import jakarta.ws.rs.ext.MessageBodyWriter; |
| 28 | +import jakarta.ws.rs.ext.Provider; |
| 29 | +import java.io.ByteArrayInputStream; |
| 30 | +import java.io.ByteArrayOutputStream; |
| 31 | +import java.io.IOException; |
| 32 | +import java.io.OutputStream; |
| 33 | +import java.lang.annotation.Annotation; |
| 34 | +import java.lang.reflect.Type; |
| 35 | +import java.math.BigInteger; |
| 36 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 37 | +import java.security.MessageDigest; |
| 38 | +import java.time.ZonedDateTime; |
| 39 | +import java.util.Arrays; |
| 40 | +import javax.xml.transform.TransformerException; |
| 41 | +import javax.xml.transform.URIResolver; |
| 42 | +import javax.xml.transform.stream.StreamSource; |
| 43 | +import net.sf.saxon.lib.UnparsedTextURIResolver; |
| 44 | +import net.sf.saxon.s9api.SaxonApiException; |
| 45 | +import net.sf.saxon.s9api.Serializer; |
| 46 | +import net.sf.saxon.s9api.Xslt30Transformer; |
| 47 | +import net.sf.saxon.s9api.XsltExecutable; |
| 48 | +import net.sf.saxon.value.DateTimeValue; |
| 49 | +import org.apache.http.HttpHeaders; |
| 50 | +import org.apache.jena.ontology.OntModelSpec; |
| 51 | +import org.apache.jena.query.ResultSet; |
| 52 | +import org.apache.jena.query.ResultSetFormatter; |
| 53 | +import org.apache.jena.rdf.model.Model; |
| 54 | +import org.slf4j.Logger; |
| 55 | +import org.slf4j.LoggerFactory; |
| 56 | + |
| 57 | +/** |
| 58 | + * |
| 59 | + * @author {@literal Martynas Jusevičius <martynas@atomgraph.com>} |
| 60 | + */ |
| 61 | +@Provider |
| 62 | +@Singleton |
| 63 | +@Produces({MediaType.TEXT_HTML + ";charset=UTF-8", MediaType.APPLICATION_XHTML_XML + ";charset=UTF-8"}) |
| 64 | +public class ResultSetXSLTWriter extends XSLTWriterBase implements MessageBodyWriter<ResultSet> |
| 65 | +{ |
| 66 | + |
| 67 | + private static final Logger log = LoggerFactory.getLogger(ResultSetXSLTWriter.class); |
| 68 | + |
| 69 | + /** |
| 70 | + * Constructs XSLT writer. |
| 71 | + * |
| 72 | + * @param xsltExec compiled XSLT stylesheet |
| 73 | + * @param ontModelSpec ontology specification |
| 74 | + * @param dataManager RDF data manager |
| 75 | + * @param messageDigest message digest |
| 76 | + */ |
| 77 | + public ResultSetXSLTWriter(XsltExecutable xsltExec, OntModelSpec ontModelSpec, DataManager dataManager, MessageDigest messageDigest) |
| 78 | + { |
| 79 | + super(xsltExec, ontModelSpec, dataManager, messageDigest); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) |
| 84 | + { |
| 85 | + return (ResultSet.class.isAssignableFrom(type)); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public void writeTo(ResultSet results, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> headerMap, OutputStream entityStream) throws IOException, WebApplicationException |
| 90 | + { |
| 91 | + if (log.isTraceEnabled()) log.trace("Writing ResultSet with HTTP headers: {} MediaType: {}", headerMap, mediaType); |
| 92 | + |
| 93 | + try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) |
| 94 | + { |
| 95 | + // authenticated agents get a different HTML representation and therefore a different entity tag |
| 96 | + if (headerMap.containsKey(HttpHeaders.ETAG) && headerMap.getFirst(HttpHeaders.ETAG) instanceof EntityTag && getSecurityContext() != null && getSecurityContext().getUserPrincipal() instanceof Agent) |
| 97 | + { |
| 98 | + EntityTag eTag = (EntityTag)headerMap.getFirst(HttpHeaders.ETAG); |
| 99 | + BigInteger eTagHash = new BigInteger(eTag.getValue(), 16); |
| 100 | + Agent agent = (Agent)getSecurityContext().getUserPrincipal(); |
| 101 | + eTagHash = eTagHash.add(BigInteger.valueOf(agent.hashCode())); |
| 102 | + headerMap.replace(HttpHeaders.ETAG, Arrays.asList(new EntityTag(eTagHash.toString(16)))); |
| 103 | + } |
| 104 | + |
| 105 | + ResultSetFormatter.outputAsXML(baos, results); |
| 106 | + |
| 107 | + Xslt30Transformer xsltTrans = getXsltExecutable().load30(); |
| 108 | + Serializer out = xsltTrans.newSerializer(); |
| 109 | + out.setOutputStream(entityStream); |
| 110 | + out.setOutputProperty(Serializer.Property.ENCODING, UTF_8.name()); |
| 111 | + |
| 112 | + if (mediaType.isCompatible(MediaType.TEXT_HTML_TYPE)) |
| 113 | + { |
| 114 | + out.setOutputProperty(Serializer.Property.METHOD, "html"); |
| 115 | + out.setOutputProperty(Serializer.Property.MEDIA_TYPE, MediaType.TEXT_HTML); |
| 116 | + out.setOutputProperty(Serializer.Property.DOCTYPE_SYSTEM, "http://www.w3.org/TR/html4/strict.dtd"); |
| 117 | + out.setOutputProperty(Serializer.Property.DOCTYPE_PUBLIC, "-//W3C//DTD HTML 4.01//EN"); |
| 118 | + } |
| 119 | + if (mediaType.isCompatible(MediaType.APPLICATION_XHTML_XML_TYPE)) |
| 120 | + { |
| 121 | + out.setOutputProperty(Serializer.Property.METHOD, "xhtml"); |
| 122 | + out.setOutputProperty(Serializer.Property.MEDIA_TYPE, MediaType.APPLICATION_XHTML_XML); |
| 123 | + out.setOutputProperty(Serializer.Property.DOCTYPE_SYSTEM, "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"); |
| 124 | + out.setOutputProperty(Serializer.Property.DOCTYPE_PUBLIC, "-//W3C//DTD XHTML 1.0 Strict//EN"); |
| 125 | + } |
| 126 | + |
| 127 | + xsltTrans.setURIResolver((URIResolver)getDataManager()); |
| 128 | + xsltTrans.getUnderlyingController().setUnparsedTextURIResolver((UnparsedTextURIResolver)getDataManager()); |
| 129 | + xsltTrans.getUnderlyingController().setCurrentDateTime(DateTimeValue.fromZonedDateTime(ZonedDateTime.now())); // TO-DO: make TZ configurable |
| 130 | + xsltTrans.setStylesheetParameters(getParameters(headerMap)); |
| 131 | + xsltTrans.transform(new StreamSource(new ByteArrayInputStream(baos.toByteArray())), out); |
| 132 | + } |
| 133 | + catch (TransformerException | SaxonApiException ex) |
| 134 | + { |
| 135 | + if (log.isErrorEnabled()) log.error("XSLT transformation failed", ex); |
| 136 | + throw new WebApplicationException(ex, Response.Status.INTERNAL_SERVER_ERROR); // TO-DO: make Mapper |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | +} |
0 commit comments