001/* 002 * Copyright 2026 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.cms.search.solr; 017 018import java.util.List; 019 020import org.apache.commons.lang3.StringUtils; 021import org.apache.solr.client.solrj.SolrClient; 022import org.apache.solr.client.solrj.impl.Http2SolrClient; 023import org.apache.solr.client.solrj.request.CoreAdminRequest; 024import org.apache.solr.common.SolrException; 025import org.apache.solr.common.SolrException.ErrorCode; 026import org.apache.solr.common.params.CoreAdminParams.CoreAdminAction; 027 028import org.ametys.runtime.model.checker.ItemChecker; 029import org.ametys.runtime.model.checker.ItemCheckerTestFailureException; 030 031/** 032 * Check the validity of the Solr configuration 033 */ 034public class SolrChecker implements ItemChecker 035{ 036 public void check(List<String> values) throws ItemCheckerTestFailureException 037 { 038 String configUrl = values.get(0); 039 String login = values.get(1); 040 String password = values.get(2); 041 Http2SolrClient.Builder solrReadClientBuilder = new Http2SolrClient.Builder(configUrl); 042 if (StringUtils.isNotBlank(login)) 043 { 044 solrReadClientBuilder.withBasicAuthCredentials(login, password); 045 } 046 try (SolrClient client = solrReadClientBuilder.build()) 047 { 048 CoreAdminRequest req = new CoreAdminRequest(); 049 req.setAction(CoreAdminAction.STATUS); 050 req.setIndexInfoNeeded(false); 051 052 req.process(client); 053 } 054 catch (SolrException e) 055 { 056 throw new ItemCheckerTestFailureException("An errror occured while trying to contact the solr server (" + ErrorCode.getErrorCode(e.code()) + ")", e); 057 } 058 catch (Exception e) 059 { 060 throw new ItemCheckerTestFailureException("An errror occured while trying to contact the solr server", e); 061 } 062 } 063 064}