Example usage for java.net URL getContent

List of usage examples for java.net URL getContent

Introduction

In this page you can find the example usage for java.net URL getContent.

Prototype

public final Object getContent() throws java.io.IOException 

Source Link

Document

Gets the contents of this URL.

Usage

From source file:dev.memento.MainActivity.java

public Object fetchUrl(String address) throws MalformedURLException, IOException {
    URL url = new URL(address);
    Object content = url.getContent();
    return content;
}

From source file:com.emc.esu.test.EsuApiTest.java

@Test
public void testGetShareableUrl() throws Exception {
    Assume.assumeFalse(isVipr);/*from w  w  w  .j  a va2s.  c  om*/
    // Create an object with content.
    String str = "Four score and twenty years ago";
    ObjectId id = this.esu.createObject(null, null, str.getBytes("UTF-8"), "text/plain");
    Assert.assertNotNull("null ID returned", id);
    cleanup.add(id);

    Calendar c = Calendar.getInstance();
    c.add(Calendar.HOUR, 4);
    Date expiration = c.getTime();
    URL u = esu.getShareableUrl(id, expiration);

    l4j.debug("Sharable URL: " + u);

    InputStream stream = (InputStream) u.getContent();
    BufferedReader br = new BufferedReader(new InputStreamReader(stream));
    String content = br.readLine();
    l4j.debug("Content: " + content);
    Assert.assertEquals("URL does not contain proper content", str, content.toString());
}

From source file:com.emc.esu.test.EsuApiTest.java

@Test
public void testGetShareableUrlWithPath() throws Exception {
    Assume.assumeFalse(isVipr);//  w  w  w  . j  ava 2 s . c  o  m
    // Create an object with content.
    String str = "Four score and twenty years ago";
    ObjectPath op = new ObjectPath("/" + rand8char() + ".txt");
    ObjectId id = this.esu.createObjectOnPath(op, null, null, str.getBytes("UTF-8"), "text/plain");
    Assert.assertNotNull("null ID returned", id);
    cleanup.add(op);

    Calendar c = Calendar.getInstance();
    c.add(Calendar.HOUR, 4);
    Date expiration = c.getTime();
    URL u = esu.getShareableUrl(op, expiration);

    l4j.debug("Sharable URL: " + u);

    InputStream stream = (InputStream) u.getContent();
    BufferedReader br = new BufferedReader(new InputStreamReader(stream));
    String content = br.readLine();
    l4j.debug("Content: " + content);
    Assert.assertEquals("URL does not contain proper content", str, content.toString());
}

From source file:com.emc.esu.test.EsuApiTest.java

@Test
public void testGetShareableUrlAndDisposition() throws Exception {
    Assume.assumeFalse(isVipr);/*from ww  w. j ava 2 s.  c o  m*/
    // Create an object with content.
    String str = "Four score and twenty years ago";
    ObjectId id = this.esu.createObject(null, null, str.getBytes("UTF-8"), "text/plain");
    Assert.assertNotNull("null ID returned", id);
    cleanup.add(id);

    String disposition = "attachment; filename=\"foo bar.txt\"";

    Calendar c = Calendar.getInstance();
    c.add(Calendar.HOUR, 4);
    Date expiration = c.getTime();
    URL u = this.esu.getShareableUrl(id, expiration, disposition);

    l4j.debug("Sharable URL: " + u);

    InputStream stream = (InputStream) u.getContent();
    BufferedReader br = new BufferedReader(new InputStreamReader(stream));
    String content = br.readLine();
    l4j.debug("Content: " + content);
    Assert.assertEquals("URL does not contain proper content", str, content.toString());
}

From source file:com.emc.esu.test.EsuApiTest.java

@Test
public void testGetShareableUrlWithPathAndDisposition() throws Exception {
    Assume.assumeFalse(isVipr);/*  w ww . ja v  a 2s . com*/
    // Create an object with content.
    String str = "Four score and twenty years ago";
    ObjectPath op = new ObjectPath("/" + rand8char() + ".txt");
    ObjectId id = this.esu.createObjectOnPath(op, null, null, str.getBytes("UTF-8"), "text/plain");
    Assert.assertNotNull("null ID returned", id);
    //cleanup.add( op );

    String disposition = "attachment; filename=\"foo bar.txt\"";

    Calendar c = Calendar.getInstance();
    c.add(Calendar.HOUR, 4);
    Date expiration = c.getTime();
    URL u = this.esu.getShareableUrl(op, expiration, disposition);

    l4j.debug("Sharable URL: " + u);

    InputStream stream = (InputStream) u.getContent();
    BufferedReader br = new BufferedReader(new InputStreamReader(stream));
    String content = br.readLine();
    l4j.debug("Content: " + content);
    Assert.assertEquals("URL does not contain proper content", str, content.toString());
}

From source file:com.emc.esu.test.EsuApiTest.java

@Test
public void testExpiredSharableUrl() throws Exception {
    Assume.assumeFalse(isVipr);/*from   www .  j a  va 2  s .  c o  m*/
    // Create an object with content.
    String str = "Four score and twenty years ago";
    ObjectId id = this.esu.createObject(null, null, str.getBytes("UTF-8"), "text/plain");
    Assert.assertNotNull("null ID returned", id);
    cleanup.add(id);

    Calendar c = Calendar.getInstance();
    c.add(Calendar.HOUR, -4);
    Date expiration = c.getTime();
    URL u = esu.getShareableUrl(id, expiration);

    l4j.debug("Sharable URL: " + u);

    try {
        InputStream stream = (InputStream) u.getContent();
        BufferedReader br = new BufferedReader(new InputStreamReader(stream));
        String content = br.readLine();
        l4j.debug("Content: " + content);
        Assert.fail("Request should have failed");
    } catch (Exception e) {
        l4j.debug("Error (expected): " + e);
    }
}

From source file:com.emc.esu.test.EsuApiTest.java

@Test
public void testGetShareableUrlWithPathAndUTF8Disposition() throws Exception {
    Assume.assumeFalse(isVipr);/*from   www.j  ava2 s . c om*/
    // Create an object with content.
    String str = "Four score and twenty years ago";
    ObjectPath op = new ObjectPath("/" + rand8char() + ".txt");
    ObjectId id = this.esu.createObjectOnPath(op, null, null, str.getBytes("UTF-8"), "text/plain");
    Assert.assertNotNull("null ID returned", id);
    //cleanup.add( op );

    // One cryllic, one accented, and one japanese character
    // RFC5987
    String disposition = "attachment; filename=\"no UTF support.txt\"; filename*=UTF-8''"
            + URLEncoder.encode(".txt", "UTF-8");

    Calendar c = Calendar.getInstance();
    c.add(Calendar.HOUR, 4);
    Date expiration = c.getTime();
    URL u = this.esu.getShareableUrl(op, expiration, disposition);

    l4j.debug("Sharable URL: " + u);

    InputStream stream = (InputStream) u.getContent();
    BufferedReader br = new BufferedReader(new InputStreamReader(stream));
    String content = br.readLine();
    l4j.debug("Content: " + content);
    Assert.assertEquals("URL does not contain proper content", str, content.toString());
}

From source file:org.mozilla.gecko.GeckoApp.java

@Override
public boolean onPrepareOptionsMenu(Menu aMenu) {
    Iterator<ExtraMenuItem> i = sExtraMenuItems.iterator();
    while (i.hasNext()) {
        final ExtraMenuItem item = i.next();
        if (aMenu.findItem(item.id) == null) {
            final MenuItem mi = aMenu.add(Menu.NONE, item.id, Menu.NONE, item.label);
            if (item.icon != null) {
                if (item.icon.startsWith("data")) {
                    byte[] raw = GeckoAppShell.decodeBase64(item.icon.substring(22),
                            GeckoAppShell.BASE64_DEFAULT);
                    Bitmap bitmap = BitmapFactory.decodeByteArray(raw, 0, raw.length);
                    BitmapDrawable drawable = new BitmapDrawable(bitmap);
                    mi.setIcon(drawable);
                } else if (item.icon.startsWith("jar:") || item.icon.startsWith("file://")) {
                    GeckoAppShell.getHandler().post(new Runnable() {
                        public void run() {
                            try {
                                URL url = new URL(item.icon);
                                InputStream is = (InputStream) url.getContent();
                                Drawable drawable = Drawable.createFromStream(is, "src");
                                mi.setIcon(drawable);
                            } catch (Exception e) {
                                Log.w(LOGTAG, "onPrepareOptionsMenu: Unable to set icon", e);
                            }//from  ww  w  .jav  a  2s. c om
                        }
                    });
                }
            }
            mi.setOnMenuItemClickListener(item);
        }
    }

    if (!sIsGeckoReady)
        aMenu.findItem(R.id.settings).setEnabled(false);

    Tab tab = Tabs.getInstance().getSelectedTab();
    MenuItem bookmark = aMenu.findItem(R.id.bookmark);
    MenuItem forward = aMenu.findItem(R.id.forward);
    MenuItem share = aMenu.findItem(R.id.share);
    MenuItem saveAsPDF = aMenu.findItem(R.id.save_as_pdf);
    MenuItem charEncoding = aMenu.findItem(R.id.char_encoding);

    if (tab == null) {
        bookmark.setEnabled(false);
        forward.setEnabled(false);
        share.setEnabled(false);
        saveAsPDF.setEnabled(false);
        return true;
    }

    bookmark.setEnabled(true);
    bookmark.setCheckable(true);

    if (tab.isBookmark()) {
        bookmark.setChecked(true);
        bookmark.setIcon(R.drawable.ic_menu_bookmark_remove);
    } else {
        bookmark.setChecked(false);
        bookmark.setIcon(R.drawable.ic_menu_bookmark_add);
    }

    forward.setEnabled(tab.canDoForward());

    // Disable share menuitem for about:, chrome: and file: URIs
    String scheme = Uri.parse(tab.getURL()).getScheme();
    boolean enabled = scheme != null
            && !(scheme.equals("about") || scheme.equals("chrome") || scheme.equals("file"));
    share.setEnabled(enabled);

    // Disable save as PDF for about:home and xul pages
    saveAsPDF.setEnabled(!(tab.getURL().equals("about:home")
            || tab.getContentType().equals("application/vnd.mozilla.xul+xml")));

    charEncoding.setVisible(GeckoPreferences.getCharEncodingState());

    return true;
}

From source file:com.emc.atmos.api.test.AtmosApiClientTest.java

@Test
public void testGetShareableUrl() throws Exception {
    Assume.assumeFalse(isVipr);/*from ww  w .  j av a  2  s .  c o m*/
    // Create an object with content.
    String str = "Four score and twenty years ago";
    ObjectId id = this.api.createObject(str.getBytes("UTF-8"), "text/plain");
    Assert.assertNotNull("null ID returned", id);
    cleanup.add(id);

    Calendar c = Calendar.getInstance();
    c.add(Calendar.HOUR, 4);
    Date expiration = c.getTime();
    URL u = api.getShareableUrl(id, expiration);

    l4j.debug("Sharable URL: " + u);

    InputStream stream = (InputStream) u.getContent();
    BufferedReader br = new BufferedReader(new InputStreamReader(stream));
    String content = br.readLine();
    l4j.debug("Content: " + content);
    Assert.assertEquals("URL does not contain proper content", str, content);
}

From source file:com.emc.atmos.api.test.AtmosApiClientTest.java

@Test
public void testGetShareableUrlWithPath() throws Exception {
    Assume.assumeFalse(isVipr);// w w w  .  j av  a  2 s .c  o m
    // Create an object with content.
    String str = "Four score and twenty years ago";
    ObjectPath op = new ObjectPath("/" + rand8char() + ".txt");
    ObjectId id = this.api.createObject(op, str.getBytes("UTF-8"), "text/plain");
    Assert.assertNotNull("null ID returned", id);
    cleanup.add(op);

    Calendar c = Calendar.getInstance();
    c.add(Calendar.HOUR, 4);
    Date expiration = c.getTime();
    URL u = api.getShareableUrl(op, expiration);

    l4j.debug("Sharable URL: " + u);

    InputStream stream = (InputStream) u.getContent();
    BufferedReader br = new BufferedReader(new InputStreamReader(stream));
    String content = br.readLine();
    l4j.debug("Content: " + content);
    Assert.assertEquals("URL does not contain proper content", str, content);
}