Example usage for org.apache.commons.io FileUtils copyURLToFile

List of usage examples for org.apache.commons.io FileUtils copyURLToFile

Introduction

In this page you can find the example usage for org.apache.commons.io FileUtils copyURLToFile.

Prototype

public static void copyURLToFile(URL source, File destination) throws IOException 

Source Link

Document

Copies bytes from the URL source to a file destination.

Usage

From source file:org.geoserver.security.xml.XMLUserGroupService.java

@Override
public void initializeFromConfig(SecurityNamedServiceConfig config) throws IOException {

    this.name = config.getName();
    validatingXMLSchema = false;/*from w ww.j av  a 2 s.  c  o m*/
    passwordEncoderName = ((SecurityUserGroupServiceConfig) config).getPasswordEncoderName();
    passwordValidatorName = ((SecurityUserGroupServiceConfig) config).getPasswordPolicyName();

    GeoServerPasswordEncoder enc = getSecurityManager().loadPasswordEncoder(passwordEncoderName);
    if (enc.getEncodingType() == PasswordEncodingType.ENCRYPT) {
        KeyStoreProvider prov = getSecurityManager().getKeyStoreProvider();
        String alias = prov.aliasForGroupService(name);
        if (prov.containsAlias(alias) == false) {
            prov.setUserGroupKey(name,
                    getSecurityManager().getRandomPassworddProvider().getRandomPasswordWithDefaultLength());
            prov.storeKeyStore();
        }
    }
    enc.initializeFor(this);

    if (config instanceof XMLSecurityServiceConfig) {
        validatingXMLSchema = ((XMLSecurityServiceConfig) config).isValidating();
        // copy schema file 
        File xsdFile = new File(getConfigRoot(), XMLConstants.FILE_UR_SCHEMA);
        if (xsdFile.exists() == false) {
            FileUtils.copyURLToFile(getClass().getResource(XMLConstants.FILE_UR_SCHEMA), xsdFile);
        }

    }

    if (config instanceof FileBasedSecurityServiceConfig) {
        String fileName = ((FileBasedSecurityServiceConfig) config).getFileName();
        userFile = new File(fileName);
        if (userFile.isAbsolute() == false) {
            userFile = new File(getConfigRoot(), fileName);
        }
        if (userFile.exists() == false) {
            FileUtils.copyURLToFile(getClass().getResource("usersTemplate.xml"), userFile);
        }
    } else {
        throw new IOException("Cannot initialize from " + config.getClass().getName());
    }
    deserialize();
}

From source file:org.geoserver.security.xml.XMLUserGroupServiceTest.java

@Test
public void testLocking() throws Exception {
    File xmlFile = File.createTempFile("users", ".xml");
    FileUtils.copyURLToFile(getClass().getResource("usersTemplate.xml"), xmlFile);
    GeoServerUserGroupService service1 = createUserGroupService("locking1", xmlFile.getCanonicalPath());
    GeoServerUserGroupService service2 = createUserGroupService("locking2", xmlFile.getCanonicalPath());
    GeoServerUserGroupStore store1 = createStore(service1);
    GeoServerUserGroupStore store2 = createStore(service2);

    GeoServerUser user = store1.createUserObject("user", "ps", true);
    GeoServerUserGroup group = store2.createGroupObject("group", true);

    // obtain a lock
    store1.addUser(user);//from   ww  w .j av  a2 s .c om
    boolean fail;
    String failMessage = "Concurrent lock not allowed";
    fail = true;
    try {
        store2.clear();
    } catch (IOException ex) {
        fail = false;
    }
    if (fail)
        Assert.fail(failMessage);

    // release lock
    store1.load();
    // get lock
    store2.addUser(user);

    fail = true;
    try {
        store1.clear();
    } catch (IOException ex) {
        fail = false;
    }
    if (fail)
        Assert.fail(failMessage);

    // release lock
    store2.store();
    store1.clear();
    store1.store();

    //// end of part one, now check all modifying methods

    // obtain a lock
    store1.addUser(user);

    fail = true;
    try {
        store2.associateUserToGroup(user, group);
    } catch (IOException ex) {
        try {
            store2.disAssociateUserFromGroup(user, group);
        } catch (IOException e) {
            fail = false;
        }
    }
    if (fail)
        Assert.fail(failMessage);

    fail = true;
    try {
        store2.updateUser(user);
    } catch (IOException ex) {
        try {
            store2.removeUser(user);
        } catch (IOException ex1) {
            try {
                store2.addUser(user);
            } catch (IOException ex2) {
                fail = false;
            }
        }
    }
    if (fail)
        Assert.fail(failMessage);

    fail = true;
    try {
        store2.updateGroup(group);
    } catch (IOException ex) {
        try {
            store2.removeGroup(group);
        } catch (IOException ex1) {
            try {
                store2.addGroup(group);
            } catch (IOException ex2) {
                fail = false;
            }
        }
    }
    if (fail)
        Assert.fail(failMessage);

    fail = true;
    try {
        store2.clear();
    } catch (IOException ex) {
        try {
            store2.store();
        } catch (IOException e) {
            fail = false;
        }
    }
    if (fail)
        Assert.fail(failMessage);

}

From source file:org.geoserver.security.xml.XMLUserGroupServiceTest.java

@Test
public void testDynamicReload() throws Exception {
    File xmlFile = File.createTempFile("users", ".xml");
    FileUtils.copyURLToFile(getClass().getResource("usersTemplate.xml"), xmlFile);
    GeoServerUserGroupService service1 = createUserGroupService("reload1", xmlFile.getCanonicalPath());
    GeoServerUserGroupService service2 = createUserGroupService("reload2", xmlFile.getCanonicalPath());

    GeoServerUserGroupStore store1 = createStore(service1);

    GeoServerUserGroup group = store1.createGroupObject("group", true);

    checkEmpty(service1);//from w  w w .  j  a  v a  2s .  c  o  m
    checkEmpty(service2);

    // prepare for syncing

    UserGroupLoadedListener listener = new UserGroupLoadedListener() {

        @Override
        public void usersAndGroupsChanged(UserGroupLoadedEvent event) {
            synchronized (this) {
                this.notifyAll();
            }
        }
    };

    service2.registerUserGroupLoadedListener(listener);

    // modifiy store1
    store1.addGroup(group);
    store1.store();
    assertTrue(service1.getUserGroups().size() == 1);
    assertTrue(service1.getGroupCount() == 1);

    // increment lastmodified adding a second manually, the test is too fast
    xmlFile.setLastModified(xmlFile.lastModified() + 2000);

    // wait for the listener to unlock when 
    // service 2 triggers a load event
    synchronized (listener) {
        listener.wait();
    }

    // here comes the magic !!!
    assertTrue(service2.getUserGroups().size() == 1);
    assertTrue(service2.getGroupCount() == 1);
}

From source file:org.geoserver.wms.eo.EoStyleCatalogListener.java

/**
 * Copies a WMS-EO style to the data directory and adds a catalog entry for it.
 * /*from  w ww  . j  a v a 2  s  .  c om*/
 * See org.geoserver.config.GeoServerLoader.initializeStyle.
 */
private void initializeStyle(String styleName, String sld) throws IOException {
    // copy the file out to the data directory if necessary
    if (resourceLoader.find("styles", sld) == null) {
        FileUtils.copyURLToFile(EoStyleCatalogListener.class.getResource(sld),
                new File(resourceLoader.findOrCreateDirectory("styles"), sld));
    }

    // create a style for it
    StyleInfo s = catalog.getFactory().createStyle();
    s.setName(styleName);
    s.setFilename(sld);
    try {
        catalog.add(s);
    } catch (RuntimeException e) {
        if (LOGGER.isLoggable(Level.WARNING)) {
            LOGGER.log(Level.WARNING, e.getMessage(), e);
        }
    }
}

From source file:org.geoserver.wms.wms_1_1_1.GetMapIntegrationTest.java

@Test
public void testLayoutLegendNPE() throws Exception {
    // set the title to null
    FeatureTypeInfo states = getCatalog().getFeatureTypeByName("states");
    states.setTitle(null);/*from  w  w w .j  a  v  a2 s  .  c om*/
    getCatalog().save(states);

    // add the layout to the data dir
    File layouts = getDataDirectory().findOrCreateDir("layouts");
    URL layout = GetMapIntegrationTest.class.getResource("test-layout.xml");
    FileUtils.copyURLToFile(layout, new File(layouts, "test-layout.xml"));

    // get a map with the layout, it used to NPE
    BufferedImage image = getAsImage(
            "wms?bbox=" + bbox + "&styles=Population&layers=" + layers + "&Format=image/png" + "&request=GetMap"
                    + "&width=550" + "&height=250" + "&srs=EPSG:4326&format_options=layout:test-layout",
            "image/png");
    // RenderedImageBrowser.showChain(image);

    // check the pixels that should be in the legend
    assertPixel(image, 12, 16, Color.RED);
    assertPixel(image, 12, 32, Color.GREEN);
    assertPixel(image, 12, 52, Color.BLUE);
}

From source file:org.geoserver.wms.wms_1_1_1.GetMapIntegrationTest.java

@Test
public void testLayoutLegendStyleTitle() throws Exception {
    // set the title to null
    FeatureTypeInfo states = getCatalog().getFeatureTypeByName("states");
    states.setTitle(null);// w  ww  . jav  a  2s  . c om
    getCatalog().save(states);

    // add the layout to the data dir
    File layouts = getDataDirectory().findOrCreateDir("layouts");
    URL layout = GetMapIntegrationTest.class.getResource("test-layout-sldtitle.xml");
    FileUtils.copyURLToFile(layout, new File(layouts, "test-layout-sldtitle.xml"));

    // get a map with the layout, it used to NPE
    BufferedImage image = getAsImage("wms?bbox=" + bbox + "&styles=Population&layers=" + layers
            + "&Format=image/png" + "&request=GetMap" + "&width=550" + "&height=250"
            + "&srs=EPSG:4326&format_options=layout:test-layout-sldtitle", "image/png");
    // RenderedImageBrowser.showChain(image);

    // check the pixels that should be in the legend
    assertPixel(image, 12, 36, Color.RED);
    assertPixel(image, 12, 52, Color.GREEN);
    assertPixel(image, 12, 72, Color.BLUE);
}

From source file:org.geoserver.wms.wms_1_1_1.GetMapIntegrationTest.java

@Test
public void testLayoutTranslucent() throws Exception {
    // add the layout to the data dir
    File layouts = getDataDirectory().findOrCreateDir("layouts");
    URL layout = GetMapIntegrationTest.class.getResource("test-layout.xml");
    FileUtils.copyURLToFile(layout, new File(layouts, "test-layout.xml"));

    // get a map with the layout after using a translucent style
    BufferedImage image = getAsImage("wms?bbox=" + bbox + "&styles=translucent&layers=" + layers
            + "&Format=image/png" + "&request=GetMap" + "&width=550" + "&height=250" + "&srs=EPSG:4326"
            + "&format_options=layout:test-layout&transparent=true", "image/png");
    // RenderedImageBrowser.showChain(image);

    // check the pixels that should be in the scale bar
    assertPixel(image, 56, 211, Color.WHITE);
    assertPixel(image, 52, 221, Color.BLACK);
}

From source file:org.geoserver.wms.wms_1_1_1.GetMapIntegrationTest.java

@Test
public void testLayoutLegendStyleTitleDPI() throws Exception {
    // set the title to null
    FeatureTypeInfo states = getCatalog().getFeatureTypeByName("states");
    states.setTitle(null);/*w  w  w  .  j a  va  2 s. c o  m*/
    getCatalog().save(states);

    // add the layout to the data dir
    File layouts = getDataDirectory().findOrCreateDir("layouts");
    URL layout = GetMapIntegrationTest.class.getResource("test-layout-sldtitle.xml");
    FileUtils.copyURLToFile(layout, new File(layouts, "test-layout-sldtitle.xml"));

    int dpi = 90 * 2;
    int width = 550 * 2;
    int height = 250 * 2;

    // get a map with the layout, it used to NPE
    BufferedImage image = getAsImage("wms?bbox=" + bbox + "&styles=Population&layers=" + layers
            + "&Format=image/png" + "&request=GetMap" + "&width=" + width + "&height=" + height
            + "&srs=EPSG:4326&format_options=layout:test-layout-sldtitle;dpi:" + dpi, "image/png");
    // RenderedImageBrowser.showChain(image);

    // check the pixels that should be in the legend
    assertPixel(image, 15, 67, Color.RED);
    assertPixel(image, 15, 107, Color.GREEN);
    assertPixel(image, 15, 147, Color.BLUE);
}

From source file:org.geotools.gml3.v3_2.GMLParsingTest.java

public void testParseFeatureCollection() throws Exception {
    File schema = File.createTempFile("test", "xsd");
    schema.deleteOnExit();/* w  w  w .  jav  a2s.  com*/
    FileUtils.copyURLToFile(getClass().getResource("test.xsd"), schema);

    Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder()
            .parse(getClass().getResourceAsStream("test.xml"));
    URL schemaURL = DataUtilities.fileToURL(schema.getAbsoluteFile());
    dom.getDocumentElement().setAttribute("xsi:schemaLocation",
            "http://www.geotools.org/test " + schemaURL.getFile());
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    TransformerFactory.newInstance().newTransformer().transform(new DOMSource(dom), new StreamResult(out));

    GMLConfiguration config = new GMLConfiguration();
    Parser p = new Parser(config);
    Object o = p.parse(new ByteArrayInputStream(out.toByteArray()));
    assertTrue(o instanceof FeatureCollection);

    FeatureCollection features = (FeatureCollection) o;
    assertEquals(3, features.size());

    FeatureIterator fi = features.features();
    try {
        for (int i = 0; i < 3; i++) {
            assertTrue(fi.hasNext());

            SimpleFeature f = (SimpleFeature) fi.next();
            assertTrue(f.getDefaultGeometry() instanceof Point);

            Point point = (Point) f.getDefaultGeometry();
            assertEquals(i / 1d, point.getX(), 0.1);
            assertEquals(i / 1d, point.getX(), 0.1);

            assertEquals(i, f.getAttribute("count"));
        }
    } finally {
        fi.close();
    }
}

From source file:org.geowebcache.config.GWCXMLConfigIntegrationTestSupport.java

@Override
public void resetConfiguration() throws Exception {
    if (configFile != null) {
        URL source = XMLConfiguration.class.getResource("geowebcache-empty.xml");
        FileUtils.copyURLToFile(source, configFile);
    }/*from   w  w  w.j av  a2 s  .c  o  m*/
    config = new XMLConfiguration(null, configDir.getAbsolutePath());
    broker = new GridSetBroker(getGridSetConfigurations());
    config.setGridSetBroker(broker);
    config.deinitialize();
    config.reinitialize();
}