================================================================= */ \r
package org.apache.poi.poifs.crypt;\r
\r
-import static org.junit.Assert.assertEquals;\r
-import static org.junit.Assert.assertFalse;\r
-import static org.junit.Assert.assertNotNull;\r
-import static org.junit.Assert.assertTrue;\r
+import static org.junit.Assert.*;\r
\r
import java.io.File;\r
import java.io.FileInputStream;\r
import java.io.InputStream;\r
import java.lang.reflect.Method;\r
import java.net.ConnectException;\r
+import java.net.HttpURLConnection;\r
+import java.net.MalformedURLException;\r
+import java.net.URL;\r
import java.security.Key;\r
import java.security.KeyPair;\r
import java.security.KeyStore;\r
signatureConfig.addSignatureFacet(new XAdESSignatureFacet());\r
signatureConfig.addSignatureFacet(new XAdESXLSignatureFacet());\r
\r
- // check for internet\r
- Process p1 = Runtime.getRuntime().exec("ping www.google.com");\r
- boolean mockTsp = (p1.waitFor() == 1);\r
+ // check for internet, no error means it works\r
+ boolean mockTsp = (getAccessError("http://timestamp.comodoca.com/rfc3161", true, 10000) == null);\r
+ \r
// http://timestamping.edelweb.fr/service/tsp\r
// http://tsa.belgium.be/connect\r
// http://timestamp.comodoca.com/authenticode\r
pkg.close();\r
}\r
\r
+ public static String getAccessError(String destinationUrl, boolean fireRequest, int timeout) {\r
+ URL url;\r
+ try {\r
+ url = new URL(destinationUrl);\r
+ } catch (MalformedURLException e) {\r
+ throw new IllegalArgumentException("Invalid destination URL", e);\r
+ }\r
+\r
+ HttpURLConnection conn = null;\r
+ try {\r
+ conn = (HttpURLConnection) url.openConnection();\r
+\r
+ // set specified timeout if non-zero\r
+ if(timeout != 0) {\r
+ conn.setConnectTimeout(timeout);\r
+ conn.setReadTimeout(timeout);\r
+ }\r
+\r
+ conn.setDoOutput(false);\r
+ conn.setDoInput(true);\r
+\r
+ /* if connecting is not possible this will throw a connection refused exception */\r
+ conn.connect();\r
+\r
+ if (fireRequest) {\r
+ InputStream is = null;\r
+ try {\r
+ is = conn.getInputStream();\r
+ } finally {\r
+ IOUtils.closeQuietly(is);\r
+ }\r
+\r
+ }\r
+ /* if connecting is possible we return true here */\r
+ return null;\r
+\r
+ } catch (IOException e) {\r
+ /* exception is thrown -> server not available */\r
+ return e.getClass().getName() + ": " + e.getMessage();\r
+ } finally {\r
+ if (conn != null) {\r
+ conn.disconnect();\r
+ }\r
+ }\r
+ }\r
+ \r
@Test\r
public void testCertChain() throws Exception {\r
KeyStore keystore = KeyStore.getInstance("PKCS12");\r