|
| 1 | +package org.testcontainers.containers; |
| 2 | + |
| 3 | +import java.io.ByteArrayOutputStream; |
| 4 | +import java.io.IOException; |
| 5 | +import java.net.URISyntaxException; |
| 6 | +import java.net.URL; |
| 7 | +import java.util.Arrays; |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.List; |
| 10 | +import java.util.Map; |
| 11 | +import java.util.zip.ZipEntry; |
| 12 | +import java.util.zip.ZipOutputStream; |
| 13 | + |
| 14 | +import org.apache.commons.io.IOUtils; |
| 15 | + |
| 16 | +import okhttp3.HttpUrl; |
| 17 | +import okhttp3.MediaType; |
| 18 | +import okhttp3.OkHttpClient; |
| 19 | +import okhttp3.Request; |
| 20 | +import okhttp3.RequestBody; |
| 21 | +import okhttp3.Response; |
| 22 | + |
| 23 | +/** |
| 24 | + * Utils class which can create collections and configurations. |
| 25 | + * |
| 26 | + * @author Simon Schneider |
| 27 | + */ |
| 28 | +public class SolrClientUtils { |
| 29 | + |
| 30 | + private static OkHttpClient httpClient = new OkHttpClient(); |
| 31 | + |
| 32 | + /** |
| 33 | + * Creates a new configuration and uploads the solrconfig.xml and schema.xml |
| 34 | + * |
| 35 | + * @param hostname the Hostname under which solr is reachable |
| 36 | + * @param port the Port on which solr is running |
| 37 | + * @param configurationName the name of the configuration which should be created |
| 38 | + * @param solrConfig the url under which the solrconfig.xml can be found |
| 39 | + * @param solrSchema the url under which the schema.xml can be found or null if the default schema should be used |
| 40 | + */ |
| 41 | + public static void uploadConfiguration(String hostname, int port, String configurationName, URL solrConfig, URL solrSchema) throws URISyntaxException, IOException { |
| 42 | + Map<String, String> parameters = new HashMap<>(); |
| 43 | + parameters.put("action", "UPLOAD"); |
| 44 | + parameters.put("name", configurationName); |
| 45 | + HttpUrl url = generateSolrURL(hostname, port, Arrays.asList("admin", "configs"), parameters); |
| 46 | + |
| 47 | + byte[] configurationZipFile = generateConfigZipFile(solrConfig, solrSchema); |
| 48 | + executePost(url, configurationZipFile); |
| 49 | + |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Creates a new collection |
| 54 | + * |
| 55 | + * @param hostname the Hostname under which solr is reachable |
| 56 | + * @param port The Port on which solr is running |
| 57 | + * @param collectionName the name of the collection which should be created |
| 58 | + * @param configurationName the name of the configuration which should used to create the collection |
| 59 | + * or null if the default configuration should be used |
| 60 | + */ |
| 61 | + public static void createCollection(String hostname, int port, String collectionName, String configurationName) throws URISyntaxException, IOException { |
| 62 | + Map<String, String> parameters = new HashMap<>(); |
| 63 | + parameters.put("action", "CREATE"); |
| 64 | + parameters.put("name", collectionName); |
| 65 | + parameters.put("numShards", "1"); |
| 66 | + parameters.put("replicationFactor", "1"); |
| 67 | + parameters.put("wt", "json"); |
| 68 | + if (configurationName != null) { |
| 69 | + parameters.put("collection.configName", configurationName); |
| 70 | + } |
| 71 | + HttpUrl url = generateSolrURL(hostname, port, Arrays.asList("admin", "collections"), parameters); |
| 72 | + executePost(url, null); |
| 73 | + } |
| 74 | + |
| 75 | + private static void executePost(HttpUrl url, byte[] data) throws IOException { |
| 76 | + |
| 77 | + RequestBody requestBody = data == null ? |
| 78 | + RequestBody.create(MediaType.parse("text/plain"), "") : |
| 79 | + RequestBody.create(MediaType.parse("application/octet-stream"), data); |
| 80 | + ; |
| 81 | + |
| 82 | + Request request = new Request.Builder() |
| 83 | + .url(url) |
| 84 | + .post(requestBody) |
| 85 | + .build(); |
| 86 | + Response response = httpClient.newCall(request).execute(); |
| 87 | + if (!response.isSuccessful()) { |
| 88 | + String responseBody = ""; |
| 89 | + if (response.body() != null) { |
| 90 | + responseBody = response.body().string(); |
| 91 | + response.close(); |
| 92 | + } |
| 93 | + throw new SolrClientUtilsException(response.code(), "Unable to upload binary\n" + responseBody); |
| 94 | + } |
| 95 | + if (response.body() != null) { |
| 96 | + response.close(); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + private static HttpUrl generateSolrURL(String hostname, int port, List<String> pathSegments, Map<String, String> parameters) throws URISyntaxException { |
| 101 | + HttpUrl.Builder builder = new HttpUrl.Builder(); |
| 102 | + builder.scheme("http"); |
| 103 | + builder.host(hostname); |
| 104 | + builder.port(port); |
| 105 | + // Path |
| 106 | + builder.addPathSegment("solr"); |
| 107 | + if (pathSegments != null) { |
| 108 | + pathSegments.forEach(builder::addPathSegment); |
| 109 | + } |
| 110 | + // Query Parameters |
| 111 | + parameters.forEach(builder::addQueryParameter); |
| 112 | + return builder.build(); |
| 113 | + } |
| 114 | + |
| 115 | + private static byte[] generateConfigZipFile(URL solrConfiguration, URL solrSchema) throws IOException { |
| 116 | + ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| 117 | + ZipOutputStream zipOutputStream = new ZipOutputStream(bos); |
| 118 | + // SolrConfig |
| 119 | + zipOutputStream.putNextEntry(new ZipEntry("solrconfig.xml")); |
| 120 | + IOUtils.copy(solrConfiguration.openStream(), zipOutputStream); |
| 121 | + zipOutputStream.closeEntry(); |
| 122 | + |
| 123 | + // Solr Schema |
| 124 | + if (solrSchema != null) { |
| 125 | + zipOutputStream.putNextEntry(new ZipEntry("schema.xml")); |
| 126 | + IOUtils.copy(solrSchema.openStream(), zipOutputStream); |
| 127 | + zipOutputStream.closeEntry(); |
| 128 | + } |
| 129 | + |
| 130 | + zipOutputStream.close(); |
| 131 | + return bos.toByteArray(); |
| 132 | + } |
| 133 | +} |
0 commit comments