use of java.net.ConnectException in project ats-framework by Axway.
the class AtsDbLoggerUtilities method attachFileToCurrentTest.
/**
* Attach a local file to the current test case in the Test Explorer DB.
* </br>The file must not be bigger than 10MB
*
* @param fileLocation the absolute path to the file
* @param testExplorerContextName the name of the web application, e.g. "TestExplorer" or "TestExplorer-3.11.0" etc.
* @param testExplorerPort the port of the web application, e.g. 8080
* @return TRUE if the operation was successful and false if not. A warning will be logged on failure.
*/
@PublicAtsApi
public boolean attachFileToCurrentTest(String fileLocation, String testExplorerContextName, int testExplorerPort) {
ERR_MSG_PREFIX = ERR_MSG_PREFIX.replace("{FILE}", fileLocation);
if (!checkFileExist(fileLocation)) {
return false;
}
if (!checkFileSizeIsNotTooLarge(fileLocation)) {
return false;
}
ActiveDbAppender dbAppender = ActiveDbAppender.getCurrentInstance();
if (dbAppender == null) {
logger.warn(ERR_MSG_PREFIX + "Perhaps the database logging is turned off");
return false;
}
final int runId = dbAppender.getRunId();
final int suiteId = dbAppender.getSuiteId();
final int testcaseId = dbAppender.getTestCaseId();
if (runId < 1 || suiteId < 1 || testcaseId < 1) {
logger.warn(ERR_MSG_PREFIX + "Perhaps the database logging is turned off or you are trying to log while a testcase is not yet started");
return false;
}
final String database = dbAppender.getDatabase();
final String host = dbAppender.getHost();
final String URL = "http://" + host + ":" + testExplorerPort + "/" + testExplorerContextName + "/UploadServlet";
URL url = null;
try {
CloseableHttpClient client = HttpClients.createDefault();
HttpPost post = new HttpPost(URL);
url = post.getURI().toURL();
if (!isURLConnetionAvailable(url)) {
return false;
}
logger.debug("POSTing " + fileLocation + " on " + URL);
File file = new File(fileLocation);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addBinaryBody("upfile", file, ContentType.DEFAULT_BINARY, fileLocation);
builder.addTextBody("dbName", database);
builder.addTextBody("runId", Integer.toString(runId));
builder.addTextBody("suiteId", Integer.toString(suiteId));
builder.addTextBody("testcaseId", Integer.toString(testcaseId));
HttpEntity entity = builder.build();
post.setEntity(entity);
checkPostExecutedSuccessfully(client.execute(post), fileLocation);
} catch (FileNotFoundException fnfe) {
logger.warn(ERR_MSG_PREFIX + "it does not exist on the local file system", fnfe);
return false;
} catch (ClientProtocolException cpe) {
logger.warn(ERR_MSG_PREFIX + "Upload to \"" + url + "\" failed", cpe);
return false;
} catch (ConnectException ce) {
logger.warn(ERR_MSG_PREFIX + "Upload to \"" + url + "\" failed", ce);
return false;
} catch (IOException ioe) {
logger.warn(ERR_MSG_PREFIX + "Upload to \"" + url + "\" failed", ioe);
return false;
}
logger.info("Successfully attached \"" + fileLocation + "\" to the current Test Explorer testcase");
return true;
}
use of java.net.ConnectException in project Gradle-demo by Arisono.
the class RetryIntercepter method intercept.
@SuppressWarnings("resource")
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Response response = chain.proceed(request);
try {
// OkhttpUtils.println("重试拦截器:"+response.isSuccessful());
while (!response.isSuccessful() && retryNum < maxRetry) {
retryNum++;
response = chain.proceed(request);
}
} catch (ConnectException e) {
OkhttpUtils.println("服务器拒绝连接.....");
} catch (IOException e) {
OkhttpUtils.println("服务器拒绝连接.....");
}
return response;
}
use of java.net.ConnectException in project zm-mailbox by Zimbra.
the class LocalConfigCLI method exec.
private void exec(String[] args) {
CommandLine cl = null;
CommandLineParser parser = new GnuParser();
try {
cl = parser.parse(mOptions, args);
} catch (ParseException pe) {
Logging.error("Failed to parse command line: " + pe);
System.exit(1);
}
if (cl.hasOption("q")) {
Logging.setQuietMode(true);
}
if (cl.hasOption("h")) {
usage();
}
// Load known keys from BackupLC if available
loadExtensionLC("com.zimbra.cs.backup.BackupLC");
// Load known keys from ZimbraOpenOfficeExt if available
loadExtensionLC("com.zimbra.openoffice.config.OpenOfficeLC");
// Load known keys from ZimbraVoice if available
loadExtensionLC("com.zimbra.cs.voice.VoiceLC");
// info/docs for supported keys
if (cl.hasOption("i")) {
checkCompatibleOptions("i", "q", cl);
LocalConfig.printDoc(System.out, cl.getArgs(), false);
return;
}
// info/docs for all keys (hidden option)
if (cl.hasOption("all")) {
checkCompatibleOptions("all", "q", cl);
LocalConfig.printDoc(System.out, cl.getArgs(), true);
return;
}
LocalConfig lc = null;
try {
lc = new LocalConfig(cl.getOptionValue("c"));
} catch (DocumentException de) {
error("failed when reading config file", de);
} catch (ConfigException ce) {
error("failed with error in config file", ce);
}
// edit
if (cl.hasOption("e")) {
checkNotRoot("-e");
checkCompatibleOptions("e", "qfrc", cl);
String[] av = cl.getArgs();
if (av == null || av.length == 0) {
error("insufficient arguments", null);
}
for (int i = 0; i < av.length; i++) {
String key = null;
String value = null;
if (cl.hasOption("r")) {
key = av[i];
value = RandomPassword.generate();
} else {
int eqidx = av[i].indexOf("=");
if (eqidx <= 0) {
// <= 0 also catches first char being =, ie no key specified
error("argument '" + av[i] + "' not in key=value form", null);
}
key = av[i].substring(0, eqidx);
value = av[i].substring(eqidx + 1, av[i].length());
}
if (KnownKey.needForceToEdit(key) && !cl.hasOption("f")) {
error("can not edit key " + key, null);
}
lc.set(key, value);
}
try {
lc.save();
} catch (Exception e) {
error("save to " + lc.getConfigFile() + " failed", e);
}
return;
}
// unset
if (cl.hasOption("u")) {
checkNotRoot("-u");
checkCompatibleOptions("u", "qfc", cl);
String[] av = cl.getArgs();
if (av == null || av.length == 0) {
error("insufficient arguments", null);
}
for (int i = 0; i < av.length; i++) {
String key = av[i];
if (!lc.isSet(key)) {
error("key " + key + " is not set", null);
}
lc.remove(key);
}
try {
lc.save();
} catch (Exception e) {
error("save to " + lc.getConfigFile() + " failed", e);
}
return;
}
// show path
if (cl.hasOption("p")) {
checkCompatibleOptions("p", "qc", cl);
System.out.println(lc.getConfigFile());
return;
}
if (cl.hasOption("l")) {
// reset logging and run native lib load
CliUtil.toolSetup("WARN");
try {
reload();
} catch (ServiceException e) {
if (e.getCause() instanceof ConnectException) {
error("'" + Provisioning.SERVICE_MAILBOX + "' service is not running", null);
} else {
error(e.getMessage(), e);
}
}
return;
}
// print values
String format = cl.getOptionValue("m");
ConfigWriter cwriter = null;
try {
cwriter = ConfigWriter.getInstance(format, cl.hasOption("x"), !cl.hasOption("s"));
} catch (ConfigException iae) {
error("failed to create writer " + format, iae);
}
try {
// changed
if (cl.hasOption("n")) {
checkCompatibleOptions("n", "qscmx", cl);
lc.printChanged(System.out, cwriter, cl.getArgs());
return;
}
// default
if (cl.hasOption("d")) {
checkCompatibleOptions("d", "qscmx", cl);
lc.printDefaults(System.out, cwriter, cl.getArgs());
return;
}
// current
checkCompatibleOptions("", "qscmx", cl);
lc.print(System.out, cwriter, cl.getArgs());
} catch (Exception e) {
error("exception occurred when printing", e);
}
}
use of java.net.ConnectException in project adempiere by adempiere.
the class Worker method run.
/**
* Worker: Read available Online Help Pages
*/
public void run() {
if (m_links == null)
return;
URL url = null;
try {
url = new URL(m_urlString);
} catch (Exception e) {
System.err.println("OnlineHelp.Worker.run (url) - " + e);
}
if (url == null)
return;
// Read Reference Page
try {
URLConnection conn = url.openConnection();
InputStream is = conn.getInputStream();
HTMLEditorKit kit = new HTMLEditorKit();
HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument();
doc.putProperty("IgnoreCharsetDirective", new Boolean(true));
kit.read(new InputStreamReader(is), doc, 0);
// Get The Links to the Help Pages
HTMLDocument.Iterator it = doc.getIterator(HTML.Tag.A);
Object target = null;
Object href = null;
while (it != null && it.isValid()) {
AttributeSet as = it.getAttributes();
// key keys
if (target == null || href == null) {
Enumeration en = as.getAttributeNames();
while (en.hasMoreElements()) {
Object o = en.nextElement();
if (target == null && o.toString().equals("target"))
// javax.swing.text.html.HTML$Attribute
target = o;
else if (href == null && o.toString().equals("href"))
href = o;
}
}
if (target != null && "Online".equals(as.getAttribute(target))) {
// Format: /help/<AD_Window_ID>/index.html
String hrefString = (String) as.getAttribute(href);
if (hrefString != null) {
try {
// System.err.println(hrefString);
String AD_Window_ID = hrefString.substring(hrefString.indexOf('/', 1), hrefString.lastIndexOf('/'));
m_links.put(AD_Window_ID, hrefString);
} catch (Exception e) {
System.err.println("OnlineHelp.Worker.run (help) - " + e);
}
}
}
it.next();
}
is.close();
} catch (ConnectException e) {
// System.err.println("OnlineHelp.Worker.run URL=" + url + " - " + e);
} catch (UnknownHostException uhe) {
// System.err.println("OnlineHelp.Worker.run " + uhe);
} catch (Exception e) {
System.err.println("OnlineHelp.Worker.run (e) " + e);
// e.printStackTrace();
} catch (Throwable t) {
System.err.println("OnlineHelp.Worker.run (t) " + t);
// t.printStackTrace();
}
// System.out.println("OnlineHelp - Links=" + m_links.size());
}
use of java.net.ConnectException in project platform_external_apache-http by android.
the class DefaultClientConnectionOperator method updateSecureConnection.
// openConnection
// non-javadoc, see interface ClientConnectionOperator
public void updateSecureConnection(OperatedClientConnection conn, HttpHost target, HttpContext context, HttpParams params) throws IOException {
if (conn == null) {
throw new IllegalArgumentException("Connection must not be null.");
}
if (target == null) {
throw new IllegalArgumentException("Target host must not be null.");
}
//@@@ is context allowed to be null?
if (params == null) {
throw new IllegalArgumentException("Parameters must not be null.");
}
if (!conn.isOpen()) {
throw new IllegalArgumentException("Connection must be open.");
}
final Scheme schm = schemeRegistry.getScheme(target.getSchemeName());
if (!(schm.getSocketFactory() instanceof LayeredSocketFactory)) {
throw new IllegalArgumentException("Target scheme (" + schm.getName() + ") must have layered socket factory.");
}
final LayeredSocketFactory lsf = (LayeredSocketFactory) schm.getSocketFactory();
final Socket sock;
try {
sock = lsf.createSocket(conn.getSocket(), target.getHostName(), schm.resolvePort(target.getPort()), true);
} catch (ConnectException ex) {
throw new HttpHostConnectException(target, ex);
}
prepareSocket(sock, context, params);
conn.update(sock, target, lsf.isSecure(sock), params);
//@@@ error handling: close the layered socket in case of exception?
}
Aggregations