use of java.net.URLConnection in project checkstyle by checkstyle.
the class PackageNamesLoaderTest method testPackagesWithSaxException.
@Test
@SuppressWarnings("unchecked")
public void testPackagesWithSaxException() throws Exception {
final URLConnection mockConnection = Mockito.mock(URLConnection.class);
when(mockConnection.getInputStream()).thenReturn(new ByteArrayInputStream(EMPTY_BYTE_ARRAY));
final URL url = getMockUrl(mockConnection);
final Enumeration<URL> enumeration = mock(Enumeration.class);
when(enumeration.hasMoreElements()).thenReturn(true);
when(enumeration.nextElement()).thenReturn(url);
final ClassLoader classLoader = mock(ClassLoader.class);
when(classLoader.getResources("checkstyle_packages.xml")).thenReturn(enumeration);
try {
PackageNamesLoader.getPackageNames(classLoader);
fail("CheckstyleException is expected");
} catch (CheckstyleException ex) {
assertTrue(ex.getCause() instanceof SAXException);
}
}
use of java.net.URLConnection in project hadoop by apache.
the class TestNameNodeHttpServer method canAccess.
private static boolean canAccess(String scheme, InetSocketAddress addr) {
if (addr == null)
return false;
try {
URL url = new URL(scheme + "://" + NetUtils.getHostPortString(addr));
URLConnection conn = connectionFactory.openConnection(url);
conn.connect();
conn.getContent();
} catch (Exception e) {
return false;
}
return true;
}
use of java.net.URLConnection in project spark by perwendel.
the class AbstractFileResolvingResource method lastModified.
@Override
public long lastModified() throws IOException {
URL url = getURL();
if (ResourceUtils.isFileURL(url) || ResourceUtils.isJarURL(url)) {
// Proceed with file system resolution...
return super.lastModified();
} else {
// Try a URL connection last-modified header...
URLConnection con = url.openConnection();
customizeConnection(con);
return con.getLastModified();
}
}
use of java.net.URLConnection in project spark by perwendel.
the class AbstractFileResolvingResource method contentLength.
@Override
public long contentLength() throws IOException {
URL url = getURL();
if (ResourceUtils.isFileURL(url)) {
// Proceed with file system resolution...
return getFile().length();
} else {
// Try a URL connection content-length header...
URLConnection con = url.openConnection();
customizeConnection(con);
return con.getContentLength();
}
}
use of java.net.URLConnection in project openhab1-addons by openhab.
the class EcoTouchConnector method setValue.
/**
* Set a value
*
* @param tag
* The register to set (e.g. "A1")
* @param value
* The 16-bit integer to set the register to
* @return value This value is a 16-bit integer.
*/
public int setValue(String tag, int value) throws Exception {
// set value
String url = "http://" + ip + "/cgi/writeTags?returnValue=true&n=1&t1=" + tag + "&v1=" + value;
StringBuilder body = null;
int loginAttempt = 0;
while (loginAttempt < 2) {
try {
URLConnection connection = new URL(url).openConnection();
if (cookies != null) {
for (String cookie : cookies) {
connection.addRequestProperty("Cookie", cookie.split(";", 2)[0]);
}
}
InputStream response = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(response));
body = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
body.append(line + "\n");
}
if (body.toString().contains("#" + tag)) {
// succeeded
break;
}
// s.th. went wrong; try to log in
throw new Exception();
} catch (Exception e) {
login();
loginAttempt++;
}
}
if (body == null || !body.toString().contains("#" + tag)) {
// failed
logger.debug("Cannot get value for tag '" + tag + "' from Waterkotte EcoTouch.");
throw new Exception("invalid response from EcoTouch");
}
// ok, the body now contains s.th. like
// #A30 S_OK
// 192 223
Matcher m = response_pattern.matcher(body.toString());
boolean b = m.find();
if (!b) {
// ill formatted response
logger.debug("ill formatted response: '" + body + "'");
throw new Exception("invalid response from EcoTouch");
}
return Integer.parseInt(m.group(3));
}
Aggregations