Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SOLR-15121: Move XSLT (tr param) to scripting contrib #2306

Merged
merged 33 commits into from Feb 15, 2021
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
0746293
light copyediting
Feb 4, 2021
20e5d56
document how to avoid shared memory issue
Feb 4, 2021
3d584d8
relocate xslt related classes into scripting contrib
Feb 4, 2021
8751dad
we are really testing the replication of subdir, not anything specifi…
Feb 4, 2021
1a749b6
relocating files to scripting and seperating out unit tests
Feb 4, 2021
15a9b76
relocate files under test-files/scripting/solr, similar to how we do …
Feb 4, 2021
8978945
dont need xslt in here
Feb 4, 2021
32deac8
moved tests to scripting
Feb 4, 2021
9025c3b
provide a subdir for testin replication that isnt tied to xslt
Feb 4, 2021
5088fc3
comment out the XSLT writer, and provide a link and some context
Feb 5, 2021
aac6bfc
working through the tests to get them to pass
Feb 5, 2021
e9af96a
fix javadoc error
Feb 5, 2021
53f50ca
here are two notional thoughts about how to enable the XSLTLoader
Feb 5, 2021
337b86d
awkward check for a class existing, and it does, then use it
Feb 5, 2021
89de9ac
back out this old attempt at pluggablity
Feb 5, 2021
d8ba6af
Remove the .util. package as not needed.
Feb 6, 2021
a80a1e3
Provide better intro to what this class does.
Feb 6, 2021
431e0a5
better package info
Feb 6, 2021
1e3164b
Reformatting using the Google Java Format...
Feb 6, 2021
e019a9f
fix up pathing
Feb 6, 2021
efd7334
use actual param name, not the variable to properly test api!
Feb 6, 2021
72bcf7b
start updating the docs, more effort needed.
Feb 6, 2021
0331878
another round of moving files around, and an attempt at exposing/disa…
Feb 8, 2021
1e71866
Tests are passing, and package structure between src/java and src/tes…
Feb 8, 2021
0233e84
Reduce white space changes
Feb 8, 2021
f3e4cbd
Clean up references to paths, and deal with the mish mash of Xslt and…
Feb 8, 2021
bccbf23
restore and document why we still have xslt package
Feb 8, 2021
8b3a35a
turns out the default setup includes the scripting jar, so we can hav…
Feb 9, 2021
dddc0b4
Round of edits to test the use of xslt in export and import, with the…
Feb 9, 2021
46f1b4e
lazy lets us use these configs for the solrj tests, which don't have …
Feb 9, 2021
8ddb329
Move XSLT processing out of XMLLoader
dsmiley Feb 13, 2021
751fa32
Move TransformerProvider.
dsmiley Feb 13, 2021
2b4a49d
Remove unused
dsmiley Feb 13, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.solr.response;
package org.apache.solr.scripting;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not the xslt package?


import java.io.BufferedReader;
import java.io.CharArrayReader;
Expand All @@ -33,37 +33,44 @@
import javax.xml.transform.stream.StreamSource;

import org.apache.solr.core.SolrConfig;
import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.common.util.XMLErrorLogger;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.util.xslt.TransformerProvider;
import org.apache.solr.response.QueryResponseWriter;
import org.apache.solr.response.SolrQueryResponse;
import org.apache.solr.response.XMLWriter;
import org.apache.solr.scripting.xslt.XSLTParams;
import org.apache.solr.scripting.xslt.TransformerProvider;

/** QueryResponseWriter which captures the output of the XMLWriter
/**
* Customize the format of your search results via XSL stylesheet applied to the default
* XML response format.
*
* QueryResponseWriter captures the output of the XMLWriter
* (in memory for now, not optimal performancewise), and applies an XSLT transform
* to it.
*/
public class XSLTResponseWriter implements QueryResponseWriter {

public static final String DEFAULT_CONTENT_TYPE = "application/xml";
public static final String CONTEXT_TRANSFORMER_KEY = "xsltwriter.transformer";
private Integer xsltCacheLifetimeSeconds = null;

private Integer xsltCacheLifetimeSeconds = null;
public static final int XSLT_CACHE_DEFAULT = 60;
private static final String XSLT_CACHE_PARAM = "xsltCacheLifetimeSeconds";
private static final String XSLT_CACHE_PARAM = "xsltCacheLifetimeSeconds";

private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
private static final XMLErrorLogger xmllog = new XMLErrorLogger(log);

@Override
public void init(@SuppressWarnings({"rawtypes"})NamedList n) {
final SolrParams p = n.toSolrParams();
xsltCacheLifetimeSeconds = p.getInt(XSLT_CACHE_PARAM,XSLT_CACHE_DEFAULT);
log.info("xsltCacheLifetimeSeconds={}", xsltCacheLifetimeSeconds);
}


@Override
public String getContentType(SolrQueryRequest request, SolrQueryResponse response) {
Transformer t = null;
Expand All @@ -73,34 +80,34 @@ public String getContentType(SolrQueryRequest request, SolrQueryResponse respons
// TODO should our parent interface throw (IO)Exception?
throw new RuntimeException("getTransformer fails in getContentType",e);
}

String mediaType = t.getOutputProperty("media-type");
if (mediaType == null || mediaType.length()==0) {
// This did not happen in my tests, mediaTypeFromXslt is set to "text/xml"
// if the XSLT transform does not contain an xsl:output element. Not sure
// if this is standard behavior or if it's just my JVM/libraries
mediaType = DEFAULT_CONTENT_TYPE;
}

if (!mediaType.contains("charset")) {
String encoding = t.getOutputProperty("encoding");
if (encoding == null || encoding.length()==0) {
encoding = "UTF-8";
}
mediaType = mediaType + "; charset=" + encoding;
}

return mediaType;
}

@Override
public void write(Writer writer, SolrQueryRequest request, SolrQueryResponse response) throws IOException {
final Transformer t = getTransformer(request);

// capture the output of the XMLWriter
final CharArrayWriter w = new CharArrayWriter();
XMLWriter.writeResponse(w,request,response);

// and write transformed result to our writer
final Reader r = new BufferedReader(new CharArrayReader(w.toCharArray()));
final StreamSource source = new StreamSource(r);
Expand All @@ -111,19 +118,19 @@ public void write(Writer writer, SolrQueryRequest request, SolrQueryResponse res
throw new IOException("XSLT transformation error", te);
}
}

/** Get Transformer from request context, or from TransformerProvider.
* This allows either getContentType(...) or write(...) to instantiate the Transformer,
* depending on which one is called first, then the other one reuses the same Transformer
*/
protected Transformer getTransformer(SolrQueryRequest request) throws IOException {
final String xslt = request.getParams().get(CommonParams.TR,null);
final String xslt = request.getParams().get(XSLTParams.TR,null);
if(xslt==null) {
throw new IOException("'" + CommonParams.TR + "' request parameter is required to use the XSLTResponseWriter");
throw new IOException("'" + XSLTParams.TR + "' request parameter is required to use the XSLTResponseWriter");
}
// not the cleanest way to achieve this
SolrConfig solrConfig = request.getCore().getSolrConfig();
// no need to synchronize access to context, right?
// no need to synchronize access to context, right?
// Nothing else happens with it at the same time
final Map<Object,Object> ctx = request.getContext();
Transformer result = (Transformer)ctx.get(CONTEXT_TRANSFORMER_KEY);
Expand Down
@@ -0,0 +1,21 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/

/**
* Classes related to applying run time scripting changes to Solr.
*/
package org.apache.solr.scripting;
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.solr.util.xslt;
package org.apache.solr.scripting.xslt;

import java.io.IOException;
import java.lang.invoke.MethodHandles;
Expand Down