Skip to content
This repository has been archived by the owner on Nov 26, 2024. It is now read-only.

Commit

Permalink
Added Chnages in Es-search in Property service
Browse files Browse the repository at this point in the history
  • Loading branch information
debasishchakraborty-egovt committed Jul 8, 2024
1 parent d03bf8f commit 0016f92
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -354,4 +354,10 @@ public MappingJackson2HttpMessageConverter jacksonConverter(ObjectMapper objectM
@Value("${inbox.property.search.allowed}")
private Boolean isInboxSearchAllowed;

@Value("${egov.indexer.es.username}")
private String esUsername;

@Value("${egov.indexer.es.password}")
private String esPassword;

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
import org.springframework.web.client.RestTemplate;

import java.util.List;
import org.springframework.context.annotation.Primary;
import javax.net.ssl.*;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Base64;

@Component
public class ElasticSearchRepository {
Expand All @@ -24,15 +29,13 @@ public class ElasticSearchRepository {

private FuzzySearchQueryBuilder queryBuilder;

private RestTemplate restTemplate;

private ObjectMapper mapper;

@Autowired
public ElasticSearchRepository(PropertyConfiguration config, FuzzySearchQueryBuilder queryBuilder, RestTemplate restTemplate, ObjectMapper mapper) {
public ElasticSearchRepository(PropertyConfiguration config, FuzzySearchQueryBuilder queryBuilder, ObjectMapper mapper) {
this.config = config;
this.queryBuilder = queryBuilder;
this.restTemplate = restTemplate;
this.mapper = mapper;
}

Expand All @@ -52,10 +55,13 @@ public Object fuzzySearchProperties(PropertyCriteria criteria, List<String> uuid

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.add("Authorization", getESEncodedCredentials());
final HttpEntity entity = new HttpEntity( headers);
// response = restTemplate.exchange(url.toString(), HttpMethod.GET, entity, Map.class);
HttpEntity<String> requestEntity = new HttpEntity<>(searchQuery, headers);
ResponseEntity response = null;
try {
response = restTemplate.postForEntity(url, requestEntity, Object.class);
response = this.restTemplate().postForEntity(url, requestEntity, Object.class);

} catch (Exception e) {
e.printStackTrace();
Expand All @@ -80,7 +86,45 @@ private String getESURL() {

return builder.toString();
}
public String getESEncodedCredentials() {
String credentials = config.getEsUsername() + ":" + config.getEsPassword();
byte[] credentialsBytes = credentials.getBytes();
byte[] base64CredentialsBytes = Base64.getEncoder().encode(credentialsBytes);
return "Basic " + new String(base64CredentialsBytes);
}
public static void trustSelfSignedSSL() {
try {
SSLContext ctx = SSLContext.getInstance("TLS");
X509TrustManager tm = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException {
}

public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException {
}

public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
ctx.init(null, new TrustManager[]{tm}, null);
SSLContext.setDefault(ctx);

// Disable hostname verification
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
public boolean verify(String hostname, javax.net.ssl.SSLSession sslSession) {
return true;
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
}

@Primary
public RestTemplate restTemplate() {
trustSelfSignedSSL();
return new RestTemplate();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ state.level.tenant.id=pb
#Elastic search properties
elasticsearch.host=http://localhost:9200/
elasticsearch.search.endpoint=/_search
egov.indexer.es.username=elastic
egov.indexer.es.password=8fwbD6HbJh6HU0oddsHm8TEI

property.es.index=property-services
pt.search.name.fuziness=2
Expand Down

0 comments on commit 0016f92

Please sign in to comment.