From b2dda44142f03441dc33b2d46aceb08c9f8ff3d5 Mon Sep 17 00:00:00 2001 From: Thorsten Vitt <thorsten.vitt@uni-wuerzburg.de> Date: Tue, 10 Sep 2013 17:06:47 +0200 Subject: [PATCH] [HTML] allow to specify target content type. This is probably a better solution to deal with the old JQuery than unconditionally delivering text/xml. --- .../services/aggregator/html/HTML.java | 5 ++-- .../services/aggregator/html/HTMLWriter.java | 30 ++++++++++++++++--- .../services/aggregator/html/HtmlAT.java | 2 +- .../services/aggregator/html/HtmlIT.java | 2 +- 4 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/main/java/info/textgrid/services/aggregator/html/HTML.java b/src/main/java/info/textgrid/services/aggregator/html/HTML.java index 8025815..6bda0f0 100644 --- a/src/main/java/info/textgrid/services/aggregator/html/HTML.java +++ b/src/main/java/info/textgrid/services/aggregator/html/HTML.java @@ -213,15 +213,16 @@ public Response get( @Description("If true, pass the information the stylesheet that its result will be embedded into some website") @QueryParam("embedded") final boolean embedded, @Description("URL of the CSS that should be referenced in the HTML that is created") @QueryParam("css") final URI css, + @Description("The requested content type. E.g., text/html or text/xml") @QueryParam("mediatype") final String mediaType, @Context final Request request) throws ObjectNotFoundFault, MetadataParseFault, IoFault, AuthFault, ProtocolNotImplementedFault, WebApplicationException, IOException, SaxonApiException, ExecutionException { - logger.fine("HTML called for root object: " + uri); + logger.fine("HTML called for root object: " + uri); final HTMLWriter writer = new HTMLWriter(this, uri, xsluri, - refreshStylesheet, pi, embedded, css, sid, request); + refreshStylesheet, pi, embedded, css, sid, mediaType, request); return writer.createResponse().build(); } diff --git a/src/main/java/info/textgrid/services/aggregator/html/HTMLWriter.java b/src/main/java/info/textgrid/services/aggregator/html/HTMLWriter.java index ea63cd9..e34a9c1 100644 --- a/src/main/java/info/textgrid/services/aggregator/html/HTMLWriter.java +++ b/src/main/java/info/textgrid/services/aggregator/html/HTMLWriter.java @@ -18,17 +18,18 @@ import java.net.URI; import java.net.URISyntaxException; import java.text.MessageFormat; +import java.util.Properties; import java.util.logging.Level; import java.util.logging.Logger; import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.CacheControl; -import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Request; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.ResponseBuilder; import javax.ws.rs.core.Response.Status; import javax.ws.rs.core.StreamingOutput; +import javax.xml.transform.OutputKeys; import javax.xml.transform.Source; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerFactory; @@ -37,6 +38,7 @@ import net.sf.saxon.s9api.QName; import net.sf.saxon.s9api.SaxonApiException; +import net.sf.saxon.s9api.Serializer; import net.sf.saxon.s9api.XdmAtomicValue; import net.sf.saxon.s9api.XsltExecutable; import net.sf.saxon.s9api.XsltTransformer; @@ -121,6 +123,8 @@ private enum SourceType { private Request request; + private Optional<String> requestedMediaType; + // Constructor and configuration public HTMLWriter(final HTML service, final URI rootURI) { @@ -135,7 +139,8 @@ public HTMLWriter(final HTML service, final URI rootURI) { public HTMLWriter(final HTML service, final URI rootURI, final URI stylesheetURI, final boolean refreshStylesheet, final boolean readStylesheetPI, final boolean embedded, - final URI css, final String sid, final Request request) { + final URI css, final String sid, final String mediaType, + final Request request) { this(service, rootURI); @@ -145,6 +150,7 @@ public HTMLWriter(final HTML service, final URI rootURI, this.embedded = embedded; this.css = Optional.fromNullable(css); this.sid(sid); + this.requestedMediaType = Optional.fromNullable(mediaType); this.request = request; } @@ -289,6 +295,12 @@ protected HTMLWriter loadStylesheet() throws SaxonApiException, embedded)); transformer.setSource(source); + + if (requestedMediaType.isPresent()) { + transformer.getUnderlyingController().setOutputProperty( + OutputKeys.MEDIA_TYPE, requestedMediaType.get()); + } + this.transformer = transformer; logger.log(Level.INFO, MessageFormat.format("Prepared XSLT stylesheet {1} for {0} after {2}", rootURI, actualStylesheet, stopwatch.toString())); return this; @@ -305,8 +317,10 @@ public void write(final OutputStream out) throws IOException, loadStylesheet(); } logger.log(Level.INFO, MessageFormat.format("Ready for transformation of {0} after {1}", rootURI, stopwatch.toString())); + final Serializer serializer = service.xsltProcessor + .newSerializer(out); transformer - .setDestination(service.xsltProcessor.newSerializer(out)); +.setDestination(serializer); transformer.transform(); } catch (final Exception e) { @@ -334,7 +348,15 @@ public ResponseBuilder createResponse() throws ObjectNotFoundFault, MetadataPars loadStylesheet(); final ResponseBuilder builder = Response.ok(); - builder.type(MediaType.TEXT_XML_TYPE); + if (requestedMediaType.isPresent()) { + builder.type(requestedMediaType.get()); + } else { + final Properties outputProperties = transformer + .getUnderlyingController().getOutputProperties(); + builder.type(outputProperties.getProperty(OutputKeys.MEDIA_TYPE, + "text/html")); + } + builder.lastModified(getContent().getMetadata().getGeneric().getGenerated().getLastModified().toGregorianCalendar().getTime()); final CacheControl cacheControl = new CacheControl(); cacheControl.setPrivate(sid.isPresent()); diff --git a/src/test/java/info/textgrid/services/aggregator/html/HtmlAT.java b/src/test/java/info/textgrid/services/aggregator/html/HtmlAT.java index 0ccec0e..7e93f9c 100644 --- a/src/test/java/info/textgrid/services/aggregator/html/HtmlAT.java +++ b/src/test/java/info/textgrid/services/aggregator/html/HtmlAT.java @@ -17,7 +17,7 @@ public void testSimpleGet() throws MalformedURLException, IOException { + System.getProperty("sid")); connection.connect(); Assert.assertEquals(200, connection.getResponseCode()); - Assert.assertEquals("text/xml", connection.getContentType()); + Assert.assertEquals("text/html", connection.getContentType()); Assert.assertTrue("Content length > 0", readContents(connection) > 0); } } diff --git a/src/test/java/info/textgrid/services/aggregator/html/HtmlIT.java b/src/test/java/info/textgrid/services/aggregator/html/HtmlIT.java index 94bacb3..ee003d9 100644 --- a/src/test/java/info/textgrid/services/aggregator/html/HtmlIT.java +++ b/src/test/java/info/textgrid/services/aggregator/html/HtmlIT.java @@ -16,7 +16,7 @@ public void testGet() throws IOException { final HttpURLConnection connection = createRequest("/html/textgrid:jfst.0"); connection.connect(); Assert.assertEquals(200, connection.getResponseCode()); - Assert.assertEquals("text/xml", connection.getContentType()); + Assert.assertEquals("text/html", connection.getContentType()); Assert.assertTrue("Content length > 0", readContents(connection) > 0); } -- GitLab