Search in sources :

Example 41 with URLConnection

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);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) URLConnection(java.net.URLConnection) URL(java.net.URL) SAXException(org.xml.sax.SAXException) Test(org.junit.Test)

Example 42 with URLConnection

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;
}
Also used : URL(java.net.URL) URLConnection(java.net.URLConnection)

Example 43 with URLConnection

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();
    }
}
Also used : URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection)

Example 44 with URLConnection

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();
    }
}
Also used : URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection)

Example 45 with URLConnection

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));
}
Also used : InputStreamReader(java.io.InputStreamReader) Matcher(java.util.regex.Matcher) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) URLConnection(java.net.URLConnection) URL(java.net.URL) MalformedURLException(java.net.MalformedURLException)

Aggregations

URLConnection (java.net.URLConnection)1686 URL (java.net.URL)1180 IOException (java.io.IOException)740 InputStream (java.io.InputStream)569 HttpURLConnection (java.net.HttpURLConnection)465 InputStreamReader (java.io.InputStreamReader)404 BufferedReader (java.io.BufferedReader)358 Test (org.junit.Test)206 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)202 File (java.io.File)196 MalformedURLException (java.net.MalformedURLException)190 BufferedInputStream (java.io.BufferedInputStream)119 FileOutputStream (java.io.FileOutputStream)112 OutputStream (java.io.OutputStream)112 FileInputStream (java.io.FileInputStream)111 JarURLConnection (java.net.JarURLConnection)106 ArrayList (java.util.ArrayList)92 MockResponse (okhttp3.mockwebserver.MockResponse)76 ByteArrayOutputStream (java.io.ByteArrayOutputStream)74 FileNotFoundException (java.io.FileNotFoundException)59