diff --git a/README.adoc b/README.adoc index 29400c4..0794f2d 100644 --- a/README.adoc +++ b/README.adoc @@ -57,9 +57,10 @@ address accordingly == Installation -=== Solr Plugin (Recommended) +=== Solr Package (Recommended as of Solr 8.6) -You can install **{project-name}** as a Solr Plugin: +==== Installation +You can install **{project-name}** as a Solr package: [source,bash,subs="verbatim,attributes"] ---- @@ -67,13 +68,43 @@ bin/solr package add-repo yasa "https://raw.githubusercontent.com/yasa-org/yasa/ bin/solr package install yasa -# Should be unnecessary in the future -curl "http://localhost:8983/solr/admin/collections?action=CREATE&name=whatever&numShards=1" -bin/solr package undeploy yasa -y -collections whatever -bin/solr package deploy yasa -y -collections whatever +bin/solr package deploy yasa -y -cluster +---- + +Then navigate your browser to http://localhost:8983/v2/yasa + +To register **{project-name}** at another path, you can use: +[source,bash,subs="verbatim,attributes"] +---- +bin/solr package deploy yasa -y -cluster -p YASA-PATH-PREFIX=mysolrui +---- + +Then navigate your browser to http://localhost:8983/v2/mysolrui + +==== Updating to a newer version +To check installed version and available versions of the package, +[source,bash,subs="verbatim,attributes"] +---- +bin/solr package list-installed + +bin/solr package list-available +---- + +To update to a newer version, +[source,bash,subs="verbatim,attributes"] +---- +bin/solr package install yasa: + +bin/solr package deploy yasa: -y -cluster -update +---- + +==== Undeploying +To undeploy, +[source,bash,subs="verbatim,attributes"] +---- +bin/solr package undeploy yasa -cluster ---- -Then navigate your browser to http://localhost:8983/v2/plugin/yasa/index.html === Standalone Mode diff --git a/pom.xml b/pom.xml index a24fb42..f0e9261 100644 --- a/pom.xml +++ b/pom.xml @@ -25,7 +25,7 @@ io.github.kezhenxu94 yasa - 0.5.1 + 0.5.2 yasa-ui diff --git a/yasa-solr-plugin/pom.xml b/yasa-solr-plugin/pom.xml index 2df305f..d927e75 100644 --- a/yasa-solr-plugin/pom.xml +++ b/yasa-solr-plugin/pom.xml @@ -23,7 +23,7 @@ yasa io.github.kezhenxu94 - 0.5.1 + 0.5.2 4.0.0 diff --git a/yasa-solr-plugin/src/main/java/io/github/kezhenxu94/YasaHandler.java b/yasa-solr-plugin/src/main/java/io/github/kezhenxu94/YasaHandler.java index 628430f..18de76e 100644 --- a/yasa-solr-plugin/src/main/java/io/github/kezhenxu94/YasaHandler.java +++ b/yasa-solr-plugin/src/main/java/io/github/kezhenxu94/YasaHandler.java @@ -18,15 +18,21 @@ package io.github.kezhenxu94; import java.io.IOException; +import java.io.InputStream; import java.io.OutputStream; +import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; import lombok.RequiredArgsConstructor; import org.apache.commons.io.IOUtils; import org.apache.http.entity.ContentType; +import org.apache.lucene.analysis.util.ResourceLoader; +import org.apache.lucene.analysis.util.ResourceLoaderAware; import org.apache.solr.api.Command; import org.apache.solr.api.EndPoint; import org.apache.solr.client.solrj.SolrRequest.METHOD; +import org.apache.solr.common.SolrException; +import org.apache.solr.common.SolrException.ErrorCode; import org.apache.solr.common.params.CommonParams; import org.apache.solr.common.params.ModifiableSolrParams; import org.apache.solr.core.CoreContainer; @@ -36,35 +42,50 @@ import org.apache.solr.response.SolrQueryResponse; import org.apache.solr.security.PermissionNameProvider; -import static io.github.kezhenxu94.YasaHandler.PLUGIN_PATH; - @EndPoint( method = METHOD.GET, - path = PLUGIN_PATH + "/*", + path = "$path-prefix/*", permission = PermissionNameProvider.Name.CONFIG_READ_PERM ) @RequiredArgsConstructor -@SuppressWarnings("unused") -public class YasaHandler { - public static final String PLUGIN_PATH = "/plugin/yasa"; - +public class YasaHandler implements ResourceLoaderAware { + @SuppressWarnings("unused") private final CoreContainer coreContainer; + private ResourceLoader loader = null; + + @Override + public void inform(ResourceLoader loader) { + this.loader = loader; + } + @Command public void call(SolrQueryRequest req, SolrQueryResponse rsp) throws IOException { - final String path = req.getHttpSolrCall().getPath(); + String path = req.getHttpSolrCall().getPath(); + String filepath = resolveFilePath(path); - String filepath = path.substring(path.indexOf(PLUGIN_PATH) + PLUGIN_PATH.length()); - if ("".equalsIgnoreCase(filepath) || "/".equalsIgnoreCase(filepath)) { - filepath = "/index.html"; + final InputStream inputStream = loader.openResource(filepath); + if (inputStream == null) { + throw new SolrException(ErrorCode.NOT_FOUND, "File not found: " + filepath); } + final byte[] data; + final String contentType; + + if ("".equals(filepath)) { + String indexPath = path.endsWith("/") ? path + "index.html" : path + "/index.html"; + indexPath = indexPath.replaceAll("____v2", "v2"); + data = ("").getBytes( + StandardCharsets.UTF_8); + contentType = ContentType.TEXT_HTML.getMimeType(); + } else { + data = IOUtils.toByteArray(inputStream); + contentType = contentType(filepath); + } final ModifiableSolrParams newParams = new ModifiableSolrParams(req.getOriginalParams()); newParams.set(CommonParams.WT, ReplicationHandler.FILE_STREAM); req.setParams(newParams); - final String contentType = contentType(filepath); - final byte[] data = IOUtils.toByteArray(getClass().getResourceAsStream(filepath)); final SolrCore.RawWriter writer = new SolrCore.RawWriter() { @Override @@ -98,4 +119,18 @@ private String contentType(final String filepath) { final String extension = filepath.split("\\.")[filepath.split("\\.").length - 1]; return types.getOrDefault(extension, "text/plain"); } + + private String resolveFilePath(String path) { + // Path can be: /____v2/yasa/index.html + if (path.split("/").length < 3) { + throw new SolrException(ErrorCode.BAD_REQUEST, "Can't parse path: " + path); + } + String basepath = "/" + path.split("/")[1] + "/" + path.split("/")[2]; + String filepath = path.substring(path.indexOf(basepath) + basepath.length()); + + if (filepath.startsWith("/")) { + filepath = filepath.substring(1); + } + return filepath; + } } diff --git a/yasa-solr-plugin/src/main/resources/manifest.json b/yasa-solr-plugin/src/main/resources/manifest.json index c1bf341..7abe30c 100644 --- a/yasa-solr-plugin/src/main/resources/manifest.json +++ b/yasa-solr-plugin/src/main/resources/manifest.json @@ -3,24 +3,35 @@ "plugins": [ { "name": "yasa", + "type": "cluster", "setup-command": { "path": "/api/cluster/plugin", "method": "POST", "payload": { "add": { - "name": "yasa", + "name": "${package-name}:${plugin-name}", "class": "yasa:io.github.kezhenxu94.YasaHandler", - "version": "${package-version}" + "version": "${package-version}", + "path-prefix": "${YASA-PATH-PREFIX}" } } }, + "verify-command": { + "path": "/api/cluster/zk/data/clusterprops.json", + "method": "GET", + "condition": "$['plugin'].['${package-name}:${plugin-name}'].['version']", + "expected": "${package-version}" + }, "uninstall-command": { "path": "/api/cluster/plugin", "method": "POST", "payload": { - "remove": "yasa" + "remove": "${package-name}:${plugin-name}" } } } - ] + ], + "parameter-defaults": { + "YASA-PATH-PREFIX": "yasa" + } } diff --git a/yasa-ui/pom.xml b/yasa-ui/pom.xml index 7a79b47..4a476aa 100644 --- a/yasa-ui/pom.xml +++ b/yasa-ui/pom.xml @@ -23,7 +23,7 @@ yasa io.github.kezhenxu94 - 0.5.1 + 0.5.2 4.0.0