19 package org.sleuthkit.autopsy.keywordsearch;
21 import java.io.IOException;
22 import java.util.HashMap;
23 import org.apache.solr.client.solrj.SolrServerException;
24 import org.apache.solr.client.solrj.impl.HttpSolrServer;
30 import org.apache.solr.common.util.ContentStreamBase.StringStream;
31 import org.openide.util.lookup.ServiceProvider;
37 import org.openide.util.NbBundle;
38 import java.net.InetAddress;
39 import java.util.MissingResourceException;
46 @ServiceProvider(service = KeywordSearchService.class)
49 private static final String BAD_IP_ADDRESS_FORMAT =
"ioexception occurred when talking to server";
50 private static final String SERVER_REFUSED_CONNECTION =
"server refused connection";
51 private static final int IS_REACHABLE_TIMEOUT_MS = 1000;
54 public void indexArtifact(BlackboardArtifact artifact)
throws TskCoreException {
55 if (artifact == null) {
61 long artifactId = artifact.getArtifactID();
70 }
catch (IllegalStateException ignore) {
76 if (sleuthkitCase == null) {
81 AbstractFile abstractFile = sleuthkitCase.getAbstractFileById(artifact.getObjectID());
82 if (abstractFile != null) {
83 dataSource = abstractFile.getDataSource();
85 dataSource = sleuthkitCase.getContentById(artifact.getObjectID());
88 if (dataSource == null) {
94 StringBuilder artifactContents =
new StringBuilder();
96 for (BlackboardAttribute attribute : artifact.getAttributes()) {
97 artifactContents.append(attribute.getAttributeType().getDisplayName());
98 artifactContents.append(
" : ");
112 if (attribute.getAttributeType().getTypeID() == BlackboardAttribute.ATTRIBUTE_TYPE.TSK_DATETIME.getTypeID()
113 || attribute.getAttributeType().getTypeID() == BlackboardAttribute.ATTRIBUTE_TYPE.TSK_DATETIME_ACCESSED.getTypeID()
114 || attribute.getAttributeType().getTypeID() == BlackboardAttribute.ATTRIBUTE_TYPE.TSK_DATETIME_CREATED.getTypeID()
115 || attribute.getAttributeType().getTypeID() == BlackboardAttribute.ATTRIBUTE_TYPE.TSK_DATETIME_MODIFIED.getTypeID()
116 || attribute.getAttributeType().getTypeID() == BlackboardAttribute.ATTRIBUTE_TYPE.TSK_DATETIME_RCVD.getTypeID()
117 || attribute.getAttributeType().getTypeID() == BlackboardAttribute.ATTRIBUTE_TYPE.TSK_DATETIME_SENT.getTypeID()
118 || attribute.getAttributeType().getTypeID() == BlackboardAttribute.ATTRIBUTE_TYPE.TSK_DATETIME_START.getTypeID()
119 || attribute.getAttributeType().getTypeID() == BlackboardAttribute.ATTRIBUTE_TYPE.TSK_DATETIME_END.getTypeID()) {
123 artifactContents.append(attribute.getDisplayString());
125 artifactContents.append(System.lineSeparator());
128 if (artifactContents.length() == 0) {
138 HashMap<String, String> solrFields =
new HashMap<>();
139 String documentId = Long.toString(artifactId);
141 solrFields.put(
Server.
Schema.ID.toString(), documentId);
144 solrFields.put(
Server.
Schema.IMAGE_ID.toString(), Long.toString(dataSource.getId()));
147 Ingester.getDefault().ingest(
new StringStream(
""), solrFields, 0);
148 }
catch (Ingester.IngesterException ex) {
149 throw new TskCoreException(ex.getCause().getMessage(), ex);
157 documentId +=
"_" + Long.toString(chunkId);
158 solrFields.replace(
Server.
Schema.ID.toString(), documentId);
160 StringStream contentStream =
new StringStream(artifactContents.toString());
163 Ingester.getDefault().ingest(contentStream, solrFields, contentStream.getSize());
164 }
catch (Ingester.IngesterException ex) {
165 throw new TskCoreException(ex.getCause().getMessage(), ex);
188 HttpSolrServer solrServer = null;
189 if (host == null || host.isEmpty()) {
193 solrServer =
new HttpSolrServer(
"http://" + host +
":" + Integer.toString(port) +
"/solr");
195 }
catch (SolrServerException ex) {
197 }
catch (IOException ex) {
198 String result = NbBundle.getMessage(
SolrSearchService.class,
"SolrConnectionCheck.HostnameOrPort");
199 String message = ex.getCause().getMessage().toLowerCase();
200 if (message.startsWith(SERVER_REFUSED_CONNECTION)) {
202 if (InetAddress.getByName(host).isReachable(IS_REACHABLE_TIMEOUT_MS)) {
204 result = NbBundle.getMessage(
SolrSearchService.class,
"SolrConnectionCheck.Port");
206 result = NbBundle.getMessage(
SolrSearchService.class,
"SolrConnectionCheck.HostnameOrPort");
208 }
catch (IOException | MissingResourceException any) {
210 result = NbBundle.getMessage(
SolrSearchService.class,
"SolrConnectionCheck.HostnameOrPort");
212 }
else if (message.startsWith(BAD_IP_ADDRESS_FORMAT)) {
213 result = NbBundle.getMessage(
SolrSearchService.class,
"SolrConnectionCheck.Hostname");
216 }
catch (NumberFormatException ex) {
218 }
catch (IllegalArgumentException ex) {
221 if (null != solrServer) {
222 solrServer.shutdown();
228 public void close() throws IOException {
static String getStringTime(long epochSeconds, TimeZone tzone)
void indexArtifact(BlackboardArtifact artifact)
static synchronized Server getServer()
SleuthkitCase getSleuthkitCase()
static Case getCurrentCase()
void tryConnect(String host, int port)