create ApiService and use it
This commit is contained in:
@@ -141,13 +141,13 @@ public abstract class AbstractUnregisterNodeWebScript<T extends NodeParameterSet
|
||||
|
||||
protected StatusResponse getCoreStatus(String nodeHostname, int nodePort, String core) {
|
||||
this.logger.debug("Retrieving status for core {} on ASIE node: {}", core, nodeHostname);
|
||||
CoreAdminApi api = this.createApi(nodeHostname, nodePort);
|
||||
CoreAdminApi api = this.getApiService().createApi(nodeHostname, nodePort, CoreAdminApi.class);
|
||||
return api.getStatus(new StatusRequest().withCore(core));
|
||||
}
|
||||
|
||||
protected void unloadCore(String nodeHostname, int nodePort, String core) {
|
||||
this.logger.info("Unloading core {} on ASIE node: {}", core, nodeHostname);
|
||||
CoreAdminApi api = this.createApi(nodeHostname, nodePort);
|
||||
CoreAdminApi api = this.getApiService().createApi(nodeHostname, nodePort, CoreAdminApi.class);
|
||||
api.unload(new UnloadRequest().withCore(core));
|
||||
}
|
||||
|
||||
|
@@ -63,7 +63,7 @@ public class ReloadNodeShardWebScript extends AbstractAsieNodeWebScript {
|
||||
throw new WebScriptException(HttpStatus.NOT_FOUND.value(), "The specified node/shard could not be found or formulated");
|
||||
|
||||
this.logger.info("Reloading core {} on ASIE node: {}", coreName, nodeHostname);
|
||||
CoreAdminApi api = this.createApi(nodeHostname, nodePort);
|
||||
CoreAdminApi api = this.getApiService().createApi(nodeHostname, nodePort, CoreAdminApi.class);
|
||||
try {
|
||||
api.create(new CreateRequest()
|
||||
.withCore(coreName)
|
||||
|
@@ -64,7 +64,7 @@ public class ReloadNodeWebScript extends AbstractAsieNodeWebScript {
|
||||
String coreInstancePath = core.getValue();
|
||||
|
||||
this.logger.info("Reloading core {} on ASIE node: {}", coreName, nodeHostname);
|
||||
CoreAdminApi api = this.createApi(nodeHostname, nodePort);
|
||||
CoreAdminApi api = this.getApiService().createApi(nodeHostname, nodePort, CoreAdminApi.class);
|
||||
try {
|
||||
api.create(new CreateRequest()
|
||||
.withCore(coreName)
|
||||
|
@@ -20,7 +20,7 @@ public abstract class AbstractAsieNodeShardWebScript extends AbstractAsieShardab
|
||||
String nodeEndpoint = this.getRequiredPathParameter(req, "nodeEndpoint");
|
||||
int colon = nodeEndpoint.lastIndexOf(':');
|
||||
String nodeHostname = colon < 0 ? nodeEndpoint : nodeEndpoint.substring(0, colon);
|
||||
int nodePort = colon < 0 ? this.getDefaultSolrPort() : Integer.parseInt(nodeEndpoint.substring(colon+1));
|
||||
int nodePort = colon < 0 ? this.getApiService().getDefaultSolrPort() : Integer.parseInt(nodeEndpoint.substring(colon+1));
|
||||
|
||||
ShardSet shardSet = this.getRequiredPathParameter(req, "shardSet", ShardSet.class);
|
||||
int shardId = this.getRequiredPathParameter(req, "shardId", Integer.class);
|
||||
|
@@ -31,7 +31,7 @@ public abstract class AbstractAsieNodeWebScript extends AbstractAsieShardableWeb
|
||||
int colon = nodeEndpoint.lastIndexOf(':');
|
||||
String nodeHostname = colon < 0 ? nodeEndpoint : nodeEndpoint.substring(0, colon);
|
||||
nodeHostname = nodeHostname.replace('_', '.');
|
||||
int nodePort = colon < 0 ? this.getDefaultSolrPort() : Integer.parseInt(nodeEndpoint.substring(colon+1));
|
||||
int nodePort = colon < 0 ? this.getApiService().getDefaultSolrPort() : Integer.parseInt(nodeEndpoint.substring(colon+1));
|
||||
|
||||
this.execute(req, res, nodeHostname, nodePort);
|
||||
}
|
||||
|
@@ -122,7 +122,7 @@ public abstract class AbstractAsieShardableWebScript extends AbstractAsieWebScri
|
||||
}
|
||||
|
||||
protected CoreAdminApi getApi(ShardInstance shard) {
|
||||
return this.createApi(shard.getHostName(), shard.getPort());
|
||||
return this.getApiService().createApi(shard.getHostName(), shard.getPort(), CoreAdminApi.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,117 +1,34 @@
|
||||
package com.inteligr8.alfresco.asie.rest;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.extensions.webscripts.WebScriptRequest;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.inteligr8.alfresco.asie.Constants;
|
||||
import com.inteligr8.alfresco.asie.api.CoreAdminApi;
|
||||
import com.inteligr8.rs.AuthorizationFilter;
|
||||
import com.inteligr8.rs.Client;
|
||||
import com.inteligr8.rs.ClientCxfConfiguration;
|
||||
import com.inteligr8.rs.ClientCxfImpl;
|
||||
|
||||
import jakarta.ws.rs.client.ClientRequestContext;
|
||||
import com.inteligr8.alfresco.asie.service.ApiService;
|
||||
|
||||
public abstract class AbstractAsieWebScript extends AbstractWebScript {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||
|
||||
@Value("${solr.secureComms}")
|
||||
private String solrSecureComms;
|
||||
|
||||
@Value("${solr.port}")
|
||||
private int solrPort;
|
||||
|
||||
@Value("${solr.port.ssl}")
|
||||
private int solrSslPort;
|
||||
|
||||
@Value("${solr.sharedSecret.header}")
|
||||
private String solrSharedSecretHeader;
|
||||
|
||||
@Value("${solr.sharedSecret}")
|
||||
private String solrSharedSecret;
|
||||
|
||||
@Value("${inteligr8.asie.basePath}")
|
||||
private String solrBaseUrl;
|
||||
|
||||
@Autowired
|
||||
@Qualifier(Constants.QUALIFIER_ASIE)
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
super.afterPropertiesSet();
|
||||
this.solrSharedSecret = StringUtils.trimToNull(this.solrSharedSecret);
|
||||
}
|
||||
@Autowired
|
||||
private ApiService api;
|
||||
|
||||
protected ObjectMapper getObjectMapper() {
|
||||
return this.objectMapper;
|
||||
}
|
||||
|
||||
protected CoreAdminApi createApi(String hostname, int port) {
|
||||
String solrBaseUrl = this.formulateSolrBaseUrl(hostname, port);
|
||||
this.logger.trace("Using Solr base URL: {}", solrBaseUrl);
|
||||
Client solrClient = this.createClient(solrBaseUrl);
|
||||
return this.getApi(solrClient);
|
||||
}
|
||||
|
||||
protected CoreAdminApi getApi(Client solrClient) {
|
||||
return solrClient.getApi(CoreAdminApi.class);
|
||||
}
|
||||
|
||||
protected int getDefaultSolrPort() {
|
||||
boolean isSsl = "https".equals(this.solrSecureComms);
|
||||
return isSsl ? this.solrSslPort : this.solrPort;
|
||||
protected ApiService getApiService() {
|
||||
return this.api;
|
||||
}
|
||||
|
||||
protected String formulateSolrBaseUrl(WebScriptRequest req) {
|
||||
String hostname = this.getRequiredPathParameter(req, "hostname");
|
||||
Integer port = this.getOptionalPathParameter(req, "port", Integer.class);
|
||||
return this.formulateSolrBaseUrl(hostname, port);
|
||||
}
|
||||
|
||||
protected String formulateSolrBaseUrl(String hostname, Integer port) {
|
||||
boolean isSsl = "https".equals(this.solrSecureComms);
|
||||
StringBuilder baseUrl = new StringBuilder(isSsl ? "https" : "http").append("://").append(hostname);
|
||||
baseUrl.append(':').append(port == null ? (isSsl ? this.solrSslPort : this.solrPort) : port);
|
||||
baseUrl.append(this.solrBaseUrl);
|
||||
return baseUrl.toString();
|
||||
}
|
||||
|
||||
protected Client createClient(final String baseUrl) {
|
||||
ClientCxfImpl client = new ClientCxfImpl(new ClientCxfConfiguration() {
|
||||
@Override
|
||||
public String getBaseUrl() {
|
||||
return baseUrl.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public AuthorizationFilter createAuthorizationFilter() {
|
||||
return solrSharedSecret == null ? null : new AuthorizationFilter() {
|
||||
@Override
|
||||
public void filter(ClientRequestContext requestContext) throws IOException {
|
||||
logger.debug("Adding authorization headers for ASIE shared auth: {}", solrSharedSecretHeader);
|
||||
requestContext.getHeaders().putSingle(solrSharedSecretHeader, solrSharedSecret);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDefaultBusEnabled() {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
client.register();
|
||||
return client;
|
||||
return this.api.formulateSolrBaseUrl(hostname, port);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -0,0 +1,118 @@
|
||||
package com.inteligr8.alfresco.asie.service;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
|
||||
import org.alfresco.repo.index.shard.ShardInstance;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.inteligr8.alfresco.asie.model.SolrHost;
|
||||
import com.inteligr8.rs.AuthorizationFilter;
|
||||
import com.inteligr8.rs.Client;
|
||||
import com.inteligr8.rs.ClientCxfConfiguration;
|
||||
import com.inteligr8.rs.ClientCxfImpl;
|
||||
|
||||
import jakarta.ws.rs.client.ClientRequestContext;
|
||||
|
||||
@Component
|
||||
public class ApiService implements InitializingBean {
|
||||
|
||||
public static final String SOLR_CORE = "alfresco";
|
||||
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||
|
||||
@Value("${solr.secureComms}")
|
||||
private String solrSecureComms;
|
||||
|
||||
@Value("${solr.port}")
|
||||
private int solrPort;
|
||||
|
||||
@Value("${solr.port.ssl}")
|
||||
private int solrSslPort;
|
||||
|
||||
@Value("${solr.sharedSecret.header}")
|
||||
private String solrSharedSecretHeader;
|
||||
|
||||
@Value("${solr.sharedSecret}")
|
||||
private String solrSharedSecret;
|
||||
|
||||
@Value("${inteligr8.asie.basePath}")
|
||||
private String solrBaseUrl;
|
||||
|
||||
@Value("${inteligr8.asie.reconciliation.nodesChunkSize:250}")
|
||||
private int nodesChunkSize;
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
this.solrSharedSecret = StringUtils.trimToNull(this.solrSharedSecret);
|
||||
}
|
||||
|
||||
public <T> T createApi(String hostname, int port, Class<T> apiClass) {
|
||||
String solrBaseUrl = this.formulateSolrBaseUrl(hostname, port);
|
||||
this.logger.trace("Using Solr base URL: {}", solrBaseUrl);
|
||||
return this.createApi(solrBaseUrl, apiClass);
|
||||
}
|
||||
|
||||
public <T> T createApi(ShardInstance instance, Class<T> apiClass) {
|
||||
URL url = SolrHost.from(instance).toUrl("http");
|
||||
return this.createApi(url.toString(), apiClass);
|
||||
}
|
||||
|
||||
public <T> T createApi(String solrBaseUrl, Class<T> apiClass) {
|
||||
Client solrClient = this.createClient(solrBaseUrl);
|
||||
return this.getApi(solrClient, apiClass);
|
||||
}
|
||||
|
||||
public <T> T getApi(Client solrClient, Class<T> apiClass) {
|
||||
return solrClient.getApi(apiClass);
|
||||
}
|
||||
|
||||
public boolean isSecure() {
|
||||
return "https".equals(this.solrSecureComms);
|
||||
}
|
||||
|
||||
public int getDefaultSolrPort() {
|
||||
return this.isSecure() ? this.solrSslPort : this.solrPort;
|
||||
}
|
||||
|
||||
public String formulateSolrBaseUrl(String hostname, Integer port) {
|
||||
boolean isSsl = "https".equals(this.solrSecureComms);
|
||||
StringBuilder baseUrl = new StringBuilder(isSsl ? "https" : "http").append("://").append(hostname);
|
||||
baseUrl.append(':').append(port == null ? (isSsl ? this.solrSslPort : this.solrPort) : port);
|
||||
baseUrl.append(this.solrBaseUrl);
|
||||
return baseUrl.toString();
|
||||
}
|
||||
|
||||
public Client createClient(final String baseUrl) {
|
||||
ClientCxfImpl client = new ClientCxfImpl(new ClientCxfConfiguration() {
|
||||
@Override
|
||||
public String getBaseUrl() {
|
||||
return baseUrl.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public AuthorizationFilter createAuthorizationFilter() {
|
||||
return solrSharedSecret == null ? null : new AuthorizationFilter() {
|
||||
@Override
|
||||
public void filter(ClientRequestContext requestContext) throws IOException {
|
||||
logger.debug("Adding authorization headers for ASIE shared auth: {}", solrSharedSecretHeader);
|
||||
requestContext.getHeaders().putSingle(solrSharedSecretHeader, solrSharedSecret);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDefaultBusEnabled() {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
client.register();
|
||||
return client;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user