Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions changelog/unreleased/SOLR-15701_complete_configsets_api.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
title: Add ConfigSets.Download and ConfigSets.GetFile to SolrJ
type: added
authors:
- name: Eric Pugh
links:
- name: SOLR-15701
url: https://issues.apache.org/jira/browse/SOLR-15701
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,26 @@
*/
package org.apache.solr.client.api.endpoint;

import static org.apache.solr.client.api.util.Constants.RAW_OUTPUT_PROPERTY;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.extensions.Extension;
import io.swagger.v3.oas.annotations.extensions.ExtensionProperty;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import jakarta.ws.rs.DELETE;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.PUT;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import java.io.IOException;
import java.io.InputStream;
import org.apache.solr.client.api.model.CloneConfigsetRequestBody;
import org.apache.solr.client.api.model.ConfigSetFileContentsResponse;
import org.apache.solr.client.api.model.ListConfigsetsResponse;
import org.apache.solr.client.api.model.SolrJerseyResponse;

Expand Down Expand Up @@ -71,6 +79,46 @@ SolrJerseyResponse deleteConfigSet(@PathParam("configSetName") String configSetN
throws Exception;
}

/**
* V2 API definition for downloading an existing configset as a ZIP archive.
*
* <p>Equivalent to GET /api/configsets/{configSetName}/download
*/
@Path("/configsets/{configSetName}")
interface Download {
@GET
@Path("/download")
@Operation(
summary = "Download a configset as a ZIP archive.",
tags = {"configsets"},
extensions = {
@Extension(properties = {@ExtensionProperty(name = RAW_OUTPUT_PROPERTY, value = "true")})
})
@Produces("application/zip")
Response downloadConfigSet(
@PathParam("configSetName") String configSetName,
@QueryParam("displayName") String displayName)
throws Exception;
}

/**
* V2 API definition for reading a single file from an existing configset.
*
* <p>Equivalent to GET /api/configsets/{configSetName}/file?path=...
*/
@Path("/configsets/{configSetName}")
interface GetFile {
@GET
@Path("/file")
@Produces(MediaType.TEXT_PLAIN)
@Operation(
summary = "Get the contents of a file in a configset.",
tags = {"configsets"})
ConfigSetFileContentsResponse getConfigSetFile(
@PathParam("configSetName") String configSetName, @QueryParam("path") String filePath)
throws Exception;
}

/**
* V2 API definitions for uploading a configset, in whole or part.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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.
*/
package org.apache.solr.client.api.model;

import com.fasterxml.jackson.annotation.JsonProperty;

/** Response type for the "get configset file contents" API. */
public class ConfigSetFileContentsResponse extends SolrJerseyResponse {

/** The path of the file within the configset (as requested). */
@JsonProperty("path")
public String path;

/** The UTF-8 text content of the file. */
@JsonProperty("content")
public String content;
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
import org.apache.solr.handler.configsets.CloneConfigSet;
import org.apache.solr.handler.configsets.ConfigSetAPIBase;
import org.apache.solr.handler.configsets.DeleteConfigSet;
import org.apache.solr.handler.configsets.DownloadConfigSet;
import org.apache.solr.handler.configsets.GetConfigSetFile;
import org.apache.solr.handler.configsets.ListConfigSets;
import org.apache.solr.handler.configsets.UploadConfigSet;
import org.apache.solr.request.SolrQueryRequest;
Expand Down Expand Up @@ -187,7 +189,12 @@ public Collection<Api> getApis() {
@Override
public Collection<Class<? extends JerseyResource>> getJerseyResources() {
return List.of(
ListConfigSets.class, CloneConfigSet.class, DeleteConfigSet.class, UploadConfigSet.class);
ListConfigSets.class,
CloneConfigSet.class,
DeleteConfigSet.class,
UploadConfigSet.class,
DownloadConfigSet.class,
GetConfigSetFile.class);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.response.SolrQueryResponse;

/** V2 API implementation for ConfigsetsApi.Clone */
/**
* V2 API implementation for creating a new configset form an existing one.
*
* <p>This API (GET /v2/configsets) is analogous to the v1 /admin/configs?action=CREATE command.
*/
public class CloneConfigSet extends ConfigSetAPIBase implements ConfigsetsApi.Clone {

@Inject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.response.SolrQueryResponse;

/** V2 API implementation for ConfigsetsApi.Delete */
/**
* V2 API implementation for deleting a configset
*
* <p>This API (GET /v2/configsets) is analogous to the v1 /admin/configs?action=DELETE command.
*/
public class DeleteConfigSet extends ConfigSetAPIBase implements ConfigsetsApi.Delete {

@Inject
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*
* 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.
*/
package org.apache.solr.handler.configsets;

import static org.apache.solr.security.PermissionNameProvider.Name.CONFIG_READ_PERM;

import jakarta.inject.Inject;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.StreamingOutput;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.apache.commons.io.file.PathUtils;
import org.apache.solr.client.api.endpoint.ConfigsetsApi;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.util.StrUtils;
import org.apache.solr.core.ConfigSetService;
import org.apache.solr.core.CoreContainer;
import org.apache.solr.jersey.PermissionName;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.response.SolrQueryResponse;

/** V2 API implementation for downloading a configset as a zip file. */
public class DownloadConfigSet extends ConfigSetAPIBase implements ConfigsetsApi.Download {

@Inject
public DownloadConfigSet(
CoreContainer coreContainer,
SolrQueryRequest solrQueryRequest,
SolrQueryResponse solrQueryResponse) {
super(coreContainer, solrQueryRequest, solrQueryResponse);
}

@Override
@PermissionName(CONFIG_READ_PERM)
public Response downloadConfigSet(String configSetName, String displayName) throws Exception {
if (StrUtils.isNullOrEmpty(configSetName)) {
throw new SolrException(
SolrException.ErrorCode.BAD_REQUEST, "No configset name provided to download");
}
if (!configSetService.checkConfigExists(configSetName)) {
throw new SolrException(
SolrException.ErrorCode.NOT_FOUND, "ConfigSet " + configSetName + " not found!");
}
final String resolvedDisplayName =
StrUtils.isNullOrEmpty(displayName) ? configSetName : displayName;
return buildZipResponse(configSetService, configSetName, resolvedDisplayName);
}

/**
* Build a ZIP download {@link Response} for the given configset.
*
* @param configSetService the service to use for downloading the configset files
* @param configSetId the internal configset name to download (may differ from displayName, e.g.
* for schema-designer's mutable copies)
* @param displayName the user-visible name used to derive the download filename
*/
public static Response buildZipResponse(
ConfigSetService configSetService, String configSetId, String displayName)
throws IOException {
final byte[] zipBytes = zipConfigSet(configSetService, configSetId);
final String safeName = displayName.replaceAll("[^a-zA-Z0-9_\\-.]", "_");
final String fileName = safeName + "_configset.zip";
return Response.ok((StreamingOutput) outputStream -> outputStream.write(zipBytes))
.type("application/zip")
.header("Content-Disposition", "attachment; filename=\"" + fileName + "\"")
.build();
}

/**
* Download the named configset from {@link ConfigSetService} and return its contents as a ZIP
* archive byte array.
*/
public static byte[] zipConfigSet(ConfigSetService configSetService, String configSetId)
throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Path tmpDirectory = Files.createTempDirectory("configset-download-");
try {
configSetService.downloadConfig(configSetId, tmpDirectory);
try (ZipOutputStream zipOut = new ZipOutputStream(baos)) {
Files.walkFileTree(
tmpDirectory,
new SimpleFileVisitor<>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
throws IOException {
if (Files.isHidden(dir)) {
return FileVisitResult.SKIP_SUBTREE;
}
String dirName = tmpDirectory.relativize(dir).toString();
if (!dirName.isEmpty()) {
if (!dirName.endsWith("/")) {
dirName += "/";
}
zipOut.putNextEntry(new ZipEntry(dirName));
zipOut.closeEntry();
}
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws IOException {
if (!Files.isHidden(file)) {
try (InputStream fis = Files.newInputStream(file)) {
ZipEntry zipEntry = new ZipEntry(tmpDirectory.relativize(file).toString());
zipOut.putNextEntry(zipEntry);
fis.transferTo(zipOut);
}
}
return FileVisitResult.CONTINUE;
}
});
}
} finally {
PathUtils.deleteDirectory(tmpDirectory);
}
return baos.toByteArray();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* 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.
*/
package org.apache.solr.handler.configsets;

import static org.apache.solr.security.PermissionNameProvider.Name.CONFIG_READ_PERM;

import jakarta.inject.Inject;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import org.apache.solr.client.api.endpoint.ConfigsetsApi;
import org.apache.solr.client.api.model.ConfigSetFileContentsResponse;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.util.StrUtils;
import org.apache.solr.core.CoreContainer;
import org.apache.solr.jersey.PermissionName;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.response.SolrQueryResponse;

/** V2 API implementation for reading the contents of a single file from an existing configset. */
public class GetConfigSetFile extends ConfigSetAPIBase implements ConfigsetsApi.GetFile {

@Inject
public GetConfigSetFile(
CoreContainer coreContainer,
SolrQueryRequest solrQueryRequest,
SolrQueryResponse solrQueryResponse) {
super(coreContainer, solrQueryRequest, solrQueryResponse);
}

@Override
@PermissionName(CONFIG_READ_PERM)
public ConfigSetFileContentsResponse getConfigSetFile(String configSetName, String filePath)
throws Exception {
if (StrUtils.isNullOrEmpty(configSetName)) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "No configset name provided");
}
if (StrUtils.isNullOrEmpty(filePath)) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "No file path provided");
}
if (!configSetService.checkConfigExists(configSetName)) {
throw new SolrException(
SolrException.ErrorCode.NOT_FOUND, "ConfigSet '" + configSetName + "' not found");
}
byte[] data = downloadFileFromConfig(configSetName, filePath);
final var response = instantiateJerseyResponse(ConfigSetFileContentsResponse.class);
response.path = filePath;
response.content =
data != null && data.length > 0 ? new String(data, StandardCharsets.UTF_8) : "";
return response;
}

private byte[] downloadFileFromConfig(String configSetName, String filePath) {
try {
final byte[] data = configSetService.downloadFileFromConfig(configSetName, filePath);
if (data == null) {
throw new SolrException(
SolrException.ErrorCode.NOT_FOUND,
"File '" + filePath + "' not found in configset '" + configSetName + "'");
}
return data;
} catch (IOException e) {
throw new SolrException(
SolrException.ErrorCode.NOT_FOUND,
"File '" + filePath + "' not found in configset '" + configSetName + "'",
e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* V2 API implementation for uploading a configset as a zip file.
*
* <p>This API (GET /v2/configsets) is analogous to the v1 /admin/configs?action=UPLOAD command.
*/
public class UploadConfigSet extends ConfigSetAPIBase implements ConfigsetsApi.Upload {

private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
Expand Down
Loading
Loading