Autopsy  4.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
MessageServiceConnectionInfo.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2015 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.events;
20 
21 import java.net.URI;
22 import java.net.URISyntaxException;
23 import javax.annotation.concurrent.Immutable;
24 import javax.jms.Connection;
25 import javax.jms.JMSException;
26 import org.apache.activemq.ActiveMQConnectionFactory;
27 import java.io.IOException;
28 import java.net.InetAddress;
29 import java.util.MissingResourceException;
30 import org.openide.util.NbBundle;
31 
35 @Immutable
36 public final class MessageServiceConnectionInfo {
37 
38  private static final String MESSAGE_SERVICE_URI = "tcp://%s:%s?wireFormat.maxInactivityDuration=0"; //NON-NLS
39  private static final String CONNECTION_TIMED_OUT = "connection timed out"; //NON-NLS
40  private static final String CONNECTION_REFUSED = "connection refused"; //NON-NLS
41  private static final String PASSWORD_OR_USERNAME_BAD = "user name ["; //NON-NLS
42  private static final int IS_REACHABLE_TIMEOUT_MS = 1000;
43  private final String userName;
44  private final String password;
45  private final String host;
46  private final int port;
47 
61  public MessageServiceConnectionInfo(String host, int port, String userName, String password) {
62  this.host = host;
63  this.port = port;
64  this.userName = userName;
65  this.password = password;
66  }
67 
73  public String getUserName() {
74  return userName;
75  }
76 
82  public String getPassword() {
83  return password;
84  }
85 
92  public String getHost() {
93  return host;
94  }
95 
101  public int getPort() {
102  return port;
103  }
104 
113  URI getURI() throws URISyntaxException {
114  return new URI(String.format(MESSAGE_SERVICE_URI, getHost(), Integer.toString(getPort())));
115  }
116 
128  public void tryConnect() throws MessageServiceException {
129  if (host == null || host.isEmpty()) {
130  throw new MessageServiceException(NbBundle.getMessage(MessageServiceConnectionInfo.class, "MessageServiceConnectionInfo.MissingHostname")); //NON-NLS
131  } else if (userName == null) {
132  throw new MessageServiceException(NbBundle.getMessage(MessageServiceConnectionInfo.class, "MessageServiceConnectionInfo.MissingUsername")); //NON-NLS
133  } else if (password == null) {
134  throw new MessageServiceException(NbBundle.getMessage(MessageServiceConnectionInfo.class, "MessageServiceConnectionInfo.MissingPassword")); //NON-NLS
135  }
136  try {
137  ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(getUserName(), getPassword(), getURI());
138  Connection connection = connectionFactory.createConnection(getUserName(), getPassword());
139  connection.start();
140  connection.close();
141  } catch (URISyntaxException ex) {
142  // The hostname or port seems bad
143  throw new MessageServiceException(NbBundle.getMessage(MessageServiceConnectionInfo.class, "MessageServiceConnectionInfo.ConnectionCheck.HostnameOrPort")); //NON-NLS
144  } catch (JMSException ex) {
145  String result;
146  Throwable cause = ex.getCause();
147  if (null != cause && null != cause.getMessage()) {
148  // there is more information from another exception
149  String msg = cause.getMessage();
150  if (msg.startsWith(CONNECTION_TIMED_OUT)) {
151  // The hostname or IP address seems bad
152  result = NbBundle.getMessage(MessageServiceConnectionInfo.class, "MessageServiceConnectionInfo.ConnectionCheck.Hostname"); //NON-NLS
153  } else if (msg.toLowerCase().startsWith(CONNECTION_REFUSED)) {
154  // The port seems bad
155  result = NbBundle.getMessage(MessageServiceConnectionInfo.class, "MessageServiceConnectionInfo.ConnectionCheck.Port"); //NON-NLS
156  } else if (msg.toLowerCase().startsWith(PASSWORD_OR_USERNAME_BAD)) {
157  // The username or password seems bad
158  result = NbBundle.getMessage(MessageServiceConnectionInfo.class, "MessageServiceConnectionInfo.ConnectionCheck.UsernameAndPassword"); //NON-NLS
159  } else {
160  // Could be either hostname or port number
161  result = NbBundle.getMessage(MessageServiceConnectionInfo.class, "MessageServiceConnectionInfo.ConnectionCheck.HostnameOrPort"); //NON-NLS
162  }
163  } else {
164  // there is no more information from another exception
165  try {
166  if (InetAddress.getByName(getHost()).isReachable(IS_REACHABLE_TIMEOUT_MS)) {
167  // if we can reach the host, then it's probably a port problem
168  result = NbBundle.getMessage(MessageServiceConnectionInfo.class, "MessageServiceConnectionInfo.ConnectionCheck.Port"); //NON-NLS
169  } else {
170  result = NbBundle.getMessage(MessageServiceConnectionInfo.class, "MessageServiceConnectionInfo.ConnectionCheck.Hostname"); //NON-NLS
171  }
172  } catch (IOException | MissingResourceException any) {
173  // it may be anything
174  result = NbBundle.getMessage(MessageServiceConnectionInfo.class, "MessageServiceConnectionInfo.ConnectionCheck.Everything"); //NON-NLS
175  }
176  }
177  throw new MessageServiceException(result);
178  }
179  }
180 }
MessageServiceConnectionInfo(String host, int port, String userName, String password)

Copyright © 2012-2016 Basis Technology. Generated on: Tue Oct 25 2016
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.