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]"));
}
}
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]"));
}
}
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();
}
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;
}
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);
}
}
Aggregations