Search in sources :

Example 1 with HttpURLConnection

use of java.net.HttpURLConnection in project jetty.project by eclipse.

the class WeldInitializationTest method testRequestParamServletAbc.

@Test
public void testRequestParamServletAbc() throws Exception {
    HttpURLConnection http = (HttpURLConnection) serverHttpURI.resolve("req-info?abc=123").toURL().openConnection();
    assertThat("response code", http.getResponseCode(), is(200));
    try (InputStream inputStream = http.getInputStream()) {
        String resp = IO.toString(inputStream);
        assertThat("Response", resp, containsString("request is PRESENT"));
        assertThat("Response", resp, containsString("parameters.size = [1]"));
        assertThat("Response", resp, containsString(" param[abc] = [123]"));
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) InputStream(java.io.InputStream) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 2 with HttpURLConnection

use of java.net.HttpURLConnection in project jetty.project by eclipse.

the class WeldInitializationTest method testRequestParamServletDefault.

@Test
public void testRequestParamServletDefault() throws Exception {
    HttpURLConnection http = (HttpURLConnection) serverHttpURI.resolve("req-info").toURL().openConnection();
    assertThat("response code", http.getResponseCode(), is(200));
    try (InputStream inputStream = http.getInputStream()) {
        String resp = IO.toString(inputStream);
        assertThat("Response", resp, containsString("request is PRESENT"));
        assertThat("Response", resp, containsString("parameters.size = [0]"));
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) InputStream(java.io.InputStream) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 3 with HttpURLConnection

use of java.net.HttpURLConnection in project jetty.project by eclipse.

the class QuickStartTest method testStandardTestWar.

@Test
public void testStandardTestWar() throws Exception {
    PreconfigureStandardTestWar.main(new String[] {});
    WebDescriptor descriptor = new WebDescriptor(Resource.newResource("./target/test-standard-preconfigured/WEB-INF/quickstart-web.xml"));
    descriptor.setValidating(true);
    descriptor.parse();
    Node node = descriptor.getRoot();
    assertThat(node, Matchers.notNullValue());
    System.setProperty("jetty.home", "target");
    //war file or dir to start
    String war = "target/test-standard-preconfigured";
    //optional jetty context xml file to configure the webapp
    Resource contextXml = Resource.newResource("src/test/resources/test.xml");
    Server server = new Server(0);
    QuickStartWebApp webapp = new QuickStartWebApp();
    webapp.setAutoPreconfigure(true);
    webapp.setWar(war);
    webapp.setContextPath("/");
    //apply context xml file
    if (contextXml != null) {
        // System.err.println("Applying "+contextXml);
        XmlConfiguration xmlConfiguration = new XmlConfiguration(contextXml.getURL());
        xmlConfiguration.configure(webapp);
    }
    server.setHandler(webapp);
    server.start();
    URL url = new URL("http://127.0.0.1:" + server.getBean(NetworkConnector.class).getLocalPort() + "/test/dump/info");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    assertEquals(200, connection.getResponseCode());
    assertThat(IO.toString((InputStream) connection.getContent()), Matchers.containsString("Dump Servlet"));
    server.stop();
}
Also used : WebDescriptor(org.eclipse.jetty.webapp.WebDescriptor) HttpURLConnection(java.net.HttpURLConnection) Server(org.eclipse.jetty.server.Server) InputStream(java.io.InputStream) Node(org.eclipse.jetty.xml.XmlParser.Node) Resource(org.eclipse.jetty.util.resource.Resource) XmlConfiguration(org.eclipse.jetty.xml.XmlConfiguration) URL(java.net.URL) Test(org.junit.Test)

Example 4 with HttpURLConnection

use of java.net.HttpURLConnection in project lucida by claritylab.

the class HTMLConverter method url2text.

/**
	 * Fetches an HTML document from a URL and converts it into plain text.
	 * 
	 * @param url URL of HTML document
	 * @return plain text or <code>null</code> if the fetching or conversion failed
	 */
public static synchronized String url2text(String url) throws SocketTimeoutException {
    // connect to URL
    URLConnection conn = null;
    try {
        conn = (new URL(url)).openConnection();
        // only allow HTTP connections
        if (!(conn instanceof HttpURLConnection))
            return null;
    } catch (IOException e) {
        return null;
    }
    // pretend to be a browser
    conn.setRequestProperty("User-agent", "Mozilla/4.0");
    conn.setConnectTimeout(TIMEOUT);
    conn.setReadTimeout(TIMEOUT);
    // fetch URL and convert HTML document
    StringBean sb = new StringBean();
    // no links
    sb.setLinks(false);
    // replace non-breaking spaces
    sb.setReplaceNonBreakingSpaces(true);
    // replace sequences of whitespaces
    sb.setCollapse(true);
    sb.setConnection(conn);
    String docText = sb.getStrings();
    return docText;
}
Also used : HttpURLConnection(java.net.HttpURLConnection) StringBean(org.htmlparser.beans.StringBean) IOException(java.io.IOException) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) URL(java.net.URL)

Example 5 with HttpURLConnection

use of java.net.HttpURLConnection in project cw-omnibus by commonsguy.

the class Downloader method onHandleIntent.

@Override
public void onHandleIntent(Intent i) {
    String filename = i.getData().getLastPathSegment();
    startForeground(FOREGROUND_ID, buildForegroundNotification(filename));
    try {
        File output = new File(getFilesDir(), filename);
        if (output.exists()) {
            output.delete();
        }
        URL url = new URL(i.getData().toString());
        HttpURLConnection c = (HttpURLConnection) url.openConnection();
        FileOutputStream fos = new FileOutputStream(output.getPath());
        BufferedOutputStream out = new BufferedOutputStream(fos);
        try {
            InputStream in = c.getInputStream();
            byte[] buffer = new byte[8192];
            int len = 0;
            while ((len = in.read(buffer)) >= 0) {
                out.write(buffer, 0, len);
            }
            out.flush();
        } finally {
            fos.getFD().sync();
            out.close();
            c.disconnect();
        }
        raiseNotification(i, output, null);
    } catch (IOException e2) {
        raiseNotification(i, null, e2);
    } finally {
        stopForeground(true);
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) URL(java.net.URL)

Aggregations

HttpURLConnection (java.net.HttpURLConnection)3826 URL (java.net.URL)2442 IOException (java.io.IOException)1632 InputStream (java.io.InputStream)1081 InputStreamReader (java.io.InputStreamReader)692 Test (org.junit.Test)649 BufferedReader (java.io.BufferedReader)573 OutputStream (java.io.OutputStream)465 MalformedURLException (java.net.MalformedURLException)372 URLConnection (java.net.URLConnection)248 HashMap (java.util.HashMap)216 OutputStreamWriter (java.io.OutputStreamWriter)208 Map (java.util.Map)199 Gson (com.google.gson.Gson)190 ByteArrayOutputStream (java.io.ByteArrayOutputStream)185 ArrayList (java.util.ArrayList)168 ExecutionException (java.util.concurrent.ExecutionException)161 AsyncTask (android.os.AsyncTask)158 File (java.io.File)158 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)157